@@ -14,7 +14,6 @@ use zebra_chain::{
14
14
parameters:: Network ,
15
15
sprout:: { self } ,
16
16
transparent,
17
- value_balance:: ValueBalance ,
18
17
} ;
19
18
20
19
use crate :: {
@@ -239,7 +238,7 @@ impl NonFinalizedState {
239
238
240
239
// Remove all invalidated_blocks at or below the finalized height
241
240
self . invalidated_blocks
242
- . retain ( |height, _blocks| * height > best_chain_root. height ) ;
241
+ . retain ( |height, _blocks| * height >= best_chain_root. height ) ;
243
242
244
243
self . update_metrics_for_chains ( ) ;
245
244
@@ -318,7 +317,11 @@ impl NonFinalizedState {
318
317
/// Reconsiders a previously invalidated block and its descendants into the non-finalized state
319
318
/// based on a block_hash. Reconsidered blocks are inserted into the previous chain and re-inserted
320
319
/// into the chain_set.
321
- pub fn reconsider_block ( & mut self , block_hash : block:: Hash ) -> Result < ( ) , ReconsiderError > {
320
+ pub fn reconsider_block (
321
+ & mut self ,
322
+ block_hash : block:: Hash ,
323
+ finalized_state : & ZebraDb ,
324
+ ) -> Result < ( ) , ReconsiderError > {
322
325
// Get the invalidated blocks that were invalidated by the given block_hash
323
326
let height = self
324
327
. invalidated_blocks
@@ -348,44 +351,24 @@ impl NonFinalizedState {
348
351
let root_parent_hash = invalidated_root. block . header . previous_block_hash ;
349
352
let parent_chain = self
350
353
. parent_chain ( root_parent_hash)
351
- . map_err ( |_| ReconsiderError :: ParentChainNotFound ( block_hash) ) ;
354
+ . map_err ( |_| ReconsiderError :: ParentChainNotFound ( block_hash) ) ? ;
352
355
353
- let modified_chain = match parent_chain {
354
- // 1. If a parent chain exist use the parent chain
355
- Ok ( parent_chain) => {
356
- let mut chain = Arc :: unwrap_or_clone ( parent_chain) ;
357
-
358
- for block in Arc :: unwrap_or_clone ( invalidated_blocks) {
359
- chain = chain. push ( block) ?;
360
- }
361
-
362
- Arc :: new ( chain)
363
- }
364
- // 2. If a parent chain does not exist create a new chain from the non finalized state tip
365
- Err ( _) => {
366
- let mut chain = Chain :: new (
367
- & self . network ,
368
- invalidated_root. height . previous ( ) . map_err ( |_| {
369
- ReconsiderError :: InvalidHeight ( Height ( invalidated_root. height . 0 - 1 ) )
370
- } ) ?,
371
- Default :: default ( ) ,
372
- Default :: default ( ) ,
373
- Default :: default ( ) ,
374
- Default :: default ( ) ,
375
- ValueBalance :: zero ( ) ,
376
- ) ;
377
-
378
- for block in Arc :: unwrap_or_clone ( invalidated_blocks) {
379
- chain = chain. push ( block) ?;
380
- }
356
+ let mut modified_chain = Arc :: unwrap_or_clone ( parent_chain) ;
381
357
382
- Arc :: new ( chain )
383
- }
384
- } ;
358
+ for block in Arc :: unwrap_or_clone ( invalidated_blocks ) {
359
+ modified_chain = modified_chain . push ( block ) ? ;
360
+ }
385
361
386
362
let ( height, hash) = modified_chain. non_finalized_tip ( ) ;
387
363
388
- self . insert_with ( modified_chain, |chain_set| {
364
+ // Only track invalidated_blocks that are not yet finalized. Once blocks are finalized (below the best_chain_root_height)
365
+ // we can discard the block.
366
+ if let Some ( best_chain_root_height) = finalized_state. finalized_tip_height ( ) {
367
+ self . invalidated_blocks
368
+ . retain ( |height, _blocks| * height >= best_chain_root_height) ;
369
+ }
370
+
371
+ self . insert_with ( Arc :: new ( modified_chain) , |chain_set| {
389
372
chain_set. retain ( |chain| chain. non_finalized_tip_hash ( ) != root_parent_hash)
390
373
} ) ;
391
374
0 commit comments