@@ -395,13 +395,6 @@ pub(super) enum RAACommitmentOrder {
395
395
// Note this is only exposed in cfg(test):
396
396
pub ( super ) struct ChannelHolder < Signer : Sign > {
397
397
pub ( super ) by_id : HashMap < [ u8 ; 32 ] , Channel < Signer > > ,
398
- /// Map from payment hash to the payment data and any HTLCs which are to us and can be
399
- /// failed/claimed by the user.
400
- ///
401
- /// Note that while this is held in the same mutex as the channels themselves, no consistency
402
- /// guarantees are made about the channels given here actually existing anymore by the time you
403
- /// go to read them!
404
- claimable_htlcs : HashMap < PaymentHash , ( events:: PaymentPurpose , Vec < ClaimableHTLC > ) > ,
405
398
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
406
399
/// for broadcast messages, where ordering isn't as strict).
407
400
pub ( super ) pending_msg_events : Vec < MessageSendEvent > ,
@@ -682,6 +675,8 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, M, T, F, L> = ChannelManage
682
675
// | |
683
676
// | |__`pending_inbound_payments`
684
677
// | |
678
+ // | |__`claimable_htlcs`
679
+ // | |
685
680
// | |__`pending_outbound_payments`
686
681
// | |
687
682
// | |__`best_block`
@@ -753,6 +748,15 @@ pub struct ChannelManager<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
753
748
#[ cfg( not( test) ) ]
754
749
forward_htlcs : Mutex < HashMap < u64 , Vec < HTLCForwardInfo > > > ,
755
750
751
+ /// Map from payment hash to the payment data and any HTLCs which are to us and can be
752
+ /// failed/claimed by the user.
753
+ ///
754
+ /// Note that, no consistency guarantees are made about the channels given here actually
755
+ /// existing anymore by the time you go to read them!
756
+ ///
757
+ /// See `ChannelManager` struct-level documentation for lock order requirements.
758
+ claimable_htlcs : Mutex < HashMap < PaymentHash , ( events:: PaymentPurpose , Vec < ClaimableHTLC > ) > > ,
759
+
756
760
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
757
761
/// and some closed channels which reached a usable state prior to being closed. This is used
758
762
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1657,13 +1661,13 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
1657
1661
1658
1662
channel_state : Mutex :: new ( ChannelHolder {
1659
1663
by_id : HashMap :: new ( ) ,
1660
- claimable_htlcs : HashMap :: new ( ) ,
1661
1664
pending_msg_events : Vec :: new ( ) ,
1662
1665
} ) ,
1663
1666
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1664
1667
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1665
1668
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1666
1669
forward_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1670
+ claimable_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1667
1671
id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
1668
1672
short_to_chan_info : FairRwLock :: new ( HashMap :: new ( ) ) ,
1669
1673
@@ -3142,8 +3146,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3142
3146
mem:: swap ( & mut forward_htlcs, & mut self . forward_htlcs . lock ( ) . unwrap ( ) ) ;
3143
3147
3144
3148
for ( short_chan_id, mut pending_forwards) in forward_htlcs {
3145
- let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3146
- let channel_state = & mut * channel_state_lock;
3147
3149
if short_chan_id != 0 {
3148
3150
macro_rules! forwarding_channel_not_found {
3149
3151
( ) => {
@@ -3242,6 +3244,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3242
3244
continue ;
3243
3245
}
3244
3246
} ;
3247
+ let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3248
+ let channel_state = & mut * channel_state_lock;
3245
3249
match channel_state. by_id . entry ( forward_chan_id) {
3246
3250
hash_map:: Entry :: Vacant ( _) => {
3247
3251
forwarding_channel_not_found ! ( ) ;
@@ -3434,7 +3438,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3434
3438
payment_secret: $payment_data. payment_secret,
3435
3439
}
3436
3440
} ;
3437
- let ( _, htlcs) = channel_state. claimable_htlcs. entry( payment_hash)
3441
+ let mut claimable_htlcs = self . claimable_htlcs. lock( ) . unwrap( ) ;
3442
+ let ( _, htlcs) = claimable_htlcs. entry( payment_hash)
3438
3443
. or_insert_with( || ( purpose( ) , Vec :: new( ) ) ) ;
3439
3444
if htlcs. len( ) == 1 {
3440
3445
if let OnionPayload :: Spontaneous ( _) = htlcs[ 0 ] . onion_payload {
@@ -3502,7 +3507,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3502
3507
check_total_value ! ( payment_data, payment_preimage) ;
3503
3508
} ,
3504
3509
OnionPayload :: Spontaneous ( preimage) => {
3505
- match channel_state . claimable_htlcs . entry ( payment_hash) {
3510
+ match self . claimable_htlcs . lock ( ) . unwrap ( ) . entry ( payment_hash) {
3506
3511
hash_map:: Entry :: Vacant ( e) => {
3507
3512
let purpose = events:: PaymentPurpose :: SpontaneousPayment ( preimage) ;
3508
3513
e. insert ( ( purpose. clone ( ) , vec ! [ claimable_htlc] ) ) ;
@@ -3791,29 +3796,29 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3791
3796
3792
3797
true
3793
3798
} ) ;
3799
+ }
3794
3800
3795
- channel_state. claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3796
- if htlcs. is_empty ( ) {
3797
- // This should be unreachable
3798
- debug_assert ! ( false ) ;
3801
+ self . claimable_htlcs . lock ( ) . unwrap ( ) . retain ( |payment_hash, ( _, htlcs) | {
3802
+ if htlcs. is_empty ( ) {
3803
+ // This should be unreachable
3804
+ debug_assert ! ( false ) ;
3805
+ return false ;
3806
+ }
3807
+ if let OnionPayload :: Invoice { .. } = htlcs[ 0 ] . onion_payload {
3808
+ // Check if we've received all the parts we need for an MPP (the value of the parts adds to total_msat).
3809
+ // In this case we're not going to handle any timeouts of the parts here.
3810
+ if htlcs[ 0 ] . total_msat == htlcs. iter ( ) . fold ( 0 , |total, htlc| total + htlc. value ) {
3811
+ return true ;
3812
+ } else if htlcs. into_iter ( ) . any ( |htlc| {
3813
+ htlc. timer_ticks += 1 ;
3814
+ return htlc. timer_ticks >= MPP_TIMEOUT_TICKS
3815
+ } ) {
3816
+ timed_out_mpp_htlcs. extend ( htlcs. into_iter ( ) . map ( |htlc| ( htlc. prev_hop . clone ( ) , payment_hash. clone ( ) ) ) ) ;
3799
3817
return false ;
3800
3818
}
3801
- if let OnionPayload :: Invoice { .. } = htlcs[ 0 ] . onion_payload {
3802
- // Check if we've received all the parts we need for an MPP (the value of the parts adds to total_msat).
3803
- // In this case we're not going to handle any timeouts of the parts here.
3804
- if htlcs[ 0 ] . total_msat == htlcs. iter ( ) . fold ( 0 , |total, htlc| total + htlc. value ) {
3805
- return true ;
3806
- } else if htlcs. into_iter ( ) . any ( |htlc| {
3807
- htlc. timer_ticks += 1 ;
3808
- return htlc. timer_ticks >= MPP_TIMEOUT_TICKS
3809
- } ) {
3810
- timed_out_mpp_htlcs. extend ( htlcs. into_iter ( ) . map ( |htlc| ( htlc. prev_hop . clone ( ) , payment_hash. clone ( ) ) ) ) ;
3811
- return false ;
3812
- }
3813
- }
3814
- true
3815
- } ) ;
3816
- }
3819
+ }
3820
+ true
3821
+ } ) ;
3817
3822
3818
3823
for htlc_source in timed_out_mpp_htlcs. drain ( ..) {
3819
3824
let receiver = HTLCDestination :: FailedPayment { payment_hash : htlc_source. 1 } ;
@@ -3846,10 +3851,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
3846
3851
pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) {
3847
3852
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
3848
3853
3849
- let removed_source = {
3850
- let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
3851
- channel_state. claimable_htlcs . remove ( payment_hash)
3852
- } ;
3854
+ let removed_source = self . claimable_htlcs . lock ( ) . unwrap ( ) . remove ( payment_hash) ;
3853
3855
if let Some ( ( _, mut sources) ) = removed_source {
3854
3856
for htlc in sources. drain ( ..) {
3855
3857
let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
@@ -4151,7 +4153,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
4151
4153
4152
4154
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
4153
4155
4154
- let removed_source = self . channel_state . lock ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
4156
+ let removed_source = self . claimable_htlcs . lock ( ) . unwrap ( ) . remove ( & payment_hash) ;
4155
4157
if let Some ( ( payment_purpose, mut sources) ) = removed_source {
4156
4158
assert ! ( !sources. is_empty( ) ) ;
4157
4159
@@ -6019,28 +6021,28 @@ where
6019
6021
}
6020
6022
true
6021
6023
} ) ;
6024
+ }
6022
6025
6023
- if let Some ( height) = height_opt {
6024
- channel_state. claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
6025
- htlcs. retain ( |htlc| {
6026
- // If height is approaching the number of blocks we think it takes us to get
6027
- // our commitment transaction confirmed before the HTLC expires, plus the
6028
- // number of blocks we generally consider it to take to do a commitment update,
6029
- // just give up on it and fail the HTLC.
6030
- if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
6031
- let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
6032
- htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
6033
-
6034
- timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
6035
- failure_code : 0x4000 | 15 ,
6036
- data : htlc_msat_height_data
6037
- } , HTLCDestination :: FailedPayment { payment_hash : payment_hash. clone ( ) } ) ) ;
6038
- false
6039
- } else { true }
6040
- } ) ;
6041
- !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
6026
+ if let Some ( height) = height_opt {
6027
+ self . claimable_htlcs . lock ( ) . unwrap ( ) . retain ( |payment_hash, ( _, htlcs) | {
6028
+ htlcs. retain ( |htlc| {
6029
+ // If height is approaching the number of blocks we think it takes us to get
6030
+ // our commitment transaction confirmed before the HTLC expires, plus the
6031
+ // number of blocks we generally consider it to take to do a commitment update,
6032
+ // just give up on it and fail the HTLC.
6033
+ if height >= htlc. cltv_expiry - HTLC_FAIL_BACK_BUFFER {
6034
+ let mut htlc_msat_height_data = byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) ;
6035
+ htlc_msat_height_data. extend_from_slice ( & byte_utils:: be32_to_array ( height) ) ;
6036
+
6037
+ timed_out_htlcs. push ( ( HTLCSource :: PreviousHopData ( htlc. prev_hop . clone ( ) ) , payment_hash. clone ( ) , HTLCFailReason :: Reason {
6038
+ failure_code : 0x4000 | 15 ,
6039
+ data : htlc_msat_height_data
6040
+ } , HTLCDestination :: FailedPayment { payment_hash : payment_hash. clone ( ) } ) ) ;
6041
+ false
6042
+ } else { true }
6042
6043
} ) ;
6043
- }
6044
+ !htlcs. is_empty ( ) // Only retain this entry if htlcs has at least one entry.
6045
+ } ) ;
6044
6046
}
6045
6047
6046
6048
self . handle_init_event_channel_failures ( failed_channels) ;
@@ -6775,16 +6777,18 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable for ChannelMana
6775
6777
}
6776
6778
}
6777
6779
6778
- let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
6779
- let mut htlc_purposes: Vec < & events:: PaymentPurpose > = Vec :: new ( ) ;
6780
- ( channel_state. claimable_htlcs . len ( ) as u64 ) . write ( writer) ?;
6781
- for ( payment_hash, ( purpose, previous_hops) ) in channel_state. claimable_htlcs . iter ( ) {
6782
- payment_hash. write ( writer) ?;
6783
- ( previous_hops. len ( ) as u64 ) . write ( writer) ?;
6784
- for htlc in previous_hops. iter ( ) {
6785
- htlc. write ( writer) ?;
6780
+ let mut htlc_purposes: Vec < events:: PaymentPurpose > = Vec :: new ( ) ;
6781
+ {
6782
+ let claimable_htlcs = self . claimable_htlcs . lock ( ) . unwrap ( ) ;
6783
+ ( claimable_htlcs. len ( ) as u64 ) . write ( writer) ?;
6784
+ for ( payment_hash, ( purpose, previous_hops) ) in claimable_htlcs. iter ( ) {
6785
+ payment_hash. write ( writer) ?;
6786
+ ( previous_hops. len ( ) as u64 ) . write ( writer) ?;
6787
+ for htlc in previous_hops. iter ( ) {
6788
+ htlc. write ( writer) ?;
6789
+ }
6790
+ htlc_purposes. push ( purpose. clone ( ) ) ;
6786
6791
}
6787
- htlc_purposes. push ( purpose) ;
6788
6792
}
6789
6793
6790
6794
let per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
@@ -7389,14 +7393,14 @@ impl<'a, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7389
7393
7390
7394
channel_state : Mutex :: new ( ChannelHolder {
7391
7395
by_id,
7392
- claimable_htlcs,
7393
7396
pending_msg_events : Vec :: new ( ) ,
7394
7397
} ) ,
7395
7398
inbound_payment_key : expanded_inbound_key,
7396
7399
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
7397
7400
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7398
7401
7399
7402
forward_htlcs : Mutex :: new ( forward_htlcs) ,
7403
+ claimable_htlcs : Mutex :: new ( claimable_htlcs) ,
7400
7404
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7401
7405
id_to_peer : Mutex :: new ( id_to_peer) ,
7402
7406
short_to_chan_info : FairRwLock :: new ( short_to_chan_info) ,
0 commit comments