Skip to content

Commit e904d68

Browse files
committed
Catch up test nodes to latest block height
In a future commit, we plan to correctly enforce that the spending transaction has a valid locktime relative to the chain for the node broascasting it in `TestBroadcaster::broadcast_transaction` to. We catch up these test node instances to their expected height, such that we do not fail said enforcement.
1 parent 69d0bfa commit e904d68

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,12 +1028,12 @@ mod tests {
10281028
}
10291029

10301030
fn create_nodes(num_nodes: usize, persist_dir: String) -> Vec<Node> {
1031+
let network = Network::Testnet;
10311032
let mut nodes = Vec::new();
10321033
for i in 0..num_nodes {
1033-
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
1034+
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster::new(network));
10341035
let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) });
10351036
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
1036-
let network = Network::Testnet;
10371037
let genesis_block = genesis_block(network);
10381038
let network_graph = Arc::new(NetworkGraph::new(network, logger.clone()));
10391039
let scorer = Arc::new(Mutex::new(TestScorer::new()));

lightning/src/chain/channelmonitor.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4169,7 +4169,7 @@ mod tests {
41694169
replay_update.updates.push(ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage: payment_preimage_1 });
41704170
replay_update.updates.push(ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage: payment_preimage_2 });
41714171

4172-
let broadcaster = TestBroadcaster::new(Arc::clone(&nodes[1].blocks));
4172+
let broadcaster = TestBroadcaster::with_blocks(Arc::clone(&nodes[1].blocks));
41734173
assert!(
41744174
pre_update_monitor.update_monitor(&replay_update, &&broadcaster, &chanmon_cfgs[1].fee_estimator, &nodes[1].logger)
41754175
.is_err());
@@ -4195,10 +4195,7 @@ mod tests {
41954195
fn test_prune_preimages() {
41964196
let secp_ctx = Secp256k1::new();
41974197
let logger = Arc::new(TestLogger::new());
4198-
let broadcaster = Arc::new(TestBroadcaster {
4199-
txn_broadcasted: Mutex::new(Vec::new()),
4200-
blocks: Arc::new(Mutex::new(Vec::new()))
4201-
});
4198+
let broadcaster = Arc::new(TestBroadcaster::new(Network::Testnet));
42024199
let fee_estimator = TestFeeEstimator { sat_per_kw: Mutex::new(253) };
42034200

42044201
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9035,7 +9035,7 @@ pub mod bench {
90359035
// calls per node.
90369036
let network = bitcoin::Network::Testnet;
90379037

9038-
let tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))};
9038+
let tx_broadcaster = test_utils::TestBroadcaster::new(network);
90399039
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
90409040
let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
90419041
let scorer = Mutex::new(test_utils::TestScorer::new());

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,10 +2435,7 @@ pub fn fail_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path: &
24352435
pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
24362436
let mut chan_mon_cfgs = Vec::new();
24372437
for i in 0..node_count {
2438-
let tx_broadcaster = test_utils::TestBroadcaster {
2439-
txn_broadcasted: Mutex::new(Vec::new()),
2440-
blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet), 0)])),
2441-
};
2438+
let tx_broadcaster = test_utils::TestBroadcaster::new(Network::Testnet);
24422439
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
24432440
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
24442441
let logger = test_utils::TestLogger::with_id(format!("node {}", i));

lightning/src/ln/functional_tests.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8434,7 +8434,7 @@ fn test_update_err_monitor_lockdown() {
84348434
let block = Block { header, txdata: vec![] };
84358435
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
84368436
// transaction lock time requirements here.
8437-
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 0));
8437+
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 200));
84388438
watchtower.chain_monitor.block_connected(&block, 200);
84398439

