From b684bc9ba01d292bf5bfdc4b13fe0eb95525ab0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 17 Mar 2016 12:47:31 +0100 Subject: [PATCH] Updating sealing when new transactions are received --- miner/src/lib.rs | 2 +- miner/src/miner.rs | 37 +++++++++++++---------- rpc/src/v1/tests/helpers/miner_service.rs | 2 +- sync/src/chain.rs | 9 ++++-- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/miner/src/lib.rs b/miner/src/lib.rs index a431bd44e..d6310eb8a 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -92,7 +92,7 @@ pub trait MinerService : Send + Sync { fn chain_new_blocks(&self, chain: &BlockChainClient, imported: &[H256], invalid: &[H256], enacted: &[H256], retracted: &[H256]); /// New chain head event. Restart mining operation. - fn prepare_sealing(&self, chain: &BlockChainClient); + fn update_sealing(&self, chain: &BlockChainClient); /// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock. fn sealing_block(&self, chain: &BlockChainClient) -> &Mutex>; diff --git a/miner/src/miner.rs b/miner/src/miner.rs index 6d5b3086e..d177d81ee 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -93,13 +93,28 @@ impl Miner { pub fn set_minimal_gas_price(&self, min_gas_price: U256) { self.transaction_queue.lock().unwrap().set_minimal_gas_price(min_gas_price); } + + /// Prepares new block for sealing including top transactions from queue. + pub fn prepare_sealing(&self, chain: &BlockChainClient) { + let no_of_transactions = 128; + // TODO: should select transactions orm queue according to gas limit of block. + let transactions = self.transaction_queue.lock().unwrap().top_transactions(no_of_transactions); + + let b = chain.prepare_sealing( + self.author(), + self.gas_floor_target(), + self.extra_data(), + transactions, + ); + *self.sealing_block.lock().unwrap() = b; + } } impl MinerService for Miner { fn clear_and_reset(&self, chain: &BlockChainClient) { self.transaction_queue.lock().unwrap().clear(); - self.prepare_sealing(chain); + self.update_sealing(chain); } fn status(&self) -> MinerStatus { @@ -121,18 +136,10 @@ impl MinerService for Miner { transaction_queue.pending_hashes() } - fn prepare_sealing(&self, chain: &BlockChainClient) { - let no_of_transactions = 128; - // TODO: should select transactions orm queue according to gas limit of block. - let transactions = self.transaction_queue.lock().unwrap().top_transactions(no_of_transactions); - - let b = chain.prepare_sealing( - self.author(), - self.gas_floor_target(), - self.extra_data(), - transactions, - ); - *self.sealing_block.lock().unwrap() = b; + fn update_sealing(&self, chain: &BlockChainClient) { + if self.sealing_enabled.load(atomic::Ordering::Relaxed) { + self.prepare_sealing(chain); + } } fn sealing_block(&self, chain: &BlockChainClient) -> &Mutex> { @@ -199,8 +206,6 @@ impl MinerService for Miner { }); } - if self.sealing_enabled.load(atomic::Ordering::Relaxed) { - self.prepare_sealing(chain); - } + self.update_sealing(chain); } } diff --git a/rpc/src/v1/tests/helpers/miner_service.rs b/rpc/src/v1/tests/helpers/miner_service.rs index 3a60bd136..9f38b374b 100644 --- a/rpc/src/v1/tests/helpers/miner_service.rs +++ b/rpc/src/v1/tests/helpers/miner_service.rs @@ -54,7 +54,7 @@ impl MinerService for TestMinerService { fn chain_new_blocks(&self, _chain: &BlockChainClient, _imported: &[H256], _invalid: &[H256], _enacted: &[H256], _retracted: &[H256]) { unimplemented!(); } /// New chain head event. Restart mining operation. - fn prepare_sealing(&self, _chain: &BlockChainClient) { unimplemented!(); } + fn update_sealing(&self, _chain: &BlockChainClient) { unimplemented!(); } /// Grab the `ClosedBlock` that we want to be sealed. Comes as a mutex that you have to lock. fn sealing_block(&self, _chain: &BlockChainClient) -> &Mutex> { diff --git a/sync/src/chain.rs b/sync/src/chain.rs index 4f6c2100a..c87dc9535 100644 --- a/sync/src/chain.rs +++ b/sync/src/chain.rs @@ -403,7 +403,7 @@ impl ChainSync { self.remove_downloaded_blocks(number + 1); } if self.have_common_block && number < self.current_base_block() + 1 { - // unkown header + // unkown header debug!(target: "sync", "Old block header {:?} ({}) is unknown, restarting sync", hash, number); self.restart(io); return Ok(()); @@ -947,7 +947,10 @@ impl ChainSync { } let chain = io.chain(); let fetch_nonce = |a: &Address| chain.nonce(a); - let _ = self.miner.import_transactions(transactions, fetch_nonce); + let res = self.miner.import_transactions(transactions, fetch_nonce); + if res.is_ok() { + self.miner.update_sealing(io.chain()); + } Ok(()) } @@ -1287,7 +1290,7 @@ impl ChainSync { } pub fn chain_new_head(&mut self, io: &mut SyncIo) { - self.miner.prepare_sealing(io.chain()); + self.miner.update_sealing(io.chain()); } }