Stop adding transactions right after we know that no other will make it to block.

This commit is contained in:
Tomasz Drwięga 2016-03-17 14:39:24 +01:00
parent c382fa7eab
commit c4021a77ca

View File

@ -418,26 +418,33 @@ impl<V> BlockChainClient for Client<V> where V: Verifier {
// Add transactions // Add transactions
let block_number = b.block().header().number(); let block_number = b.block().header().number();
let min_tx_gas = U256::from(self.engine.schedule(&b.env_info()).tx_gas);
let gas_limit = *b.block().header().gas_limit(); let gas_limit = *b.block().header().gas_limit();
let mut gas_left = gas_limit; let mut gas_left = gas_limit;
let mut invalid_transactions = HashSet::new(); let mut invalid_transactions = HashSet::new();
for tx in transactions { for tx in transactions {
let hash = tx.hash(); // Stop early if we are sure that no other transaction will be included
let gas = tx.gas; if gas_left < min_tx_gas {
// TODO [todr] It seems that calculating gas_left here doesn't really look nice. After moving this function break;
}
// TODO [todr] It seems that calculating gas_left here doesn't look nice. After moving this function
// to miner crate we should consider rewriting this logic in some better way. // to miner crate we should consider rewriting this logic in some better way.
if gas > gas_left { if tx.gas > gas_left {
trace!(target: "miner", "Skipping adding transaction to block because of gas limit: {:?}", hash); trace!(target: "miner", "Skipping adding transaction to block because of gas limit: {:?}", tx.hash());
continue; continue;
} }
// Push transaction to block
let hash = tx.hash();
let import = b.push_transaction(tx, None); let import = b.push_transaction(tx, None);
match import { match import {
Err(e) => { Err(e) => {
trace!(target: "miner", "Error adding transaction to block: number={}. transaction_hash={:?}, Error: {:?}",
block_number, hash, e);
invalid_transactions.insert(hash); invalid_transactions.insert(hash);
trace!(target: "miner",
"Error adding transaction to block: number={}. transaction_hash={:?}, Error: {:?}",
block_number, hash, e);
}, },
Ok(receipt) => { Ok(receipt) => {
gas_left = gas_limit - receipt.gas_used; gas_left = gas_limit - receipt.gas_used;