@@ -442,6 +442,17 @@ 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, for channels which have been force-closed, a ChannelMonitor may not be
451
+ /// re-serialized after generating a claim event (even if this ChannelManager is), resulting in
452
+ /// us "claiming" a payment twice.
453
+ /// Locked *after* channel_state.
454
+ outbound_pending_payments : Mutex < HashSet < [ u8 ; 32 ] > > ,
455
+
445
456
our_network_key : SecretKey ,
446
457
our_network_pubkey : PublicKey ,
447
458
@@ -895,6 +906,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
895
906
pending_msg_events : Vec :: new ( ) ,
896
907
} ) ,
897
908
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
909
+ outbound_pending_payments : Mutex :: new ( HashSet :: new ( ) ) ,
898
910
899
911
our_network_key : keys_manager. get_node_secret ( ) ,
900
912
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( ) ) ,
@@ -1449,7 +1461,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1449
1461
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 > {
1450
1462
log_trace ! ( self . logger, "Attempting to send payment for path with next hop {}" , path. first( ) . unwrap( ) . short_channel_id) ;
1451
1463
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" ) ;
1464
+ let session_priv_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
1465
+ let session_priv = SecretKey :: from_slice ( & session_priv_bytes[ ..] ) . expect ( "RNG is busted" ) ;
1453
1466
1454
1467
let onion_keys = onion_utils:: construct_onion_keys ( & self . secp_ctx , & path, & session_priv)
1455
1468
. map_err ( |_| APIError :: RouteError { err : "Pubkey along hop was maliciously selected" } ) ?;
@@ -1460,6 +1473,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1460
1473
let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, prng_seed, payment_hash) ;
1461
1474
1462
1475
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
1476
+ assert ! ( self . outbound_pending_payments. lock( ) . unwrap( ) . insert( session_priv_bytes) ) ;
1463
1477
1464
1478
let err: Result < ( ) , _ > = loop {
1465
1479
let mut channel_lock = self . channel_state . lock ( ) . unwrap ( ) ;
@@ -2188,17 +2202,23 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2188
2202
self . fail_htlc_backwards_internal ( channel_state,
2189
2203
htlc_src, & payment_hash, HTLCFailReason :: Reason { failure_code, data : onion_failure_data} ) ;
2190
2204
} ,
2191
- HTLCSource :: OutboundRoute { .. } => {
2192
- self . pending_events . lock ( ) . unwrap ( ) . push (
2193
- events:: Event :: PaymentFailed {
2194
- payment_hash,
2195
- rejected_by_dest : false ,
2205
+ HTLCSource :: OutboundRoute { session_priv, .. } => {
2206
+ if {
2207
+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2208
+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2209
+ self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2210
+ } {
2211
+ self . pending_events . lock ( ) . unwrap ( ) . push (
2212
+ events:: Event :: PaymentFailed {
2213
+ payment_hash,
2214
+ rejected_by_dest : false ,
2196
2215
#[ cfg( test) ]
2197
- error_code : None ,
2216
+ error_code : None ,
2198
2217
#[ cfg( test) ]
2199
- error_data : None ,
2200
- }
2201
- )
2218
+ error_data : None ,
2219
+ }
2220
+ )
2221
+ }
2202
2222
} ,
2203
2223
} ;
2204
2224
}
@@ -2220,7 +2240,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2220
2240
// from block_connected which may run during initialization prior to the chain_monitor
2221
2241
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
2222
2242
match source {
2223
- HTLCSource :: OutboundRoute { ref path, .. } => {
2243
+ HTLCSource :: OutboundRoute { ref path, session_priv, .. } => {
2244
+ if {
2245
+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2246
+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2247
+ !self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2248
+ } {
2249
+ return ;
2250
+ }
2224
2251
log_trace ! ( self . logger, "Failing outbound payment HTLC with payment_hash {}" , log_bytes!( payment_hash. 0 ) ) ;
2225
2252
mem:: drop ( channel_state_lock) ;
2226
2253
match & onion_error {
@@ -2449,12 +2476,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2449
2476
2450
2477
fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < Signer > > , source : HTLCSource , payment_preimage : PaymentPreimage ) {
2451
2478
match source {
2452
- HTLCSource :: OutboundRoute { .. } => {
2479
+ HTLCSource :: OutboundRoute { session_priv , .. } => {
2453
2480
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
- } ) ;
2481
+ if {
2482
+ let mut session_priv_bytes = [ 0 ; 32 ] ;
2483
+ session_priv_bytes. copy_from_slice ( & session_priv[ ..] ) ;
2484
+ self . outbound_pending_payments . lock ( ) . unwrap ( ) . remove ( & session_priv_bytes)
2485
+ } {
2486
+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
2487
+ pending_events. push ( events:: Event :: PaymentSent {
2488
+ payment_preimage
2489
+ } ) ;
2490
+ }
2458
2491
} ,
2459
2492
HTLCSource :: PreviousHopData ( hop_data) => {
2460
2493
let prev_outpoint = hop_data. outpoint ;
@@ -4423,6 +4456,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
4423
4456
pending_payment. write ( writer) ?;
4424
4457
}
4425
4458
4459
+ let outbound_pending_payments = self . outbound_pending_payments . lock ( ) . unwrap ( ) ;
4460
+ ( outbound_pending_payments. len ( ) as u64 ) . write ( writer) ?;
4461
+ for session_priv in outbound_pending_payments. iter ( ) {
4462
+ session_priv. write ( writer) ?;
4463
+ }
4464
+
4426
4465
Ok ( ( ) )
4427
4466
}
4428
4467
}
@@ -4662,6 +4701,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4662
4701
}
4663
4702
}
4664
4703
4704
+ let outbound_pending_payments_count: u64 = Readable :: read ( reader) ?;
4705
+ let mut outbound_pending_payments: HashSet < [ u8 ; 32 ] > = HashSet :: with_capacity ( cmp:: min ( outbound_pending_payments_count as usize , MAX_ALLOC_SIZE /32 ) ) ;
4706
+ for _ in 0 ..outbound_pending_payments_count {
4707
+ if !outbound_pending_payments. insert ( Readable :: read ( reader) ?) {
4708
+ return Err ( DecodeError :: InvalidValue ) ;
4709
+ }
4710
+ }
4711
+
4665
4712
let mut secp_ctx = Secp256k1 :: new ( ) ;
4666
4713
secp_ctx. seeded_randomize ( & args. keys_manager . get_secure_random_bytes ( ) ) ;
4667
4714
@@ -4681,6 +4728,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4681
4728
pending_msg_events : Vec :: new ( ) ,
4682
4729
} ) ,
4683
4730
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
4731
+ outbound_pending_payments : Mutex :: new ( outbound_pending_payments) ,
4684
4732
4685
4733
our_network_key : args. keys_manager . get_node_secret ( ) ,
4686
4734
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & args. keys_manager . get_node_secret ( ) ) ,
0 commit comments