Skip to content

Commit 568decf

Browse files
Factor invoice requests into payment path length limiting
Async payments include the original invoice request in the payment onion. Since invreqs may include blinded paths, it's important to factor them into our max path length calculations since they may take up a significant portion of the 1300-byte onion.
1 parent 9ac4080 commit 568decf

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

lightning/src/ln/onion_utils.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ pub(crate) const MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY: u64 = 100_000_000;
320320

321321
pub(crate) fn set_max_path_length(
322322
route_params: &mut RouteParameters, recipient_onion: &RecipientOnionFields,
323-
keysend_preimage: Option<PaymentPreimage>, best_block_height: u32,
323+
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<&InvoiceRequest>,
324+
best_block_height: u32,
324325
) -> Result<(), ()> {
325326
const PAYLOAD_HMAC_LEN: usize = 32;
326327
let unblinded_intermed_payload_len = msgs::OutboundOnionPayload::Forward {
@@ -367,7 +368,7 @@ pub(crate) fn set_max_path_length(
367368
&recipient_onion,
368369
best_block_height,
369370
&keysend_preimage,
370-
None,
371+
invoice_request,
371372
|_, payload| {
372373
num_reserved_bytes = num_reserved_bytes
373374
.saturating_add(payload.serialized_length())

lightning/src/ln/outbound_payment.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,9 @@ impl OutboundPayments {
932932
custom_tlvs: vec![],
933933
};
934934
let route = match self.find_initial_route(
935-
payment_id, payment_hash, &recipient_onion, None, &mut route_params, router,
936-
&first_hops, &inflight_htlcs, node_signer, best_block_height, logger,
935+
payment_id, payment_hash, &recipient_onion, keysend_preimage, invoice_request.as_ref(),
936+
&mut route_params, router, &first_hops, &inflight_htlcs, node_signer, best_block_height,
937+
logger,
937938
) {
938939
Ok(route) => route,
939940
Err(e) => {
@@ -1045,7 +1046,7 @@ impl OutboundPayments {
10451046

10461047
if let Err(()) = onion_utils::set_max_path_length(
10471048
&mut route_params, &RecipientOnionFields::spontaneous_empty(), Some(keysend_preimage),
1048-
best_block_height
1049+
Some(invreq), best_block_height
10491050
) {
10501051
abandon_with_entry!(entry, PaymentFailureReason::RouteNotFound);
10511052
return Err(Bolt12PaymentError::SendingFailed(RetryableSendFailure::OnionPacketSizeExceeded))
@@ -1175,8 +1176,8 @@ impl OutboundPayments {
11751176
}
11761177

11771178
fn find_initial_route<R: Deref, NS: Deref, IH, L: Deref>(
1178-
&self, payment_id: PaymentId, payment_hash: PaymentHash,
1179-
recipient_onion: &RecipientOnionFields, keysend_preimage: Option<PaymentPreimage>,
1179+
&self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: &RecipientOnionFields,
1180+
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<&InvoiceRequest>,
11801181
route_params: &mut RouteParameters, router: &R, first_hops: &Vec<ChannelDetails>,
11811182
inflight_htlcs: &IH, node_signer: &NS, best_block_height: u32, logger: &L,
11821183
) -> Result<Route, RetryableSendFailure>
@@ -1195,7 +1196,7 @@ impl OutboundPayments {
11951196
}
11961197

11971198
onion_utils::set_max_path_length(
1198-
route_params, recipient_onion, keysend_preimage, best_block_height
1199+
route_params, recipient_onion, keysend_preimage, invoice_request, best_block_height
11991200
)
12001201
.map_err(|()| {
12011202
log_error!(logger, "Can't construct an onion packet without exceeding 1300-byte onion \
@@ -1243,7 +1244,7 @@ impl OutboundPayments {
12431244
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
12441245
{
12451246
let route = self.find_initial_route(
1246-
payment_id, payment_hash, &recipient_onion, keysend_preimage, &mut route_params, router,
1247+
payment_id, payment_hash, &recipient_onion, keysend_preimage, None, &mut route_params, router,
12471248
&first_hops, &inflight_htlcs, node_signer, best_block_height, logger,
12481249
)?;
12491250

lightning/src/routing/router.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,9 @@ impl RouteParameters {
613613
&mut self, recipient_onion: &RecipientOnionFields, is_keysend: bool, best_block_height: u32
614614
) -> Result<(), ()> {
615615
let keysend_preimage_opt = is_keysend.then(|| PaymentPreimage([42; 32]));
616+
// TODO: no way to account for the invoice request here yet
616617
onion_utils::set_max_path_length(
617-
self, recipient_onion, keysend_preimage_opt, best_block_height
618+
self, recipient_onion, keysend_preimage_opt, None, best_block_height
618619
)
619620
}
620621
}

0 commit comments

Comments
 (0)