Skip to content

Commit b5ccb98

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 b16e613 commit b5ccb98

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 3 additions & 2 deletions
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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,9 @@ impl OutboundPayments {
931931
custom_tlvs: vec![],
932932
};
933933
let route = match self.find_initial_route(
934-
payment_id, payment_hash, &recipient_onion, None, &mut route_params, router,
935-
&first_hops, &inflight_htlcs, node_signer, best_block_height, logger,
934+
payment_id, payment_hash, &recipient_onion, keysend_preimage, invoice_request.as_ref(),
935+
&mut route_params, router, &first_hops, &inflight_htlcs, node_signer, best_block_height,
936+
logger,
936937
) {
937938
Ok(route) => route,
938939
Err(e) => {
@@ -1044,7 +1045,7 @@ impl OutboundPayments {
10441045

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

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

11961197
onion_utils::set_max_path_length(
1197-
route_params, recipient_onion, keysend_preimage, best_block_height
1198+
route_params, recipient_onion, keysend_preimage, invoice_request, best_block_height
11981199
).map_err(|()| RetryableSendFailure::OnionPacketSizeExceeded)?;
11991200

12001201
let mut route = router.find_route_with_id(
@@ -1237,7 +1238,7 @@ impl OutboundPayments {
12371238
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
12381239
{
12391240
let route = self.find_initial_route(
1240-
payment_id, payment_hash, &recipient_onion, keysend_preimage, &mut route_params, router,
1241+
payment_id, payment_hash, &recipient_onion, keysend_preimage, None, &mut route_params, router,
12411242
&first_hops, &inflight_htlcs, node_signer, best_block_height, logger,
12421243
)?;
12431244

lightning/src/routing/router.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,9 @@ impl RouteParameters {
641641
&mut self, recipient_onion: &RecipientOnionFields, is_keysend: bool, best_block_height: u32
642642
) -> Result<(), ()> {
643643
let keysend_preimage_opt = is_keysend.then(|| PaymentPreimage([42; 32]));
644+
// TODO: no way to account for the invoice request here yet
644645
onion_utils::set_max_path_length(
645-
self, recipient_onion, keysend_preimage_opt, best_block_height
646+
self, recipient_onion, keysend_preimage_opt, None, best_block_height
646647
)
647648
}
648649
}

0 commit comments

Comments
 (0)