@@ -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<M: Deref, T: Deref, K: Deref, F: Deref, L: 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
@@ -1645,13 +1649,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
1645
1649
channel_state : Mutex :: new ( ChannelHolder {
1646
1650
by_id : HashMap :: new ( ) ,
1647
1651
short_to_chan_info : HashMap :: new ( ) ,
1648
- claimable_htlcs : HashMap :: new ( ) ,
1649
1652
pending_msg_events : Vec :: new ( ) ,
1650
1653
} ) ,
1651
1654
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1652
1655
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1653
1656
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1654
1657
forward_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1658
+ claimable_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1655
1659
id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
1656
1660
1657
1661
our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
@@ -3065,9 +3069,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3065
3069
mem:: swap ( & mut forward_htlcs, & mut self . forward_htlcs . lock ( ) . unwrap ( ) ) ;
3066
3070
3067
3071
for ( short_chan_id, mut pending_forwards) in forward_htlcs {
3068
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3069
- let channel_state = & mut * channel_state_lock;
3070
3072
if short_chan_id != 0 {
3073
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3074
+ let channel_state = & mut * channel_state_lock;
3071
3075
let forward_chan_id = match channel_state. short_to_chan_info . get ( & short_chan_id) {
3072
3076
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
3073
3077
None => {
@@ -3348,7 +3352,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3348
3352
payment_secret: $payment_data. payment_secret,
3349
3353
}
3350
3354
} ;
3351
- let ( _, htlcs) = channel_state. claimable_htlcs. entry( payment_hash)
3355
+ let mut claimable_htlcs = self . claimable_htlcs. lock( ) . unwrap( ) ;
3356
+ let ( _, htlcs) = claimable_htlcs. entry( payment_hash)
3352
3357
. or_insert_with( || ( purpose( ) , Vec :: new( ) ) ) ;
3353
3358
if htlcs. len( ) == 1 {
3354
3359
if let OnionPayload :: Spontaneous ( _) = htlcs[ 0 ] . onion_payload {
@@ -3416,7 +3421,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3416
3421
check_total_value ! ( payment_data, payment_preimage) ;
3417
3422
} ,
3418
3423
OnionPayload :: Spontaneous ( preimage) => {
3419
- match channel_state . claimable_htlcs . entry ( payment_hash) {
3424
+ match self . claimable_htlcs . lock ( ) . unwrap ( ) . entry ( payment_hash) {
3420
3425
hash_map:: Entry :: Vacant ( e) => {
3421
3426
let purpose = events:: PaymentPurpose :: SpontaneousPayment ( preimage) ;
3422
3427
e. insert ( ( purpose. clone ( ) , vec ! [ claimable_htlc] ) ) ;
@@ -3669,7 +3674,9 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3669
3674
true
3670
3675
} ) ;
3671
3676
3672
- channel_state. claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3677
+ mem:: drop ( channel_state_lock) ;
3678
+
3679
+ self . claimable_htlcs . lock ( ) . unwrap ( ) . retain ( |payment_hash, ( _, htlcs) | {
3673
3680
if htlcs. is_empty ( ) {
3674
3681
// This should be unreachable
3675
3682
debug_assert ! ( false ) ;
@@ -3720,10 +3727,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3720
3727
pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) {
3721
3728
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3722
3729
3723
- let removed_source = {
3724
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3725
- channel_state. claimable_htlcs . remove ( payment_hash)
3726
- } ;
3730
+ let removed_source = self . claimable_htlcs . lock ( ) . unwrap ( ) . remove ( payment_hash) ;
3727
3731
if let Some ( ( _, mut sources) ) = removed_source {
3728
3732
for htlc in sources. drain ( ..) {
3729
3733
let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
@@ -4025,7 +4029,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4025
4029
4026
4030
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
4027
4031
4028
- let removed_source = self . channel_state . lock ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
4032
+ let removed_source = self . claimable_htlcs . lock ( ) . unwrap ( ) . remove ( & payment_hash) ;
4029
4033
if let Some ( ( payment_purpose, mut sources) ) = removed_source {
4030
4034
assert ! ( !sources. is_empty( ) ) ;
4031
4035
@@ -5908,28 +5912,28 @@ where
5908
5912
}
5909
5913
true
5910
5914
} ) ;
5915
+ }
5911
5916
5912
- if let Some ( height) = height_opt {
5913
- channel_state. claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
5914
- htlcs. retain ( |htlc| {
5915
- // If height is approaching the number of blocks we think it takes us to get
5916
- // our commitment transaction confirmed before the HTLC expires, plus the
5917
- // number of blocks we generally consider it to take to do a commitment update,
5918
- // just give up on it and fail the HTLC.
5919
- if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
5920
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
5921
- htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
5922
-
5923
- timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
5924
- failure_code : 0x4000 | 15 ,
5925
- data : htlc_msat_height_data
5926
- } , HTLCDestination :: FailedPayment { payment_hash : payment_hash. clone ( ) } ) ) ;
5927
- false
5928
- } else { true }
5929
- } ) ;
5930
- !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
5917
+ if let Some ( height) = height_opt {
5918
+ self . claimable_htlcs . lock ( ) . unwrap ( ) . retain ( |payment_hash, ( _, htlcs) | {
5919
+ htlcs. retain ( |htlc| {
5920
+ // If height is approaching the number of blocks we think it takes us to get
5921
+ // our commitment transaction confirmed before the HTLC expires, plus the
5922
+ // number of blocks we generally consider it to take to do a commitment update,
5923
+ // just give up on it and fail the HTLC.
5924
+ if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
5925
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
5926
+ htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
5927
+
5928
+ timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
5929
+ failure_code : 0x4000 | 15 ,
5930
+ data : htlc_msat_height_data
5931
+ } , HTLCDestination :: FailedPayment { payment_hash : payment_hash. clone ( ) } ) ) ;
5932
+ false
5933
+ } else { true }
5931
5934
} ) ;
5932
- }
5935
+ !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
5936
+ } ) ;
5933
5937
}
5934
5938
5935
5939
self . handle_init_event_channel_failures ( failed_channels) ;
@@ -6664,16 +6668,18 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable for ChannelMana
6664
6668
}
6665
6669
}
6666
6670
6667
- let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
6668
- let mut htlc_purposes: Vec < & events:: PaymentPurpose > = Vec :: new ( ) ;
6669
- ( channel_state. claimable_htlcs . len ( ) as u64 ) . write ( writer) ?;
6670
- for ( payment_hash, ( purpose, previous_hops) ) in channel_state. claimable_htlcs . iter ( ) {
6671
- payment_hash. write ( writer) ?;
6672
- ( previous_hops. len ( ) as u64 ) . write ( writer) ?;
6673
- for htlc in previous_hops. iter ( ) {
6674
- htlc. write ( writer) ?;
6671
+ let mut htlc_purposes: Vec < events:: PaymentPurpose > = Vec :: new ( ) ;
6672
+ {
6673
+ let claimable_htlcs = self . claimable_htlcs . lock ( ) . unwrap ( ) ;
6674
+ ( claimable_htlcs. len ( ) as u64 ) . write ( writer) ?;
6675
+ for ( payment_hash, ( purpose, previous_hops) ) in claimable_htlcs. iter ( ) {
6676
+ payment_hash. write ( writer) ?;
6677
+ ( previous_hops. len ( ) as u64 ) . write ( writer) ?;
6678
+ for htlc in previous_hops. iter ( ) {
6679
+ htlc. write ( writer) ?;
6680
+ }
6681
+ htlc_purposes. push ( purpose. clone ( ) ) ;
6675
6682
}
6676
- htlc_purposes. push ( purpose) ;
6677
6683
}
6678
6684
6679
6685
let per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -7279,14 +7285,14 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7279
7285
channel_state : Mutex :: new ( ChannelHolder {
7280
7286
by_id,
7281
7287
short_to_chan_info,
7282
- claimable_htlcs,
7283
7288
pending_msg_events : Vec :: new ( ) ,
7284
7289
} ) ,
7285
7290
inbound_payment_key : expanded_inbound_key,
7286
7291
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
7287
7292
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7288
7293
7289
7294
forward_htlcs : Mutex :: new ( forward_htlcs) ,
7295
+ claimable_htlcs : Mutex :: new ( claimable_htlcs) ,
7290
7296
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7291
7297
id_to_peer : Mutex :: new ( id_to_peer) ,
7292
7298
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
0 commit comments