make fn submit_seal more idiomatic (#10843)

* make fn submit_seal more idiomatic

* applied review suggestions
This commit is contained in:
Marek Kotewicz 2019-07-05 18:58:45 +02:00 committed by David
parent d5584a01c7
commit b97bf81adf

View File

@ -1306,31 +1306,43 @@ impl miner::MinerService for Miner {
// Note used for external submission (PoW) and internally by sealing engines. // Note used for external submission (PoW) and internally by sealing engines.
fn submit_seal(&self, block_hash: H256, seal: Vec<Bytes>) -> Result<SealedBlock, Error> { fn submit_seal(&self, block_hash: H256, seal: Vec<Bytes>) -> Result<SealedBlock, Error> {
let result = let action = if self.options.enable_resubmission {
if let Some(b) = self.sealing.lock().queue.get_used_if( GetAction::Clone
if self.options.enable_resubmission { } else {
GetAction::Clone GetAction::Take
} else { };
GetAction::Take
},
|b| &b.header.bare_hash() == &block_hash
) {
trace!(target: "miner", "Submitted block {}={} with seal {:?}", block_hash, b.header.bare_hash(), seal);
b.lock().try_seal(&*self.engine, seal).or_else(|e| {
warn!(target: "miner", "Mined solution rejected: {}", e);
Err(Error::PowInvalid.into())
})
} else {
warn!(target: "miner", "Submitted solution rejected: Block unknown or out of date.");
Err(Error::PowHashInvalid.into())
};
result.and_then(|sealed| { let block = self.sealing.lock().queue
let n = sealed.header.number(); .get_used_if(action, |b| &b.header.bare_hash() == &block_hash)
let h = sealed.header.hash(); .ok_or_else(|| {
info!(target: "miner", "Submitted block imported OK. #{}: {}", Colour::White.bold().paint(format!("{}", n)), Colour::White.bold().paint(format!("{:x}", h))); warn!(target: "miner", "Submitted solution rejected: Block unknown or out of date.");
Ok(sealed) Error::PowHashInvalid
}) })?;
trace!(
target: "miner", "Submitted block {hash}={bare_hash} with seal {seal:?}",
hash = block_hash,
bare_hash = block.header.bare_hash(),
seal = seal
);
let sealed = block.lock()
.try_seal(&*self.engine, seal)
.map_err(|e| {
warn!(target: "miner", "Mined solution rejected: {}", e);
Error::PowInvalid
})?;
let n = sealed.header.number();
let h = sealed.header.hash();
info!(
target: "miner", "Submitted block imported OK. #{number}: {hash}",
number = Colour::White.bold().paint(n.to_string()),
hash = Colour::White.bold().paint(format!("{:x}", h))
);
Ok(sealed)
} }
fn chain_new_blocks<C>(&self, chain: &C, imported: &[H256], _invalid: &[H256], enacted: &[H256], retracted: &[H256], is_internal_import: bool) fn chain_new_blocks<C>(&self, chain: &C, imported: &[H256], _invalid: &[H256], enacted: &[H256], retracted: &[H256], is_internal_import: bool)