Skip to content

Commit

Permalink
Checks the finalized state first to create a new chain from non final…
Browse files Browse the repository at this point in the history
…ized blocks only before checking parent_chain.
  • Loading branch information
elijahhampton committed Feb 22, 2025
1 parent 0844080 commit e91b4cb
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions zebra-state/src/service/non_finalized_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,30 @@ impl NonFinalizedState {
.ok_or(ReconsiderError::InvalidatedBlocksEmpty)?;

let root_parent_hash = invalidated_root.block.header.previous_block_hash;
let parent_chain = self
.parent_chain(root_parent_hash)
.map_err(|_| ReconsiderError::ParentChainNotFound(block_hash))?;

let mut modified_chain = Arc::unwrap_or_clone(parent_chain);
// If the parent is the tip of the finalized_state we create a new chain and insert it
// into the non finalized state
let chain_result = if root_parent_hash == finalized_state.finalized_tip_hash() {
let chain = Chain::new(
&self.network,
finalized_state
.finalized_tip_height()
.ok_or(ReconsiderError::ParentChainNotFound(block_hash))?,
finalized_state.sprout_tree_for_tip(),
finalized_state.sapling_tree_for_tip(),
finalized_state.orchard_tree_for_tip(),
finalized_state.history_tree(),
finalized_state.finalized_value_pool(),
);
Arc::new(chain)
} else {
// The parent is not the finalized_tip and still exist in the NonFinalizedState
// or else we return an error due to the parent not existing in the NonFinalizedState
self.parent_chain(root_parent_hash)
.map_err(|_| ReconsiderError::ParentChainNotFound(block_hash))?
};

let mut modified_chain = Arc::unwrap_or_clone(chain_result);
for block in Arc::unwrap_or_clone(invalidated_blocks) {
modified_chain = modified_chain.push(block)?;
}
Expand Down

0 comments on commit e91b4cb

Please sign in to comment.