@@ -442,6 +442,18 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
442442 /// Locked *after* channel_state.
443443 pending_inbound_payments : Mutex < HashMap < PaymentHash , PendingInboundPayment > > ,
444444
445+ /// The session_priv bytes of outbound payments which are pending resolution.
446+ /// The authoritative state of these HTLCs resides either within Channels or ChannelMonitors
447+ /// (if the channel has been force-closed), however we track them here to prevent duplicative
448+ /// PaymentSent/PaymentFailed events. Specifically, in the case of a duplicative
449+ /// update_fulfill_htlc message after a reconnect, we may "claim" a payment twice.
450+ /// Additionally, because ChannelMonitors are often not re-serialized after connecting block(s)
451+ /// which may generate a claim event, we may receive similar duplicate claim/fail MonitorEvents
452+ /// after reloading from disk while replaying blocks against ChannelMonitors.
453+ ///
454+ /// Locked *after* channel_state.
455+ outbound_pending_payments : Mutex < HashSet < [ u8 ; 32 ] > > ,
456+
445457 our_network_key : SecretKey ,
446458 our_network_pubkey : PublicKey ,
447459
@@ -895,6 +907,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
895907 pending_msg_events : Vec :: new ( ) ,
896908 } ) ,
897909 pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
910+ outbound_pending_payments : Mutex :: new ( HashSet :: new ( ) ) ,
898911
899912 our_network_key : keys_manager. get_node_secret ( ) ,
900913 our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( ) ) ,
@@ -1449,7 +1462,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14491462 pub ( crate ) fn send_payment_along_path ( & self , path : & Vec < RouteHop > , payment_hash : & PaymentHash , payment_secret : & Option < PaymentSecret > , total_value : u64 , cur_height : u32 ) -> Result < ( ) , APIError > {
14501463 log_trace ! ( self . logger, "Attempting to send payment for path with next hop {}" , path. first( ) . unwrap( ) . short_channel_id) ;
14511464 let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
1452- let session_priv = SecretKey :: from_slice ( & self . keys_manager . get_secure_random_bytes ( ) [ ..] ) . expect ( "RNG is busted" ) ;
1465+ let session_priv_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
1466+ let session_priv = SecretKey :: from_slice ( & session_priv_bytes[ ..] ) . expect ( "RNG is busted" ) ;
14531467
14541468 let onion_keys = onion_utils:: construct_onion_keys ( & self . secp_ctx , & path, & session_priv)
14551469 . map_err ( |_| APIError :: RouteError { err : "Pubkey along hop was maliciously selected" } ) ?;
@@ -1460,6 +1474,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
14601474 let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, prng_seed, payment_hash) ;
14611475
14621476 let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
1477+ assert ! ( self . outbound_pending_payments. lock( ) . unwrap( ) . insert( session_priv_bytes) ) ;
14631478
14641479 let err: Result < ( ) , _ > = loop {
14651480 let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
@@ -2188,17 +2203,23 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21882203 self . fail_htlc_backwards_internal ( channel_state,
21892204 htlc_src, & payment_hash, HTLCFailReason :: Reason { failure_code, data : onion_failure_data} ) ;
21902205 } ,
2191- HTLCSource :: OutboundRoute { .. } => {
2192- self . pending_events . lock ( ) . unwrap ( ) . push (
2193- events:: Event :: PaymentFailed {
2194- payment_hash,
2195- rejected_by_dest : false ,
2206+ HTLCSource :: OutboundRoute { session_priv, .. } => {
2207+ if {
2208+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2209+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2210+ self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2211+ } {
2212+ self . pending_events . lock ( ) . unwrap ( ) . push (
2213+ events:: Event :: PaymentFailed {
2214+ payment_hash,
2215+ rejected_by_dest : false ,
21962216#[ cfg( test) ]
2197- error_code : None ,
2217+ error_code : None ,
21982218#[ cfg( test) ]
2199- error_data : None ,
2200- }
2201- )
2219+ error_data : None ,
2220+ }
2221+ )
2222+ }
22022223 } ,
22032224 } ;
22042225 }
@@ -2220,7 +2241,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22202241 // from block_connected which may run during initialization prior to the chain_monitor
22212242 // being fully configured. See the docs for `ChannelManagerReadArgs` for more.
22222243 match source {
2223- HTLCSource :: OutboundRoute { ref path, .. } => {
2244+ HTLCSource :: OutboundRoute { ref path, session_priv, .. } => {
2245+ if {
2246+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2247+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2248+ !self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2249+ } {
2250+ return ;
2251+ }
22242252 log_trace ! ( self . logger, "Failing outbound payment HTLC with payment_hash {}" , log_bytes!( payment_hash. 0 ) ) ;
22252253 mem:: drop ( channel_state_lock) ;
22262254 match & onion_error {
@@ -2449,12 +2477,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24492477
24502478 fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < Signer > > , source : HTLCSource , payment_preimage : PaymentPreimage ) {
24512479 match source {
2452- HTLCSource :: OutboundRoute { .. } => {
2480+ HTLCSource :: OutboundRoute { session_priv , .. } => {
24532481 mem:: drop ( channel_state_lock) ;
2454- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
2455- pending_events. push ( events:: Event :: PaymentSent {
2456- payment_preimage
2457- } ) ;
2482+ if {
2483+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2484+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2485+ self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2486+ } {
2487+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
2488+ pending_events. push ( events:: Event :: PaymentSent {
2489+ payment_preimage
2490+ } ) ;
2491+ }
24582492 } ,
24592493 HTLCSource :: PreviousHopData ( hop_data) => {
24602494 let prev_outpoint = hop_data. outpoint ;
@@ -4423,6 +4457,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
44234457 pending_payment. write ( writer) ?;
44244458 }
44254459
4460+ let outbound_pending_payments = self . outbound_pending_payments . lock ( ) . unwrap ( ) ;
4461+ ( outbound_pending_payments. len ( ) as u64 ) . write ( writer) ?;
4462+ for session_priv in outbound_pending_payments. iter ( ) {
4463+ session_priv. write ( writer) ?;
4464+ }
4465+
44264466 Ok ( ( ) )
44274467 }
44284468}
@@ -4662,6 +4702,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
46624702 }
46634703 }
46644704
4705+ let outbound_pending_payments_count: u64 = Readable :: read ( reader) ?;
4706+ let mut outbound_pending_payments: HashSet < [ u8 ; 32 ] > = HashSet :: with_capacity ( cmp:: min ( outbound_pending_payments_count as usize , MAX_ALLOC_SIZE /32 ) ) ;
4707+ for _ in 0 ..outbound_pending_payments_count {
4708+ if !outbound_pending_payments. insert ( Readable :: read ( reader) ?) {
4709+ return Err ( DecodeError :: InvalidValue ) ;
4710+ }
4711+ }
4712+
46654713 let mut secp_ctx = Secp256k1 :: new ( ) ;
46664714 secp_ctx. seeded_randomize ( & args. keys_manager . get_secure_random_bytes ( ) ) ;
46674715
@@ -4681,6 +4729,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
46814729 pending_msg_events : Vec :: new ( ) ,
46824730 } ) ,
46834731 pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
4732+ outbound_pending_payments : Mutex :: new ( outbound_pending_payments) ,
46844733
46854734 our_network_key : args. keys_manager . get_node_secret ( ) ,
46864735 our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & args. keys_manager . get_node_secret ( ) ) ,
0 commit comments