Merge pull request #2441 from arik-so/2023-07-taproot-signer-wrapped
[rust-lightning] / lightning / src / ln / payment_tests.rs
index 01580368c6ded7bb52337cb3b0c1ae67aae026e3..c266285713378648fffaebc7606ed22e154b1dcf 100644 (file)
@@ -17,7 +17,7 @@ use crate::sign::EntropySource;
 use crate::chain::transaction::OutPoint;
 use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason, PaymentPurpose};
 use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
-use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS, RecentPaymentDetails, RecipientOnionFields, HTLCForwardInfo, PendingHTLCRouting, PendingAddHTLCInfo};
+use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS, RecentPaymentDetails, RecipientOnionFields, HTLCForwardInfo, PendingHTLCRouting, PendingAddHTLCInfo};
 use crate::ln::features::Bolt11InvoiceFeatures;
 use crate::ln::{msgs, PaymentSecret, PaymentPreimage};
 use crate::ln::msgs::ChannelMessageHandler;
@@ -602,7 +602,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
                MessageSendEvent::HandleError { node_id, action: msgs::ErrorAction::SendErrorMessage { ref msg } } => {
                        assert_eq!(node_id, nodes[1].node.get_our_node_id());
                        nodes[1].node.handle_error(&nodes[0].node.get_our_node_id(), msg);
-                       check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", 
+                       check_closed_event!(nodes[1], 1, ClosureReason::CounterpartyForceClosed { peer_msg: UntrustedString(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}",
                                &nodes[1].node.get_our_node_id())) }, [nodes[0].node.get_our_node_id()], 100000);
                        check_added_monitors!(nodes[1], 1);
                        assert_eq!(nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0).len(), 1);
@@ -643,7 +643,7 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
                mine_transaction(&nodes[0], &as_commitment_tx);
        }
        mine_transaction(&nodes[0], &bs_htlc_claim_txn[0]);
-       expect_payment_sent!(nodes[0], payment_preimage_1);
+       expect_payment_sent(&nodes[0], payment_preimage_1, None, true, false);
        connect_blocks(&nodes[0], TEST_FINAL_CLTV*4 + 20);
        let (first_htlc_timeout_tx, second_htlc_timeout_tx) = {
                let mut txn = nodes[0].tx_broadcaster.unique_txn_broadcast();
@@ -1005,7 +1005,7 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
        if payment_timeout {
                expect_payment_failed!(nodes[0], payment_hash, false);
        } else {
-               expect_payment_sent!(nodes[0], payment_preimage);
+               expect_payment_sent(&nodes[0], payment_preimage, None, true, false);
        }
 
        // If we persist the ChannelManager after we get the PaymentSent event, we shouldn't get it
@@ -1022,7 +1022,7 @@ fn do_test_dup_htlc_onchain_fails_on_reload(persist_manager_post_event: bool, co
        } else if payment_timeout {
                expect_payment_failed!(nodes[0], payment_hash, false);
        } else {
-               expect_payment_sent!(nodes[0], payment_preimage);
+               expect_payment_sent(&nodes[0], payment_preimage, None, true, false);
        }
 
        // Note that if we re-connect the block which exposed nodes[0] to the payment preimage (but
@@ -1074,7 +1074,7 @@ fn test_fulfill_restart_failure() {
 
        let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
        nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
-       expect_payment_sent_without_paths!(nodes[0], payment_preimage);
+       expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
 
        // Now reload nodes[1]...
        reload_node!(nodes[1], &chan_manager_serialized, &[&chan_0_monitor_serialized], persister, new_chain_monitor, nodes_1_deserialized);
@@ -1775,6 +1775,7 @@ fn do_test_intercepted_payment(test: InterceptTest) {
                        },
                        _ => panic!("Unexpected event")
                }
+               check_added_monitors(&nodes[0], 1);
        } else if test == InterceptTest::Timeout {
                let mut block = create_dummy_block(nodes[0].best_block_hash(), 42, Vec::new());
                connect_block(&nodes[0], &block);
@@ -1929,7 +1930,7 @@ fn do_accept_underpaying_htlcs_config(num_mpp_parts: usize) {
                payment_preimage);
        // The sender doesn't know that the penultimate hop took an extra fee.
        expect_payment_sent(&nodes[0], payment_preimage,
-               Some(Some(total_fee_msat - skimmed_fee_msat * num_mpp_parts as u64)), true);
+               Some(Some(total_fee_msat - skimmed_fee_msat * num_mpp_parts as u64)), true, true);
 }
 
 #[derive(PartialEq)]
