@@ -216,6 +216,17 @@ fn finalize_pops_from_best_chain_for_network(network: Network) -> Result<()> {
216
216
Ok ( ( ) )
217
217
}
218
218
219
+ #[ test]
220
+ fn invalidate_block_removes_block_and_descendants_from_chain ( ) -> Result < ( ) > {
221
+ let _init_guard = zebra_test:: init ( ) ;
222
+
223
+ for network in Network :: iter ( ) {
224
+ invalidate_block_removes_block_and_descendants_from_chain_for_network ( network) ?;
225
+ }
226
+
227
+ Ok ( ( ) )
228
+ }
229
+
219
230
fn invalidate_block_removes_block_and_descendants_from_chain_for_network (
220
231
network : Network ,
221
232
) -> Result < ( ) > {
@@ -294,16 +305,88 @@ fn invalidate_block_removes_block_and_descendants_from_chain_for_network(
294
305
}
295
306
296
307
#[ test]
297
- fn invalidate_block_removes_block_and_descendants_from_chain ( ) -> Result < ( ) > {
308
+ fn reconsider_block_and_reconsider_chain_correctly_reconsiders_blocks_and_descendants ( ) -> Result < ( ) >
309
+ {
298
310
let _init_guard = zebra_test:: init ( ) ;
299
311
300
312
for network in Network :: iter ( ) {
301
- invalidate_block_removes_block_and_descendants_from_chain_for_network ( network) ?;
313
+ reconsider_block_inserts_block_and_descendants_into_chain_for_network ( network. clone ( ) ) ?;
302
314
}
303
315
304
316
Ok ( ( ) )
305
317
}
306
318
319
+ fn reconsider_block_inserts_block_and_descendants_into_chain_for_network (
320
+ network : Network ,
321
+ ) -> Result < ( ) > {
322
+ let block1: Arc < Block > = Arc :: new ( network. test_block ( 653599 , 583999 ) . unwrap ( ) ) ;
323
+ let block2 = block1. make_fake_child ( ) . set_work ( 10 ) ;
324
+ let block3 = block2. make_fake_child ( ) . set_work ( 1 ) ;
325
+
326
+ let mut state = NonFinalizedState :: new ( & network) ;
327
+ let finalized_state = FinalizedState :: new (
328
+ & Config :: ephemeral ( ) ,
329
+ & network,
330
+ #[ cfg( feature = "elasticsearch" ) ]
331
+ false ,
332
+ ) ;
333
+
334
+ let fake_value_pool = ValueBalance :: < NonNegative > :: fake_populated_pool ( ) ;
335
+ finalized_state. set_finalized_value_pool ( fake_value_pool) ;
336
+
337
+ state. commit_new_chain ( block1. clone ( ) . prepare ( ) , & finalized_state) ?;
338
+ state. commit_block ( block2. clone ( ) . prepare ( ) , & finalized_state) ?;
339
+ state. commit_block ( block3. clone ( ) . prepare ( ) , & finalized_state) ?;
340
+
341
+ assert_eq ! (
342
+ state
343
+ . best_chain( )
344
+ . unwrap_or( & Arc :: new( Chain :: default ( ) ) )
345
+ . blocks
346
+ . len( ) ,
347
+ 3
348
+ ) ;
349
+
350
+ // Invalidate block2 to update the invalidated_blocks NonFinalizedState
351
+ state. invalidate_block ( block2. hash ( ) ) ;
352
+
353
+ // Perform checks to ensure the invalidated_block and descendants were added to the invalidated_block
354
+ // state
355
+ let post_invalidated_chain = state. best_chain ( ) . unwrap ( ) ;
356
+
357
+ assert_eq ! ( post_invalidated_chain. blocks. len( ) , 1 ) ;
358
+ assert ! (
359
+ post_invalidated_chain. contains_block_hash( block1. hash( ) ) ,
360
+ "the new modified chain should contain block1"
361
+ ) ;
362
+
363
+ assert ! (
364
+ !post_invalidated_chain. contains_block_hash( block2. hash( ) ) ,
365
+ "the new modified chain should not contain block2"
366
+ ) ;
367
+ assert ! (
368
+ !post_invalidated_chain. contains_block_hash( block3. hash( ) ) ,
369
+ "the new modified chain should not contain block3"
370
+ ) ;
371
+
372
+ // Reconsider block2 and check that both block2 and block3 were `reconsidered` into the
373
+ // best chain
374
+ state. reconsider_block ( block2. hash ( ) ) ?;
375
+
376
+ let best_chain = state. best_chain ( ) . unwrap ( ) ;
377
+
378
+ assert ! (
379
+ best_chain. contains_block_hash( block2. hash( ) ) ,
380
+ "the best chain should again contain block2"
381
+ ) ;
382
+ assert ! (
383
+ best_chain. contains_block_hash( block3. hash( ) ) ,
384
+ "the best chain should again contain block3"
385
+ ) ;
386
+
387
+ Ok ( ( ) )
388
+ }
389
+
307
390
#[ test]
308
391
// This test gives full coverage for `take_chain_if`
309
392
fn commit_block_extending_best_chain_doesnt_drop_worst_chains ( ) -> Result < ( ) > {
0 commit comments