Make tests more robust against different connection styles
[rust-lightning] / lightning / src / ln / payment_tests.rs
index 1e5495a35dc1635597e65ec5327f1fbbc6ec7dda..088ad855683327d1c89bd6aa963aa8417d807a41 100644 (file)
@@ -367,7 +367,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
        let nodes_0_deserialized: ChannelManager<EnforcingSigner, &test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestLogger>;
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
-       let (_, _, chan_id, funding_tx) = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
+       let chan_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).2;
        let (_, _, chan_id_2, _) = create_announced_chan_between_nodes(&nodes, 1, 2, InitFeatures::known(), InitFeatures::known());
 
        // Serialize the ChannelManager prior to sending payments
@@ -504,14 +504,19 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
        expect_payment_sent!(nodes[0], payment_preimage_1);
        connect_blocks(&nodes[0], TEST_FINAL_CLTV*4 + 20);
        let as_htlc_timeout_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
-       check_spends!(as_htlc_timeout_txn[2], funding_tx);
-       check_spends!(as_htlc_timeout_txn[0], as_commitment_tx);
-       check_spends!(as_htlc_timeout_txn[1], as_commitment_tx);
        assert_eq!(as_htlc_timeout_txn.len(), 3);
-       if as_htlc_timeout_txn[0].input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
-               confirm_transaction(&nodes[0], &as_htlc_timeout_txn[1]);
+       let (first_htlc_timeout_tx, second_htlc_timeout_tx) = if as_htlc_timeout_txn[0] == as_commitment_tx {
+               (&as_htlc_timeout_txn[1], &as_htlc_timeout_txn[2])
        } else {
-               confirm_transaction(&nodes[0], &as_htlc_timeout_txn[0]);
+               assert_eq!(as_htlc_timeout_txn[2], as_commitment_tx);
+               (&as_htlc_timeout_txn[0], &as_htlc_timeout_txn[1])
+       };
+       check_spends!(first_htlc_timeout_tx, as_commitment_tx);
+       check_spends!(second_htlc_timeout_tx, as_commitment_tx);
+       if first_htlc_timeout_tx.input[0].previous_output == bs_htlc_claim_txn[0].input[0].previous_output {
+               confirm_transaction(&nodes[0], &second_htlc_timeout_tx);
+       } else {
+               confirm_transaction(&nodes[0], &first_htlc_timeout_tx);
        }
        nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
        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
        let funding_txo = OutPoint { txid: funding_tx.txid(), index: 0 };
        let mon_updates: Vec<_> = chanmon_cfgs[0].persister.chain_sync_monitor_persistences.lock().unwrap()
                .get_mut(&funding_txo).unwrap().drain().collect();
-       assert_eq!(mon_updates.len(), 1);
+       // If we are using chain::Confirm instead of chain::Listen, we will get the same update twice
+       assert!(mon_updates.len() == 1 || mon_updates.len() == 2);
        assert!(nodes[0].chain_monitor.release_pending_monitor_events().is_empty());
        assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
 
@@ -643,7 +649,9 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
        chanmon_cfgs[0].persister.set_update_ret(Ok(()));
        let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
        get_monitor!(nodes[0], chan_id).write(&mut chan_0_monitor_serialized).unwrap();
-       nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, mon_updates[0]).unwrap();
+       for update in mon_updates {
+               nodes[0].chain_monitor.chain_monitor.channel_monitor_updated(funding_txo, update).unwrap();
+       }
        if payment_timeout {
                expect_payment_failed!(nodes[0], payment_hash, true);
        } else {