Skip to content

Commit 3451246

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 d875059 commit 3451246

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

60866086
let nodes = create_network(3);
60876087

@@ -6099,6 +6099,7 @@ mod tests {
60996099
// Broadcast legit commitment tx from C on B's chain
61006100
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
61016101
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6102+
check_spends!(commitment_tx[0], chan_2.3.clone());
61026103
nodes[2].node.claim_funds(payment_preimage);
61036104
{
61046105
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6124,7 +6125,14 @@ mod tests {
61246125
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61256126
_ => panic!("Unexpected event"),
61266127
}
6127-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6128+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
6129+
assert_eq!(node_txn.len(), 3);
6130+
check_spends!(node_txn[0], commitment_tx[0].clone());
6131+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6132+
check_spends!(node_txn[1], chan_2.3.clone());
6133+
check_spends!(node_txn[2], node_txn[1].clone());
6134+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6135+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
61286136

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

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

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

61756218
#[test]
@@ -6178,7 +6221,7 @@ mod tests {
61786221
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
61796222
// broadcasting the right event to other nodes in payment path.
61806223
// A ------------------> B ----------------------> C (timeout)
6181-
// A's commitment tx C's commitment tx
6224+
// B's commitment tx C's commitment tx
61826225
// \ \
61836226
// B's HTLC timeout tx B's timeout tx
61846227

@@ -6197,6 +6240,7 @@ mod tests {
61976240

61986241
// Brodacast legit commitment tx from C on B's chain
61996242
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6243+
check_spends!(commitment_tx[0], chan_2.3.clone());
62006244
nodes[2].node.fail_htlc_backwards(&payment_hash, PaymentFailReason::PreimageUnknown);
62016245
{
62026246
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6222,23 +6266,36 @@ mod tests {
62226266
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
62236267
_ => panic!("Unexpected event"),
62246268
}
6225-
let mut funding_tx_map = HashMap::new();
6226-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
6227-
commitment_tx[0].verify(&funding_tx_map).unwrap();
6269+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
6270+
assert_eq!(node_txn.len(), 1);
6271+
check_spends!(node_txn[0], chan_2.3.clone());
6272+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
62286273

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

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

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

62866345
#[test]

0 commit comments

Comments
 (0)