Skip to content

Commit 2bd7e48

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 5b2b798 commit 2bd7e48

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
@@ -6077,9 +6077,9 @@ mod tests {
60776077
// ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
60786078
// broadcasting the right event to other nodes in payment path.
60796079
// A --------------------> B ----------------------> C (preimage)
6080-
// A's commitment tx C's commitment tx
6081-
// \ \
6082-
// B's preimage tx C's HTLC Success tx
6080+
// C's commitment tx
6081+
// \
6082+
// C's HTLC Success tx
60836083

60846084
let nodes = create_network(3);
60856085

@@ -6097,6 +6097,7 @@ mod tests {
60976097
// Broadcast legit commitment tx from C on B's chain
60986098
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
60996099
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6100+
check_spends!(commitment_tx[0], chan_2.3.clone());
61006101
nodes[2].node.claim_funds(payment_preimage);
61016102
{
61026103
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6122,7 +6123,14 @@ mod tests {
61226123
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61236124
_ => panic!("Unexpected event"),
61246125
}
6125-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6126+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
6127+
assert_eq!(node_txn.len(), 3);
6128+
check_spends!(node_txn[0], commitment_tx[0].clone());
6129+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6130+
check_spends!(node_txn[1], chan_2.3.clone());
6131+
check_spends!(node_txn[2], node_txn[1].clone());
6132+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6133+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
61266134

61276135
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
61286136
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
@@ -6147,18 +6155,42 @@ mod tests {
61476155
},
61486156
_ => panic!("Unexpected event"),
61496157
};
6158+
{
6159+
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)
6160+
assert_eq!(node_txn.len(), 4);
6161+
assert_eq!(node_txn[0], node_txn[3]);
6162+
check_spends!(node_txn[0], commitment_tx[0].clone());
6163+
check_spends!(node_txn[3], commitment_tx[0].clone());
6164+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6165+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
6166+
check_spends!(node_txn[1], chan_2.3.clone());
6167+
check_spends!(node_txn[2], node_txn[1].clone());
6168+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6169+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
6170+
node_txn.clear()
6171+
}
61506172

61516173
// Broadcast legit commitment tx from A on B's chain
61526174
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
61536175
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6176+
check_spends!(commitment_tx[0], chan_1.3.clone());
61546177
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
61556178
let events = nodes[1].node.get_and_clear_pending_msg_events();
61566179
assert_eq!(events.len(), 1);
61576180
match events[0] {
61586181
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61596182
_ => panic!("Unexpected event"),
61606183
}
6161-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6184+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
6185+
assert_eq!(node_txn.len(), 3);
6186+
assert_eq!(node_txn[0], node_txn[2]);
6187+
check_spends!(node_txn[0], commitment_tx[0].clone());
6188+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133);
6189+
check_spends!(node_txn[2], commitment_tx[0].clone());
6190+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
6191+
check_spends!(node_txn[1], chan_1.3.clone());
6192+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6193+
let commitment_tx = node_txn[1].clone();
61626194

61636195
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
61646196
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
@@ -6168,6 +6200,17 @@ mod tests {
61686200
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61696201
_ => panic!("Unexpected event"),
61706202
}
6203+
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)
6204+
assert_eq!(node_txn.len(), 4);
6205+
assert_eq!(node_txn[0], node_txn[3]);
6206+
check_spends!(node_txn[0], commitment_tx.clone());
6207+
check_spends!(node_txn[3], commitment_tx.clone());
6208+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6209+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
6210+
check_spends!(node_txn[1], chan_1.3.clone());
6211+
check_spends!(node_txn[2], node_txn[1].clone());
6212+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6213+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
61716214
}
61726215