@@ -2353,6 +2354,7 @@ fn auto_retry_partial_failure() {
        assert_eq!(bs_claim_update.update_fulfill_htlcs.len(), 1);
 
        nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_claim_update.update_fulfill_htlcs[0]);
+       expect_payment_sent(&nodes[0], payment_preimage, None, false, false);
        nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_claim_update.commitment_signed);
        check_added_monitors!(nodes[0], 1);
        let (as_third_raa, as_third_cs) = get_revoke_commit_msgs!(nodes[0], nodes[1].node.get_our_node_id());
@@ -2367,6 +2369,7 @@ fn auto_retry_partial_failure() {
 
        nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_third_raa);
        check_added_monitors!(nodes[0], 1);
+       expect_payment_path_successful!(nodes[0]);
 
        nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.update_fulfill_htlcs[0]);
        nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &bs_second_claim_update.update_fulfill_htlcs[1]);
@@ -2383,7 +2386,10 @@ fn auto_retry_partial_failure() {
 
        nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_second_raa);
        check_added_monitors!(nodes[0], 1);
-       expect_payment_sent!(nodes[0], payment_preimage);
+       let events = nodes[0].node.get_and_clear_pending_events();
+       assert_eq!(events.len(), 2);
+       if let Event::PaymentPathSuccessful { .. } = events[0] {} else { panic!(); }
+       if let Event::PaymentPathSuccessful { .. } = events[1] {} else { panic!(); }
 }
 
 #[test]
@@ -3172,7 +3178,7 @@ fn test_threaded_payment_retries() {
        }
 }
 
