Skip to content

Commit aed435a

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 11e77e6 commit aed435a

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
@@ -4281,9 +4281,9 @@ mod tests {
42814281
// ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
42824282
// broadcasting the right event to other nodes in payment path.
42834283
// A --------------------> B ----------------------> C (preimage)
4284-
// A's commitment tx C's commitment tx
4285-
// \ \
4286-
// B's preimage tx C's HTLC Success tx
4284+
// C's commitment tx
4285+
// \
4286+
// C's HTLC Success tx
42874287

42884288
let nodes = create_network(3);
42894289

@@ -4301,6 +4301,7 @@ mod tests {
43014301
// Broadcast legit commitment tx from C on B's chain
43024302
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
43034303
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4304+
check_spends!(commitment_tx[0], chan_2.3.clone());
43044305
nodes[2].node.claim_funds(payment_preimage);
43054306
{
43064307
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -4326,7 +4327,14 @@ mod tests {
43264327
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43274328
_ => panic!("Unexpected event"),
43284329
}
4329-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4330+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
4331+
assert_eq!(node_txn.len(), 3);
4332+
check_spends!(node_txn[0], commitment_tx[0].clone());
4333+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4334+
check_spends!(node_txn[1], chan_2.3.clone());
4335+
check_spends!(node_txn[2], node_txn[1].clone());
4336+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4337+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
43304338

43314339
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
43324340
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
@@ -4351,18 +4359,42 @@ mod tests {
43514359
},
43524360
_ => panic!("Unexpected event"),
43534361
};
4362+
{
4363+
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)
4364+
assert_eq!(node_txn.len(), 4);
4365+
assert_eq!(node_txn[0], node_txn[3]);
4366+
check_spends!(node_txn[0], commitment_tx[0].clone());
4367+
check_spends!(node_txn[3], commitment_tx[0].clone());
4368+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4369+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
4370+
check_spends!(node_txn[1], chan_2.3.clone());
4371+
check_spends!(node_txn[2], node_txn[1].clone());
4372+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4373+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
4374+
node_txn.clear()
4375+
}
43544376

43554377
// Broadcast legit commitment tx from A on B's chain
43564378
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
43574379
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4380+
check_spends!(commitment_tx[0], chan_1.3.clone());
43584381
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
43594382
let events = nodes[1].node.get_and_clear_pending_events();
43604383
assert_eq!(events.len(), 1);
43614384
match events[0] {
43624385
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43634386
_ => panic!("Unexpected event"),
43644387
}
4365-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4388+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
4389+
assert_eq!(node_txn.len(), 3);
4390+
assert_eq!(node_txn[0], node_txn[2]);
4391+
check_spends!(node_txn[0], commitment_tx[0].clone());
4392+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133);
4393+
check_spends!(node_txn[2], commitment_tx[0].clone());
4394+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
4395+
check_spends!(node_txn[1], chan_1.3.clone());
4396+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4397+
let commitment_tx = node_txn[1].clone();
43664398

43674399
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
43684400
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
@@ -4372,6 +4404,17 @@ mod tests {
43724404
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43734405
_ => panic!("Unexpected event"),
43744406
}
4407+
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)
4408+
assert_eq!(node_txn.len(), 4);
4409+
assert_eq!(node_txn[0], node_txn[3]);
4410+
check_spends!(node_txn[0], commitment_tx.clone());
4411+
check_spends!(node_txn[3], commitment_tx.clone());
4412+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4413+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
4414+
check_spends!(node_txn[1], chan_1.3.clone());
4415+
check_spends!(node_txn[2], node_txn[1].clone());
4416+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4417+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
43754418
}
43764419

