@@ -1387,6 +1387,20 @@ mod tests {
13871387 }
13881388 }
13891389
1390+ fn advance_chain ( node : & mut Node , num_blocks : u32 ) {
1391+ for i in 1 ..=num_blocks {
1392+ let prev_blockhash = node. best_block . block_hash ( ) ;
1393+ let height = node. best_block . height ( ) + 1 ;
1394+ let header = create_dummy_header ( prev_blockhash, height) ;
1395+ node. best_block = BestBlock :: new ( header. block_hash ( ) , height) ;
1396+ if i == num_blocks {
1397+ node. node . best_block_updated ( & header, height) ;
1398+ node. chain_monitor . best_block_updated ( & header, height) ;
1399+ node. sweeper . best_block_updated ( & header, height) ;
1400+ }
1401+ }
1402+ }
1403+
13901404 fn confirm_transaction ( node : & mut Node , tx : & Transaction ) {
13911405 confirm_transaction_depth ( node, tx, ANTI_REORG_DELAY ) ;
13921406 }
@@ -1652,14 +1666,38 @@ mod tests {
16521666 _ => panic ! ( "Unexpected event: {:?}" , event) ,
16531667 }
16541668
1669+ // Check we generate an initial sweeping tx.
16551670 assert_eq ! ( nodes[ 0 ] . sweeper. tracked_spendable_outputs( ) . len( ) , 1 ) ;
16561671 let tracked_output = nodes[ 0 ] . sweeper . tracked_spendable_outputs ( ) . first ( ) . unwrap ( ) . clone ( ) ;
1657- let sweep_tx = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . pop ( ) . unwrap ( ) ;
1658- assert_eq ! ( sweep_tx. txid( ) , tracked_output. latest_spending_tx. as_ref( ) . unwrap( ) . txid( ) ) ;
1672+ let sweep_tx_0 = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . pop ( ) . unwrap ( ) ;
1673+ assert_eq ! ( sweep_tx_0. txid( ) , tracked_output. latest_spending_tx. as_ref( ) . unwrap( ) . txid( ) ) ;
1674+
1675+ // Check we rebroadcast the same sweeping tx up to the regeneration threshold.
1676+ advance_chain ( & mut nodes[ 0 ] , 143 ) ;
16591677
1660- confirm_transaction_depth ( & mut nodes[ 0 ] , & sweep_tx, 5 ) ;
16611678 assert_eq ! ( nodes[ 0 ] . sweeper. tracked_spendable_outputs( ) . len( ) , 1 ) ;
1662- confirm_transaction_depth ( & mut nodes[ 0 ] , & sweep_tx, ANTI_REORG_DELAY ) ;
1679+ let tracked_output = nodes[ 0 ] . sweeper . tracked_spendable_outputs ( ) . first ( ) . unwrap ( ) . clone ( ) ;
1680+ let sweep_tx_1 = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . pop ( ) . unwrap ( ) ;
1681+ assert_eq ! ( sweep_tx_1. txid( ) , tracked_output. latest_spending_tx. as_ref( ) . unwrap( ) . txid( ) ) ;
1682+ assert_eq ! ( sweep_tx_0, sweep_tx_1) ;
1683+
1684+ // Check we generate a different sweeping tx when hitting the regeneration threshold.
1685+ advance_chain ( & mut nodes[ 0 ] , 1 ) ;
1686+
1687+ assert_eq ! ( nodes[ 0 ] . sweeper. tracked_spendable_outputs( ) . len( ) , 1 ) ;
1688+ let tracked_output = nodes[ 0 ] . sweeper . tracked_spendable_outputs ( ) . first ( ) . unwrap ( ) . clone ( ) ;
1689+ let sweep_tx_2 = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . pop ( ) . unwrap ( ) ;
1690+ assert_eq ! ( sweep_tx_2. txid( ) , tracked_output. latest_spending_tx. as_ref( ) . unwrap( ) . txid( ) ) ;
1691+ assert_ne ! ( sweep_tx_0, sweep_tx_2) ;
1692+ assert_ne ! ( sweep_tx_1, sweep_tx_2) ;
1693+
1694+ // Check we still track the spendable outputs up to ANTI_REORG_DELAY confirmations.
1695+ confirm_transaction_depth ( & mut nodes[ 0 ] , & sweep_tx_2, 5 ) ;
1696+ assert_eq ! ( nodes[ 0 ] . sweeper. tracked_spendable_outputs( ) . len( ) , 1 ) ;
1697+
1698+ // Check we stop tracking the spendable outputs when one of the txs reaches
1699+ // ANTI_REORG_DELAY confirmations.
1700+ confirm_transaction_depth ( & mut nodes[ 0 ] , & sweep_tx_0, ANTI_REORG_DELAY ) ;
16631701 assert_eq ! ( nodes[ 0 ] . sweeper. tracked_spendable_outputs( ) . len( ) , 0 ) ;
16641702
16651703 if !std:: thread:: panicking ( ) {
0 commit comments