Skip to content

Commit c571243

Browse files
author
Antoine Riard
committed
Harden test_htlc_on_chain_success with asserts
Harden test_htlc_on_chain_timeout with asserts, refactor useless tests
1 parent f8785a4 commit c571243

File tree

1 file changed

+91
-32
lines changed

1 file changed

+91
-32
lines changed

src/ln/channelmanager.rs

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,9 +4287,9 @@ mod tests {
42874287
// ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
42884288
// broadcasting the right event to other nodes in payment path.
42894289
// A --------------------> B ----------------------> C (preimage)
4290-
// A's commitment tx C's commitment tx
4291-
// \ \
4292-
// B's preimage tx C's HTLC Success tx
4290+
// C's commitment tx
4291+
// \
4292+
// C's HTLC Success tx
42934293

42944294
let nodes = create_network(3);
42954295

@@ -4307,6 +4307,7 @@ mod tests {
43074307
// Broadcast legit commitment tx from C on B's chain
43084308
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
43094309
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4310+
check_spends!(commitment_tx[0], chan_2.3.clone());
43104311
nodes[2].node.claim_funds(payment_preimage);
43114312
{
43124313
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -4332,7 +4333,14 @@ mod tests {
43324333
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43334334
_ => panic!("Unexpected event"),
43344335
}
4335-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4336+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
4337+
assert_eq!(node_txn.len(), 3);
4338+
check_spends!(node_txn[0], commitment_tx[0].clone());
4339+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4340+
check_spends!(node_txn[1], chan_2.3.clone());
4341+
check_spends!(node_txn[2], node_txn[1].clone());
4342+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4343+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
43364344

43374345
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
43384346
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
@@ -4357,18 +4365,42 @@ mod tests {
43574365
},
43584366
_ => panic!("Unexpected event"),
43594367
};
4368+
{
4369+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();; // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 1 (timeout tx) * 2 (block-rescan)
4370+
assert_eq!(node_txn.len(), 4);
4371+
assert_eq!(node_txn[0], node_txn[3]);
4372+
check_spends!(node_txn[0], commitment_tx[0].clone());
4373+
check_spends!(node_txn[3], commitment_tx[0].clone());
4374+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4375+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
4376+
check_spends!(node_txn[1], chan_2.3.clone());
4377+
check_spends!(node_txn[2], node_txn[1].clone());
4378+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4379+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
4380+
node_txn.clear()
4381+
}
43604382

43614383
// Broadcast legit commitment tx from A on B's chain
43624384
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
43634385
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4386+
check_spends!(commitment_tx[0], chan_1.3.clone());
43644387
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
43654388
let events = nodes[1].node.get_and_clear_pending_events();
43664389
assert_eq!(events.len(), 1);
43674390
match events[0] {
43684391
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43694392
_ => panic!("Unexpected event"),
43704393
}
4371-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4394+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
4395+
assert_eq!(node_txn.len(), 3);
4396+
assert_eq!(node_txn[0], node_txn[2]);
4397+
check_spends!(node_txn[0], commitment_tx[0].clone());
4398+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133);
4399+
check_spends!(node_txn[2], commitment_tx[0].clone());
4400+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
4401+
check_spends!(node_txn[1], chan_1.3.clone());
4402+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4403+
let commitment_tx = node_txn[1].clone();
43724404

43734405
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
43744406
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
@@ -4378,6 +4410,17 @@ mod tests {
43784410
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43794411
_ => panic!("Unexpected event"),
43804412
}
4413+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 1 (timeout tx) * 2 (block-rescan)
4414+
assert_eq!(node_txn.len(), 4);
4415+
assert_eq!(node_txn[0], node_txn[3]);
4416+
check_spends!(node_txn[0], commitment_tx.clone());
4417+
check_spends!(node_txn[3], commitment_tx.clone());
4418+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4419+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
4420+
check_spends!(node_txn[1], chan_1.3.clone());
4421+
check_spends!(node_txn[2], node_txn[1].clone());
4422+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4423+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
43814424
}
43824425

