Skip to content

Commit a9a4126

Browse files
Include invreq in payment onion when sending async payments
Past commits have set us up to include invoice requests in outbound async payment onions. Here we actually pull the invoice request from where it's stored in outbound_payments and pass it into the correct utility for inclusion in the onion. Per <lightning/bolts#1149>, when paying a static invoice we need to include our original invoice request in the HTLC onion since the recipient wouldn't have received it previously.
1 parent bed00a7 commit a9a4126

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
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: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,20 +951,26 @@ impl OutboundPayments {
951951
payment_hash, recipient_onion.clone(), keysend_preimage, &route, Some(retry_strategy),
952952
payment_params, entropy_source, best_block_height
953953
);
954-
match self.pending_outbound_payments.lock().unwrap().entry(payment_id) {
955-
hash_map::Entry::Occupied(entry) => match entry.get() {
956-
PendingOutboundPayment::InvoiceReceived { .. }
957-
| PendingOutboundPayment::StaticInvoiceReceived { .. } => {
958-
*entry.into_mut() = retryable_payment;
954+
let mut invoice_request_opt = None;
955+
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
956+
match outbounds.entry(payment_id) {
957+
hash_map::Entry::Occupied(entry) => match entry.remove() {
958+
PendingOutboundPayment::InvoiceReceived { .. } => {
959+
outbounds.insert(payment_id, retryable_payment);
960+
},
961+
PendingOutboundPayment::StaticInvoiceReceived { invoice_request, .. } => {
962+
invoice_request_opt = Some(invoice_request);
963+
outbounds.insert(payment_id, retryable_payment);
959964
},
960965
_ => return Err(Bolt12PaymentError::DuplicateInvoice),
961966
},
962967
hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice),
963968
}
969+
core::mem::drop(outbounds);
964970

965971
let result = self.pay_route_internal(
966-
&route, payment_hash, &recipient_onion, keysend_preimage, None, payment_id,
967-
Some(route_params.final_value_msat), onion_session_privs, node_signer,
972+
&route, payment_hash, &recipient_onion, keysend_preimage, invoice_request_opt.as_ref(),
973+
payment_id, Some(route_params.final_value_msat), onion_session_privs, node_signer,
968974
best_block_height, &send_payment_along_path
969975
);
970976
log_info!(
@@ -1037,7 +1043,7 @@ impl OutboundPayments {
10371043

10381044
if let Err(()) = onion_utils::set_max_path_length(
10391045
&mut route_params, &RecipientOnionFields::spontaneous_empty(), Some(keysend_preimage),
1040-
best_block_height
1046+
Some(invreq), best_block_height
10411047
) {
10421048
abandon_with_entry!(entry, PaymentFailureReason::RouteNotFound);
10431049
return Err(Bolt12PaymentError::SendingFailed(RetryableSendFailure::OnionPacketSizeExceeded))
@@ -1186,7 +1192,7 @@ impl OutboundPayments {
11861192
}
11871193

11881194
onion_utils::set_max_path_length(
1189-
route_params, recipient_onion, keysend_preimage, best_block_height
1195+
route_params, recipient_onion, keysend_preimage, None, best_block_height
11901196
)
11911197
.map_err(|()| {
11921198
log_error!(logger, "Can't construct an onion packet without exceeding 1300-byte onion \

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)