@@ -401,16 +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
- /// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
405
- ///
406
- /// Note that because we may have an SCID Alias as the key we can have two entries per channel,
407
- /// though in practice we probably won't be receiving HTLCs for a channel both via the alias
408
- /// and via the classic SCID.
409
- ///
410
- /// Note that while this is held in the same mutex as the channels themselves, no consistency
411
- /// guarantees are made about the existence of a channel with the short id here, nor the short
412
- /// ids in the PendingHTLCInfo!
413
- pub ( super ) forward_htlcs : HashMap < u64 , Vec < HTLCForwardInfo > > ,
414
404
/// Map from payment hash to the payment data and any HTLCs which are to us and can be
415
405
/// failed/claimed by the user.
416
406
///
@@ -722,6 +712,19 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
722
712
/// Locked *after* channel_state.
723
713
pending_outbound_payments : Mutex < HashMap < PaymentId , PendingOutboundPayment > > ,
724
714
715
+ /// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
716
+ ///
717
+ /// Note that because we may have an SCID Alias as the key we can have two entries per channel,
718
+ /// though in practice we probably won't be receiving HTLCs for a channel both via the alias
719
+ /// and via the classic SCID.
720
+ ///
721
+ /// Note that no consistency guarantees are made about the existence of a channel with the
722
+ /// `short_channel_id` here, nor the `short_channel_id` in the `PendingHTLCInfo`!
723
+ #[ cfg( test) ]
724
+ pub ( super ) forward_htlcs : Mutex < HashMap < u64 , Vec < HTLCForwardInfo > > > ,
725
+ #[ cfg( not( test) ) ]
726
+ forward_htlcs : Mutex < HashMap < u64 , Vec < HTLCForwardInfo > > > ,
727
+
725
728
/// The set of outbound SCID aliases across all our channels, including unconfirmed channels
726
729
/// and some closed channels which reached a usable state prior to being closed. This is used
727
730
/// only to avoid duplicates, and is not persisted explicitly to disk, but rebuilt from the
@@ -1595,13 +1598,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1595
1598
channel_state : Mutex :: new ( ChannelHolder {
1596
1599
by_id : HashMap :: new ( ) ,
1597
1600
short_to_chan_info : HashMap :: new ( ) ,
1598
- forward_htlcs : HashMap :: new ( ) ,
1599
1601
claimable_htlcs : HashMap :: new ( ) ,
1600
1602
pending_msg_events : Vec :: new ( ) ,
1601
1603
} ) ,
1602
1604
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1603
1605
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1604
1606
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1607
+ forward_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
1605
1608
id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
1606
1609
1607
1610
our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
@@ -3005,7 +3008,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3005
3008
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
3006
3009
let channel_state = & mut * channel_state_lock;
3007
3010
3008
- for ( short_chan_id, mut pending_forwards) in channel_state. forward_htlcs . drain ( ) {
3011
+ let mut forward_htlcs = HashMap :: new ( ) ;
3012
+ mem:: swap ( & mut forward_htlcs, & mut self . forward_htlcs . lock ( ) . unwrap ( ) ) ;
3013
+
3014
+ for ( short_chan_id, mut pending_forwards) in forward_htlcs {
3009
3015
if short_chan_id != 0 {
3010
3016
let forward_chan_id = match channel_state. short_to_chan_info . get ( & short_chan_id) {
3011
3017
Some ( ( _cp_id, chan_id) ) => chan_id. clone ( ) ,
@@ -3904,17 +3910,19 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
3904
3910
} ;
3905
3911
3906
3912
let mut forward_event = None ;
3907
- if channel_state_lock. forward_htlcs . is_empty ( ) {
3913
+ let mut forward_htlcs = self . forward_htlcs . lock ( ) . unwrap ( ) ;
3914
+ if forward_htlcs. is_empty ( ) {
3908
3915
forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) ) ;
3909
3916
}
3910
- match channel_state_lock . forward_htlcs . entry ( short_channel_id) {
3917
+ match forward_htlcs. entry ( short_channel_id) {
3911
3918
hash_map:: Entry :: Occupied ( mut entry) => {
3912
3919
entry. get_mut ( ) . push ( HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } ) ;
3913
3920
} ,
3914
3921
hash_map:: Entry :: Vacant ( entry) => {
3915
3922
entry. insert ( vec ! ( HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } ) ) ;
3916
3923
}
3917
3924
}
3925
+ mem:: drop ( forward_htlcs) ;
3918
3926
mem:: drop ( channel_state_lock) ;
3919
3927
let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
3920
3928
if let Some ( time) = forward_event {
@@ -4862,11 +4870,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4862
4870
let mut forward_event = None ;
4863
4871
if !pending_forwards. is_empty ( ) {
4864
4872
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
4865
- if channel_state. forward_htlcs . is_empty ( ) {
4873
+ let mut forward_htlcs = self . forward_htlcs . lock ( ) . unwrap ( ) ;
4874
+ if forward_htlcs. is_empty ( ) {
4866
4875
forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) )
4867
4876
}
4868
4877
for ( forward_info, prev_htlc_id) in pending_forwards. drain ( ..) {
4869
- match channel_state . forward_htlcs . entry ( match forward_info. routing {
4878
+ match forward_htlcs. entry ( match forward_info. routing {
4870
4879
PendingHTLCRouting :: Forward { short_channel_id, .. } => short_channel_id,
4871
4880
PendingHTLCRouting :: Receive { .. } => 0 ,
4872
4881
PendingHTLCRouting :: ReceiveKeysend { .. } => 0 ,
@@ -6552,8 +6561,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
6552
6561
}
6553
6562
}
6554
6563
6555
- ( channel_state. forward_htlcs . len ( ) as u64 ) . write ( writer) ?;
6556
- for ( short_channel_id, pending_forwards) in channel_state. forward_htlcs . iter ( ) {
6564
+ let forward_htlcs = self . forward_htlcs . lock ( ) . unwrap ( ) ;
6565
+ ( forward_htlcs. len ( ) as u64 ) . write ( writer) ?;
6566
+ for ( short_channel_id, pending_forwards) in forward_htlcs. iter ( ) {
6557
6567
short_channel_id. write ( writer) ?;
6558
6568
( pending_forwards. len ( ) as u64 ) . write ( writer) ?;
6559
6569
for forward in pending_forwards {
@@ -7165,14 +7175,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7165
7175
channel_state : Mutex :: new ( ChannelHolder {
7166
7176
by_id,
7167
7177
short_to_chan_info,
7168
- forward_htlcs,
7169
7178
claimable_htlcs,
7170
7179
pending_msg_events : Vec :: new ( ) ,
7171
7180
} ) ,
7172
7181
inbound_payment_key : expanded_inbound_key,
7173
7182
pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
7174
7183
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7175
7184
7185
+ forward_htlcs : Mutex :: new ( forward_htlcs) ,
7176
7186
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7177
7187
id_to_peer : Mutex :: new ( id_to_peer) ,
7178
7188
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
0 commit comments