84408440
// Try to update ChannelMonitor
@@ -8486,6 +8486,9 @@ fn test_concurrent_monitor_claim() {
84868486
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
84878487
let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
84888488
let persister = test_utils::TestPersister::new();
8489+
let alice_broadcaster = test_utils::TestBroadcaster::with_blocks(
8490+
Arc::new(Mutex::new(nodes[0].blocks.lock().unwrap().clone())),
8491+
);
84898492
let watchtower_alice = {
84908493
let new_monitor = {
84918494
let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
@@ -8494,20 +8497,21 @@ fn test_concurrent_monitor_claim() {
84948497
assert!(new_monitor == *monitor);
84958498
new_monitor
84968499
};
8497-
let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8500+
let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &alice_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
84988501
assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
84998502
watchtower
85008503
};
85018504
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
85028505
let block = Block { header, txdata: vec![] };
8503-
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
8504-
// transaction lock time requirements here.
8505-
chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (block.clone(), 0));
8506-
watchtower_alice.chain_monitor.block_connected(&block, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8506+
// Make Alice aware of enough blocks that it doesn't think we're violating transaction lock time
8507+
// requirements here.
8508+
const HTLC_TIMEOUT_BROADCAST: u32 = CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS;
8509+
alice_broadcaster.blocks.lock().unwrap().resize((HTLC_TIMEOUT_BROADCAST) as usize, (block.clone(), HTLC_TIMEOUT_BROADCAST));
8510+
watchtower_alice.chain_monitor.block_connected(&block, HTLC_TIMEOUT_BROADCAST);
85078511

85088512
// Watchtower Alice should have broadcast a commitment/HTLC-timeout
85098513
let alice_state = {
8510-
let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast();
8514+
let mut txn = alice_broadcaster.txn_broadcast();
85118515
assert_eq!(txn.len(), 2);
85128516
txn.remove(0)
85138517
};
@@ -8516,6 +8520,7 @@ fn test_concurrent_monitor_claim() {
85168520
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
85178521
let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
85188522
let persister = test_utils::TestPersister::new();
8523+
let bob_broadcaster = test_utils::TestBroadcaster::with_blocks(Arc::clone(&alice_broadcaster.blocks));
85198524
let watchtower_bob = {
85208525
let new_monitor = {
85218526
let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
@@ -8524,12 +8529,12 @@ fn test_concurrent_monitor_claim() {
85248529
assert!(new_monitor == *monitor);
85258530
new_monitor
85268531
};
8527-
let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
8532+
let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &bob_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
85288533
assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
85298534
watchtower
85308535
};
85318536
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
8532-
watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8537+
watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, HTLC_TIMEOUT_BROADCAST - 1);
85338538

85348539
// Route another payment to generate another update with still previous HTLC pending
85358540
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 3000000);
@@ -8556,21 +8561,26 @@ fn test_concurrent_monitor_claim() {
85568561

85578562
//// Provide one more block to watchtower Bob, expect broadcast of commitment and HTLC-Timeout
85588563
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
8559-
watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8564+
watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, HTLC_TIMEOUT_BROADCAST);
85608565

85618566
// Watchtower Bob should have broadcast a commitment/HTLC-timeout
85628567
let bob_state_y;
85638568
{
8564-
let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast();
8569+
let mut txn = bob_broadcaster.txn_broadcast();
85658570
assert_eq!(txn.len(), 2);
85668571
bob_state_y = txn.remove(0);
85678572
};
85688573

85698574
// We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
85708575
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
8571-
watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![bob_state_y.clone()] }, CHAN_CONFIRM_DEPTH + 2 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
8576+
let height = HTLC_TIMEOUT_BROADCAST + 1;
8577+
connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
8578+
check_closed_broadcast(&nodes[0], 1, true);
8579+
check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false);
8580+
watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![bob_state_y.clone()] }, height);
8581+
check_added_monitors(&nodes[0], 1);
85728582
{
8573-
let htlc_txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast();
8583+
let htlc_txn = alice_broadcaster.txn_broadcast();
85748584
assert_eq!(htlc_txn.len(), 2);
85758585
check_spends!(htlc_txn[0], bob_state_y);
85768586
// Alice doesn't clean up the old HTLC claim since it hasn't seen a conflicting spend for

lightning/src/util/test_utils.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,15 @@ pub struct TestBroadcaster {
308308
}
309309

310310
impl TestBroadcaster {
311-
pub fn new(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> TestBroadcaster {
312-
TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks }
311+
pub fn new(network: Network) -> Self {
312+
Self {
313+
txn_broadcasted: Mutex::new(Vec::new()),
314+
blocks: Arc::new(Mutex::new(vec![(genesis_block(network), 0)])),
315+
}
316+
}
317+
318+
pub fn with_blocks(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> Self {
319+
Self { txn_broadcasted: Mutex::new(Vec::new()), blocks }
313320
}
314321

315322
pub fn txn_broadcast(&self) -> Vec<Transaction> {

0 commit comments

Comments
 (0)