Skip to content

Commit 4a3f258

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 213eb68 commit 4a3f258

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
@@ -4260,9 +4260,9 @@ mod tests {
42604260
// ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
42614261
// broadcasting the right event to other nodes in payment path.
42624262
// A --------------------> B ----------------------> C (preimage)
4263-
// A's commitment tx C's commitment tx
4264-
// \ \
4265-
// B's preimage tx C's HTLC Success tx
4263+
// C's commitment tx
4264+
// \
4265+
// C's HTLC Success tx
42664266

42674267
let nodes = create_network(3);
42684268

@@ -4280,6 +4280,7 @@ mod tests {
42804280
// Broadcast legit commitment tx from C on B's chain
42814281
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
42824282
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4283+
check_spends!(commitment_tx[0], chan_2.3.clone());
42834284
nodes[2].node.claim_funds(payment_preimage);
42844285
{
42854286
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -4305,7 +4306,14 @@ mod tests {
43054306
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43064307
_ => panic!("Unexpected event"),
43074308
}
4308-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4309+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
4310+
assert_eq!(node_txn.len(), 3);
4311+
check_spends!(node_txn[0], commitment_tx[0].clone());
4312+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4313+
check_spends!(node_txn[1], chan_2.3.clone());
4314+
check_spends!(node_txn[2], node_txn[1].clone());
4315+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4316+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
43094317

43104318
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
43114319
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
@@ -4330,18 +4338,42 @@ mod tests {
43304338
},
43314339
_ => panic!("Unexpected event"),
43324340
};
4341+
{
4342+
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)
4343+
assert_eq!(node_txn.len(), 4);
4344+
assert_eq!(node_txn[0], node_txn[3]);
4345+
check_spends!(node_txn[0], commitment_tx[0].clone());
4346+
check_spends!(node_txn[3], commitment_tx[0].clone());
4347+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4348+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
4349+
check_spends!(node_txn[1], chan_2.3.clone());
4350+
check_spends!(node_txn[2], node_txn[1].clone());
4351+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4352+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
4353+
node_txn.clear()
4354+
}
43334355

43344356
// Broadcast legit commitment tx from A on B's chain
43354357
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
43364358
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4359+
check_spends!(commitment_tx[0], chan_1.3.clone());
43374360
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
43384361
let events = nodes[1].node.get_and_clear_pending_events();
43394362
assert_eq!(events.len(), 1);
43404363
match events[0] {
43414364
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43424365
_ => panic!("Unexpected event"),
43434366
}
4344-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4367+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
4368+
assert_eq!(node_txn.len(), 3);
4369+
assert_eq!(node_txn[0], node_txn[2]);
4370+
check_spends!(node_txn[0], commitment_tx[0].clone());
4371+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133);
4372+
check_spends!(node_txn[2], commitment_tx[0].clone());
4373+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
4374+
check_spends!(node_txn[1], chan_1.3.clone());
4375+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4376+
let commitment_tx = node_txn[1].clone();
43454377

43464378
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
43474379
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
@@ -4351,6 +4383,17 @@ mod tests {
43514383
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
43524384
_ => panic!("Unexpected event"),
43534385
}
4386+
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)
4387+
assert_eq!(node_txn.len(), 4);
4388+
assert_eq!(node_txn[0], node_txn[3]);
4389+
check_spends!(node_txn[0], commitment_tx.clone());
4390+
check_spends!(node_txn[3], commitment_tx.clone());
4391+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
4392+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
4393+
check_spends!(node_txn[1], chan_1.3.clone());
4394+
check_spends!(node_txn[2], node_txn[1].clone());
4395+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
4396+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
43544397
}
43554398

