Skip to content

Commit c639920

Browse files
committed
Make tests more robust against different connection styles
In the next commit we'll randomize the `ConnectStyle` used in each test. However, some tests are slightly too prescriptive, which we address here in a few places.
1 parent 9ac483b commit c639920

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,23 +1280,41 @@ fn test_duplicate_htlc_different_direction_onchain() {
12801280
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
12811281
connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
12821282

1283-
// Check we only broadcast 1 timeout tx
12841283
let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
12851284
assert_eq!(claim_txn.len(), 8);
1286-
assert_eq!(claim_txn[1], claim_txn[4]);
1287-
assert_eq!(claim_txn[2], claim_txn[5]);
1288-
check_spends!(claim_txn[1], chan_1.3);
1289-
check_spends!(claim_txn[2], claim_txn[1]);
1290-
check_spends!(claim_txn[7], claim_txn[1]);
1285+
1286+
check_spends!(claim_txn[0], remote_txn[0]); // Immediate HTLC claim with preimage
1287+
1288+
check_spends!(claim_txn[1], chan_1.3); // Alternative commitment tx
1289+
check_spends!(claim_txn[2], claim_txn[1]); // HTLC spend in alternative commitment tx
1290+
1291+
let bump_tx = if claim_txn[1] == claim_txn[4] {
1292+
assert_eq!(claim_txn[1], claim_txn[4]);
1293+
assert_eq!(claim_txn[2], claim_txn[5]);
1294+
1295+
check_spends!(claim_txn[7], claim_txn[1]); // HTLC timeout on alternative commitment tx
1296+
1297+
check_spends!(claim_txn[3], remote_txn[0]); // HTLC timeout on broadcasted commitment tx
1298+
&claim_txn[3]
1299+
} else {
1300+
assert_eq!(claim_txn[1], claim_txn[3]);
1301+
assert_eq!(claim_txn[2], claim_txn[4]);
1302+
1303+
check_spends!(claim_txn[5], claim_txn[1]); // HTLC timeout on alternative commitment tx
1304+
1305+
check_spends!(claim_txn[7], remote_txn[0]); // HTLC timeout on broadcasted commitment tx
1306+
1307+
&claim_txn[7]
1308+
};
12911309

12921310
assert_eq!(claim_txn[0].input.len(), 1);
1293-
assert_eq!(claim_txn[3].input.len(), 1);
1294-
assert_eq!(claim_txn[0].input[0].previous_output, claim_txn[3].input[0].previous_output);
1311+
assert_eq!(bump_tx.input.len(), 1);
1312+
assert_eq!(claim_txn[0].input[0].previous_output, bump_tx.input[0].previous_output);
12951313

12961314
assert_eq!(claim_txn[0].input.len(), 1);
12971315
assert_eq!(claim_txn[0].input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT); // HTLC 1 <--> 0, preimage tx
1298-
check_spends!(claim_txn[0], remote_txn[0]);
12991316
assert_eq!(remote_txn[0].output[claim_txn[0].input[0].previous_output.vout as usize].value, 800);
1317+
13001318
assert_eq!(claim_txn[6].input.len(), 1);
13011319
assert_eq!(claim_txn[6].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // HTLC 0 <--> 1, timeout tx
13021320
check_spends!(claim_txn[6], remote_txn[0]);
@@ -2351,7 +2369,8 @@ fn test_justice_tx() {
23512369
chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
23522370
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
23532371
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2354-
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2372+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2373+
*nodes[0].connect_style.borrow_mut() = ConnectStyle::FullBlockViaListen;
23552374
// Create some new channels:
23562375
let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
23572376

@@ -2583,11 +2602,7 @@ fn claim_htlc_outputs_single_tx() {
25832602
expect_payment_failed!(nodes[1], payment_hash_2, true);
25842603

25852604
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2586-
assert_eq!(node_txn.len(), 9);
2587-
// ChannelMonitor: justice tx revoked offered htlc, justice tx revoked received htlc, justice tx revoked to_local (3)
2588-
// ChannelManager: local commmitment + local HTLC-timeout (2)
2589-
// ChannelMonitor: bumped justice tx, after one increase, bumps on HTLC aren't generated not being substantial anymore, bump on revoked to_local isn't generated due to more room for expiration (2)
2590-
// ChannelMonitor: local commitment + local HTLC-timeout (2)
2605+
assert!(node_txn.len() == 9 || node_txn.len() == 10);
25912606

25922607
// Check the pair local commitment and HTLC-timeout broadcast due to HTLC expiration
25932608
assert_eq!(node_txn[0].input.len(), 1);
@@ -5283,21 +5298,30 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
52835298
let htlc_timeout_tx;
52845299
{ // Extract one of the two HTLC-Timeout transaction
52855300
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
5286-
// ChannelMonitor: timeout tx * 3, ChannelManager: local commitment tx
5287-
assert_eq!(node_txn.len(), 4);
5301+
// ChannelMonitor: timeout tx * 2-or-3, ChannelManager: local commitment tx
5302+
assert!(node_txn.len() == 4 || node_txn.len() == 3);
52885303
check_spends!(node_txn[0], chan_2.3);
52895304

52905305
check_spends!(node_txn[1], commitment_txn[0]);
52915306
assert_eq!(node_txn[1].input.len(), 1);
5292-
check_spends!(node_txn[2], commitment_txn[0]);
5293-
assert_eq!(node_txn[2].input.len(), 1);
5294-
assert_eq!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
5295-
check_spends!(node_txn[3], commitment_txn[0]);
5296-
assert_ne!(node_txn[1].input[0].previous_output, node_txn[3].input[0].previous_output);
5307+
5308+
if node_txn.len() > 3 {
5309+
check_spends!(node_txn[2], commitment_txn[0]);
5310+
assert_eq!(node_txn[2].input.len(), 1);
5311+
assert_eq!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
5312+
5313+
check_spends!(node_txn[3], commitment_txn[0]);
5314+
assert_ne!(node_txn[1].input[0].previous_output, node_txn[3].input[0].previous_output);
5315+
} else {
5316+
check_spends!(node_txn[2], commitment_txn[0]);
5317+
assert_ne!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
5318+
}
52975319

52985320
assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
52995321
assert_eq!(node_txn[2].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5300-
assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5322+
if node_txn.len() > 3 {
5323+
assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5324+
}
53015325
htlc_timeout_tx = node_txn[1].clone();
53025326
}
53035327

@@ -7957,13 +7981,24 @@ fn test_bump_penalty_txn_on_remote_commitment() {
79577981
assert_eq!(node_txn[6].input.len(), 1);
79587982
check_spends!(node_txn[0], remote_txn[0]);
79597983
check_spends!(node_txn[6], remote_txn[0]);
7960-
assert_eq!(node_txn[0].input[0].previous_output, node_txn[3].input[0].previous_output);
7961-
preimage_bump = node_txn[3].clone();
79627984

79637985
check_spends!(node_txn[1], chan.3);
79647986
check_spends!(node_txn[2], node_txn[1]);
7965-
assert_eq!(node_txn[1], node_txn[4]);
7966-
assert_eq!(node_txn[2], node_txn[5]);
7987+
7988+
if node_txn[0].input[0].previous_output == node_txn[3].input[0].previous_output {
7989+
preimage_bump = node_txn[3].clone();
7990+
check_spends!(node_txn[3], remote_txn[0]);
7991+
7992+
assert_eq!(node_txn[1], node_txn[4]);
7993+
assert_eq!(node_txn[2], node_txn[5]);
7994+
} else {
7995+
preimage_bump = node_txn[7].clone();
7996+
check_spends!(node_txn[7], remote_txn[0]);
7997+
assert_eq!(node_txn[0].input[0].previous_output, node_txn[7].input[0].previous_output);
7998+
7999+
assert_eq!(node_txn[1], node_txn[3]);
8000+
assert_eq!(node_txn[2], node_txn[4]);
8001+
}
79678002

79688003
timeout = node_txn[6].txid();
79698004
let index = node_txn[6].input[0].previous_output.vout;

lightning/src/ln/payment_tests.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
367367
let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
368368
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
369369

370-
let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
370+
let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
371371
let (_, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
372372

373373
// Serialize the ChannelManager prior to sending payments
@@ -504,14 +504,19 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
504504
expect_payment_sent!(nodes[0], payment_preimage_1);
505505
connect_blocks(&nodes[0], TEST_FINAL_CLTV*4 + 20);
506506
let as_htlc_timeout_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
507-
check_spends!(as_htlc_timeout_txn[2], funding_tx);
508-
check_spends!(as_htlc_timeout_txn[0], as_commitment_tx);
509-
check_spends!(as_htlc_timeout_txn[1], as_commitment_tx);
510507
assert_eq!(as_htlc_timeout_txn.len(), 3);
511-
if as_htlc_timeout_txn[0].input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
512-
confirm_transaction(&nodes[0], &as_htlc_timeout_txn[1]);
508+
let (first_htlc_timeout_tx, second_htlc_timeout_tx) = if as_htlc_timeout_txn[0] == as_commitment_tx {
509+
(&as_htlc_timeout_txn[1], &as_htlc_timeout_txn[2])
513510
} else {
514-
confirm_transaction(&nodes[0], &as_htlc_timeout_txn[0]);
511+
assert_eq!(as_htlc_timeout_txn[2], as_commitment_tx);
512+
(&as_htlc_timeout_txn[0], &as_htlc_timeout_txn[1])
513+
};
514+
check_spends!(first_htlc_timeout_tx, as_commitment_tx);
515+
check_spends!(second_htlc_timeout_tx, as_commitment_tx);
516+
if first_htlc_timeout_tx.input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
517+
confirm_transaction(&nodes[0], &second_htlc_timeout_tx);
518+
} else {
519+
confirm_transaction(&nodes[0], &first_htlc_timeout_tx);
515520
}
516521
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
517522
expect_payment_failed_conditions!(nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
@@ -627,7 +632,8 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
627632
let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
628633
let mon_updates: Vec<_> = chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap()
629634
.get_mut(&funding_txo).unwrap().drain().collect();
630-
assert_eq!(mon_updates.len(), 1);
635+
// If we are using chain::Confirm instead of chain::Listen, we will get the same update twice
636+
assert!(mon_updates.len() == 1 || mon_updates.len() == 2);
631637
assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
632638
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
633639

@@ -643,7 +649,9 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
643649
chanmon_cfgs[0].persister.set_update_ret(Ok(()));
644650
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
645651
get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
646-
nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, mon_updates[0]).unwrap();
652+
for update in mon_updates {
653+
nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, update).unwrap();
654+
}
647655
if payment_timeout {
648656
expect_payment_failed!(nodes[0], payment_hash, true);
649657
} else {

0 commit comments

Comments
 (0)