Skip to content

Commit bbf99aa

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 a2c725c commit bbf99aa

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
@@ -6013,9 +6013,9 @@ mod tests {
60136013
// ChainWatchInterface and pass the preimage backward accordingly. So here we test that ChannelManager is
60146014
// broadcasting the right event to other nodes in payment path.
60156015
// A --------------------> B ----------------------> C (preimage)
6016-
// A's commitment tx C's commitment tx
6017-
// \ \
6018-
// B's preimage tx C's HTLC Success tx
6016+
// C's commitment tx
6017+
// \
6018+
// C's HTLC Success tx
60196019

60206020
let nodes = create_network(3);
60216021

@@ -6033,6 +6033,7 @@ mod tests {
60336033
// Broadcast legit commitment tx from C on B's chain
60346034
// Broadcast HTLC Success transation by C on received output from C's commitment tx on B's chain
60356035
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6036+
check_spends!(commitment_tx[0], chan_2.3.clone());
60366037
nodes[2].node.claim_funds(payment_preimage);
60376038
{
60386039
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6058,7 +6059,14 @@ mod tests {
60586059
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
60596060
_ => panic!("Unexpected event"),
60606061
}
6061-
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6062+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 2 (commitment tx, HTLC-Success tx), ChannelMonitor : 1 (HTLC-Success tx)
6063+
assert_eq!(node_txn.len(), 3);
6064+
check_spends!(node_txn[0], commitment_tx[0].clone());
6065+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6066+
check_spends!(node_txn[1], chan_2.3.clone());
6067+
check_spends!(node_txn[2], node_txn[1].clone());
6068+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6069+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 138);
60626070

60636071
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
60646072
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn}, 1);
@@ -6083,18 +6091,42 @@ mod tests {
60836091
},
60846092
_ => panic!("Unexpected event"),
60856093
};
6094+
{
6095+
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)
6096+
assert_eq!(node_txn.len(), 4);
6097+
assert_eq!(node_txn[0], node_txn[3]);
6098+
check_spends!(node_txn[0], commitment_tx[0].clone());
6099+
check_spends!(node_txn[3], commitment_tx[0].clone());
6100+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6101+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
6102+
check_spends!(node_txn[1], chan_2.3.clone());
6103+
check_spends!(node_txn[2], node_txn[1].clone());
6104+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6105+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
6106+
node_txn.clear()
6107+
}
60866108

60876109
// Broadcast legit commitment tx from A on B's chain
60886110
// Broadcast preimage tx by B on offered output from A commitment tx on A's chain
60896111
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6112+
check_spends!(commitment_tx[0], chan_1.3.clone());
60906113
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
60916114
let events = nodes[1].node.get_and_clear_pending_msg_events();
60926115
assert_eq!(events.len(), 1);
60936116
match events[0] {
60946117
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
60956118
_ => panic!("Unexpected event"),
60966119
}
6097-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6120+
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx), ChannelMonitor : 1 (HTLC-Success) * 2 (block-rescan)
6121+
assert_eq!(node_txn.len(), 3);
6122+
assert_eq!(node_txn[0], node_txn[2]);
6123+
check_spends!(node_txn[0], commitment_tx[0].clone());
6124+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 133);
6125+
check_spends!(node_txn[2], commitment_tx[0].clone());
6126+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
6127+
check_spends!(node_txn[1], chan_1.3.clone());
6128+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6129+
let commitment_tx = node_txn[1].clone();
60986130

60996131
// Verify that A's ChannelManager is able to extract preimage from preimage tx and pass it backward
61006132
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
@@ -6104,6 +6136,17 @@ mod tests {
61046136
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
61056137
_ => panic!("Unexpected event"),
61066138
}
6139+
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)
6140+
assert_eq!(node_txn.len(), 4);
6141+
assert_eq!(node_txn[0], node_txn[3]);
6142+
check_spends!(node_txn[0], commitment_tx.clone());
6143+
check_spends!(node_txn[3], commitment_tx.clone());
6144+
assert_eq!(node_txn[0].input[0].witness.clone().last().unwrap().len(), 138);
6145+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 138);
6146+
check_spends!(node_txn[1], chan_1.3.clone());
6147+
check_spends!(node_txn[2], node_txn[1].clone());
6148+
assert_eq!(node_txn[1].input[0].witness.clone().last().unwrap().len(), 71);
6149+
assert_eq!(node_txn[2].input[0].witness.clone().last().unwrap().len(), 133);
61076150
}
61086151

