@@ -14,12 +14,12 @@ use bitcoin::hashes::sha256::Hash as Sha256;
14
14
use bitcoin:: secp256k1:: { self , Secp256k1 , SecretKey } ;
15
15
16
16
use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
17
- use crate :: events:: { self , PaymentFailureReason } ;
17
+ use crate :: events:: { self , PaymentFailureReason , Event , PaymentPurpose } ;
18
18
use crate :: ln:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
19
19
use crate :: ln:: channelmanager:: { ChannelDetails , EventCompletionAction , HTLCSource , PaymentId } ;
20
20
use crate :: ln:: onion_utils:: { DecodedOnionFailure , HTLCFailReason } ;
21
21
use crate :: offers:: invoice:: Bolt12Invoice ;
22
- use crate :: routing:: router:: { InFlightHtlcs , Path , PaymentParameters , Route , RouteParameters , Router } ;
22
+ use crate :: routing:: router:: { InFlightHtlcs , Path , PaymentParameters , Route , RouteParameters , Router , Payee } ;
23
23
use crate :: util:: errors:: APIError ;
24
24
use crate :: util:: logger:: Logger ;
25
25
use crate :: util:: time:: Time ;
@@ -416,6 +416,8 @@ pub enum RetryableSendFailure {
416
416
/// [`Event::PaymentSent`]: crate::events::Event::PaymentSent
417
417
/// [`Event::PaymentFailed`]: crate::events::Event::PaymentFailed
418
418
DuplicatePayment ,
419
+ /// The intended recipient rejected our payment.
420
+ RecipientRejected ,
419
421
}
420
422
421
423
/// If a payment fails to send with [`ChannelManager::send_payment_with_route`], it can be in one
@@ -677,7 +679,7 @@ impl OutboundPayments {
677
679
678
680
pub ( super ) fn send_payment < R : Deref , ES : Deref , NS : Deref , IH , SP , L : Deref > (
679
681
& self , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields , payment_id : PaymentId ,
680
- retry_strategy : Retry , route_params : RouteParameters , router : & R ,
682
+ retry_strategy : Retry , route_params : RouteParameters , payment_preimage : Option < PaymentPreimage > , router : & R ,
681
683
first_hops : Vec < ChannelDetails > , compute_inflight_htlcs : IH , entropy_source : & ES ,
682
684
node_signer : & NS , best_block_height : u32 , logger : & L ,
683
685
pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : SP ,
@@ -691,7 +693,7 @@ impl OutboundPayments {
691
693
SP : Fn ( SendAlongPathArgs ) -> Result < ( ) , APIError > ,
692
694
{
693
695
self . send_payment_internal ( payment_id, payment_hash, recipient_onion, None , retry_strategy,
694
- route_params, router, first_hops, & compute_inflight_htlcs, entropy_source, node_signer,
696
+ route_params, payment_preimage , router, first_hops, & compute_inflight_htlcs, entropy_source, node_signer,
695
697
best_block_height, logger, pending_events, & send_payment_along_path)
696
698
}
697
699
@@ -730,7 +732,7 @@ impl OutboundPayments {
730
732
. unwrap_or_else ( || PaymentPreimage ( entropy_source. get_secure_random_bytes ( ) ) ) ;
731
733
let payment_hash = PaymentHash ( Sha256 :: hash ( & preimage. 0 ) . into_inner ( ) ) ;
732
734
self . send_payment_internal ( payment_id, payment_hash, recipient_onion, Some ( preimage) ,
733
- retry_strategy, route_params, router, first_hops, inflight_htlcs, entropy_source,
735
+ retry_strategy, route_params, Some ( preimage ) , router, first_hops, inflight_htlcs, entropy_source,
734
736
node_signer, best_block_height, logger, pending_events, send_payment_along_path)
735
737
. map ( |( ) | payment_hash)
736
738
}
@@ -882,7 +884,7 @@ impl OutboundPayments {
882
884
fn send_payment_internal < R : Deref , NS : Deref , ES : Deref , IH , SP , L : Deref > (
883
885
& self , payment_id : PaymentId , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
884
886
keysend_preimage : Option < PaymentPreimage > , retry_strategy : Retry , route_params : RouteParameters ,
885
- router : & R , first_hops : Vec < ChannelDetails > , inflight_htlcs : IH , entropy_source : & ES ,
887
+ payment_preimage : Option < PaymentPreimage > , router : & R , first_hops : Vec < ChannelDetails > , inflight_htlcs : IH , entropy_source : & ES ,
886
888
node_signer : & NS , best_block_height : u32 , logger : & L ,
887
889
pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : SP ,
888
890
) -> Result < ( ) , RetryableSendFailure >
@@ -902,16 +904,56 @@ impl OutboundPayments {
902
904
}
903
905
}
904
906
905
- let mut route = router. find_route_with_id (
906
- & node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) , & route_params,
907
+ let payer = node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) ;
908
+ let route = match router. find_route_with_id (
909
+ & payer, & route_params,
907
910
Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) , inflight_htlcs ( ) ,
908
911
payment_hash, payment_id,
909
- ) . map_err ( |_| {
910
- log_error ! ( logger, "Failed to find route for payment with id {} and hash {}" ,
911
- payment_id, payment_hash) ;
912
- RetryableSendFailure :: RouteNotFound
913
- } ) ?;
914
-
912
+ ) {
913
+ Ok ( res) => Some ( res) ,
914
+ Err ( _) => {
915
+ // The following code handles self payments.
916
+ if let Payee :: Clear { node_id, .. } = route_params. payment_params . payee {
917
+ let is_phantom_payee = match node_signer. get_node_id ( Recipient :: PhantomNode ) {
918
+ Ok ( phantom_node_id) => node_id == phantom_node_id,
919
+ Err ( _) => false ,
920
+ } ;
921
+ if node_id == payer || is_phantom_payee {
922
+ let dummy_route = Route {
923
+ paths : vec ! [ Path {
924
+ hops: vec![ ] ,
925
+ blinded_tail: None ,
926
+ } ] ,
927
+ route_params : Some ( route_params. clone ( ) ) ,
928
+ } ;
929
+
930
+ // We add a new pending payment only to mark it as fulfilled immediately, to protect against
931
+ // duplicate payments.
932
+ let _ = self . add_new_pending_payment ( payment_hash,
933
+ recipient_onion. clone ( ) , payment_id, keysend_preimage, & dummy_route, Some ( retry_strategy) ,
934
+ Some ( route_params. payment_params . clone ( ) ) , entropy_source, best_block_height)
935
+ . map_err ( |_| {
936
+ log_error ! ( logger, "Payment with id {} is already pending. New payment had payment hash {}" ,
937
+ payment_id, payment_hash) ;
938
+ RetryableSendFailure :: DuplicatePayment
939
+ } ) ?;
940
+ let mut pending_outbounds_lock = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
941
+ let payment = pending_outbounds_lock. get_mut ( & payment_id) . unwrap ( ) ;
942
+ payment. mark_fulfilled ( ) ;
943
+ let mut pending_events_lock = pending_events. lock ( ) . unwrap ( ) ;
944
+ pending_events_lock. push_back ( ( Event :: PaymentSent { payment_id : Some ( payment_id) , payment_preimage : payment_preimage. unwrap ( ) ,
945
+ payment_hash, fee_paid_msat : None } , None ) ) ;
946
+ return Ok ( ( ) ) ;
947
+ }
948
+ }
949
+ None
950
+ }
951
+ } ;
952
+ if route. is_none ( ) {
953
+ log_error ! ( logger, "Failed to find route for payment with id {} and hash {}" , payment_id, payment_hash) ;
954
+ return Err ( RetryableSendFailure :: RouteNotFound ) ;
955
+ }
956
+ let mut route = route. unwrap ( ) ;
915
957
if route. route_params . as_ref ( ) != Some ( & route_params) {
916
958
debug_assert ! ( false ,
917
959
"Routers are expected to return a Route which includes the requested RouteParameters" ) ;
@@ -1897,7 +1939,7 @@ mod tests {
1897
1939
} else {
1898
1940
let err = outbound_payments. send_payment (
1899
1941
PaymentHash ( [ 0 ; 32 ] ) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( [ 0 ; 32 ] ) ,
1900
- Retry :: Attempts ( 0 ) , expired_route_params, & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
1942
+ Retry :: Attempts ( 0 ) , expired_route_params, None , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
1901
1943
& & keys_manager, & & keys_manager, 0 , & & logger, & pending_events, |_| Ok ( ( ) ) ) . unwrap_err ( ) ;
1902
1944
if let RetryableSendFailure :: PaymentExpired = err { } else { panic ! ( "Unexpected error" ) ; }
1903
1945
}
@@ -1939,7 +1981,7 @@ mod tests {
1939
1981
} else {
1940
1982
let err = outbound_payments. send_payment (
1941
1983
PaymentHash ( [ 0 ; 32 ] ) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( [ 0 ; 32 ] ) ,
1942
- Retry :: Attempts ( 0 ) , route_params, & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
1984
+ Retry :: Attempts ( 0 ) , route_params, None , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
1943
1985
& & keys_manager, & & keys_manager, 0 , & & logger, & pending_events, |_| Ok ( ( ) ) ) . unwrap_err ( ) ;
1944
1986
if let RetryableSendFailure :: RouteNotFound = err {
1945
1987
} else { panic ! ( "Unexpected error" ) ; }
@@ -1987,7 +2029,7 @@ mod tests {
1987
2029
let pending_events = Mutex :: new ( VecDeque :: new ( ) ) ;
1988
2030
outbound_payments. send_payment (
1989
2031
PaymentHash ( [ 0 ; 32 ] ) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( [ 0 ; 32 ] ) ,
1990
- Retry :: Attempts ( 0 ) , route_params. clone ( ) , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
2032
+ Retry :: Attempts ( 0 ) , route_params. clone ( ) , None , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
1991
2033
& & keys_manager, & & keys_manager, 0 , & & logger, & pending_events,
1992
2034
|_| Err ( APIError :: ChannelUnavailable { err : "test" . to_owned ( ) } ) ) . unwrap ( ) ;
1993
2035
let mut events = pending_events. lock ( ) . unwrap ( ) ;
@@ -2005,15 +2047,15 @@ mod tests {
2005
2047
// Ensure that a MonitorUpdateInProgress "error" will not result in a PaymentPathFailed event.
2006
2048
outbound_payments. send_payment (
2007
2049
PaymentHash ( [ 0 ; 32 ] ) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( [ 0 ; 32 ] ) ,
2008
- Retry :: Attempts ( 0 ) , route_params. clone ( ) , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
2050
+ Retry :: Attempts ( 0 ) , route_params. clone ( ) , None , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
2009
2051
& & keys_manager, & & keys_manager, 0 , & & logger, & pending_events,
2010
2052
|_| Err ( APIError :: MonitorUpdateInProgress ) ) . unwrap ( ) ;
2011
2053
assert_eq ! ( pending_events. lock( ) . unwrap( ) . len( ) , 0 ) ;
2012
2054
2013
2055
// Ensure that any other error will result in a PaymentPathFailed event but no blamed scid.
2014
2056
outbound_payments. send_payment (
2015
2057
PaymentHash ( [ 0 ; 32 ] ) , RecipientOnionFields :: spontaneous_empty ( ) , PaymentId ( [ 1 ; 32 ] ) ,
2016
- Retry :: Attempts ( 0 ) , route_params. clone ( ) , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
2058
+ Retry :: Attempts ( 0 ) , route_params. clone ( ) , None , & & router, vec ! [ ] , || InFlightHtlcs :: new ( ) ,
2017
2059
& & keys_manager, & & keys_manager, 0 , & & logger, & pending_events,
2018
2060
|_| Err ( APIError :: APIMisuseError { err : "test" . to_owned ( ) } ) ) . unwrap ( ) ;
2019
2061
let events = pending_events. lock ( ) . unwrap ( ) ;
0 commit comments