@@ -18,7 +18,7 @@ use crate::{
18
18
constants:: MAX_NON_FINALIZED_CHAIN_FORKS ,
19
19
request:: { ContextuallyVerifiedBlock , FinalizableBlock } ,
20
20
service:: { check, finalized_state:: ZebraDb } ,
21
- HashOrHeight , SemanticallyVerifiedBlock , ValidateContextError ,
21
+ SemanticallyVerifiedBlock , ValidateContextError ,
22
22
} ;
23
23
24
24
mod chain;
@@ -270,45 +270,33 @@ impl NonFinalizedState {
270
270
Ok ( ( ) )
271
271
}
272
272
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
274
274
/// the new chain into the chain_set and discard the previous.
275
275
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 {
277
277
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
+ } ;
283
279
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 {
290
281
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
+ } ) ;
295
293
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
+ } ;
298
296
299
- let invalidated_descendants = new_chain. invalidate_block_and_descendants ( & block_height) ;
300
297
self . invalidated_blocks
301
- . insert ( block_hash, Arc :: new ( invalidated_descendants ) ) ;
298
+ . insert ( block_hash, Arc :: new ( invalidated_blocks ) ) ;
302
299
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
-
312
300
self . update_metrics_for_chains ( ) ;
313
301
self . update_metrics_bars ( ) ;
314
302
}
0 commit comments