Rename variable
[rust-lightning] / lightning / src / ln / outbound_payment.rs
index c452a660e4239dee0d96d42ea28552796b25ed35..ac9eb74823ff15ac444209021399ab8443f6573f 100644 (file)
@@ -16,7 +16,9 @@ use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
 use crate::sign::{EntropySource, NodeSigner, Recipient};
 use crate::events::{self, PaymentFailureReason};
 use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
-use crate::ln::channelmanager::{ChannelDetails, EventCompletionAction, HTLCSource, PaymentId};
+use crate::ln::channel_state::ChannelDetails;
+use crate::ln::channelmanager::{EventCompletionAction, HTLCSource, PaymentId};
+use crate::ln::onion_utils;
 use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
 use crate::offers::invoice::Bolt12Invoice;
 use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router};
@@ -69,7 +71,7 @@ pub(crate) enum PendingOutboundPayment {
                keysend_preimage: Option<PaymentPreimage>,
                custom_tlvs: Vec<(u64, Vec<u8>)>,
                pending_amt_msat: u64,
-               /// Used to track the fee paid. Only present if the payment was serialized on 0.0.103+.
+               /// Used to track the fee paid. Present iff the payment was serialized on 0.0.103+.
                pending_fee_msat: Option<u64>,
                /// The total payment amount across all paths, used to verify that a retry is not overpaying.
                total_msat: u64,
@@ -421,6 +423,12 @@ pub enum RetryableSendFailure {
        /// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
        /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
        DuplicatePayment,
+       /// The [`RecipientOnionFields::payment_metadata`], [`RecipientOnionFields::custom_tlvs`], or
+       /// [`BlindedPath`]s provided are too large and caused us to exceed the maximum onion packet size
+       /// of 1300 bytes.
+       ///
+       /// [`BlindedPath`]: crate::blinded_path::BlindedPath
+       OnionPacketSizeExceeded,
 }
 
 /// If a payment fails to send with [`ChannelManager::send_payment_with_route`], it can be in one
@@ -493,9 +501,9 @@ pub enum PaymentSendFailure {
        },
 }
 
-/// An error when attempting to pay a BOLT 12 invoice.
+/// An error when attempting to pay a [`Bolt12Invoice`].
 #[derive(Clone, Debug, PartialEq, Eq)]
-pub(super) enum Bolt12PaymentError {
+pub enum Bolt12PaymentError {
        /// The invoice was not requested.
        UnexpectedInvoice,
        /// Payment for an invoice with the corresponding [`PaymentId`] was already initiated.
@@ -799,9 +807,11 @@ impl OutboundPayments {
                        hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice),
                };
 
-               let pay_params = PaymentParameters::from_bolt12_invoice(&invoice);
+               let payment_params = PaymentParameters::from_bolt12_invoice(&invoice);
                let amount_msat = invoice.amount_msats();
-               let mut route_params = RouteParameters::from_payment_params_and_value(pay_params, amount_msat);
+               let mut route_params = RouteParameters::from_payment_params_and_value(
+                       payment_params, amount_msat
+               );
                if let Some(max_fee_msat) = max_total_routing_fee_msat {
                        route_params.max_total_routing_fee_msat = Some(max_fee_msat);
                }
@@ -886,7 +896,7 @@ impl OutboundPayments {
        /// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
        fn send_payment_internal<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
                &self, payment_id: PaymentId, payment_hash: PaymentHash, recipient_onion: RecipientOnionFields,
-               keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, route_params: RouteParameters,
+               keysend_preimage: Option<PaymentPreimage>, retry_strategy: Retry, mut route_params: RouteParameters,
                router: &R, first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES,
                node_signer: &NS, best_block_height: u32, logger: &L,
                pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: SP,
@@ -907,6 +917,15 @@ impl OutboundPayments {
                        }
                }
 
+               onion_utils::set_max_path_length(
+                       &mut route_params, &recipient_onion, keysend_preimage, best_block_height
+               )
+                       .map_err(|()| {
+                               log_error!(logger, "Can't construct an onion packet without exceeding 1300-byte onion \
+                                       hop_data length for payment with id {} and hash {}", payment_id, payment_hash);
+                               RetryableSendFailure::OnionPacketSizeExceeded
+                       })?;
+
                let mut route = router.find_route_with_id(
                        &node_signer.get_node_id(Recipient::Node).unwrap(), &route_params,
                        Some(&first_hops.iter().collect::<Vec<_>>()), inflight_htlcs(),
@@ -1834,7 +1853,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
 
 #[cfg(test)]
 mod tests {
-       use bitcoin::network::constants::Network;
+       use bitcoin::network::Network;
        use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
 
        use core::time::Duration;