@@ -18,7 +18,7 @@ use crate::{
1818 constants:: MAX_NON_FINALIZED_CHAIN_FORKS ,
1919 request:: { ContextuallyVerifiedBlock , FinalizableBlock } ,
2020 service:: { check, finalized_state:: ZebraDb } ,
21- HashOrHeight , SemanticallyVerifiedBlock , ValidateContextError ,
21+ SemanticallyVerifiedBlock , ValidateContextError ,
2222} ;
2323
2424mod chain;
@@ -270,45 +270,33 @@ impl NonFinalizedState {
270270 Ok ( ( ) )
271271 }
272272
273- /// Invalidate block with has `block_hash` and all descendants from the non-finalized state. Insert
273+ /// Invalidate block with hash `block_hash` and all descendants from the non-finalized state. Insert
274274 /// the new chain into the chain_set and discard the previous.
275275 pub fn invalidate_block ( & mut self , block_hash : Hash ) {
276- if ! self . any_chain_contains ( & block_hash) {
276+ let Some ( chain ) = self . find_chain ( |chain| chain . contains_block_hash ( block_hash) ) else {
277277 return ;
278- }
279-
280- let mut chain = self
281- . find_chain ( |chain| chain. contains_block_hash ( block_hash) )
282- . expect ( "block hash exist in a chain" ) ;
278+ } ;
283279
284- // If the non-finalized chain root has the intended hash drop the chain
285- // and return early
286- let root = chain
287- . block ( HashOrHeight :: Height ( chain. non_finalized_root_height ( ) ) )
288- . unwrap ( ) ;
289- if root. hash == block_hash {
280+ let invalidated_blocks = if chain. non_finalized_root_hash ( ) == block_hash {
290281 self . chain_set . remove ( & chain) ;
291- self . update_metrics_for_chains ( ) ;
292- self . update_metrics_bars ( ) ;
293- return ;
294- }
282+ chain. blocks . values ( ) . cloned ( ) . collect ( )
283+ } else {
284+ let ( new_chain, invalidated_blocks) = chain
285+ . invalidate_block ( block_hash)
286+ . expect ( "already checked that chain contains hash" ) ;
287+
288+ // Add the new chain fork or updated chain to the set of recent chains, and
289+ // remove the chain containing the hash of the block from chain set
290+ self . insert_with ( Arc :: new ( new_chain. clone ( ) ) , |chain_set| {
291+ chain_set. retain ( |c| !c. contains_block_hash ( block_hash) )
292+ } ) ;
295293
296- let new_chain = Arc :: make_mut ( & mut chain ) ;
297- let block_height = new_chain . height_by_hash ( block_hash ) . unwrap ( ) ;
294+ invalidated_blocks
295+ } ;
298296
299- let invalidated_descendants = new_chain. invalidate_block_and_descendants ( & block_height) ;
300297 self . invalidated_blocks
301- . insert ( block_hash, Arc :: new ( invalidated_descendants ) ) ;
298+ . insert ( block_hash, Arc :: new ( invalidated_blocks ) ) ;
302299
303- // If the new chain still contains blocks:
304- // - add the new chain fork or updated chain to the set of recent chains
305- // - remove the chain containing the hash of the block from the chain set
306- if !new_chain. is_empty ( ) {
307- self . insert_with ( Arc :: new ( new_chain. clone ( ) ) , |chain_set| {
308- chain_set. retain ( |c| !c. contains_block_hash ( block_hash) )
309- } ) ;
310- }
311-
312300 self . update_metrics_for_chains ( ) ;
313301 self . update_metrics_bars ( ) ;
314302 }
0 commit comments