@@ -401,13 +401,6 @@ pub(super) struct ChannelHolder<Signer: Sign> {
401
401
/// SCIDs being added once the funding transaction is confirmed at the channel's required
402
402
/// confirmation depth.
403
403
pub ( super ) short_to_chan_info : HashMap < u64 , ( PublicKey , [ u8 ; 32 ] ) > ,
404
- /// Map from payment hash to the payment data and any HTLCs which are to us and can be
405
- /// failed/claimed by the user.
406
- ///
407
- /// Note that while this is held in the same mutex as the channels themselves, no consistency
408
- /// guarantees are made about the channels given here actually existing anymore by the time you
409
- /// go to read them!
410
- claimable_htlcs : HashMap < PaymentHash , ( events:: PaymentPurpose , Vec < ClaimableHTLC > ) > ,
411
404
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
412
405
/// for broadcast messages, where ordering isn't as strict).
413
406
pub ( super ) pending_msg_events : Vec < MessageSendEvent > ,
@@ -691,6 +684,8 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
691
684
// | |
692
685
// | |__`pending_inbound_payments`
693
686
// | |
687
+ // | |__`claimable_htlcs`
688
+ // | |
694
689
// | |__`pending_outbound_payments`
695
690
// | |
696
691
// | |__`best_block`
@@ -762,6 +757,15 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
762
757
#[ cfg( not( test) ) ]
763
758
forward_htlcs : Mutex < HashMap < u64 , Vec < HTLCForwardInfo > > > ,
764
759
760
+ /// Map from payment hash to the payment data and any HTLCs which are to us and can be
761
+ /// failed/claimed by the user.
762
+ ///
763
+ /// Note that, no consistency guarantees are made about the channels given here actually
764
+ /// existing anymore by the time you go to read them!
765
+ ///
766
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
767
+ claimable_htlcs : Mutex < HashMap < PaymentHash , ( events:: PaymentPurpose , Vec < ClaimableHTLC > ) > > ,
768
+
765
769
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
766
770
/// and some closed channels which reached a usable state prior to being closed. This is used
767
771
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1626,13 +1630,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1626
1630
channel_state : Mutex :: new ( ChannelHolder {
1627
1631
by_id : HashMap :: new ( ) ,
1628
1632
short_to_chan_info : HashMap :: new ( ) ,
1629
- claimable_htlcs : HashMap :: new ( ) ,
1630
1633
pending_msg_events : Vec :: new ( ) ,
1631
1634
} ) ,
1632
1635
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1633
1636
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1634
1637
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1635
1638
forward_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1639
+ claimable_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1636
1640
id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
1637
1641
1638
1642
our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
@@ -3046,9 +3050,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3046
3050
mem:: swap ( & mut forward_htlcs, & mut self . forward_htlcs . lock ( ) . unwrap ( ) ) ;
3047
3051
3048
3052
for ( short_chan_id, mut pending_forwards) in forward_htlcs {
3049
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3050
- let channel_state = & mut * channel_state_lock;
3051
3053
if short_chan_id != 0 {
3054
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3055
+ let channel_state = & mut * channel_state_lock;
3052
3056
let forward_chan_id = match channel_state. short_to_chan_info . get ( & short_chan_id) {
3053
3057
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
3054
3058
None => {
@@ -3329,7 +3333,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3329
3333
payment_secret: $payment_data. payment_secret,
3330
3334
}
3331
3335
} ;
3332
- let ( _, htlcs) = channel_state. claimable_htlcs. entry( payment_hash)
3336
+ let mut claimable_htlcs = self . claimable_htlcs. lock( ) . unwrap( ) ;
3337
+ let ( _, htlcs) = claimable_htlcs. entry( payment_hash)
3333
3338
. or_insert_with( || ( purpose( ) , Vec :: new( ) ) ) ;
3334
3339
if htlcs. len( ) == 1 {
3335
3340
if let OnionPayload :: Spontaneous ( _) = htlcs[ 0 ] . onion_payload {
@@ -3397,7 +3402,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3397
3402
check_total_value ! ( payment_data, payment_preimage) ;
3398
3403
} ,
3399
3404
OnionPayload :: Spontaneous ( preimage) => {
3400
- match channel_state . claimable_htlcs . entry ( payment_hash) {
3405
+ match self . claimable_htlcs . lock ( ) . unwrap ( ) . entry ( payment_hash) {
3401
3406
hash_map:: Entry :: Vacant ( e) => {
3402
3407
let purpose = events:: PaymentPurpose :: SpontaneousPayment ( preimage) ;
3403
3408
e. insert ( ( purpose. clone ( ) , vec ! [ claimable_htlc] ) ) ;
@@ -3650,7 +3655,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3650
3655
true
3651
3656
} ) ;
3652
3657
3653
- channel_state. claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3658
+ mem:: drop ( channel_state) ;
3659
+
3660
+ self . claimable_htlcs . lock ( ) . unwrap ( ) . retain ( |payment_hash, ( _, htlcs) | {
3654
3661
if htlcs. is_empty ( ) {
3655
3662
// This should be unreachable
3656
3663
debug_assert ! ( false ) ;
@@ -3701,10 +3708,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3701
3708
pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) {
3702
3709
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3703
3710
3704
- let removed_source = {
3705
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3706
- channel_state. claimable_htlcs . remove ( payment_hash)
3707
- } ;
3711
+ let removed_source = self . claimable_htlcs . lock ( ) . unwrap ( ) . remove ( payment_hash) ;
3708
3712
if let Some ( ( _, mut sources) ) = removed_source {
3709
3713
for htlc in sources. drain ( ..) {
3710
3714
let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
@@ -4006,7 +4010,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4006
4010
4007
4011
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
4008
4012
4009
- let removed_source = self . channel_state . lock ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
4013
+ let removed_source = self . claimable_htlcs . lock ( ) . unwrap ( ) . remove ( & payment_hash) ;
4010
4014
if let Some ( ( payment_purpose, mut sources) ) = removed_source {
4011
4015
assert ! ( !sources. is_empty( ) ) ;
4012
4016
@@ -5883,28 +5887,28 @@ where
5883
5887
}
5884
5888
true
5885
5889
} ) ;
5890
+ }
5886
5891
5887
- if let Some ( height) = height_opt {
5888
- channel_state. claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
5889
- htlcs. retain ( |htlc| {
5890
- // If height is approaching the number of blocks we think it takes us to get
5891
- // our commitment transaction confirmed before the HTLC expires, plus the
5892
- // number of blocks we generally consider it to take to do a commitment update,
5893
- // just give up on it and fail the HTLC.
5894
- if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
5895
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
5896
- htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
5897
-
5898
- timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
5899
- failure_code : 0x4000 | 15 ,
5900
- data : htlc_msat_height_data
5901
- } , HTLCDestination :: FailedPayment { payment_hash : payment_hash. clone ( ) } ) ) ;
5902
- false
5903
- } else { true }
5904
- } ) ;
5905
- !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
5892
+ if let Some ( height) = height_opt {
5893
+ self . claimable_htlcs . lock ( ) . unwrap ( ) . retain ( |payment_hash, ( _, htlcs) | {
5894
+ htlcs. retain ( |htlc| {
5895
+ // If height is approaching the number of blocks we think it takes us to get
5896
+ // our commitment transaction confirmed before the HTLC expires, plus the
5897
+ // number of blocks we generally consider it to take to do a commitment update,
5898
+ // just give up on it and fail the HTLC.
5899
+ if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
5900
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
5901
+ htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
5902
+
5903
+ timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
5904
+ failure_code : 0x4000 | 15 ,
5905
+ data : htlc_msat_height_data
5906
+ } , HTLCDestination :: FailedPayment { payment_hash : payment_hash. clone ( ) } ) ) ;
5907
+ false
5908
+ } else { true }
5906
5909
} ) ;
5907
- }
5910
+ !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
5911
+ } ) ;
5908
5912
}
5909
5913
5910
5914
self . handle_init_event_channel_failures ( failed_channels) ;
@@ -6639,16 +6643,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
6639
6643
}
6640
6644
}
6641
6645
6642
- let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
6643
- let mut htlc_purposes: Vec < & events:: PaymentPurpose > = Vec :: new ( ) ;
6644
- ( channel_state. claimable_htlcs . len ( ) as u64 ) . write ( writer) ?;
6645
- for ( payment_hash, ( purpose, previous_hops) ) in channel_state. claimable_htlcs . iter ( ) {
6646
- payment_hash. write ( writer) ?;
6647
- ( previous_hops. len ( ) as u64 ) . write ( writer) ?;
6648
- for htlc in previous_hops. iter ( ) {
6649
- htlc. write ( writer) ?;
6646
+ let mut htlc_purposes: Vec < events:: PaymentPurpose > = Vec :: new ( ) ;
6647
+ {
6648
+ let claimable_htlcs = self . claimable_htlcs . lock ( ) . unwrap ( ) ;
6649
+ ( claimable_htlcs. len ( ) as u64 ) . write ( writer) ?;
6650
+ for ( payment_hash, ( purpose, previous_hops) ) in claimable_htlcs. iter ( ) {
6651
+ payment_hash. write ( writer) ?;
6652
+ ( previous_hops. len ( ) as u64 ) . write ( writer) ?;
6653
+ for htlc in previous_hops. iter ( ) {
6654
+ htlc. write ( writer) ?;
6655
+ }
6656
+ htlc_purposes. push ( purpose. clone ( ) ) ;
6650
6657
}
6651
- htlc_purposes. push ( purpose) ;
6652
6658
}
6653
6659
6654
6660
let per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -7244,14 +7250,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7244
7250
channel_state : Mutex :: new ( ChannelHolder {
7245
7251
by_id,
7246
7252
short_to_chan_info,
7247
- claimable_htlcs,
7248
7253
pending_msg_events : Vec :: new ( ) ,
7249
7254
} ) ,
7250
7255
inbound_payment_key : expanded_inbound_key,
7251
7256
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
7252
7257
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7253
7258
7254
7259
forward_htlcs : Mutex :: new ( forward_htlcs) ,
7260
+ claimable_htlcs : Mutex :: new ( claimable_htlcs) ,
7255
7261
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7256
7262
id_to_peer : Mutex :: new ( id_to_peer) ,
7257
7263
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
0 commit comments