-fn do_no_missing_sent_on_midpoint_reload(persist_manager_with_payment: bool) {
+fn do_no_missing_sent_on_reload(persist_manager_with_payment: bool, at_midpoint: bool) {
        // Test that if we reload in the middle of an HTLC claim commitment signed dance we'll still
        // receive the PaymentSent event even if the ChannelManager had no idea about the payment when
        // it was last persisted.
@@ -3201,10 +3207,20 @@ fn do_no_missing_sent_on_midpoint_reload(persist_manager_with_payment: bool) {
        check_added_monitors!(nodes[1], 1);
        expect_payment_claimed!(nodes[1], our_payment_hash, 1_000_000);
 
-       let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
-       nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
-       check_added_monitors!(nodes[0], 1);
+       if at_midpoint {
+               let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+               nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fulfill_htlcs[0]);
+               nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &updates.commitment_signed);
+               check_added_monitors!(nodes[0], 1);
+       } else {
+               let htlc_fulfill_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+               nodes[0].node.handle_update_fulfill_htlc(&nodes[1].node.get_our_node_id(), &htlc_fulfill_updates.update_fulfill_htlcs[0]);
+               commitment_signed_dance!(nodes[0], nodes[1], htlc_fulfill_updates.commitment_signed, false);
+               // Ignore the PaymentSent event which is now pending on nodes[0] - if we were to handle it we'd
+               // be expected to ignore the eventual conflicting PaymentFailed, but by not looking at it we
+               // expect to get the PaymentSent again later.
+               check_added_monitors(&nodes[0], 0);
+       }
 
        // The ChannelMonitor should always be the latest version, as we're required to persist it
        // during the commitment signed handling.
@@ -3250,8 +3266,14 @@ fn do_no_missing_sent_on_midpoint_reload(persist_manager_with_payment: bool) {
 
 #[test]
 fn no_missing_sent_on_midpoint_reload() {
-       do_no_missing_sent_on_midpoint_reload(false);
-       do_no_missing_sent_on_midpoint_reload(true);
+       do_no_missing_sent_on_reload(false, true);
+       do_no_missing_sent_on_reload(true, true);
+}
+
+#[test]
+fn no_missing_sent_on_reload() {
+       do_no_missing_sent_on_reload(false, false);
+       do_no_missing_sent_on_reload(true, false);
 }
 
 fn do_claim_from_closed_chan(fail_payment: bool) {
@@ -3451,17 +3473,7 @@ fn do_test_custom_tlvs(spontaneous: bool, even_tlvs: bool, known_tlvs: bool) {
        let events = nodes[1].node.get_and_clear_pending_events();
        assert_eq!(events.len(), 1);
        match events[0] {
-               Event::PaymentClaimable { ref purpose, amount_msat, ref onion_fields, .. } => {
-                       match &purpose {
-                               PaymentPurpose::InvoicePayment { payment_secret, .. } => {
-                                       assert_eq!(our_payment_secret, *payment_secret);
-                                       assert_eq!(Some(*payment_secret), onion_fields.as_ref().unwrap().payment_secret);
-                               },
-                               PaymentPurpose::SpontaneousPayment(payment_preimage) => {
-                                       assert_eq!(our_payment_preimage, *payment_preimage);
-                               },
-                       }
-                       assert_eq!(amount_msat, amt_msat);
+               Event::PaymentClaimable { ref onion_fields, .. } => {
                        assert_eq!(onion_fields.clone().unwrap().custom_tlvs().clone(), custom_tlvs);
                },
                _ => panic!("Unexpected event"),
@@ -3518,26 +3530,12 @@ fn test_retry_custom_tlvs() {
        nodes[0].node.send_payment(payment_hash, onion_fields,
                payment_id, route_params.clone(), Retry::Attempts(1)).unwrap();
        check_added_monitors!(nodes[0], 1); // one monitor per path
-       let mut events = nodes[0].node.get_and_clear_pending_msg_events();
-       assert_eq!(events.len(), 1);
 
        // Add the HTLC along the first hop.
-       let fail_path_msgs_1 = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
-       let (update_add, commitment_signed) = match fail_path_msgs_1 {
-               MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate {
-                       ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs,
-                       ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed }
-               } => {
-                       assert_eq!(update_add_htlcs.len(), 1);
-                       assert!(update_fail_htlcs.is_empty());
-                       assert!(update_fulfill_htlcs.is_empty());
-                       assert!(update_fail_malformed_htlcs.is_empty());
-                       assert!(update_fee.is_none());
-                       (update_add_htlcs[0].clone(), commitment_signed.clone())
-               },
-               _ => panic!("Unexpected event"),
-       };
-       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
+       let htlc_updates = get_htlc_update_msgs(&nodes[0], &nodes[1].node.get_our_node_id());
+       let msgs::CommitmentUpdate { update_add_htlcs, commitment_signed, .. } = htlc_updates;
+       assert_eq!(update_add_htlcs.len(), 1);
+       nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add_htlcs[0]);
        commitment_signed_dance!(nodes[1], nodes[0], commitment_signed, false);
 
        // Attempt to forward the payment and complete the path's failure.
@@ -3547,15 +3545,14 @@ fn test_retry_custom_tlvs() {
                        node_id: Some(nodes[2].node.get_our_node_id()),
                        channel_id: chan_2_id
                }]);
-       let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
-       assert!(htlc_updates.update_add_htlcs.is_empty());
-       assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
-       assert!(htlc_updates.update_fulfill_htlcs.is_empty());
-       assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
        check_added_monitors!(nodes[1], 1);
-       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(),
-               &htlc_updates.update_fail_htlcs[0]);
-       commitment_signed_dance!(nodes[0], nodes[1], htlc_updates.commitment_signed, false);
+
+       let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
+       let msgs::CommitmentUpdate { update_fail_htlcs, commitment_signed, .. } = htlc_updates;
+       assert_eq!(update_fail_htlcs.len(), 1);
+       nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &update_fail_htlcs[0]);
+       commitment_signed_dance!(nodes[0], nodes[1], commitment_signed, false);
+
        let mut events = nodes[0].node.get_and_clear_pending_events();
        match events[1] {
                Event::PendingHTLCsForwardable { .. } => {},
@@ -3577,11 +3574,12 @@ fn test_retry_custom_tlvs() {
        assert_eq!(events.len(), 1);
        let payment_claimable = pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000,
                payment_hash, Some(payment_secret), events.pop().unwrap(), true, None).unwrap();
-       let onion_fields = match payment_claimable {
-               Event::PaymentClaimable { onion_fields, .. } => onion_fields,
+       match payment_claimable {
+               Event::PaymentClaimable { onion_fields, .. } => {
+                       assert_eq!(onion_fields.unwrap().custom_tlvs(), &custom_tlvs);
+               },
                _ => panic!("Unexpected event"),
        };
-       assert_eq!(onion_fields.unwrap().custom_tlvs(), &custom_tlvs);
        claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
 }
 
@@ -3665,7 +3663,8 @@ fn do_test_custom_tlvs_consistency(first_tlvs: Vec<(u64, Vec<u8>)>, second_tlvs:
        {
                let mut events = nodes[0].node.get_and_clear_pending_msg_events();
                assert_eq!(events.len(), 1);
-               pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], amt_msat, our_payment_hash, Some(our_payment_secret), events.pop().unwrap(), false, None);
+               pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], amt_msat, our_payment_hash,
+                       Some(our_payment_secret), events.pop().unwrap(), false, None);
        }
        assert!(nodes[3].node.get_and_clear_pending_events().is_empty());
 
