diff --git a/zebra-state/src/request.rs b/zebra-state/src/request.rs index f67868b0dac..e40db4f5334 100644 --- a/zebra-state/src/request.rs +++ b/zebra-state/src/request.rs @@ -221,6 +221,17 @@ pub struct FinalizedWithTrees { pub treestate: Option, } +impl FinalizedWithTrees { + pub fn new(block: ContextuallyValidBlock, treestate: Treestate) -> Self { + let finalized = FinalizedBlock::from(block); + + Self { + finalized, + treestate: Some(treestate), + } + } +} + impl From> for FinalizedWithTrees { fn from(block: Arc) -> Self { Self::from(FinalizedBlock::from(block)) diff --git a/zebra-state/src/service/non_finalized_state.rs b/zebra-state/src/service/non_finalized_state.rs index 7b3448d0425..c1540b24762 100644 --- a/zebra-state/src/service/non_finalized_state.rs +++ b/zebra-state/src/service/non_finalized_state.rs @@ -19,7 +19,7 @@ use zebra_chain::{ use crate::{ request::{ContextuallyValidBlock, FinalizedWithTrees}, service::{check, finalized_state::ZebraDb}, - FinalizedBlock, PreparedBlock, ValidateContextError, + PreparedBlock, ValidateContextError, }; mod chain; @@ -92,14 +92,14 @@ impl NonFinalizedState { let mut best_chain = chains.next_back().expect("there's at least one chain"); // clone if required - let write_best_chain = Arc::make_mut(&mut best_chain); + let mut_best_chain = Arc::make_mut(&mut best_chain); // extract the rest into side_chains so they can be mutated let side_chains = chains; // Pop the lowest height block from the best chain to be finalized, and // also obtain its associated treestate. - let (finalizing, treestate) = write_best_chain.pop_root(); + let (best_chain_root, root_treestate) = mut_best_chain.pop_root(); // add best_chain back to `self.chain_set` if !best_chain.is_empty() { @@ -107,11 +107,11 @@ impl NonFinalizedState { } // for each remaining chain in side_chains - for mut chain in side_chains { - if chain.non_finalized_root_hash() != finalizing.hash { + for mut side_chain in side_chains { + if side_chain.non_finalized_root_hash() != best_chain_root.hash { // If we popped the root, the chain would be empty or orphaned, // so just drop it now. - drop(chain); + drop(side_chain); continue; } @@ -119,24 +119,20 @@ impl NonFinalizedState { // otherwise, the popped root block is the same as the finalizing block // clone if required - let write_chain = Arc::make_mut(&mut chain); + let mut_side_chain = Arc::make_mut(&mut side_chain); // remove the first block from `chain` - let (chain_start, _treestate) = write_chain.pop_root(); - assert_eq!(chain_start.hash, finalizing.hash); + let (side_chain_root, _treestate) = mut_side_chain.pop_root(); + assert_eq!(side_chain_root.hash, best_chain_root.hash); // add the chain back to `self.chain_set` - self.chain_set.insert(chain); + self.chain_set.insert(side_chain); } self.update_metrics_for_chains(); // Add the treestate to the finalized block. - let finalized = FinalizedBlock::from(finalizing); - let mut finalized_with_trees = FinalizedWithTrees::from(finalized); - finalized_with_trees.treestate = Some(treestate); - - finalized_with_trees + FinalizedWithTrees::new(best_chain_root, root_treestate) } /// Commit block to the non-finalized state, on top of: