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::{PublicKey, Secp256k1, SecretKey};
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::PaymentSecret;
15 use crate::ln::channelmanager;
16 use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
17 use crate::ln::features::BlindedHopFeatures;
18 use crate::ln::functional_test_utils::*;
20 use crate::ln::msgs::ChannelMessageHandler;
21 use crate::ln::onion_utils;
22 use crate::ln::onion_utils::INVALID_ONION_BLINDING;
23 use crate::ln::outbound_payment::Retry;
24 use crate::prelude::*;
25 use crate::routing::router::{PaymentParameters, RouteParameters};
26 use crate::util::config::UserConfig;
27 use crate::util::test_utils;
29 pub fn get_blinded_route_parameters(
30 amt_msat: u64, payment_secret: PaymentSecret, node_ids: Vec<PublicKey>,
31 channel_upds: &[&msgs::UnsignedChannelUpdate], keys_manager: &test_utils::TestKeysInterface
32 ) -> RouteParameters {
33 let mut intermediate_nodes = Vec::new();
34 for (node_id, chan_upd) in node_ids.iter().zip(channel_upds) {
35 intermediate_nodes.push(ForwardNode {
38 short_channel_id: chan_upd.short_channel_id,
39 payment_relay: PaymentRelay {
40 cltv_expiry_delta: chan_upd.cltv_expiry_delta,
41 fee_proportional_millionths: chan_upd.fee_proportional_millionths,
42 fee_base_msat: chan_upd.fee_base_msat,
44 payment_constraints: PaymentConstraints {
45 max_cltv_expiry: u32::max_value(),
46 htlc_minimum_msat: chan_upd.htlc_minimum_msat,
48 features: BlindedHopFeatures::empty(),
50 htlc_maximum_msat: chan_upd.htlc_maximum_msat,
53 let payee_tlvs = ReceiveTlvs {
55 payment_constraints: PaymentConstraints {
56 max_cltv_expiry: u32::max_value(),
57 htlc_minimum_msat: channel_upds.last().unwrap().htlc_minimum_msat,
60 let mut secp_ctx = Secp256k1::new();
61 let blinded_path = BlindedPath::new_for_payment(
62 &intermediate_nodes[..], *node_ids.last().unwrap(), payee_tlvs,
63 channel_upds.last().unwrap().htlc_maximum_msat, keys_manager, &secp_ctx
66 RouteParameters::from_payment_params_and_value(
67 PaymentParameters::blinded(vec![blinded_path]), amt_msat
72 fn one_hop_blinded_path() {
73 do_one_hop_blinded_path(true);
74 do_one_hop_blinded_path(false);
77 fn do_one_hop_blinded_path(success: bool) {
78 let chanmon_cfgs = create_chanmon_cfgs(2);
79 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
80 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
81 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
82 let chan_upd = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0).0.contents;
85 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None);
86 let payee_tlvs = ReceiveTlvs {
88 payment_constraints: PaymentConstraints {
89 max_cltv_expiry: u32::max_value(),
90 htlc_minimum_msat: chan_upd.htlc_minimum_msat,
93 let mut secp_ctx = Secp256k1::new();
94 let blinded_path = BlindedPath::one_hop_for_payment(
95 nodes[1].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[1].keys_manager, &secp_ctx
98 let route_params = RouteParameters::from_payment_params_and_value(
99 PaymentParameters::blinded(vec![blinded_path]),
102 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
103 PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
104 check_added_monitors(&nodes[0], 1);
105 pass_along_route(&nodes[0], &[&[&nodes[1]]], amt_msat, payment_hash, payment_secret);
107 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
109 fail_payment(&nodes[0], &[&nodes[1]], payment_hash);
114 fn mpp_to_one_hop_blinded_path() {
115 let chanmon_cfgs = create_chanmon_cfgs(4);
116 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
117 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
118 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
119 let mut secp_ctx = Secp256k1::new();
121 create_announced_chan_between_nodes(&nodes, 0, 1);
122 create_announced_chan_between_nodes(&nodes, 0, 2);
123 let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents;
124 create_announced_chan_between_nodes(&nodes, 2, 3).0.contents;
126 let amt_msat = 15_000_000;
127 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
128 let payee_tlvs = ReceiveTlvs {
130 payment_constraints: PaymentConstraints {
131 max_cltv_expiry: u32::max_value(),
132 htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
135 let blinded_path = BlindedPath::one_hop_for_payment(
136 nodes[3].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[3].keys_manager, &secp_ctx
139 let bolt12_features =
140 channelmanager::provided_bolt12_invoice_features(&UserConfig::default());
141 let route_params = RouteParameters::from_payment_params_and_value(
142 PaymentParameters::blinded(vec![blinded_path]).with_bolt12_features(bolt12_features).unwrap(),
145 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
146 check_added_monitors(&nodes[0], 2);
148 let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
149 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
150 assert_eq!(events.len(), 2);
152 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
153 pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(),
154 Some(payment_secret), ev.clone(), false, None);
156 let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
157 pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(),
158 Some(payment_secret), ev.clone(), true, None);
159 claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
162 enum ForwardCheckFail {
163 // Fail a check on the inbound onion payload. In this case, we underflow when calculating the
164 // outgoing cltv_expiry.
166 // The forwarding node's payload is encoded as a receive, i.e. the next hop HMAC is [0; 32].
167 ForwardPayloadEncodedAsReceive,
168 // Fail a check on the outbound channel. In this case, our next-hop peer is offline.
169 OutboundChannelCheck,
173 fn forward_checks_failure() {
174 do_forward_checks_failure(ForwardCheckFail::InboundOnionCheck);
175 do_forward_checks_failure(ForwardCheckFail::ForwardPayloadEncodedAsReceive);
176 do_forward_checks_failure(ForwardCheckFail::OutboundChannelCheck);
179 fn do_forward_checks_failure(check: ForwardCheckFail) {
180 // Ensure we'll fail backwards properly if a forwarding check fails on initial update_add
182 let chanmon_cfgs = create_chanmon_cfgs(3);
183 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
184 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
185 let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
186 // We need the session priv to construct a bogus onion packet later.
187 *nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some([3; 32]);
188 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
189 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
192 let (_, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
193 let route_params = get_blinded_route_parameters(amt_msat, payment_secret,
194 nodes.iter().skip(1).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_1_2],
195 &chanmon_cfgs[2].keys_manager);
197 let route = get_route(&nodes[0], &route_params).unwrap();
198 node_cfgs[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
199 nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
200 check_added_monitors(&nodes[0], 1);
202 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
203 assert_eq!(events.len(), 1);
204 let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
205 let mut payment_event = SendEvent::from_event(ev);
207 let mut update_add = &mut payment_event.msgs[0];
209 ForwardCheckFail::InboundOnionCheck => {
210 update_add.cltv_expiry = 10; // causes outbound CLTV expiry to underflow
212 ForwardCheckFail::ForwardPayloadEncodedAsReceive => {
213 let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
214 let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
215 let cur_height = nodes[0].best_block_info().1;
216 let (mut onion_payloads, ..) = onion_utils::build_onion_payloads(
217 &route.paths[0], amt_msat, RecipientOnionFields::spontaneous_empty(), cur_height, &None).unwrap();
218 // Remove the receive payload so the blinded forward payload is encoded as a final payload
219 // (i.e. next_hop_hmac == [0; 32])
220 onion_payloads.pop();
221 update_add.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash).unwrap();
223 ForwardCheckFail::OutboundChannelCheck => {
224 // The intro node will see that the next-hop peer is disconnected and fail the HTLC backwards.
225 nodes[1].node.peer_disconnected(&nodes[2].node.get_our_node_id());
228 nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
229 check_added_monitors!(nodes[1], 0);
230 do_commitment_signed_dance(&nodes[1], &nodes[0], &payment_event.commitment_msg, true, true);
232 let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
233 nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
234 do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
235 expect_payment_failed_conditions(&nodes[0], payment_hash, false,
236 PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));