From ca01596a654f5e2402cbb1ffdcc07b3351aa0d12 Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 7 Nov 2018 18:59:08 +0100 Subject: [PATCH] Allow to seal work on latest block (#9876) * Allow to seal work on latest block. * Test from @todr to check sealing conditions. --- ethcore/src/miner/miner.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index ca5d5f7b5..39dac1f2b 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -576,10 +576,9 @@ impl Miner { trace!(target: "miner", "requires_reseal: sealing enabled"); // Disable sealing if there were no requests for SEALING_TIMEOUT_IN_BLOCKS - let had_requests = sealing.last_request.map(|last_request| { - best_block > last_request - && best_block - last_request <= SEALING_TIMEOUT_IN_BLOCKS - }).unwrap_or(false); + let had_requests = sealing.last_request.map(|last_request| + best_block.saturating_sub(last_request) <= SEALING_TIMEOUT_IN_BLOCKS + ).unwrap_or(false); // keep sealing enabled if any of the conditions is met let sealing_enabled = self.forced_sealing() @@ -1394,6 +1393,33 @@ mod tests { assert_eq!(miner.prepare_pending_block(&client), BlockPreparationStatus::NotPrepared); } + #[test] + fn should_not_return_stale_work_packages() { + // given + let client = TestBlockChainClient::default(); + let miner = miner(); + + // initial work package should create the pending block + let res = miner.work_package(&client); + assert_eq!(res.unwrap().1, 1); + // This should be true, since there were some requests. + assert_eq!(miner.requires_reseal(0), true); + + // when new block is imported + let client = generate_dummy_client(2); + let imported = [0.into()]; + let empty = &[]; + miner.chain_new_blocks(&*client, &imported, empty, &imported, empty, false); + + // then + // This should be false, because it's too early. + assert_eq!(miner.requires_reseal(2), false); + // but still work package should be ready + let res = miner.work_package(&*client); + assert_eq!(res.unwrap().1, 3); + assert_eq!(miner.prepare_pending_block(&*client), BlockPreparationStatus::NotPrepared); + } + #[test] fn should_not_use_pending_block_if_best_block_is_higher() { // given