From: Valentine Wallace Date: Thu, 29 Aug 2024 19:12:41 +0000 (-0400) Subject: Set max path len on receipt of static invoice. X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=8569830eb09338bd4deed80f2617ceaeadc6ec4e;p=rust-lightning Set max path len on receipt of static invoice. Because we may receive a static invoice to pay days before the recipient actually comes back online to receive the payment, it's good to do as many checks as we can up-front. Here we ensure that the blinded paths provided in the invoice won't cause us to exceed the maximum onion packet size. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index dbd44ec0d..f52542855 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -4326,9 +4326,11 @@ where ) -> Result<(), Bolt12PaymentError> { let mut res = Ok(()); PersistenceNotifierGuard::optionally_notify(self, || { + let best_block_height = self.best_block.read().unwrap().height; let features = self.bolt12_invoice_features(); let outbound_pmts_res = self.pending_outbound_payments.static_invoice_received( - invoice, payment_id, features, &*self.entropy_source, &self.pending_events + invoice, payment_id, features, best_block_height, &*self.entropy_source, + &self.pending_events ); let payment_release_secret = match outbound_pmts_res { Ok(secret) => secret, diff --git a/lightning/src/ln/outbound_payment.rs b/lightning/src/ln/outbound_payment.rs index f6ba4d9f8..e616c445f 100644 --- a/lightning/src/ln/outbound_payment.rs +++ b/lightning/src/ln/outbound_payment.rs @@ -937,7 +937,7 @@ impl OutboundPayments { #[cfg(async_payments)] pub(super) fn static_invoice_received( &self, invoice: &StaticInvoice, payment_id: PaymentId, features: Bolt12InvoiceFeatures, - entropy_source: ES, + best_block_height: u32, entropy_source: ES, pending_events: &Mutex)>> ) -> Result<[u8; 32], Bolt12PaymentError> where ES::Target: EntropySource { macro_rules! abandon_with_entry { @@ -988,6 +988,15 @@ impl OutboundPayments { let pay_params = PaymentParameters::from_static_invoice(invoice); let mut route_params = RouteParameters::from_payment_params_and_value(pay_params, amount_msat); route_params.max_total_routing_fee_msat = *max_total_routing_fee_msat; + + if let Err(()) = onion_utils::set_max_path_length( + &mut route_params, &RecipientOnionFields::spontaneous_empty(), Some(keysend_preimage), + best_block_height + ) { + abandon_with_entry!(entry, PaymentFailureReason::RouteNotFound); + return Err(Bolt12PaymentError::SendingFailed(RetryableSendFailure::OnionPacketSizeExceeded)) + } + *entry.into_mut() = PendingOutboundPayment::StaticInvoiceReceived { payment_hash, keysend_preimage,