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 //! Tests for calculating the maximum length of a path based on the payment metadata, custom TLVs,
11 //! and/or blinded paths present.
13 use bitcoin::secp256k1::Secp256k1;
14 use crate::blinded_path::BlindedPath;
15 use crate::blinded_path::payment::{PaymentConstraints, PaymentContext, ReceiveTlvs};
16 use crate::events::MessageSendEventsProvider;
17 use crate::ln::PaymentSecret;
18 use crate::ln::blinded_payment_tests::get_blinded_route_parameters;
19 use crate::ln::channelmanager::PaymentId;
20 use crate::ln::functional_test_utils::*;
22 use crate::ln::onion_utils;
23 use crate::ln::onion_utils::MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY;
24 use crate::ln::outbound_payment::{RecipientOnionFields, Retry, RetryableSendFailure};
25 use crate::prelude::*;
26 use crate::routing::router::{DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, PaymentParameters, RouteParameters};
27 use crate::util::errors::APIError;
28 use crate::util::ser::Writeable;
29 use crate::util::test_utils;
31 // 3+32 (payload length and HMAC) + 2+8 (amt_to_forward) +
32 // 2+4 (outgoing_cltv_value) + 2+8 (short_channel_id)
33 const INTERMED_PAYLOAD_LEN_ESTIMATE: usize = 61;
35 // Length of the HMAC of an onion payload when encoded into the packet.
36 const PAYLOAD_HMAC_LEN: usize = 32;
39 fn large_payment_metadata() {
40 // Test that we'll limit our maximum path length based on the size of the provided
41 // payment_metadata, and refuse to send at all prior to pathfinding if it's too large.
42 let chanmon_cfgs = create_chanmon_cfgs(3);
43 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
44 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
45 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
46 create_announced_chan_between_nodes(&nodes, 0, 1);
47 create_announced_chan_between_nodes(&nodes, 1, 2);
49 let amt_msat = 100_000;
51 // Construct payment_metadata such that we can send the payment to the next hop but no further
52 // without exceeding the max onion packet size.
53 let final_payload_len_without_metadata = msgs::OutboundOnionPayload::Receive {
54 payment_data: Some(msgs::FinalOnionHopData {
55 payment_secret: PaymentSecret([0; 32]), total_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY
57 payment_metadata: None,
58 keysend_preimage: None,
59 custom_tlvs: &Vec::new(),
60 sender_intended_htlc_amt_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
61 cltv_expiry_height: nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
62 }.serialized_length();
63 let max_metadata_len = 1300
65 - crate::util::ser::BigSize(1200).serialized_length() // metadata length
66 - 2 // onion payload varint prefix increased ser size due to metadata
68 - final_payload_len_without_metadata;
69 let mut payment_metadata = vec![42; max_metadata_len];
71 // Check that the maximum-size metadata is sendable.
72 let (mut route_0_1, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], &nodes[1], amt_msat);
73 let mut recipient_onion_max_md_size = RecipientOnionFields {
74 payment_secret: Some(payment_secret),
75 payment_metadata: Some(payment_metadata.clone()),
76 custom_tlvs: Vec::new(),
78 nodes[0].node.send_payment(payment_hash, recipient_onion_max_md_size.clone(), PaymentId(payment_hash.0), route_0_1.route_params.clone().unwrap(), Retry::Attempts(0)).unwrap();
79 check_added_monitors!(nodes[0], 1);
80 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
81 assert_eq!(events.len(), 1);
82 let path = &[&nodes[1]];
83 let args = PassAlongPathArgs::new(&nodes[0], path, amt_msat, payment_hash, events.pop().unwrap())
84 .with_payment_secret(payment_secret)
85 .with_payment_metadata(payment_metadata.clone());
86 do_pass_along_path(args);
87 claim_payment_along_route(&nodes[0], &[&[&nodes[1]]], false, payment_preimage);
89 // Check that the payment parameter for max path length will prevent us from routing past our
90 // next-hop peer given the payment_metadata size.
91 let (mut route_0_2, payment_hash_2, payment_preimage_2, payment_secret_2) = get_route_and_payment_hash!(&nodes[0], &nodes[2], amt_msat);
92 let mut route_params_0_2 = route_0_2.route_params.clone().unwrap();
93 route_params_0_2.payment_params.max_path_length = 1;
94 nodes[0].router.expect_find_route_query(route_params_0_2);
95 let err = nodes[0].node.send_payment(payment_hash_2, recipient_onion_max_md_size.clone(), PaymentId(payment_hash_2.0), route_0_2.route_params.clone().unwrap(), Retry::Attempts(0)).unwrap_err();
96 assert_eq!(err, RetryableSendFailure::RouteNotFound);
98 // If our payment_metadata contains 1 additional byte, we'll fail prior to pathfinding.
99 let mut recipient_onion_too_large_md = recipient_onion_max_md_size.clone();
100 recipient_onion_too_large_md.payment_metadata.as_mut().map(|mut md| md.push(42));
101 let err = nodes[0].node.send_payment(payment_hash, recipient_onion_too_large_md.clone(), PaymentId(payment_hash.0), route_0_1.route_params.clone().unwrap(), Retry::Attempts(0)).unwrap_err();
102 assert_eq!(err, RetryableSendFailure::OnionPacketSizeExceeded);
104 // Confirm that we'll fail to construct an onion packet given this payment_metadata that's too
105 // large for even a 1-hop path.
106 let secp_ctx = Secp256k1::signing_only();
107 route_0_1.paths[0].hops[0].fee_msat = MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY;
108 route_0_1.paths[0].hops[0].cltv_expiry_delta = DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA;
109 let err = onion_utils::create_payment_onion(&secp_ctx, &route_0_1.paths[0], &test_utils::privkey(42), MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY, &recipient_onion_too_large_md, nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, &payment_hash, &None, [0; 32]).unwrap_err();
111 APIError::InvalidRoute { err } => {
112 assert_eq!(err, "Route size too large considering onion data");
117 // If we remove enough payment_metadata bytes to allow for 2 hops, we're now able to send to
119 let mut recipient_onion_allows_2_hops = RecipientOnionFields {
120 payment_secret: Some(payment_secret_2),
121 payment_metadata: Some(vec![42; max_metadata_len - INTERMED_PAYLOAD_LEN_ESTIMATE]),
122 custom_tlvs: Vec::new(),
124 let mut route_params_0_2 = route_0_2.route_params.clone().unwrap();
125 route_params_0_2.payment_params.max_path_length = 2;
126 nodes[0].router.expect_find_route_query(route_params_0_2);
127 nodes[0].node.send_payment(payment_hash_2, recipient_onion_allows_2_hops.clone(), PaymentId(payment_hash_2.0), route_0_2.route_params.unwrap(), Retry::Attempts(0)).unwrap();
128 check_added_monitors!(nodes[0], 1);
129 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
130 assert_eq!(events.len(), 1);
131 let path = &[&nodes[1], &nodes[2]];
132 let args = PassAlongPathArgs::new(&nodes[0], path, amt_msat, payment_hash_2, events.pop().unwrap())
133 .with_payment_secret(payment_secret_2)
134 .with_payment_metadata(recipient_onion_allows_2_hops.payment_metadata.unwrap());
135 do_pass_along_path(args);
136 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage_2);
140 fn one_hop_blinded_path_with_custom_tlv() {
141 // Test that we'll limit our maximum path length when paying to a 1-hop blinded path based on the
142 // size of the provided custom TLV, and refuse to send at all prior to pathfinding if it's too
144 let chanmon_cfgs = create_chanmon_cfgs(3);
145 let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
146 let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
147 let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
148 create_announced_chan_between_nodes(&nodes, 0, 1);
149 let chan_upd_1_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0).0.contents;
151 // Construct the route parameters for sending to nodes[2]'s 1-hop blinded path.
152 let amt_msat = 100_000;
153 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[2], Some(amt_msat), None);
154 let payee_tlvs = ReceiveTlvs {
156 payment_constraints: PaymentConstraints {
157 max_cltv_expiry: u32::max_value(),
158 htlc_minimum_msat: chan_upd_1_2.htlc_minimum_msat,
160 payment_context: PaymentContext::unknown(),
162 let mut secp_ctx = Secp256k1::new();
163 let blinded_path = BlindedPath::one_hop_for_payment(
164 nodes[2].node.get_our_node_id(), payee_tlvs, TEST_FINAL_CLTV as u16,
165 &chanmon_cfgs[2].keys_manager, &secp_ctx
167 let route_params = RouteParameters::from_payment_params_and_value(
168 PaymentParameters::blinded(vec![blinded_path.clone()]),
172 // Calculate the maximum custom TLV value size where a valid onion packet is still possible.
173 const CUSTOM_TLV_TYPE: u64 = 65537;
174 let final_payload_len_without_custom_tlv = msgs::OutboundOnionPayload::BlindedReceive {
175 sender_intended_htlc_amt_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
176 total_msat: MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
177 cltv_expiry_height: nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
178 encrypted_tlvs: &blinded_path.1.blinded_hops[0].encrypted_payload,
179 intro_node_blinding_point: Some(blinded_path.1.blinding_point),
180 keysend_preimage: None,
181 custom_tlvs: &Vec::new()
182 }.serialized_length();
183 let max_custom_tlv_len = 1300
184 - crate::util::ser::BigSize(CUSTOM_TLV_TYPE).serialized_length() // custom TLV type
185 - crate::util::ser::BigSize(1200).serialized_length() // custom TLV length
186 - 1 // onion payload varint prefix increased ser size due to custom TLV
188 - final_payload_len_without_custom_tlv;
190 // Check that we can send the maximum custom TLV with 1 blinded hop.
191 let recipient_onion_max_custom_tlv_size = RecipientOnionFields::spontaneous_empty()
192 .with_custom_tlvs(vec![(CUSTOM_TLV_TYPE, vec![42; max_custom_tlv_len])])
194 nodes[1].node.send_payment(payment_hash, recipient_onion_max_custom_tlv_size.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap();
195 check_added_monitors(&nodes[1], 1);
197 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
198 assert_eq!(events.len(), 1);
199 let path = &[&nodes[2]];
200 let args = PassAlongPathArgs::new(&nodes[1], path, amt_msat, payment_hash, events.pop().unwrap())
201 .with_payment_secret(payment_secret)
202 .with_custom_tlvs(recipient_onion_max_custom_tlv_size.custom_tlvs.clone());
203 do_pass_along_path(args);
204 claim_payment_along_route(&nodes[1], &[&[&nodes[2]]], false, payment_preimage);
206 // If 1 byte is added to the custom TLV value, we'll fail to send prior to pathfinding.
207 let mut recipient_onion_too_large_custom_tlv = recipient_onion_max_custom_tlv_size.clone();
208 recipient_onion_too_large_custom_tlv.custom_tlvs[0].1.push(42);
209 let err = nodes[1].node.send_payment(payment_hash, recipient_onion_too_large_custom_tlv, PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap_err();
210 assert_eq!(err, RetryableSendFailure::OnionPacketSizeExceeded);
212 // With the maximum-size custom TLV, our max path length is limited to 1, so attempting to route
213 // nodes[0] -> nodes[2] will fail.
214 let err = nodes[0].node.send_payment(payment_hash, recipient_onion_max_custom_tlv_size.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap_err();
215 assert_eq!(err, RetryableSendFailure::RouteNotFound);
217 // If we remove enough custom TLV bytes to allow for 1 intermediate unblinded hop, we're now able
218 // to send nodes[0] -> nodes[2].
219 let mut recipient_onion_allows_2_hops = recipient_onion_max_custom_tlv_size.clone();
220 recipient_onion_allows_2_hops.custom_tlvs[0].1.resize(max_custom_tlv_len - INTERMED_PAYLOAD_LEN_ESTIMATE, 0);
221 nodes[0].node.send_payment(payment_hash, recipient_onion_allows_2_hops.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap();
222 check_added_monitors(&nodes[0], 1);
224 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
225 assert_eq!(events.len(), 1);
226 let path = &[&nodes[1], &nodes[2]];
227 let args = PassAlongPathArgs::new(&nodes[0], path, amt_msat, payment_hash, events.pop().unwrap())
228 .with_payment_secret(payment_secret)
229 .with_custom_tlvs(recipient_onion_allows_2_hops.custom_tlvs);
230 do_pass_along_path(args);
231 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
235 fn blinded_path_with_custom_tlv() {
236 // Test that we'll limit our maximum path length when paying to a blinded path based on the size
237 // of the provided custom TLV, and refuse to send at all prior to pathfinding if it's too large.
238 let chanmon_cfgs = create_chanmon_cfgs(4);
239 let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
240 let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
241 let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
242 create_announced_chan_between_nodes(&nodes, 0, 1);
243 create_announced_chan_between_nodes(&nodes, 1, 2);
244 let chan_upd_2_3 = create_announced_chan_between_nodes_with_value(&nodes, 2, 3, 1_000_000, 0).0.contents;
246 // Construct the route parameters for sending to nodes[3]'s blinded path.
247 let amt_msat = 100_000;
248 let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
249 let route_params = get_blinded_route_parameters(amt_msat, payment_secret, 1, 1_0000_0000,
250 nodes.iter().skip(2).map(|n| n.node.get_our_node_id()).collect(), &[&chan_upd_2_3],
251 &chanmon_cfgs[3].keys_manager);
253 // Calculate the maximum custom TLV value size where a valid onion packet is still possible.
254 const CUSTOM_TLV_TYPE: u64 = 65537;
255 let mut route = get_route(&nodes[1], &route_params).unwrap();
256 let reserved_packet_bytes_without_custom_tlv: usize = onion_utils::build_onion_payloads(
257 &route.paths[0], MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
258 &RecipientOnionFields::spontaneous_empty(),
259 nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, &None
264 .map(|payload| payload.serialized_length() + PAYLOAD_HMAC_LEN)
266 let max_custom_tlv_len = 1300
267 - crate::util::ser::BigSize(CUSTOM_TLV_TYPE).serialized_length() // custom TLV type
268 - crate::util::ser::BigSize(1200).serialized_length() // custom TLV length
269 - 2 // onion payload varint prefix increased ser size due to custom TLV
270 - reserved_packet_bytes_without_custom_tlv;
272 // Check that we can send the maximum custom TLV size with 0 intermediate unblinded hops.
273 let recipient_onion_max_custom_tlv_size = RecipientOnionFields::spontaneous_empty()
274 .with_custom_tlvs(vec![(CUSTOM_TLV_TYPE, vec![42; max_custom_tlv_len])])
276 nodes[1].node.send_payment(payment_hash, recipient_onion_max_custom_tlv_size.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap();
277 check_added_monitors(&nodes[1], 1);
279 let mut events = nodes[1].node.get_and_clear_pending_msg_events();
280 assert_eq!(events.len(), 1);
281 let path = &[&nodes[2], &nodes[3]];
282 let args = PassAlongPathArgs::new(&nodes[1], path, amt_msat, payment_hash, events.pop().unwrap())
283 .with_payment_secret(payment_secret)
284 .with_custom_tlvs(recipient_onion_max_custom_tlv_size.custom_tlvs.clone());
285 do_pass_along_path(args);
286 claim_payment_along_route(&nodes[1], &[&[&nodes[2], &nodes[3]]], false, payment_preimage);
288 // If 1 byte is added to the custom TLV value, we'll fail to send prior to pathfinding.
289 let mut recipient_onion_too_large_custom_tlv = recipient_onion_max_custom_tlv_size.clone();
290 recipient_onion_too_large_custom_tlv.custom_tlvs[0].1.push(42);
291 let err = nodes[1].node.send_payment(payment_hash, recipient_onion_too_large_custom_tlv.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap_err();
292 assert_eq!(err, RetryableSendFailure::OnionPacketSizeExceeded);
294 // Confirm that we can't construct an onion packet given this too-large custom TLV.
295 let secp_ctx = Secp256k1::signing_only();
296 route.paths[0].hops[0].fee_msat = MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY;
297 route.paths[0].hops[0].cltv_expiry_delta = DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA;
298 let err = onion_utils::create_payment_onion(&secp_ctx, &route.paths[0], &test_utils::privkey(42), MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY, &recipient_onion_too_large_custom_tlv, nodes[0].best_block_info().1 + DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, &payment_hash, &None, [0; 32]).unwrap_err();
300 APIError::InvalidRoute { err } => {
301 assert_eq!(err, "Route size too large considering onion data");
306 // With the maximum-size custom TLV, we can't have any intermediate unblinded hops, so attempting
307 // to route nodes[0] -> nodes[3] will fail.
308 let err = nodes[0].node.send_payment(payment_hash, recipient_onion_max_custom_tlv_size.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap_err();
309 assert_eq!(err, RetryableSendFailure::RouteNotFound);
311 // If we remove enough custom TLV bytes to allow for 1 intermediate unblinded hop, we're now able
312 // to send nodes[0] -> nodes[3].
313 let mut recipient_onion_allows_2_hops = recipient_onion_max_custom_tlv_size.clone();
314 recipient_onion_allows_2_hops.custom_tlvs[0].1.resize(max_custom_tlv_len - INTERMED_PAYLOAD_LEN_ESTIMATE, 0);
315 nodes[0].node.send_payment(payment_hash, recipient_onion_allows_2_hops.clone(), PaymentId(payment_hash.0), route_params.clone(), Retry::Attempts(0)).unwrap();
316 check_added_monitors(&nodes[0], 1);
318 let mut events = nodes[0].node.get_and_clear_pending_msg_events();
319 assert_eq!(events.len(), 1);
320 let path = &[&nodes[1], &nodes[2], &nodes[3]];
321 let args = PassAlongPathArgs::new(&nodes[0], path, amt_msat, payment_hash, events.pop().unwrap())
322 .with_payment_secret(payment_secret)
323 .with_custom_tlvs(recipient_onion_allows_2_hops.custom_tlvs);
324 do_pass_along_path(args);
325 claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2], &nodes[3]]], false, payment_preimage);