@@ -24,7 +24,7 @@ use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
2424use crate :: offers:: invoice:: Bolt12Invoice ;
2525use crate :: offers:: invoice_request:: InvoiceRequest ;
2626use crate :: offers:: nonce:: Nonce ;
27- use crate :: routing:: router:: { BlindedTail , InFlightHtlcs , Path , PaymentParameters , Route , RouteParameters , Router } ;
27+ use crate :: routing:: router:: { BlindedTail , InFlightHtlcs , RouteParametersConfig , Path , PaymentParameters , Route , RouteParameters , Router } ;
2828use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
2929use crate :: util:: errors:: APIError ;
3030use crate :: util:: logger:: Logger ;
@@ -61,7 +61,11 @@ pub(crate) enum PendingOutboundPayment {
6161 AwaitingInvoice {
6262 expiration : StaleExpiration ,
6363 retry_strategy : Retry ,
64+ // Deprecated: Retained for backward compatibility.
65+ // If set during read, this field overrides `RouteParameters::max_total_routing_fee_msat`
66+ // instead of `RouteParametersConfig::max_total_routing_fee_msat`.
6467 max_total_routing_fee_msat : Option < u64 > ,
68+ route_params_config : Option < RouteParametersConfig > ,
6569 retryable_invoice_request : Option < RetryableInvoiceRequest >
6670 } ,
6771 // This state will never be persisted to disk because we transition from `AwaitingInvoice` to
@@ -70,9 +74,12 @@ pub(crate) enum PendingOutboundPayment {
7074 InvoiceReceived {
7175 payment_hash : PaymentHash ,
7276 retry_strategy : Retry ,
73- // Note this field is currently just replicated from AwaitingInvoice but not actually
74- // used anywhere.
77+ // Deprecated: Retained for backward compatibility.
7578 max_total_routing_fee_msat : Option < u64 > ,
79+ // Currently unused, but replicated from `AwaitingInvoice` to avoid potential
80+ // race conditions where this field might be missing upon reload. It may be required
81+ // for future retries.
82+ route_params_config : Option < RouteParametersConfig > ,
7683 } ,
7784 // This state applies when we are paying an often-offline recipient and another node on the
7885 // network served us a static invoice on the recipient's behalf in response to our invoice
@@ -849,14 +856,24 @@ impl OutboundPayments {
849856 match self . pending_outbound_payments . lock ( ) . unwrap ( ) . entry ( payment_id) {
850857 hash_map:: Entry :: Occupied ( entry) => match entry. get ( ) {
851858 PendingOutboundPayment :: AwaitingInvoice {
852- retry_strategy : retry, max_total_routing_fee_msat : max_total_fee, ..
859+ retry_strategy : retry, max_total_routing_fee_msat : max_total_fee, route_params_config , ..
853860 } => {
854861 retry_strategy = * retry;
855- max_total_routing_fee_msat = * max_total_fee;
862+ // If max_total_fee is present, update route_params_config with the specified fee.
863+ // This supports the standard behavior during downgrades.
864+ let route_params_config = max_total_fee. map_or ( * route_params_config, |fee_msat| {
865+ route_params_config. map ( |params_override|
866+ params_override. with_max_total_routing_fee_msat ( fee_msat)
867+ ) . or_else ( || Some ( RouteParametersConfig :: new ( ) . with_max_total_routing_fee_msat ( fee_msat) ) )
868+ } ) ;
869+ max_total_routing_fee_msat = route_params_config. and_then (
870+ |params| params. max_total_routing_fee_msat
871+ ) ;
856872 * entry. into_mut ( ) = PendingOutboundPayment :: InvoiceReceived {
857873 payment_hash,
858874 retry_strategy : * retry,
859- max_total_routing_fee_msat,
875+ max_total_routing_fee_msat : * max_total_fee,
876+ route_params_config : route_params_config,
860877 } ;
861878 } ,
862879 _ => return Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
@@ -1004,7 +1021,7 @@ impl OutboundPayments {
10041021 match self . pending_outbound_payments . lock ( ) . unwrap ( ) . entry ( payment_id) {
10051022 hash_map:: Entry :: Occupied ( mut entry) => match entry. get ( ) {
10061023 PendingOutboundPayment :: AwaitingInvoice {
1007- retry_strategy, retryable_invoice_request, max_total_routing_fee_msat , ..
1024+ retry_strategy, retryable_invoice_request, route_params_config , ..
10081025 } => {
10091026 let invreq = & retryable_invoice_request
10101027 . as_ref ( )
@@ -1031,7 +1048,7 @@ impl OutboundPayments {
10311048 let payment_hash = PaymentHash ( Sha256 :: hash ( & keysend_preimage. 0 ) . to_byte_array ( ) ) ;
10321049 let pay_params = PaymentParameters :: from_static_invoice ( invoice) ;
10331050 let mut route_params = RouteParameters :: from_payment_params_and_value ( pay_params, amount_msat) ;
1034- route_params. max_total_routing_fee_msat = * max_total_routing_fee_msat;
1051+ route_params. max_total_routing_fee_msat = route_params_config . map ( |params| params . max_total_routing_fee_msat ) . flatten ( ) ;
10351052
10361053 if let Err ( ( ) ) = onion_utils:: set_max_path_length (
10371054 & mut route_params, & RecipientOnionFields :: spontaneous_empty ( ) , Some ( keysend_preimage) ,
@@ -1599,6 +1616,10 @@ impl OutboundPayments {
15991616 max_total_routing_fee_msat : Option < u64 > , retryable_invoice_request : Option < RetryableInvoiceRequest >
16001617 ) -> Result < ( ) , ( ) > {
16011618 let mut pending_outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
1619+ let route_params_config = max_total_routing_fee_msat. map (
1620+ |fee_msats| RouteParametersConfig :: new ( )
1621+ . with_max_total_routing_fee_msat ( fee_msats)
1622+ ) ;
16021623 match pending_outbounds. entry ( payment_id) {
16031624 hash_map:: Entry :: Occupied ( _) => Err ( ( ) ) ,
16041625 hash_map:: Entry :: Vacant ( entry) => {
@@ -1608,7 +1629,9 @@ impl OutboundPayments {
16081629 entry. insert ( PendingOutboundPayment :: AwaitingInvoice {
16091630 expiration,
16101631 retry_strategy,
1611- max_total_routing_fee_msat,
1632+ route_params_config,
1633+ // Retained for downgrade support.
1634+ max_total_routing_fee_msat : None ,
16121635 retryable_invoice_request,
16131636 } ) ;
16141637
@@ -2240,10 +2263,12 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
22402263 ( 2 , retry_strategy, required) ,
22412264 ( 4 , max_total_routing_fee_msat, option) ,
22422265 ( 5 , retryable_invoice_request, option) ,
2266+ ( 7 , route_params_config, option) ,
22432267 } ,
22442268 ( 7 , InvoiceReceived ) => {
22452269 ( 0 , payment_hash, required) ,
22462270 ( 2 , retry_strategy, required) ,
2271+ ( 3 , route_params_config, option) ,
22472272 ( 4 , max_total_routing_fee_msat, option) ,
22482273 } ,
22492274 // Added in 0.0.125. Prior versions will drop these outbounds on downgrade, which is safe because
0 commit comments