Skip to content

Commit f7159c2

Browse files
outbound_payment: set max path length in PaymentParameters.
So the router knows how long the maximum payment path can be.
1 parent 4202e4e commit f7159c2

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ use crate::blinded_path::BlindedHop;
1111
use crate::crypto::chacha20::ChaCha20;
1212
use crate::crypto::streams::ChaChaReader;
1313
use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
14+
use crate::ln::features::{ChannelFeatures, NodeFeatures};
1415
use crate::ln::msgs;
1516
use crate::ln::types::{PaymentHash, PaymentPreimage};
1617
use crate::ln::wire::Encode;
1718
use crate::routing::gossip::NetworkUpdate;
18-
use crate::routing::router::{Path, RouteHop};
19+
use crate::routing::router::{Path, Payee, RouteHop, RouteParameters, MAX_PATH_LENGTH_ESTIMATE};
1920
use crate::sign::NodeSigner;
2021
use crate::util::errors::{self, APIError};
2122
use crate::util::logger::Logger;
@@ -310,6 +311,81 @@ where
310311
Ok((cur_value_msat, cur_cltv))
311312
}
312313

314+
pub(crate) fn set_max_path_length(
315+
route_params: &mut RouteParameters, recipient_onion: &RecipientOnionFields,
316+
keysend_preimage: Option<PaymentPreimage>, best_block_height: u32,
317+
) -> Result<(), ()> {
318+
const PAYLOAD_HMAC_LEN: usize = 32;
319+
let unblinded_intermed_payload_len = msgs::OutboundOnionPayload::Forward {
320+
short_channel_id: 42,
321+
amt_to_forward: u64::max_value(),
322+
outgoing_cltv_value: u32::max_value(),
323+
}
324+
.serialized_length()
325+
.saturating_add(PAYLOAD_HMAC_LEN);
326+
327+
let (cltv_expiry_delta, num_reserved_hops, blinded_tail_opt) =
328+
match &route_params.payment_params.payee {
329+
Payee::Blinded { route_hints, .. } => {
330+
let (blinded_payinfo, largest_path) = route_hints
331+
.iter()
332+
.max_by(|(_, path_a), (_, path_b)| {
333+
path_a.serialized_length().cmp(&path_b.serialized_length())
334+
})
335+
.ok_or(())?;
336+
let blinded_tail = BlindedTailHopIter {
337+
hops: largest_path.blinded_hops.iter(),
338+
blinding_point: largest_path.blinding_point,
339+
final_value_msat: route_params.final_value_msat,
340+
excess_final_cltv_expiry_delta: 0,
341+
};
342+
(
343+
blinded_payinfo.cltv_expiry_delta as u32,
344+
largest_path.blinded_hops.len(),
345+
Some(blinded_tail),
346+
)
347+
},
348+
Payee::Clear { final_cltv_expiry_delta, .. } => {
349+
(*final_cltv_expiry_delta, 1 as usize, None)
350+
},
351+
};
352+
353+
let unblinded_route_hop = RouteHop {
354+
pubkey: PublicKey::from_slice(&[2; 33]).unwrap(),
355+
node_features: NodeFeatures::empty(),
356+
short_channel_id: 42,
357+
channel_features: ChannelFeatures::empty(),
358+
fee_msat: route_params.final_value_msat,
359+
cltv_expiry_delta,
360+
maybe_announced_channel: false,
361+
};
362+
let mut num_reserved_bytes: usize = 0;
363+
let build_payloads_res = build_onion_payloads_callback(
364+
core::iter::once(&unblinded_route_hop),
365+
blinded_tail_opt,
366+
route_params.final_value_msat,
367+
&recipient_onion,
368+
best_block_height,
369+
&keysend_preimage,
370+
|_, payload| {
371+
num_reserved_bytes = num_reserved_bytes
372+
.saturating_add(payload.serialized_length())
373+
.saturating_add(PAYLOAD_HMAC_LEN);
374+
},
375+
);
376+
debug_assert!(build_payloads_res.is_ok());
377+
378+
let max_path_length = 1300usize
379+
.checked_sub(num_reserved_bytes)
380+
.map(|p| p / unblinded_intermed_payload_len)
381+
.and_then(|l| u8::try_from(l.saturating_add(num_reserved_hops)).ok())
382+
.ok_or(())?;
383+
384+
route_params.payment_params.max_path_length =
385+
core::cmp::min(max_path_length, MAX_PATH_LENGTH_ESTIMATE);
386+
Ok(())
387+
}
388+
313389
/// Length of the onion data packet. Before TLV-based onions this was 20 65-byte hops, though now
314390
/// the hops can be of variable length.
315391
pub(crate) const ONION_DATA_LEN: usize = 20 * 65;

lightning/src/ln/outbound_payment.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::sign::{EntropySource, NodeSigner, Recipient};
1717
use crate::events::{self, PaymentFailureReason};
1818
use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
1919
use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, PaymentId};
20+
use crate::ln::onion_utils;
2021
use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
2122
use crate::offers::invoice::Bolt12Invoice;
2223
use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router};
@@ -421,6 +422,12 @@ pub enum RetryableSendFailure {
421422
/// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
422423
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
423424
DuplicatePayment,
425+
/// The [`RecipientOnionFields::payment_metadata`], [`RecipientOnionFields::custom_tlvs`], or
426+
/// [`BlindedPath`]s provided are too large and caused us to exceed the maximum onion packet size
427+
/// of 1300 bytes.
428+
///
429+
/// [`BlindedPath`]: crate::blinded_path::BlindedPath
430+
OnionPacketSizeExceeded,
424431
}
425432

426433
/// If a payment fails to send with [`ChannelManager::send_payment_with_route`], it can be in one
@@ -886,7 +893,7 @@ impl OutboundPayments {
886893
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
887894
fn send_payment_internal<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
888895
&self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
889-
keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, route_params: RouteParameters,
896+
keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, mut route_params: RouteParameters,
890897
router: &R, first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES,
891898
node_signer: &NS, best_block_height: u32, logger: &L,
892899
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: SP,
@@ -907,6 +914,15 @@ impl OutboundPayments {
907914
}
908915
}
909916

917+
onion_utils::set_max_path_length(
918+
&mut route_params, &recipient_onion, keysend_preimage, best_block_height
919+
)
920+
.map_err(|()| {
921+
log_error!(logger, "Can't construct an onion packet without exceeding 1300-byte onion \
922+
hop_data length for payment with id {} and hash {}", payment_id, payment_hash);
923+
RetryableSendFailure::OnionPacketSizeExceeded
924+
})?;
925+
910926
let mut route = router.find_route_with_id(
911927
&node_signer.get_node_id(Recipient::Node).unwrap(), &route_params,
912928
Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs(),

0 commit comments

Comments
 (0)