Skip to content

Commit 2ea5229

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 3ca9856 commit 2ea5229

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,20 +1283,39 @@ fn test_duplicate_htlc_different_direction_onchain() {
12831283
// Check we only broadcast 1 timeout tx
12841284
let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
12851285
assert_eq!(claim_txn.len(), 8);
1286-
assert_eq!(claim_txn[1], claim_txn[4]);
1287-
assert_eq!(claim_txn[2], claim_txn[5]);
1286+
1287+
check_spends!(claim_txn[0], remote_txn[0]);
1288+
12881289
check_spends!(claim_txn[1], chan_1.3);
12891290
check_spends!(claim_txn[2], claim_txn[1]);
1290-
check_spends!(claim_txn[7], claim_txn[1]);
1291+
1292+
let bump_tx = if claim_txn[1] == claim_txn[4] {
1293+
assert_eq!(claim_txn[1], claim_txn[4]);
1294+
assert_eq!(claim_txn[2], claim_txn[5]);
1295+
1296+
check_spends!(claim_txn[7], claim_txn[1]);
1297+
1298+
check_spends!(claim_txn[3], remote_txn[0]);
1299+
&claim_txn[3]
1300+
} else {
1301+
assert_eq!(claim_txn[1], claim_txn[3]);
1302+
assert_eq!(claim_txn[2], claim_txn[4]);
1303+
1304+
check_spends!(claim_txn[5], claim_txn[1]);
1305+
1306+
check_spends!(claim_txn[7], remote_txn[0]);
1307+
1308+
&claim_txn[7]
1309+
};
12911310

12921311
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);
1312+
assert_eq!(bump_tx.input.len(), 1);
1313+
assert_eq!(claim_txn[0].input[0].previous_output, bump_tx.input[0].previous_output);
12951314

12961315
assert_eq!(claim_txn[0].input.len(), 1);
12971316
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]);
12991317
assert_eq!(remote_txn[0].output[claim_txn[0].input[0].previous_output.vout as usize].value, 800);
1318+
13001319
assert_eq!(claim_txn[6].input.len(), 1);
13011320
assert_eq!(claim_txn[6].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT); // HTLC 0 <--> 1, timeout tx
13021321
check_spends!(claim_txn[6], remote_txn[0]);
@@ -2351,7 +2370,8 @@ fn test_justice_tx() {
23512370
chanmon_cfgs[1].keys_manager.disable_revocation_policy_check = true;
23522371
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
23532372
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &user_cfgs);
2354-
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2373+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
2374+
*nodes[0].connect_style.borrow_mut() = ConnectStyle::FullBlockViaListen;
23552375
// Create some new channels:
23562376
let chan_5 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
23572377

@@ -2583,7 +2603,7 @@ fn claim_htlc_outputs_single_tx() {
25832603
expect_payment_failed!(nodes[1], payment_hash_2, true);
25842604

25852605
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
2586-
assert_eq!(node_txn.len(), 9);
2606+
assert!(node_txn.len() == 9 || node_txn.len() == 10);
25872607
// ChannelMonitor: justice tx revoked offered htlc, justice tx revoked received htlc, justice tx revoked to_local (3)
25882608
// ChannelManager: local commmitment + local HTLC-timeout (2)
25892609
// 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)
@@ -5283,21 +5303,30 @@ fn test_duplicate_payment_hash_one_failure_one_success() {
52835303
let htlc_timeout_tx;
52845304
{ // Extract one of the two HTLC-Timeout transaction
52855305
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);
5306+
// ChannelMonitor: timeout tx * 2-or-3, ChannelManager: local commitment tx
5307+
assert!(node_txn.len() == 4 || node_txn.len() == 3);
52885308
check_spends!(node_txn[0], chan_2.3);
52895309

52905310
check_spends!(node_txn[1], commitment_txn[0]);
52915311
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);
5312+
5313+
if node_txn.len() > 3 {
5314+
check_spends!(node_txn[2], commitment_txn[0]);
5315+
assert_eq!(node_txn[2].input.len(), 1);
5316+
assert_eq!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
5317+
5318+
check_spends!(node_txn[3], commitment_txn[0]);
5319+
assert_ne!(node_txn[1].input[0].previous_output, node_txn[3].input[0].previous_output);
5320+
} else {
5321+
check_spends!(node_txn[2], commitment_txn[0]);
5322+
assert_ne!(node_txn[1].input[0].previous_output, node_txn[2].input[0].previous_output);
5323+
}
52975324

52985325
assert_eq!(node_txn[1].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
52995326
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);
5327+
if node_txn.len() > 3 {
5328+
assert_eq!(node_txn[3].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
5329+
}
53015330
htlc_timeout_tx = node_txn[1].clone();
53025331
}
53035332

@@ -7957,13 +7986,24 @@ fn test_bump_penalty_txn_on_remote_commitment() {
79577986
assert_eq!(node_txn[6].input.len(), 1);
79587987
check_spends!(node_txn[0], remote_txn[0]);
79597988
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();
79627989

79637990
check_spends!(node_txn[1], chan.3);
79647991
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]);
7992+
7993+
if node_txn[0].input[0].previous_output == node_txn[3].input[0].previous_output {
7994+
preimage_bump = node_txn[3].clone();
7995+
check_spends!(node_txn[3], remote_txn[0]);
7996+
7997+
assert_eq!(node_txn[1], node_txn[4]);
7998+
assert_eq!(node_txn[2], node_txn[5]);
7999+
} else {
8000+
preimage_bump = node_txn[7].clone();
8001+
check_spends!(node_txn[7], remote_txn[0]);
8002+
assert_eq!(node_txn[0].input[0].previous_output, node_txn[7].input[0].previous_output);
8003+
8004+
assert_eq!(node_txn[1], node_txn[3]);
8005+
assert_eq!(node_txn[2], node_txn[4]);
8006+
}
79678007

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

lightning/src/ln/payment_tests.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,14 +504,20 @@ 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])
510+
} else {
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!(as_htlc_timeout_txn[2], funding_tx);
515+
check_spends!(first_htlc_timeout_tx, as_commitment_tx);
516+
check_spends!(second_htlc_timeout_tx, as_commitment_tx);
517+
if first_htlc_timeout_tx.input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
518+
confirm_transaction(&nodes[0], &second_htlc_timeout_tx);
513519
} else {
514-
confirm_transaction(&nodes[0], &as_htlc_timeout_txn[0]);
520+
confirm_transaction(&nodes[0], &first_htlc_timeout_tx);
515521
}
516522
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
517523
expect_payment_failed_conditions!(nodes[0], payment_hash, false, PaymentFailedConditions::new().mpp_parts_remain());
@@ -627,7 +633,8 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
627633
let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
628634
let mon_updates: Vec<_> = chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap()
629635
.get_mut(&funding_txo).unwrap().drain().collect();
630-
assert_eq!(mon_updates.len(), 1);
636+
// If we are using chain::Confirm instead of chain::Listen, we will get the same update twice
637+
assert!(mon_updates.len() == 1 || mon_updates.len() == 2);
631638
assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
632639
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
633640

@@ -643,7 +650,9 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
643650
chanmon_cfgs[0].persister.set_update_ret(Ok(()));
644651
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
645652
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();
653+
for update in mon_updates {
654+
nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, update).unwrap();
655+
}
647656
if payment_timeout {
648657
expect_payment_failed!(nodes[0], payment_hash, true);
649658
} else {

0 commit comments

Comments
 (0)