Skip to content

Commit 3fb6ef0

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 b7e83a1 commit 3fb6ef0

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

60906090
let nodes = create_network(3);
60916091

@@ -6103,6 +6103,7 @@ mod tests {
61036103
// Broadcast legit commitment tx from C on B's chain
61046104
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
61056105
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6106+
check_spends!(commitment_tx[0], chan_2.3.clone());
61066107
nodes[2].node.claim_funds(payment_preimage);
61076108
{
61086109
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6128,7 +6129,14 @@ mod tests {
61286129
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61296130
_ => panic!("Unexpected event"),
61306131
}
6131-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6132+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
6133+
assert_eq!(node_txn.len(), 3);
6134+
check_spends!(node_txn[0], commitment_tx[0].clone());
6135+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6136+
check_spends!(node_txn[1], chan_2.3.clone());
6137+
check_spends!(node_txn[2], node_txn[1].clone());
6138+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6139+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
61326140

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

61576179
// Broadcast legit commitment tx from A on B's chain
61586180
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
61596181
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6182+
check_spends!(commitment_tx[0], chan_1.3.clone());
61606183
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
61616184
let events = nodes[1].node.get_and_clear_pending_msg_events();
61626185
assert_eq!(events.len(), 1);
61636186
match events[0] {
61646187
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61656188
_ => panic!("Unexpected event"),
61666189
}
6167-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6190+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
6191+
assert_eq!(node_txn.len(), 3);
6192+
assert_eq!(node_txn[0], node_txn[2]);
6193+
check_spends!(node_txn[0], commitment_tx[0].clone());
6194+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133);
6195+
check_spends!(node_txn[2], commitment_tx[0].clone());
6196+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
6197+
check_spends!(node_txn[1], chan_1.3.clone());
6198+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6199+
let commitment_tx = node_txn[1].clone();
61686200

61696201
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
61706202
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
@@ -6174,6 +6206,17 @@ mod tests {
61746206
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61756207
_ => panic!("Unexpected event"),
61766208
}
6209+
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)
6210+
assert_eq!(node_txn.len(), 4);
6211+
assert_eq!(node_txn[0], node_txn[3]);
6212+
check_spends!(node_txn[0], commitment_tx.clone());
6213+
check_spends!(node_txn[3], commitment_tx.clone());
6214+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6215+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
6216+
check_spends!(node_txn[1], chan_1.3.clone());
6217+
check_spends!(node_txn[2], node_txn[1].clone());
6218+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6219+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
61776220
}
61786221

61796222
#[test]
@@ -6182,7 +6225,7 @@ mod tests {
61826225
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
61836226
// broadcasting the right event to other nodes in payment path.
61846227
// A ------------------> B ----------------------> C (timeout)
6185-
// A's commitment tx C's commitment tx
6228+
// B's commitment tx C's commitment tx
61866229
// \ \
61876230
// B's HTLC timeout tx B's timeout tx
61886231

@@ -6201,6 +6244,7 @@ mod tests {
62016244

62026245
// Brodacast legit commitment tx from C on B's chain
62036246
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6247+
check_spends!(commitment_tx[0], chan_2.3.clone());
62046248
nodes[2].node.fail_htlc_backwards(&payment_hash, PaymentFailReason::PreimageUnknown);
62056249
{
62066250
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6226,23 +6270,36 @@ mod tests {
62266270
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
62276271
_ => panic!("Unexpected event"),
62286272
}
6229-
let mut funding_tx_map = HashMap::new();
6230-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
6231-
commitment_tx[0].verify(&funding_tx_map).unwrap();
6273+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
6274+
assert_eq!(node_txn.len(), 1);
6275+
check_spends!(node_txn[0], chan_2.3.clone());
6276+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
62326277

62336278
// Broadcast timeout transaction by B on received output fron C's commitment tx on B's chain
62346279
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
62356280
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
6236-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6237-
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout), ChannelMonitor : 6 (commitment tx, HTLC-Timeout, timeout tx) * 2 (block-rescan)
6238-
assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
6239-
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
6240-
6241-
let mut commitment_tx_map = HashMap::new();
6242-
commitment_tx_map.insert(commitment_tx[0].txid(), commitment_tx[0].clone());
6243-
node_txn[0].verify(&commitment_tx_map).unwrap();
6244-
6245-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
6281+
let timeout_tx;
6282+
{
6283+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
6284+
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)
6285+
assert_eq!(node_txn[0], node_txn[5]);
6286+
assert_eq!(node_txn[1], node_txn[6]);
6287+
assert_eq!(node_txn[2], node_txn[7]);
6288+
check_spends!(node_txn[0], commitment_tx[0].clone());
6289+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
6290+
check_spends!(node_txn[1], chan_2.3.clone());
6291+
check_spends!(node_txn[2], node_txn[1].clone());
6292+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
6293+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
6294+
check_spends!(node_txn[3], chan_2.3.clone());
6295+
check_spends!(node_txn[4], node_txn[3].clone());
6296+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
6297+
assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133);
6298+
timeout_tx = node_txn[0].clone();
6299+
node_txn.clear();
6300+
}
6301+
6302+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
62466303
{
62476304
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
62486305
assert_eq!(added_monitors.len(), 1);
@@ -6264,27 +6321,29 @@ mod tests {
62646321
},
62656322
_ => panic!("Unexpected event"),
62666323
};
6324+
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
6325+
assert_eq!(node_txn.len(), 0);
62676326

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

6280-
// Verify that A's ChannelManager is able to detect that HTLC is timeout by a HTLC Timeout tx and react backward in consequence
6281-
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
6331+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
62826332
let events = nodes[0].node.get_and_clear_pending_msg_events();
62836333
assert_eq!(events.len(), 1);
62846334
match events[0] {
62856335
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
62866336
_ => panic!("Unexpected event"),
62876337
}
6338+
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
6339+
assert_eq!(node_txn.len(), 4);
6340+
assert_eq!(node_txn[0], node_txn[3]);
6341+
check_spends!(node_txn[0], commitment_tx[0].clone());
6342+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
6343+
check_spends!(node_txn[1], chan_1.3.clone());
6344+
check_spends!(node_txn[2], node_txn[1].clone());
6345+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
6346+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
62886347
}
62896348

62906349
#[test]

0 commit comments

Comments
 (0)