@@ -851,7 +851,7 @@ impl OutboundPayments {
851851 entropy_source : & ES , node_signer : & NS , node_id_lookup : & NL ,
852852 secp_ctx : & Secp256k1 < secp256k1:: All > , best_block_height : u32 , logger : & L ,
853853 pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > ,
854- send_payment_along_path : SP ,
854+ send_payment_along_path : SP , with_manual_handling : bool
855855 ) -> Result < ( ) , Bolt12PaymentError >
856856 where
857857 R :: Target : Router ,
@@ -863,25 +863,14 @@ impl OutboundPayments {
863863 SP : Fn ( SendAlongPathArgs ) -> Result < ( ) , APIError > ,
864864 {
865865 let payment_hash = invoice. payment_hash ( ) ;
866- let params_config;
867- let retry_strategy;
868- match self . pending_outbound_payments . lock ( ) . unwrap ( ) . entry ( payment_id) {
869- hash_map:: Entry :: Occupied ( entry) => match entry. get ( ) {
870- PendingOutboundPayment :: AwaitingInvoice {
871- retry_strategy : retry, route_params_config, ..
872- } => {
873- retry_strategy = * retry;
874- params_config = * route_params_config;
875- * entry. into_mut ( ) = PendingOutboundPayment :: InvoiceReceived {
876- payment_hash,
877- retry_strategy : * retry,
878- route_params_config : * route_params_config,
879- } ;
880- } ,
881- _ => return Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
882- } ,
883- hash_map:: Entry :: Vacant ( _) => return Err ( Bolt12PaymentError :: UnexpectedInvoice ) ,
884- }
866+
867+ // When manual invoice handling is enabled, the corresponding `PendingOutboundPayment` entry
868+ // is already updated at the time the invoice is received. This ensures that `InvoiceReceived`
869+ // event generation remains idempotent, even if the same invoice is received again before the
870+ // current one is handled by the user.
871+ if !with_manual_handling { self . mark_invoice_received ( payment_id, payment_hash) ? }
872+
873+ let ( retry_strategy, params_config) = self . received_invoice_details ( payment_id) ?;
885874
886875 if invoice. invoice_features ( ) . requires_unknown_bits_from ( & features) {
887876 self . abandon_payment (
@@ -1789,6 +1778,44 @@ impl OutboundPayments {
17891778 }
17901779 }
17911780
1781+ pub ( super ) fn mark_invoice_received (
1782+ & self , payment_id : PaymentId , payment_hash : PaymentHash
1783+ ) -> Result < ( ) , Bolt12PaymentError > {
1784+ match self . pending_outbound_payments . lock ( ) . unwrap ( ) . entry ( payment_id) {
1785+ hash_map:: Entry :: Occupied ( entry) => match entry. get ( ) {
1786+ PendingOutboundPayment :: AwaitingInvoice {
1787+ retry_strategy : retry, route_params_config, ..
1788+ } => {
1789+ * entry. into_mut ( ) = PendingOutboundPayment :: InvoiceReceived {
1790+ payment_hash,
1791+ retry_strategy : * retry,
1792+ route_params_config : * route_params_config,
1793+ } ;
1794+
1795+ Ok ( ( ) )
1796+ } ,
1797+ _ => Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
1798+ } ,
1799+ hash_map:: Entry :: Vacant ( _) => Err ( Bolt12PaymentError :: UnexpectedInvoice ) ,
1800+ }
1801+ }
1802+
1803+ fn received_invoice_details (
1804+ & self , payment_id : PaymentId ,
1805+ ) -> Result < ( Retry , RouteParametersConfig ) , Bolt12PaymentError > {
1806+ match self . pending_outbound_payments . lock ( ) . unwrap ( ) . entry ( payment_id) {
1807+ hash_map:: Entry :: Occupied ( entry) => match entry. get ( ) {
1808+ PendingOutboundPayment :: InvoiceReceived {
1809+ retry_strategy, route_params_config, ..
1810+ } => {
1811+ return Ok ( ( * retry_strategy, * route_params_config) )
1812+ } ,
1813+ _ => return Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
1814+ } ,
1815+ hash_map:: Entry :: Vacant ( _) => return Err ( Bolt12PaymentError :: UnexpectedInvoice ) ,
1816+ }
1817+ }
1818+
17921819 fn pay_route_internal < NS : Deref , F > (
17931820 & self , route : & Route , payment_hash : PaymentHash , recipient_onion : & RecipientOnionFields ,
17941821 keysend_preimage : Option < PaymentPreimage > , invoice_request : Option < & InvoiceRequest > ,
@@ -2868,7 +2895,7 @@ mod tests {
28682895 outbound_payments. send_payment_for_bolt12_invoice(
28692896 & invoice, payment_id, &&router, vec![ ] , Bolt12InvoiceFeatures :: empty( ) ,
28702897 || InFlightHtlcs :: new( ) , &&keys_manager, &&keys_manager, & EmptyNodeIdLookUp { } ,
2871- & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( )
2898+ & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( ) , false
28722899 ) ,
28732900 Err ( Bolt12PaymentError :: SendingFailed ( RetryableSendFailure :: PaymentExpired ) ) ,
28742901 ) ;
@@ -2930,7 +2957,7 @@ mod tests {
29302957 outbound_payments. send_payment_for_bolt12_invoice(
29312958 & invoice, payment_id, &&router, vec![ ] , Bolt12InvoiceFeatures :: empty( ) ,
29322959 || InFlightHtlcs :: new( ) , &&keys_manager, &&keys_manager, & EmptyNodeIdLookUp { } ,
2933- & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( )
2960+ & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( ) , false
29342961 ) ,
29352962 Err ( Bolt12PaymentError :: SendingFailed ( RetryableSendFailure :: RouteNotFound ) ) ,
29362963 ) ;
@@ -3005,7 +3032,7 @@ mod tests {
30053032 outbound_payments. send_payment_for_bolt12_invoice(
30063033 & invoice, payment_id, &&router, vec![ ] , Bolt12InvoiceFeatures :: empty( ) ,
30073034 || InFlightHtlcs :: new( ) , &&keys_manager, &&keys_manager, & EmptyNodeIdLookUp { } ,
3008- & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( )
3035+ & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( ) , false
30093036 ) ,
30103037 Err ( Bolt12PaymentError :: UnexpectedInvoice ) ,
30113038 ) ;
@@ -3025,7 +3052,7 @@ mod tests {
30253052 outbound_payments. send_payment_for_bolt12_invoice(
30263053 & invoice, payment_id, &&router, vec![ ] , Bolt12InvoiceFeatures :: empty( ) ,
30273054 || InFlightHtlcs :: new( ) , &&keys_manager, &&keys_manager, & EmptyNodeIdLookUp { } ,
3028- & secp_ctx, 0 , &&logger, & pending_events, |_| Ok ( ( ) )
3055+ & secp_ctx, 0 , &&logger, & pending_events, |_| Ok ( ( ) ) , false
30293056 ) ,
30303057 Ok ( ( ) ) ,
30313058 ) ;
@@ -3036,7 +3063,7 @@ mod tests {
30363063 outbound_payments. send_payment_for_bolt12_invoice(
30373064 & invoice, payment_id, &&router, vec![ ] , Bolt12InvoiceFeatures :: empty( ) ,
30383065 || InFlightHtlcs :: new( ) , &&keys_manager, &&keys_manager, & EmptyNodeIdLookUp { } ,
3039- & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( )
3066+ & secp_ctx, 0 , &&logger, & pending_events, |_| panic!( ) , false
30403067 ) ,
30413068 Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
30423069 ) ;
0 commit comments