Skip to content

Commit

Permalink
Reduce time taken to collect VerifiedTransactions (#1563)
Browse files Browse the repository at this point in the history
* Added timing probes.

* Reduced recover_signer() call when assembling the list of txns in execute_block().
Makes it up to 1,500x faster.

* Made it more idiomatic.

* Removed logging/probes.

* Fixed review comments.
  • Loading branch information
shawn-zil authored Oct 7, 2024
1 parent 062f76f commit 7a24a13
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions zilliqa/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2627,29 +2627,36 @@ impl Consensus {
trace!("applying {} transactions to state", transactions.len());
}

let transactions: Result<Vec<_>> = transactions.into_iter().map(|tx| tx.verify()).collect();
let mut transactions = transactions?;

let transaction_hashes = transactions
.iter()
.map(|tx| format!("{:?}", tx.hash))
.join(",");
let mut verified_txns = Vec::new();

// We re-inject any missing Intershard transactions (or really, any missing
// transactions) from our mempool. If any txs are unavailable either in the
// transactions) from our mempool. If any txs are unavailable in both the
// message or locally, the proposal cannot be applied
for (idx, tx_hash) in block.transactions.iter().enumerate() {
if transactions.get(idx).is_some_and(|tx| tx.hash == *tx_hash) {
// all good
} else {
let Some(local_tx) = self.transaction_pool.pop_transaction(*tx_hash) else {
warn!("Proposal {} at view {} referenced a transaction {} that was neither included in the broadcast nor found locally - cannot apply block", block.hash(), block.view(), tx_hash);
return Ok(());
};
transactions.insert(idx, local_tx);
}
// Prefer to insert verified txn from pool. This is faster.
let txn = match self.transaction_pool.pop_transaction(*tx_hash) {
Some(txn) => txn,
_ => match transactions
.get(idx)
.map(|sig_tx| sig_tx.clone().verify())
.transpose()?
{
// Otherwise, recover txn from proposal. This is slower.
Some(txn) if txn.hash == *tx_hash => txn,
_ => {
warn!("Proposal {} at view {} referenced a transaction {} that was neither included in the broadcast nor found locally - cannot apply block", block.hash(), block.view(), tx_hash);
return Ok(());
}
},
};
verified_txns.push(txn);
}

let transaction_hashes = verified_txns
.iter()
.map(|tx| format!("{:?}", tx.hash))
.join(",");

self.apply_rewards(committee, &parent, block.view(), &block.header.qc.cosigned)?;

// ZIP-9: Sink gas to zero account
Expand All @@ -2666,7 +2673,7 @@ impl Consensus {
let mut receipts_trie = eth_trie::EthTrie::new(Arc::new(MemoryDB::new(true)));
let mut transactions_trie = eth_trie::EthTrie::new(Arc::new(MemoryDB::new(true)));

for (tx_index, txn) in transactions.into_iter().enumerate() {
for (tx_index, txn) in verified_txns.into_iter().enumerate() {
self.new_transaction(txn.clone())?;
let tx_hash = txn.hash;
let mut inspector = TouchedAddressInspector::default();
Expand Down Expand Up @@ -2770,7 +2777,6 @@ impl Consensus {

// Tell the block store to request more blocks if it can.
self.block_store.request_missing_blocks()?;

Ok(())
}

Expand Down

0 comments on commit 7a24a13

Please sign in to comment.