@@ -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
@@ -895,6 +907,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
895
907
pending_msg_events : Vec :: new ( ) ,
896
908
} ) ,
897
909
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
910
+ outbound_pending_payments : Mutex :: new ( HashSet :: new ( ) ) ,
898
911
899
912
our_network_key : keys_manager. get_node_secret ( ) ,
900
913
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
1449
1462
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
1463
log_trace ! ( self . logger, "Attempting to send payment for path with next hop {}" , path. first( ) . unwrap( ) . short_channel_id) ;
1451
1464
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" ) ;
1453
1467
1454
1468
let onion_keys = onion_utils:: construct_onion_keys ( & self . secp_ctx , & path, & session_priv)
1455
1469
. 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
1460
1474
let onion_packet = onion_utils:: construct_onion_packet ( onion_payloads, onion_keys, prng_seed, payment_hash) ;
1461
1475
1462
1476
let _persistence_guard = PersistenceNotifierGuard :: new ( & self . total_consistency_lock , & self . persistence_notifier ) ;
1477
+ assert ! ( self . outbound_pending_payments. lock( ) . unwrap( ) . insert( session_priv_bytes) ) ;
1463
1478
1464
1479
let err: Result < ( ) , _ > = loop {
1465
1480
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
2188
2203
self . fail_htlc_backwards_internal ( channel_state,
2189
2204
htlc_src, & payment_hash, HTLCFailReason :: Reason { failure_code, data : onion_failure_data} ) ;
2190
2205
} ,
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 ,
2196
2216
#[ cfg( test) ]
2197
- error_code : None ,
2217
+ error_code : None ,
2198
2218
#[ cfg( test) ]
2199
- error_data : None ,
2200
- }
2201
- )
2219
+ error_data : None ,
2220
+ }
2221
+ )
2222
+ }
2202
2223
} ,
2203
2224
} ;
2204
2225
}
@@ -2220,7 +2241,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2220
2241
// from block_connected which may run during initialization prior to the chain_monitor
2221
2242
// being fully configured. See the docs for `ChannelManagerReadArgs` for more.
2222
2243
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
+ }
2224
2252
log_trace ! ( self . logger, "Failing outbound payment HTLC with payment_hash {}" , log_bytes!( payment_hash. 0 ) ) ;
2225
2253
mem:: drop ( channel_state_lock) ;
2226
2254
match & onion_error {
@@ -2449,12 +2477,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2449
2477
2450
2478
fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder < Signer > > , source : HTLCSource , payment_preimage : PaymentPreimage ) {
2451
2479
match source {
2452
- HTLCSource :: OutboundRoute { .. } => {
2480
+ HTLCSource :: OutboundRoute { session_priv , .. } => {
2453
2481
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
+ }
2458
2492
} ,
2459
2493
HTLCSource :: PreviousHopData ( hop_data) => {
2460
2494
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
4423
4457
pending_payment. write ( writer) ?;
4424
4458
}
4425
4459
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
+
4426
4466
Ok ( ( ) )
4427
4467
}
4428
4468
}
@@ -4662,6 +4702,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4662
4702
}
4663
4703
}
4664
4704
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
+
4665
4713
let mut secp_ctx = Secp256k1 :: new ( ) ;
4666
4714
secp_ctx. seeded_randomize ( & args. keys_manager . get_secure_random_bytes ( ) ) ;
4667
4715
@@ -4681,6 +4729,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
4681
4729
pending_msg_events : Vec :: new ( ) ,
4682
4730
} ) ,
4683
4731
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
4732
+ outbound_pending_payments : Mutex :: new ( outbound_pending_payments) ,
4684
4733
4685
4734
our_network_key : args. keys_manager . get_node_secret ( ) ,
4686
4735
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & args. keys_manager . get_node_secret ( ) ) ,
0 commit comments