@@ -368,7 +368,8 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
368368 // must receive mined block which will confirm the transactions.
369369 {
370370 let ( height, block) = emitter. next_block ( ) ?. expect ( "must get mined block" ) ;
371- let _ = chain. apply_update ( block_to_chain_update ( & block, height) ) ?;
371+ let _ = chain
372+ . apply_update ( CheckPoint :: from_header ( & block. header , height) . into_update ( false ) ) ?;
372373 let indexed_additions = indexed_tx_graph. apply_block_relevant ( block, height) ;
373374 assert ! ( indexed_additions. graph. txs. is_empty( ) ) ;
374375 assert ! ( indexed_additions. graph. txouts. is_empty( ) ) ;
@@ -685,34 +686,59 @@ fn mempool_during_reorg() -> anyhow::Result<()> {
685686 env. send ( & addr, Amount :: from_sat ( 2100 ) ) ?;
686687 }
687688
688- // perform reorgs at different heights
689- for reorg_count in 1 ..TIP_DIFF {
690- // sync emitter to tip
691- while emitter. next_header ( ) ?. is_some ( ) { }
689+ // sync emitter to tip, first mempool emission should include all txs (as we haven't emitted
690+ // from the mempool yet)
691+ while emitter. next_header ( ) ?. is_some ( ) { }
692+ assert_eq ! (
693+ emitter
694+ . mempool( ) ?
695+ . into_iter( )
696+ . map( |( tx, _) | tx. txid( ) )
697+ . collect:: <BTreeSet <_>>( ) ,
698+ env. client
699+ . get_raw_mempool( ) ?
700+ . into_iter( )
701+ . collect:: <BTreeSet <_>>( ) ,
702+ "first mempool emission should include all txs" ,
703+ ) ;
692704
705+ // perform reorgs at different heights, these reorgs will not comfirm transactions in the
706+ // mempool
707+ for reorg_count in 1 ..TIP_DIFF {
693708 println ! ( "REORG COUNT: {}" , reorg_count) ;
694709 env. reorg_empty_blocks ( reorg_count) ?;
695710
696- // we recalculate this at every loop as reorgs may evict transactions from mempool
697- let tx_introductions = env
711+ // This is a map of mempool txids to tip height where the tx was introduced to the mempool
712+ // we recalculate this at every loop as reorgs may evict transactions from mempool. We use
713+ // the introduction height to determine whether we expect a tx to appear in a mempool
714+ // emission.
715+ // TODO: How can have have reorg logic in `TestEnv` NOT blacklast old blocks first?
716+ let tx_introductions = dbg ! ( env
698717 . client
699718 . get_raw_mempool_verbose( ) ?
700719 . into_iter( )
701720 . map( |( txid, entry) | ( txid, entry. height as usize ) )
702- . collect :: < BTreeMap < _ , _ > > ( ) ;
721+ . collect:: <BTreeMap <_, _>>( ) ) ;
703722
723+ // `next_header` emits the replacement block of the reorg
704724 if let Some ( ( height, _) ) = emitter. next_header ( ) ? {
705- // the mempool emission (that follows the first block emission after reorg) should return
706- // the entire mempool contents
725+ println ! ( "\t - replacement height: {}" , height) ;
726+
727+ // the mempool emission (that follows the first block emission after reorg) should only
728+ // include mempool txs introduced at reorg height or greater
707729 let mempool = emitter
708730 . mempool ( ) ?
709731 . into_iter ( )
710732 . map ( |( tx, _) | tx. txid ( ) )
711733 . collect :: < BTreeSet < _ > > ( ) ;
712- let exp_mempool = tx_introductions. keys ( ) . copied ( ) . collect :: < BTreeSet < _ > > ( ) ;
734+ let exp_mempool = tx_introductions
735+ . iter ( )
736+ . filter ( |( _, & intro_h) | intro_h >= ( height as usize ) )
737+ . map ( |( & txid, _) | txid)
738+ . collect :: < BTreeSet < _ > > ( ) ;
713739 assert_eq ! (
714740 mempool, exp_mempool,
715- "the first mempool emission after reorg should include all mempool txs"
741+ "the first mempool emission after reorg should only include mempool txs introduced at reorg height or greater "
716742 ) ;
717743
718744 let mempool = emitter
@@ -738,6 +764,9 @@ fn mempool_during_reorg() -> anyhow::Result<()> {
738764 . collect:: <Vec <_>>( ) ,
739765 ) ;
740766 }
767+
768+ // sync emitter to tip
769+ while emitter. next_header ( ) ?. is_some ( ) { }
741770 }
742771
743772 Ok ( ( ) )
0 commit comments