43564399
#[test]
@@ -4359,7 +4402,7 @@ mod tests {
43594402
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
43604403
// broadcasting the right event to other nodes in payment path.
43614404
// A ------------------> B ----------------------> C (timeout)
4362-
// A's commitment tx C's commitment tx
4405+
// B's commitment tx C's commitment tx
43634406
// \ \
43644407
// B's HTLC timeout tx B's timeout tx
43654408

@@ -4378,6 +4421,7 @@ mod tests {
43784421

43794422
// Brodacast legit commitment tx from C on B's chain
43804423
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
4424+
check_spends!(commitment_tx[0], chan_2.3.clone());
43814425
nodes[2].node.fail_htlc_backwards(&payment_hash);
43824426
{
43834427
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -4403,23 +4447,36 @@ mod tests {
44034447
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
44044448
_ => panic!("Unexpected event"),
44054449
}
4406-
let mut funding_tx_map = HashMap::new();
4407-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
4408-
commitment_tx[0].verify(&funding_tx_map).unwrap();
4450+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
4451+
assert_eq!(node_txn.len(), 1);
4452+
check_spends!(node_txn[0], chan_2.3.clone());
4453+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
44094454

44104455
// Broadcast timeout transaction by B on received output fron C's commitment tx on B's chain
44114456
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
44124457
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
4413-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4414-
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout), ChannelMonitor : 6 (commitment tx, HTLC-Timeout, timeout tx) * 2 (block-rescan)
4415-
assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
4416-
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
4417-
4418-
let mut commitment_tx_map = HashMap::new();
4419-
commitment_tx_map.insert(commitment_tx[0].txid(), commitment_tx[0].clone());
4420-
node_txn[0].verify(&commitment_tx_map).unwrap();
4421-
4422-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
4458+
let timeout_tx;
4459+
{
4460+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
4461+
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)
4462+
assert_eq!(node_txn[0], node_txn[5]);
4463+
assert_eq!(node_txn[1], node_txn[6]);
4464+
assert_eq!(node_txn[2], node_txn[7]);
4465+
check_spends!(node_txn[0], commitment_tx[0].clone());
4466+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
4467+
check_spends!(node_txn[1], chan_2.3.clone());
4468+
check_spends!(node_txn[2], node_txn[1].clone());
4469+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
4470+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
4471+
check_spends!(node_txn[3], chan_2.3.clone());
4472+
check_spends!(node_txn[4], node_txn[3].clone());
4473+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
4474+
assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133);
4475+
timeout_tx = node_txn[0].clone();
4476+
node_txn.clear();
4477+
}
4478+
4479+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
44234480
{
44244481
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
44254482
assert_eq!(added_monitors.len(), 1);
@@ -4441,27 +4498,29 @@ mod tests {
44414498
},
44424499
_ => panic!("Unexpected event"),
44434500
};
4501+
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
4502+
assert_eq!(node_txn.len(), 0);
44444503

4445-
// Broadcast legit commitment tx from A on B's chain
4446-
// Broadcast HTLC Timeout tx by B on offered output from A commitment tx on A's chain
4447-
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4448-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
4449-
let events = nodes[1].node.get_and_clear_pending_events();
4450-
assert_eq!(events.len(), 1);
4451-
match events[0] {
4452-
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
4453-
_ => panic!("Unexpected event"),
4454-
}
4455-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
4504+
// Broadcast legit commitment tx from B on A's chain
4505+
let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
4506+
check_spends!(commitment_tx[0], chan_1.3.clone());
44564507

4457-
// Verify that A's ChannelManager is able to detect that HTLC is timeout by a HTLC Timeout tx and react backward in consequence
4458-
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
4508+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
44594509
let events = nodes[0].node.get_and_clear_pending_events();
44604510
assert_eq!(events.len(), 1);
44614511
match events[0] {
44624512
Event::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
44634513
_ => panic!("Unexpected event"),
44644514
}
4515+
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
4516+
assert_eq!(node_txn.len(), 4);
4517+
assert_eq!(node_txn[0], node_txn[3]);
4518+
check_spends!(node_txn[0], commitment_tx[0].clone());
4519+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
4520+
check_spends!(node_txn[1], chan_1.3.clone());
4521+
check_spends!(node_txn[2], node_txn[1].clone());
4522+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
4523+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
44654524
}
44664525

44674526
#[test]

0 commit comments

Comments
 (0)