1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 use bitcoin::secp256k1::Secp256k1;
11 use crate::blinded_path::BlindedPath;
12 use crate::blinded_path::payment::{ForwardNode, ForwardTlvs, PaymentConstraints, PaymentRelay, ReceiveTlvs};
13 use crate::events::MessageSendEventsProvider;
14 use crate::ln::channelmanager;
15 use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
16 use crate::ln::features::BlindedHopFeatures;
17 use crate::ln::functional_test_utils::*;
18 use crate::ln::msgs::ChannelMessageHandler;
19 use crate::ln::onion_utils::INVALID_ONION_BLINDING;
20 use crate::ln::outbound_payment::Retry;
21 use crate::prelude::*;
22 use crate::routing::router::{PaymentParameters, RouteParameters};
23 use crate::util::config::UserConfig;
26 fn one_hop_blinded_path() {
27 do_one_hop_blinded_path(true);
28 do_one_hop_blinded_path(false);
31 fn do_one_hop_blinded_path(success: bool) {
32 let chanmon_cfgs = create_chanmon_cfgs(2);
33 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
34 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
35 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
36 let chan_upd = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0).0.contents;
39 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None);
40 let payee_tlvs = ReceiveTlvs {
42 payment_constraints: PaymentConstraints {
43 max_cltv_expiry: u32::max_value(),
44 htlc_minimum_msat: chan_upd.htlc_minimum_msat,
47 let mut secp_ctx = Secp256k1::new();
48 let blinded_path = BlindedPath::one_hop_for_payment(
49 nodes[1].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[1].keys_manager, &secp_ctx
52 let route_params = RouteParameters::from_payment_params_and_value(
53 PaymentParameters::blinded(vec![blinded_path]),
56 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
57 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
58 check_added_monitors(&nodes[0], 1);
59 pass_along_route(&nodes[0], &[&[&nodes[1]]], amt_msat, payment_hash, payment_secret);
61 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
63 fail_payment(&nodes[0], &[&nodes[1]], payment_hash);
68 fn mpp_to_one_hop_blinded_path() {
69 let chanmon_cfgs = create_chanmon_cfgs(4);
70 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
71 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
72 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
73 let mut secp_ctx = Secp256k1::new();
75 create_announced_chan_between_nodes(&nodes, 0, 1);
76 create_announced_chan_between_nodes(&nodes, 0, 2);
77 let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents;
78 create_announced_chan_between_nodes(&nodes, 2, 3).0.contents;
80 let amt_msat = 15_000_000;
81 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
82 let payee_tlvs = ReceiveTlvs {
84 payment_constraints: PaymentConstraints {
85 max_cltv_expiry: u32::max_value(),
86 htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
89 let blinded_path = BlindedPath::one_hop_for_payment(
90 nodes[3].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[3].keys_manager, &secp_ctx
94 channelmanager::provided_bolt12_invoice_features(&UserConfig::default());
95 let route_params = RouteParameters::from_payment_params_and_value(
96 PaymentParameters::blinded(vec![blinded_path]).with_bolt12_features(bolt12_features).unwrap(),
99 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
100 check_added_monitors(&nodes[0], 2);
102 let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
103 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
104 assert_eq!(events.len(), 2);
106 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
107 pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(),
108 Some(payment_secret), ev.clone(), false, None);
110 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
111 pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(),
112 Some(payment_secret), ev.clone(), true, None);
113 claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
117 fn forward_checks_failure() {
118 // Ensure we'll fail backwards properly if a forwarding check fails on initial update_add
120 let chanmon_cfgs = create_chanmon_cfgs(3);
121 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
122 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
123 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
124 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
125 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
128 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
129 let intermediate_nodes = vec![ForwardNode {
130 node_id: nodes[1].node.get_our_node_id(),
132 short_channel_id: chan_upd_1_2.short_channel_id,
133 payment_relay: PaymentRelay {
134 cltv_expiry_delta: chan_upd_1_2.cltv_expiry_delta,
135 fee_proportional_millionths: chan_upd_1_2.fee_proportional_millionths,
136 fee_base_msat: chan_upd_1_2.fee_base_msat,
138 payment_constraints: PaymentConstraints {
139 max_cltv_expiry: u32::max_value(),
140 htlc_minimum_msat: chan_upd_1_2.htlc_minimum_msat,
142 features: BlindedHopFeatures::empty(),
144 htlc_maximum_msat: chan_upd_1_2.htlc_maximum_msat,
146 let payee_tlvs = ReceiveTlvs {
148 payment_constraints: PaymentConstraints {
149 max_cltv_expiry: u32::max_value(),
150 htlc_minimum_msat: chan_upd_1_2.htlc_minimum_msat,
153 let mut secp_ctx = Secp256k1::new();
154 let blinded_path = BlindedPath::new_for_payment(
155 &intermediate_nodes[..], nodes[2].node.get_our_node_id(), payee_tlvs,
156 chan_upd_1_2.htlc_maximum_msat, &chanmon_cfgs[2].keys_manager, &secp_ctx
159 let route_params = RouteParameters::from_payment_params_and_value(
160 PaymentParameters::blinded(vec![blinded_path]), amt_msat);
161 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
162 check_added_monitors(&nodes[0], 1);
164 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
165 assert_eq!(events.len(), 1);
166 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
167 let mut payment_event = SendEvent::from_event(ev);
169 let mut update_add = &mut payment_event.msgs[0];
170 update_add.cltv_expiry = 10; // causes outbound CLTV expiry to underflow
171 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
172 check_added_monitors!(nodes[1], 0);
173 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, true, true);
175 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
176 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
177 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
178 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
179 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));