61736216
#[test]
@@ -6176,7 +6219,7 @@ mod tests {
61766219
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
61776220
// broadcasting the right event to other nodes in payment path.
61786221
// A ------------------> B ----------------------> C (timeout)
6179-
// A's commitment tx C's commitment tx
6222+
// B's commitment tx C's commitment tx
61806223
// \ \
61816224
// B's HTLC timeout tx B's timeout tx
61826225

@@ -6195,6 +6238,7 @@ mod tests {
61956238

61966239
// Brodacast legit commitment tx from C on B's chain
61976240
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6241+
check_spends!(commitment_tx[0], chan_2.3.clone());
61986242
nodes[2].node.fail_htlc_backwards(&payment_hash, PaymentFailReason::PreimageUnknown);
61996243
{
62006244
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6220,23 +6264,36 @@ mod tests {
62206264
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
62216265
_ => panic!("Unexpected event"),
62226266
}
6223-
let mut funding_tx_map = HashMap::new();
6224-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
6225-
commitment_tx[0].verify(&funding_tx_map).unwrap();
6267+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
6268+
assert_eq!(node_txn.len(), 1);
6269+
check_spends!(node_txn[0], chan_2.3.clone());
6270+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
62266271

62276272
// Broadcast timeout transaction by B on received output fron C's commitment tx on B's chain
62286273
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
62296274
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
6230-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6231-
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout), ChannelMonitor : 6 (commitment tx, HTLC-Timeout, timeout tx) * 2 (block-rescan)
6232-
assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
6233-
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
6234-
6235-
let mut commitment_tx_map = HashMap::new();
6236-
commitment_tx_map.insert(commitment_tx[0].txid(), commitment_tx[0].clone());
6237-
node_txn[0].verify(&commitment_tx_map).unwrap();
6238-
6239-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
6275+
let timeout_tx;
6276+
{
6277+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
6278+
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)
6279+
assert_eq!(node_txn[0], node_txn[5]);
6280+
assert_eq!(node_txn[1], node_txn[6]);
6281+
assert_eq!(node_txn[2], node_txn[7]);
6282+
check_spends!(node_txn[0], commitment_tx[0].clone());
6283+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
6284+
check_spends!(node_txn[1], chan_2.3.clone());
6285+
check_spends!(node_txn[2], node_txn[1].clone());
6286+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
6287+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
6288+
check_spends!(node_txn[3], chan_2.3.clone());
6289+
check_spends!(node_txn[4], node_txn[3].clone());
6290+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
6291+
assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133);
6292+
timeout_tx = node_txn[0].clone();
6293+
node_txn.clear();
6294+
}
6295+
6296+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
62406297
{
62416298
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
62426299
assert_eq!(added_monitors.len(), 1);
@@ -6258,27 +6315,29 @@ mod tests {
62586315
},
62596316
_ => panic!("Unexpected event"),
62606317
};
6318+
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
6319+
assert_eq!(node_txn.len(), 0);
62616320

6262-
// Broadcast legit commitment tx from A on B's chain
6263-
// Broadcast HTLC Timeout tx by B on offered output from A commitment tx on A's chain
6264-
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6265-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
6266-
let events = nodes[1].node.get_and_clear_pending_msg_events();
6267-
assert_eq!(events.len(), 1);
6268-
match events[0] {
6269-
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
6270-
_ => panic!("Unexpected event"),
6271-
}
6272-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6321+
// Broadcast legit commitment tx from B on A's chain
6322+
let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6323+
check_spends!(commitment_tx[0], chan_1.3.clone());
62736324

6274-
// Verify that A's ChannelManager is able to detect that HTLC is timeout by a HTLC Timeout tx and react backward in consequence
6275-
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
6325+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
62766326
let events = nodes[0].node.get_and_clear_pending_msg_events();
62776327
assert_eq!(events.len(), 1);
62786328
match events[0] {
62796329
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
62806330
_ => panic!("Unexpected event"),
62816331
}
6332+
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
6333+
assert_eq!(node_txn.len(), 4);
6334+
assert_eq!(node_txn[0], node_txn[3]);
6335+
check_spends!(node_txn[0], commitment_tx[0].clone());
6336+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
6337+
check_spends!(node_txn[1], chan_1.3.clone());
6338+
check_spends!(node_txn[2], node_txn[1].clone());
6339+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
6340+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
62826341
}
62836342

62846343
#[test]

0 commit comments

Comments
 (0)