43774420
#[test]
@@ -4380,7 +4423,7 @@ mod tests {
43804423
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
43814424
// broadcasting the right event to other nodes in payment path.
43824425
// A ------------------> B ----------------------> C (timeout)
4383-
// A's commitment tx C's commitment tx
4426+
// B's commitment tx C's commitment tx
43844427
// \ \
43854428
// B's HTLC timeout tx B's timeout tx
43864429

@@ -4399,6 +4442,7 @@ mod tests {
43994442

44004443
// Brodacast legit commitment tx from C on B's chain
44014444
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4445+
check_spends!(commitment_tx[0], chan_2.3.clone());
44024446
nodes[2].node.fail_htlc_backwards(&payment_hash);
44034447
{
44044448
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -4424,23 +4468,36 @@ mod tests {
44244468
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
44254469
_ => panic!("Unexpected event"),
44264470
}
4427-
let mut funding_tx_map = HashMap::new();
4428-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
4429-
commitment_tx[0].verify(&funding_tx_map).unwrap();
4471+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
4472+
assert_eq!(node_txn.len(), 1);
4473+
check_spends!(node_txn[0], chan_2.3.clone());
4474+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
44304475

44314476
// Broadcast timeout transaction by B on received output fron C's commitment tx on B's chain
44324477
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
44334478
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
4434-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4435-
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout), ChannelMonitor : 6 (commitment tx, HTLC-Timeout, timeout tx) * 2 (block-rescan)
4436-
assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
4437-
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
4438-
4439-
let mut commitment_tx_map = HashMap::new();
4440-
commitment_tx_map.insert(commitment_tx[0].txid(), commitment_tx[0].clone());
4441-
node_txn[0].verify(&commitment_tx_map).unwrap();
4442-
4443-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
4479+
let timeout_tx;
4480+
{
4481+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
4482+
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)
4483+
assert_eq!(node_txn[0], node_txn[5]);
4484+
assert_eq!(node_txn[1], node_txn[6]);
4485+
assert_eq!(node_txn[2], node_txn[7]);
4486+
check_spends!(node_txn[0], commitment_tx[0].clone());
4487+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
4488+
check_spends!(node_txn[1], chan_2.3.clone());
4489+
check_spends!(node_txn[2], node_txn[1].clone());
4490+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
4491+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
4492+
check_spends!(node_txn[3], chan_2.3.clone());
4493+
check_spends!(node_txn[4], node_txn[3].clone());
4494+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
4495+
assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133);
4496+
timeout_tx = node_txn[0].clone();
4497+
node_txn.clear();
4498+
}
4499+
4500+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
44444501
{
44454502
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
44464503
assert_eq!(added_monitors.len(), 1);
@@ -4462,27 +4519,29 @@ mod tests {
44624519
},
44634520
_ => panic!("Unexpected event"),
44644521
};
4522+
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
4523+
assert_eq!(node_txn.len(), 0);
44654524

4466-
// Broadcast legit commitment tx from A on B's chain
4467-
// Broadcast HTLC Timeout tx by B on offered output from A commitment tx on A's chain
4468-
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4469-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
4470-
let events = nodes[1].node.get_and_clear_pending_events();
4471-
assert_eq!(events.len(), 1);
4472-
match events[0] {
4473-
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4474-
_ => panic!("Unexpected event"),
4475-
}
4476-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4525+
// Broadcast legit commitment tx from B on A's chain
4526+
let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4527+
check_spends!(commitment_tx[0], chan_1.3.clone());
44774528

4478-
// Verify that A's ChannelManager is able to detect that HTLC is timeout by a HTLC Timeout tx and react backward in consequence
4479-
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
4529+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
44804530
let events = nodes[0].node.get_and_clear_pending_events();
44814531
assert_eq!(events.len(), 1);
44824532
match events[0] {
44834533
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
44844534
_ => panic!("Unexpected event"),
44854535
}
4536+
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
4537+
assert_eq!(node_txn.len(), 4);
4538+
assert_eq!(node_txn[0], node_txn[3]);
4539+
check_spends!(node_txn[0], commitment_tx[0].clone());
4540+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
4541+
check_spends!(node_txn[1], chan_1.3.clone());
4542+
check_spends!(node_txn[2], node_txn[1].clone());
4543+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
4544+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
44864545
}
44874546

44884547
#[test]

0 commit comments

Comments
 (0)