43834426
#[test]
@@ -4386,7 +4429,7 @@ mod tests {
43864429
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
43874430
// broadcasting the right event to other nodes in payment path.
43884431
// A ------------------> B ----------------------> C (timeout)
4389-
// A's commitment tx C's commitment tx
4432+
// B's commitment tx C's commitment tx
43904433
// \ \
43914434
// B's HTLC timeout tx B's timeout tx
43924435

@@ -4405,6 +4448,7 @@ mod tests {
44054448

44064449
// Brodacast legit commitment tx from C on B's chain
44074450
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4451+
check_spends!(commitment_tx[0], chan_2.3.clone());
44084452
nodes[2].node.fail_htlc_backwards(&payment_hash);
44094453
{
44104454
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -4430,23 +4474,36 @@ mod tests {
44304474
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
44314475
_ => panic!("Unexpected event"),
44324476
}
4433-
let mut funding_tx_map = HashMap::new();
4434-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
4435-
commitment_tx[0].verify(&funding_tx_map).unwrap();
4477+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
4478+
assert_eq!(node_txn.len(), 1);
4479+
check_spends!(node_txn[0], chan_2.3.clone());
4480+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
44364481

44374482
// Broadcast timeout transaction by B on received output fron C's commitment tx on B's chain
44384483
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
44394484
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
4440-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4441-
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout), ChannelMonitor : 6 (commitment tx, HTLC-Timeout, timeout tx) * 2 (block-rescan)
4442-
assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
4443-
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
4444-
4445-
let mut commitment_tx_map = HashMap::new();
4446-
commitment_tx_map.insert(commitment_tx[0].txid(), commitment_tx[0].clone());
4447-
node_txn[0].verify(&commitment_tx_map).unwrap();
4448-
4449-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
4485+
let timeout_tx;
4486+
{
4487+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
4488+
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 6 (HTLC-Timeout tx, commitment tx, timeout tx) * 2 (block-rescan)
4489+
assert_eq!(node_txn[0], node_txn[5]);
4490+
assert_eq!(node_txn[1], node_txn[6]);
4491+
assert_eq!(node_txn[2], node_txn[7]);
4492+
check_spends!(node_txn[0], commitment_tx[0].clone());
4493+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
4494+
check_spends!(node_txn[1], chan_2.3.clone());
4495+
check_spends!(node_txn[2], node_txn[1].clone());
4496+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
4497+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
4498+
check_spends!(node_txn[3], chan_2.3.clone());
4499+
check_spends!(node_txn[4], node_txn[3].clone());
4500+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
4501+
assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133);
4502+
timeout_tx = node_txn[0].clone();
4503+
node_txn.clear();
4504+
}
4505+
4506+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
44504507
{
44514508
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
44524509
assert_eq!(added_monitors.len(), 1);
@@ -4468,27 +4525,29 @@ mod tests {
44684525
},
44694526
_ => panic!("Unexpected event"),
44704527
};
4528+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // Well... here we detect our own htlc_timeout_tx so no tx to be generated
4529+
assert_eq!(node_txn.len(), 0);
44714530

4472-
// Broadcast legit commitment tx from A on B's chain
4473-
// Broadcast HTLC Timeout tx by B on offered output from A commitment tx on A's chain
4474-
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4475-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
4476-
let events = nodes[1].node.get_and_clear_pending_events();
4477-
assert_eq!(events.len(), 1);
4478-
match events[0] {
4479-
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4480-
_ => panic!("Unexpected event"),
4481-
}
4482-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4531+
// Broadcast legit commitment tx from B on A's chain
4532+
let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4533+
check_spends!(commitment_tx[0], chan_1.3.clone());
44834534

4484-
// Verify that A's ChannelManager is able to detect that HTLC is timeout by a HTLC Timeout tx and react backward in consequence
4485-
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
4535+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
44864536
let events = nodes[0].node.get_and_clear_pending_events();
44874537
assert_eq!(events.len(), 1);
44884538
match events[0] {
44894539
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
44904540
_ => panic!("Unexpected event"),
44914541
}
4542+
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Timeout tx), ChannelMonitor : 2 (timeout tx) * 2 block-rescan
4543+
assert_eq!(node_txn.len(), 4);
4544+
assert_eq!(node_txn[0], node_txn[3]);
4545+
check_spends!(node_txn[0], commitment_tx[0].clone());
4546+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
4547+
check_spends!(node_txn[1], chan_1.3.clone());
4548+
check_spends!(node_txn[2], node_txn[1].clone());
4549+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
4550+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
44924551
}
44934552

44944553
#[test]

0 commit comments

Comments
 (0)