From 1cdad996d32c2fbdb712606434ef2f2c4764dfab Mon Sep 17 00:00:00 2001 From: aggstam Date: Fri, 6 Oct 2023 18:29:12 +0300 Subject: [PATCH] validator/consensus: cache fork rank for better performance --- src/validator/consensus.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/validator/consensus.rs b/src/validator/consensus.rs index 21180b6fe..b136ef6cb 100644 --- a/src/validator/consensus.rs +++ b/src/validator/consensus.rs @@ -274,7 +274,7 @@ impl Consensus { // If a fork index was found, replace forks with the mutated one, // otherwise push the new fork. - fork.proposals.push(proposal.hash); + fork.append_proposal(proposal.hash)?; fork.slots = vec![]; match index { Some(i) => { @@ -288,7 +288,7 @@ impl Consensus { Ok(()) } - /// Auxiliary function to find current best ranked fork. + /// Auxiliary function to find current best ranked fork index. pub fn best_fork_index(&self) -> Result { // Check if node has any forks if self.forks.is_empty() { @@ -299,7 +299,7 @@ impl Consensus { let mut best = 0; let mut index = 0; for (f_index, fork) in self.forks.iter().enumerate() { - let rank = fork.rank()?; + let rank = fork.rank; if rank <= best { continue } @@ -502,6 +502,8 @@ pub struct Fork { pub slots: Vec, /// Valid pending transaction hashes pub mempool: Vec, + /// Current fork rank, cached for better performance + pub rank: u64, } impl Fork { @@ -510,7 +512,16 @@ impl Fork { let mempool = blockchain.get_pending_txs()?.iter().map(|tx| blake3::hash(&serialize(tx))).collect(); let overlay = BlockchainOverlay::new(blockchain)?; - Ok(Self { overlay, module, proposals: vec![], slots: vec![], mempool }) + Ok(Self { overlay, module, proposals: vec![], slots: vec![], mempool, rank: 0 }) + } + + /// Auxiliary function to append a proposal and recalculate current + /// fork rank + pub fn append_proposal(&mut self, proposal: blake3::Hash) -> Result<()> { + self.proposals.push(proposal); + self.rank = self.rank()?; + + Ok(()) } /// Auxiliary function to retrieve last proposal @@ -664,7 +675,8 @@ impl Fork { let proposals = self.proposals.clone(); let slots = self.slots.clone(); let mempool = self.mempool.clone(); + let rank = self.rank; - Ok(Self { overlay, module, proposals, slots, mempool }) + Ok(Self { overlay, module, proposals, slots, mempool, rank }) } }