Skip to content

Commit

Permalink
f Improve test, check regeneration behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Feb 12, 2024
1 parent e5a8ba2 commit f107d77
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions lightning-background-processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit f107d77

Please sign in to comment.