Skip to content

Commit d71f7ec

Browse files
elijahhamptonarya2
authored andcommitted
Checks the finalized state first to create a new chain from non finalized blocks only before checking parent_chain.
1 parent 52fc9d9 commit d71f7ec

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

zebra-state/src/service/non_finalized_state.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,30 @@ impl NonFinalizedState {
349349
.ok_or(ReconsiderError::InvalidatedBlocksEmpty)?;
350350

351351
let root_parent_hash = invalidated_root.block.header.previous_block_hash;
352-
let parent_chain = self
353-
.parent_chain(root_parent_hash)
354-
.map_err(|_| ReconsiderError::ParentChainNotFound(block_hash))?;
355352

356-
let mut modified_chain = Arc::unwrap_or_clone(parent_chain);
353+
// If the parent is the tip of the finalized_state we create a new chain and insert it
354+
// into the non finalized state
355+
let chain_result = if root_parent_hash == finalized_state.finalized_tip_hash() {
356+
let chain = Chain::new(
357+
&self.network,
358+
finalized_state
359+
.finalized_tip_height()
360+
.ok_or(ReconsiderError::ParentChainNotFound(block_hash))?,
361+
finalized_state.sprout_tree_for_tip(),
362+
finalized_state.sapling_tree_for_tip(),
363+
finalized_state.orchard_tree_for_tip(),
364+
finalized_state.history_tree(),
365+
finalized_state.finalized_value_pool(),
366+
);
367+
Arc::new(chain)
368+
} else {
369+
// The parent is not the finalized_tip and still exist in the NonFinalizedState
370+
// or else we return an error due to the parent not existing in the NonFinalizedState
371+
self.parent_chain(root_parent_hash)
372+
.map_err(|_| ReconsiderError::ParentChainNotFound(block_hash))?
373+
};
357374

375+
let mut modified_chain = Arc::unwrap_or_clone(chain_result);
358376
for block in Arc::unwrap_or_clone(invalidated_blocks) {
359377
modified_chain = modified_chain.push(block)?;
360378
}

0 commit comments

Comments
 (0)