From eff8db90fd544886eb14e5fb30d24504baf0a4ae Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Mon, 12 Feb 2024 12:34:19 +0100 Subject: [PATCH] f Improve test, check regeneration behavior --- lightning-background-processor/src/lib.rs | 46 +++++++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index 2b8ee97befb..cb8d5236146 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -1387,6 +1387,20 @@ mod tests { } } + fn advance_chain(node: &mut Node, num_blocks: u32) { + for i in 1..=num_blocks { + let prev_blockhash = node.best_block.block_hash(); + let height = node.best_block.height() + 1; + let header = create_dummy_header(prev_blockhash, height); + node.best_block = BestBlock::new(header.block_hash(), height); + if i == num_blocks { + node.node.best_block_updated(&header, height); + node.chain_monitor.best_block_updated(&header, height); + node.sweeper.best_block_updated(&header, height); + } + } + } + fn confirm_transaction(node: &mut Node, tx: &Transaction) { confirm_transaction_depth(node, tx, ANTI_REORG_DELAY); } @@ -1652,14 +1666,38 @@ mod tests { _ => panic!("Unexpected event: {:?}", event), } + // Check we generate an initial sweeping tx. assert_eq!(nodes[0].sweeper.tracked_spendable_outputs().len(), 1); let tracked_output = nodes[0].sweeper.tracked_spendable_outputs().first().unwrap().clone(); - let sweep_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap(); - assert_eq!(sweep_tx.txid(), tracked_output.latest_spending_tx.as_ref().unwrap().txid()); + let sweep_tx_0 = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap(); + assert_eq!(sweep_tx_0.txid(), tracked_output.latest_spending_tx.as_ref().unwrap().txid()); + + // Check we rebroadcast the same sweeping tx up to the regeneration threshold. + advance_chain(&mut nodes[0], 143); - confirm_transaction_depth(&mut nodes[0], &sweep_tx, 5); assert_eq!(nodes[0].sweeper.tracked_spendable_outputs().len(), 1); - confirm_transaction_depth(&mut nodes[0], &sweep_tx, ANTI_REORG_DELAY); + let tracked_output = nodes[0].sweeper.tracked_spendable_outputs().first().unwrap().clone(); + let sweep_tx_1 = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap(); + assert_eq!(sweep_tx_1.txid(), tracked_output.latest_spending_tx.as_ref().unwrap().txid()); + assert_eq!(sweep_tx_0, sweep_tx_1); + + // Check we generate a different sweeping tx when hitting the regeneration threshold. + advance_chain(&mut nodes[0], 1); + + assert_eq!(nodes[0].sweeper.tracked_spendable_outputs().len(), 1); + let tracked_output = nodes[0].sweeper.tracked_spendable_outputs().first().unwrap().clone(); + let sweep_tx_2 = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().pop().unwrap(); + assert_eq!(sweep_tx_2.txid(), tracked_output.latest_spending_tx.as_ref().unwrap().txid()); + assert_ne!(sweep_tx_0, sweep_tx_2); + assert_ne!(sweep_tx_1, sweep_tx_2); + + // Check we still track the spendable outputs up to ANTI_REORG_DELAY confirmations. + confirm_transaction_depth(&mut nodes[0], &sweep_tx_2, 5); + assert_eq!(nodes[0].sweeper.tracked_spendable_outputs().len(), 1); + + // Check we stop tracking the spendable outputs when one of the txs reaches + // ANTI_REORG_DELAY confirmations. + confirm_transaction_depth(&mut nodes[0], &sweep_tx_0, ANTI_REORG_DELAY); assert_eq!(nodes[0].sweeper.tracked_spendable_outputs().len(), 0); if !std::thread::panicking() {