Updating sealing when new transactions are received

This commit is contained in:
Tomasz Drwięga 2016-03-17 12:47:31 +01:00 committed by arkpar
parent b18a7bde9a
commit 2aae862330
4 changed files with 30 additions and 33 deletions

View File

@ -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<Option<ClosedBlock>>;

View File

@ -95,10 +95,26 @@ impl Miner {
self.transaction_queue.lock().unwrap().set_minimal_gas_price(min_gas_price);
}
<<<<<<< HEAD
fn update_gas_limit(&self, chain: &BlockChainClient) {
let gas_limit = HeaderView::new(&chain.best_block_header()).gas_limit();
let mut queue = self.transaction_queue.lock().unwrap();
queue.set_gas_limit(gas_limit);
=======
/// 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;
>>>>>>> b684bc9... Updating sealing when new transactions are received
}
}
@ -106,7 +122,7 @@ 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 {
@ -130,26 +146,10 @@ impl MinerService for Miner {
transaction_queue.pending_hashes()
}
fn prepare_sealing(&self, chain: &BlockChainClient) {
let transactions = self.transaction_queue.lock().unwrap().top_transactions();
let b = chain.prepare_sealing(
self.author(),
self.gas_floor_target(),
self.extra_data(),
transactions,
);
*self.sealing_block.lock().unwrap() = b.map(|(block, invalid_transactions)| {
let mut queue = self.transaction_queue.lock().unwrap();
queue.remove_all(
&invalid_transactions.into_iter().collect::<Vec<H256>>(),
|a: &Address| AccountDetails {
nonce: chain.nonce(a),
balance: chain.balance(a),
}
);
block
});
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<Option<ClosedBlock>> {
@ -239,9 +239,6 @@ impl MinerService for Miner {
});
}
// Update mined block
if self.sealing_enabled.load(atomic::Ordering::Relaxed) {
self.prepare_sealing(chain);
}
self.update_sealing(chain);
}
}

View File

@ -61,7 +61,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<Option<ClosedBlock>> {

View File

@ -951,11 +951,11 @@ impl ChainSync {
transactions.push(tx);
}
let chain = io.chain();
let fetch_account = |a: &Address| AccountDetails {
nonce: chain.nonce(a),
balance: chain.balance(a),
};
let _ = self.miner.import_transactions(transactions, fetch_account);
let fetch_nonce = |a: &Address| chain.nonce(a);
let res = self.miner.import_transactions(transactions, fetch_nonce);
if res.is_ok() {
self.miner.update_sealing(io.chain());
}
Ok(())
}
@ -1297,7 +1297,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());
}
}