@@ -992,9 +992,9 @@ impl OutboundPayments {
992992 ) ;
993993 if let Err ( e) = result {
994994 self . handle_pay_route_err (
995- e, payment_id, payment_hash, route, route_params, router, first_hops,
996- & inflight_htlcs, entropy_source, node_signer, best_block_height, logger,
997- pending_events , & send_payment_along_path
995+ e, payment_id, payment_hash, route, route_params, onion_session_privs , router, first_hops,
996+ & inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events ,
997+ & send_payment_along_path
998998 ) ;
999999 }
10001000 Ok ( ( ) )
@@ -1274,7 +1274,11 @@ impl OutboundPayments {
12741274 log_info ! ( logger, "Sending payment with id {} and hash {} returned {:?}" ,
12751275 payment_id, payment_hash, res) ;
12761276 if let Err ( e) = res {
1277- self . handle_pay_route_err ( e, payment_id, payment_hash, route, route_params, router, first_hops, & inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, & send_payment_along_path) ;
1277+ self . handle_pay_route_err (
1278+ e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1279+ & inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1280+ & send_payment_along_path
1281+ ) ;
12781282 }
12791283 Ok ( ( ) )
12801284 }
@@ -1430,15 +1434,21 @@ impl OutboundPayments {
14301434 best_block_height, & send_payment_along_path) ;
14311435 log_info ! ( logger, "Result retrying payment id {}: {:?}" , & payment_id, res) ;
14321436 if let Err ( e) = res {
1433- self . handle_pay_route_err ( e, payment_id, payment_hash, route, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path) ;
1437+ self . handle_pay_route_err (
1438+ e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1439+ inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1440+ send_payment_along_path
1441+ ) ;
14341442 }
14351443 }
14361444
14371445 fn handle_pay_route_err < R : Deref , NS : Deref , ES : Deref , IH , SP , L : Deref > (
14381446 & self , err : PaymentSendFailure , payment_id : PaymentId , payment_hash : PaymentHash , route : Route ,
1439- mut route_params : RouteParameters , router : & R , first_hops : Vec < ChannelDetails > ,
1440- inflight_htlcs : & IH , entropy_source : & ES , node_signer : & NS , best_block_height : u32 , logger : & L ,
1441- pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : & SP ,
1447+ mut route_params : RouteParameters , onion_session_privs : Vec < [ u8 ; 32 ] > , router : & R ,
1448+ first_hops : Vec < ChannelDetails > , inflight_htlcs : & IH , entropy_source : & ES , node_signer : & NS ,
1449+ best_block_height : u32 , logger : & L ,
1450+ pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > ,
1451+ send_payment_along_path : & SP ,
14421452 )
14431453 where
14441454 R :: Target : Router ,
@@ -1467,11 +1477,13 @@ impl OutboundPayments {
14671477 } ,
14681478 PaymentSendFailure :: PathParameterError ( results) => {
14691479 log_error ! ( logger, "Failed to send to route due to parameter error in a single path. Your router is buggy" ) ;
1480+ self . remove_session_privs ( payment_id, & route, onion_session_privs) ;
14701481 Self :: push_path_failed_evs_and_scids ( payment_id, payment_hash, & mut route_params, route. paths , results. into_iter ( ) , logger, pending_events) ;
14711482 self . abandon_payment ( payment_id, PaymentFailureReason :: UnexpectedError , pending_events) ;
14721483 } ,
14731484 PaymentSendFailure :: ParameterError ( e) => {
14741485 log_error ! ( logger, "Failed to send to route due to parameter error: {:?}. Your router is buggy" , e) ;
1486+ self . remove_session_privs ( payment_id, & route, onion_session_privs) ;
14751487 self . abandon_payment ( payment_id, PaymentFailureReason :: UnexpectedError , pending_events) ;
14761488 } ,
14771489 PaymentSendFailure :: DuplicatePayment => debug_assert ! ( false ) , // unreachable
@@ -1511,6 +1523,21 @@ impl OutboundPayments {
15111523 }
15121524 }
15131525
1526+ // If a payment fails after adding the pending payment but before any HTLCs are locked into
1527+ // channels, we need to clear the session_privs in order for abandoning the payment to succeed.
1528+ fn remove_session_privs (
1529+ & self , payment_id : PaymentId , route : & Route , onion_session_privs : Vec < [ u8 ; 32 ] >
1530+ ) {
1531+ if let Some ( payment) = self . pending_outbound_payments . lock ( ) . unwrap ( ) . get_mut ( & payment_id) {
1532+ for ( path, session_priv_bytes) in route. paths . iter ( ) . zip ( onion_session_privs. into_iter ( ) ) {
1533+ let removed = payment. remove ( & session_priv_bytes, Some ( path) ) ;
1534+ debug_assert ! ( removed, "This can't happen as the payment has an entry for this path added by callers" ) ;
1535+ }
1536+ } else {
1537+ debug_assert ! ( false , "This can't happen as the payment was added by callers" ) ;
1538+ }
1539+ }
1540+
15141541 pub ( super ) fn send_probe < ES : Deref , NS : Deref , F > (
15151542 & self , path : Path , probing_cookie_secret : [ u8 ; 32 ] , entropy_source : & ES , node_signer : & NS ,
15161543 best_block_height : u32 , send_payment_along_path : F
@@ -1784,7 +1811,7 @@ impl OutboundPayments {
17841811 let cur_height = best_block_height + 1 ;
17851812 let mut results = Vec :: new ( ) ;
17861813 debug_assert_eq ! ( route. paths. len( ) , onion_session_privs. len( ) ) ;
1787- for ( path, session_priv_bytes) in route. paths . iter ( ) . zip ( onion_session_privs. into_iter ( ) ) {
1814+ for ( path, session_priv_bytes) in route. paths . iter ( ) . zip ( onion_session_privs. iter ( ) ) {
17881815 let mut path_res = send_payment_along_path ( SendAlongPathArgs {
17891816 path : & path, payment_hash : & payment_hash, recipient_onion, total_value,
17901817 cur_height, payment_id, keysend_preimage : & keysend_preimage, invoice_request,
@@ -1880,9 +1907,15 @@ impl OutboundPayments {
18801907 // If we failed to send any paths, remove the new PaymentId from the `pending_outbound_payments`
18811908 // map as the payment is free to be resent.
18821909 fn remove_outbound_if_all_failed ( & self , payment_id : PaymentId , err : & PaymentSendFailure ) {
1883- if let & PaymentSendFailure :: AllFailedResendSafe ( _) = err {
1884- let removed = self . pending_outbound_payments . lock ( ) . unwrap ( ) . remove ( & payment_id) . is_some ( ) ;
1885- debug_assert ! ( removed, "We should always have a pending payment to remove here" ) ;
1910+ match err {
1911+ PaymentSendFailure :: AllFailedResendSafe ( _)
1912+ | PaymentSendFailure :: ParameterError ( _)
1913+ | PaymentSendFailure :: PathParameterError ( _) =>
1914+ {
1915+ let removed = self . pending_outbound_payments . lock ( ) . unwrap ( ) . remove ( & payment_id) . is_some ( ) ;
1916+ debug_assert ! ( removed, "We should always have a pending payment to remove here" ) ;
1917+ } ,
1918+ PaymentSendFailure :: DuplicatePayment | PaymentSendFailure :: PartialFailure { .. } => { }
18861919 }
18871920 }
18881921
0 commit comments