@@ -3704,27 +3703,17 @@ fn do_test_custom_tlvs_consistency(first_tlvs: Vec<(u64, Vec<u8>)>, second_tlvs:
        if let Some(expected_tlvs) = expected_receive_tlvs {
                // Claim and match expected
                let events = nodes[3].node.get_and_clear_pending_events();
-               println!("events: {:?}", events);
                assert_eq!(events.len(), 1);
                match events[0] {
-                       Event::PaymentClaimable { ref purpose, amount_msat, ref onion_fields, .. } => {
-                               match &purpose {
-                                       PaymentPurpose::InvoicePayment { payment_secret, .. } => {
-                                               assert_eq!(our_payment_secret, *payment_secret);
-                                               assert_eq!(Some(*payment_secret), onion_fields.as_ref().unwrap().payment_secret);
-                                       },
-                                       PaymentPurpose::SpontaneousPayment(payment_preimage) => {
-                                               assert_eq!(our_payment_preimage, *payment_preimage);
-                                       },
-                               }
-                               assert_eq!(amount_msat, amt_msat);
+                       Event::PaymentClaimable { ref onion_fields, .. } => {
                                assert_eq!(onion_fields.clone().unwrap().custom_tlvs, expected_tlvs);
                        },
                        _ => panic!("Unexpected event"),
                }
 
-               do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, our_payment_preimage);
-               expect_payment_sent(&nodes[0], our_payment_preimage, Some(Some(2000)), true);
+               do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]],
+                       false, our_payment_preimage);
+               expect_payment_sent(&nodes[0], our_payment_preimage, Some(Some(2000)), true, true);
        } else {
                // Expect fail back
                let expected_destinations = vec![HTLCDestination::FailedPayment { payment_hash: our_payment_hash }];
@@ -3735,14 +3724,19 @@ fn do_test_custom_tlvs_consistency(first_tlvs: Vec<(u64, Vec<u8>)>, second_tlvs:
                nodes[2].node.handle_update_fail_htlc(&nodes[3].node.get_our_node_id(), &fail_updates_1.update_fail_htlcs[0]);
                commitment_signed_dance!(nodes[2], nodes[3], fail_updates_1.commitment_signed, false);
 
-               expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[3].node.get_our_node_id()), channel_id: chan_2_3.2 }]);
+               expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[2], vec![
+                       HTLCDestination::NextHopChannel {
+                               node_id: Some(nodes[3].node.get_our_node_id()),
+                               channel_id: chan_2_3.2
+                       }]);
                check_added_monitors!(nodes[2], 1);
 
                let fail_updates_2 = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
                nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &fail_updates_2.update_fail_htlcs[0]);
                commitment_signed_dance!(nodes[0], nodes[2], fail_updates_2.commitment_signed, false);
 
-               expect_payment_failed_conditions(&nodes[0], our_payment_hash, true, PaymentFailedConditions::new().mpp_parts_remain());
+               expect_payment_failed_conditions(&nodes[0], our_payment_hash, true,
+                       PaymentFailedConditions::new().mpp_parts_remain());
        }
 }