@@ -442,6 +442,18 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
442
442
/// Locked *after* channel_state.
443
443
pending_inbound_payments : Mutex < HashMap < PaymentHash , PendingInboundPayment > > ,
444
444
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
+
445
457
our_network_key : SecretKey ,
446
458
our_network_pubkey : PublicKey ,
447
459
@@ -913,6 +925,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
913
925
pending_msg_events : Vec :: new ( ) ,
914
926
} ) ,
915
927
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
928
+ outbound_pending_payments : Mutex :: new ( HashSet :: new ( ) ) ,
916
929
917
930
our_network_key : keys_manager. get_node_secret ( ) ,
918
931
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( ) ) ,
@@ -1467,7 +1480,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1467
1480
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 > {
1468
1481
log_trace ! ( self . logger, "Attempting to send payment for path with next hop {}" , path. first( ) . unwrap( ) . short_channel_id) ;
1469
1482
let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
1470
- let session_priv = SecretKey :: from_slice ( & self . keys_manager . get_secure_random_bytes ( ) [ ..] ) . expect ( "RNG is busted" ) ;
1483
+ let session_priv_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
1484
+ let session_priv = SecretKey :: from_slice ( & session_priv_bytes[ ..] ) . expect ( "RNG is busted" ) ;
1471
1485
1472
1486
let onion_keys = onion_utils:: construct_onion_keys ( & self . secp_ctx , & path, & session_priv)
1473
1487
. map_err ( |_| APIError :: RouteError { err : "Pubkey along hop was maliciously selected" } ) ?;
@@ -1478,6 +1492,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1478
1492
let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, prng_seed, payment_hash) ;
1479
1493
1480
1494
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
1495
+ assert ! ( self . outbound_pending_payments. lock( ) . unwrap( ) . insert( session_priv_bytes) ) ;
1481
1496
1482
1497
let err: Result < ( ) , _ > = loop {
1483
1498
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
@@ -2228,17 +2243,23 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2228
2243
self . fail_htlc_backwards_internal ( channel_state,
2229
2244
htlc_src, & payment_hash, HTLCFailReason :: Reason { failure_code, data : onion_failure_data} ) ;
2230
2245
} ,
2231
- HTLCSource :: OutboundRoute { .. } => {
2232
- self . pending_events . lock ( ) . unwrap ( ) . push (
2233
- events:: Event :: PaymentFailed {
2234
- payment_hash,
2235
- rejected_by_dest : false ,
2246
+ HTLCSource :: OutboundRoute { session_priv, .. } => {
2247
+ if {
2248
+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2249
+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2250
+ self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2251
+ } {
2252
+ self . pending_events . lock ( ) . unwrap ( ) . push (
2253
+ events:: Event :: PaymentFailed {
2254
+ payment_hash,
2255
+ rejected_by_dest : false ,
2236
2256
#[ cfg( test) ]
2237
- error_code : None ,
2257
+ error_code : None ,
2238
2258
#[ cfg( test) ]
2239
- error_data : None ,
2240
- }
2241
- )
2259
+ error_data : None ,
2260
+ }
2261
+ )
2262
+ }
2242
2263
} ,
2243
2264
} ;
2244
2265
}
@@ -2260,7 +2281,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2260
2281
// from block_connected which may run during initialization prior to the chain_monitor
2261
2282
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
2262
2283
match source {
2263
- HTLCSource :: OutboundRoute { ref path, .. } => {
2284
+ HTLCSource :: OutboundRoute { ref path, session_priv, .. } => {
2285
+ if {
2286
+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2287
+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2288
+ !self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2289
+ } {
2290
+ return ;
2291
+ }
2264
2292
log_trace ! ( self . logger, "Failing outbound payment HTLC with payment_hash {}" , log_bytes!( payment_hash. 0 ) ) ;
2265
2293
mem:: drop ( channel_state_lock) ;
2266
2294
match & onion_error {
@@ -2489,12 +2517,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2489
2517
2490
2518
fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < Signer > > , source : HTLCSource , payment_preimage : PaymentPreimage ) {
2491
2519
match source {
2492
- HTLCSource :: OutboundRoute { .. } => {
2520
+ HTLCSource :: OutboundRoute { session_priv , .. } => {
2493
2521
mem:: drop ( channel_state_lock) ;
2494
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
2495
- pending_events. push ( events:: Event :: PaymentSent {
2496
- payment_preimage
2497
- } ) ;
2522
+ if {
2523
+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2524
+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2525
+ self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2526
+ } {
2527
+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
2528
+ pending_events. push ( events:: Event :: PaymentSent {
2529
+ payment_preimage
2530
+ } ) ;
2531
+ }
2498
2532
} ,
2499
2533
HTLCSource :: PreviousHopData ( hop_data) => {
2500
2534
let prev_outpoint = hop_data. outpoint ;
@@ -4470,6 +4504,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
4470
4504
pending_payment. write ( writer) ?;
4471
4505
}
4472
4506
4507
+ let outbound_pending_payments = self . outbound_pending_payments . lock ( ) . unwrap ( ) ;
4508
+ ( outbound_pending_payments. len ( ) as u64 ) . write ( writer) ?;
4509
+ for session_priv in outbound_pending_payments. iter ( ) {
4510
+ session_priv. write ( writer) ?;
4511
+ }
4512
+
4473
4513
Ok ( ( ) )
4474
4514
}
4475
4515
}
@@ -4709,6 +4749,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4709
4749
}
4710
4750
}
4711
4751
4752
+ let outbound_pending_payments_count: u64 = Readable :: read ( reader) ?;
4753
+ let mut outbound_pending_payments: HashSet < [ u8 ; 32 ] > = HashSet :: with_capacity ( cmp:: min ( outbound_pending_payments_count as usize , MAX_ALLOC_SIZE /32 ) ) ;
4754
+ for _ in 0 ..outbound_pending_payments_count {
4755
+ if !outbound_pending_payments. insert ( Readable :: read ( reader) ?) {
4756
+ return Err ( DecodeError :: InvalidValue ) ;
4757
+ }
4758
+ }
4759
+
4712
4760
let mut secp_ctx = Secp256k1 :: new ( ) ;
4713
4761
secp_ctx. seeded_randomize ( & args. keys_manager . get_secure_random_bytes ( ) ) ;
4714
4762
@@ -4728,6 +4776,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4728
4776
pending_msg_events : Vec :: new ( ) ,
4729
4777
} ) ,
4730
4778
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
4779
+ outbound_pending_payments : Mutex :: new ( outbound_pending_payments) ,
4731
4780
4732
4781
our_network_key : args. keys_manager . get_node_secret ( ) ,
4733
4782
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & args. keys_manager . get_node_secret ( ) ) ,
0 commit comments