61096152
#[test]
@@ -6112,7 +6155,7 @@ mod tests {
61126155
// ChainWatchInterface and timeout the HTLC bacward accordingly. So here we test that ChannelManager is
61136156
// broadcasting the right event to other nodes in payment path.
61146157
// A ------------------> B ----------------------> C (timeout)
6115-
// A's commitment tx C's commitment tx
6158+
// B's commitment tx C's commitment tx
61166159
// \ \
61176160
// B's HTLC timeout tx B's timeout tx
61186161

@@ -6131,6 +6174,7 @@ mod tests {
61316174

61326175
// Brodacast legit commitment tx from C on B's chain
61336176
let commitment_tx = nodes[2].node.channel_state.lock().unwrap().by_id.get(&chan_2.2).unwrap().last_local_commitment_txn.clone();
6177+
check_spends!(commitment_tx[0], chan_2.3.clone());
61346178
nodes[2].node.fail_htlc_backwards(&payment_hash, PaymentFailReason::PreimageUnknown);
61356179
{
61366180
let mut added_monitors = nodes[2].chan_monitor.added_monitors.lock().unwrap();
@@ -6156,23 +6200,36 @@ mod tests {
61566200
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
61576201
_ => panic!("Unexpected event"),
61586202
}
6159-
let mut funding_tx_map = HashMap::new();
6160-
funding_tx_map.insert(chan_2.3.txid(), chan_2.3.clone());
6161-
commitment_tx[0].verify(&funding_tx_map).unwrap();
6203+
let node_txn = nodes[2].tx_broadcaster.txn_broadcasted.lock().unwrap().clone(); // ChannelManager : 1 (commitment tx)
6204+
assert_eq!(node_txn.len(), 1);
6205+
check_spends!(node_txn[0], chan_2.3.clone());
6206+
assert_eq!(node_txn[0].input[0].witness.last().unwrap().len(), 71);
61626207

61636208
// Broadcast timeout transaction by B on received output fron C's commitment tx on B's chain
61646209
// Verify that B's ChannelManager is able to detect that HTLC is timeout by its own tx and react backward in consequence
61656210
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
6166-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6167-
assert_eq!(node_txn.len(), 8); // ChannelManager : 2 (commitment tx, HTLC-Timeout), ChannelMonitor : 6 (commitment tx, HTLC-Timeout, timeout tx) * 2 (block-rescan)
6168-
assert_eq!(node_txn[2].input[0].previous_output.txid, node_txn[1].txid());
6169-
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
6170-
6171-
let mut commitment_tx_map = HashMap::new();
6172-
commitment_tx_map.insert(commitment_tx[0].txid(), commitment_tx[0].clone());
6173-
node_txn[0].verify(&commitment_tx_map).unwrap();
6174-
6175-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![node_txn[0].clone()]}, 1);
6211+
let timeout_tx;
6212+
{
6213+
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
6214+
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)
6215+
assert_eq!(node_txn[0], node_txn[5]);
6216+
assert_eq!(node_txn[1], node_txn[6]);
6217+
assert_eq!(node_txn[2], node_txn[7]);
6218+
check_spends!(node_txn[0], commitment_tx[0].clone());
6219+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
6220+
check_spends!(node_txn[1], chan_2.3.clone());
6221+
check_spends!(node_txn[2], node_txn[1].clone());
6222+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
6223+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
6224+
check_spends!(node_txn[3], chan_2.3.clone());
6225+
check_spends!(node_txn[4], node_txn[3].clone());
6226+
assert_eq!(node_txn[3].input[0].witness.clone().last().unwrap().len(), 71);
6227+
assert_eq!(node_txn[4].input[0].witness.clone().last().unwrap().len(), 133);
6228+
timeout_tx = node_txn[0].clone();
6229+
node_txn.clear();
6230+
}
6231+
6232+
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![timeout_tx]}, 1);
61766233
{
61776234
let mut added_monitors = nodes[1].chan_monitor.added_monitors.lock().unwrap();
61786235
assert_eq!(added_monitors.len(), 1);
@@ -6194,27 +6251,29 @@ mod tests {
61946251
},
61956252
_ => panic!("Unexpected event"),
61966253
};
6254+
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
6255+
assert_eq!(node_txn.len(), 0);
61976256

6198-
// Broadcast legit commitment tx from A on B's chain
6199-
// Broadcast HTLC Timeout tx by B on offered output from A commitment tx on A's chain
6200-
let commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6201-
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 1);
6202-
let events = nodes[1].node.get_and_clear_pending_msg_events();
6203-
assert_eq!(events.len(), 1);
6204-
match events[0] {
6205-
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
6206-
_ => panic!("Unexpected event"),
6207-
}
6208-
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
6257+
// Broadcast legit commitment tx from B on A's chain
6258+
let commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan_1.2).unwrap().last_local_commitment_txn.clone();
6259+
check_spends!(commitment_tx[0], chan_1.3.clone());
62096260

6210-
// Verify that A's ChannelManager is able to detect that HTLC is timeout by a HTLC Timeout tx and react backward in consequence
6211-
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: node_txn }, 1);
6261+
nodes[0].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![commitment_tx[0].clone()]}, 200);
62126262
let events = nodes[0].node.get_and_clear_pending_msg_events();
62136263
assert_eq!(events.len(), 1);
62146264
match events[0] {
62156265
MessageSendEvent::BroadcastChannelUpdate { msg: msgs::ChannelUpdate { .. } } => {},
62166266
_ => panic!("Unexpected event"),
62176267
}
6268+
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
6269+
assert_eq!(node_txn.len(), 4);
6270+
assert_eq!(node_txn[0], node_txn[3]);
6271+
check_spends!(node_txn[0], commitment_tx[0].clone());
6272+
assert_eq!(node_txn[0].clone().input[0].witness.last().unwrap().len(), 138);
6273+
check_spends!(node_txn[1], chan_1.3.clone());
6274+
check_spends!(node_txn[2], node_txn[1].clone());
6275+
assert_eq!(node_txn[1].clone().input[0].witness.last().unwrap().len(), 71);
6276+
assert_eq!(node_txn[2].clone().input[0].witness.last().unwrap().len(), 133);
62186277
}
62196278

62206279
#[test]

0 commit comments

Comments
 (0)