@@ -401,16 +401,6 @@ pub(super) struct ChannelHolder<Signer: Sign> {
401401 /// SCIDs being added once the funding transaction is confirmed at the channel's required
402402 /// confirmation depth.
403403 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 > > ,
414404 /// Map from payment hash to the payment data and any HTLCs which are to us and can be
415405 /// failed/claimed by the user.
416406 ///
@@ -722,6 +712,19 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
722712 /// Locked *after* channel_state.
723713 pending_outbound_payments : Mutex < HashMap < PaymentId , PendingOutboundPayment > > ,
724714
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+
725728 /// The set of outbound SCID aliases across all our channels, including unconfirmed channels
726729 /// and some closed channels which reached a usable state prior to being closed. This is used
727730 /// 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
15951598 channel_state : Mutex :: new ( ChannelHolder {
15961599 by_id : HashMap :: new ( ) ,
15971600 short_to_chan_info : HashMap :: new ( ) ,
1598- forward_htlcs : HashMap :: new ( ) ,
15991601 claimable_htlcs : HashMap :: new ( ) ,
16001602 pending_msg_events : Vec :: new ( ) ,
16011603 } ) ,
16021604 outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
16031605 pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
16041606 pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1607+ forward_htlcs : Mutex :: new ( HashMap :: new ( ) ) ,
16051608 id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
16061609
16071610 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
30053008 let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
30063009 let channel_state = & mut * channel_state_lock;
30073010
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 {
30093015 if short_chan_id != 0 {
30103016 let forward_chan_id = match channel_state. short_to_chan_info . get ( & short_chan_id) {
30113017 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
39043910 } ;
39053911
39063912 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 ( ) {
39083915 forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) ) ;
39093916 }
3910- match channel_state_lock . forward_htlcs . entry ( short_channel_id) {
3917+ match forward_htlcs. entry ( short_channel_id) {
39113918 hash_map:: Entry :: Occupied ( mut entry) => {
39123919 entry. get_mut ( ) . push ( HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } ) ;
39133920 } ,
39143921 hash_map:: Entry :: Vacant ( entry) => {
39153922 entry. insert ( vec ! ( HTLCForwardInfo :: FailHTLC { htlc_id, err_packet } ) ) ;
39163923 }
39173924 }
3925+ mem:: drop ( forward_htlcs) ;
39183926 mem:: drop ( channel_state_lock) ;
39193927 let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
39203928 if let Some ( time) = forward_event {
@@ -4862,11 +4870,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
48624870 let mut forward_event = None ;
48634871 if !pending_forwards. is_empty ( ) {
48644872 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 ( ) {
48664875 forward_event = Some ( Duration :: from_millis ( MIN_HTLC_RELAY_HOLDING_CELL_MILLIS ) )
48674876 }
48684877 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 {
48704879 PendingHTLCRouting :: Forward { short_channel_id, .. } => short_channel_id,
48714880 PendingHTLCRouting :: Receive { .. } => 0 ,
48724881 PendingHTLCRouting :: ReceiveKeysend { .. } => 0 ,
@@ -6552,8 +6561,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
65526561 }
65536562 }
65546563
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 ( ) {
65576567 short_channel_id. write ( writer) ?;
65586568 ( pending_forwards. len ( ) as u64 ) . write ( writer) ?;
65596569 for forward in pending_forwards {
@@ -7165,14 +7175,14 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
71657175 channel_state : Mutex :: new ( ChannelHolder {
71667176 by_id,
71677177 short_to_chan_info,
7168- forward_htlcs,
71697178 claimable_htlcs,
71707179 pending_msg_events : Vec :: new ( ) ,
71717180 } ) ,
71727181 inbound_payment_key : expanded_inbound_key,
71737182 pending_inbound_payments : Mutex :: new ( pending_inbound_payments) ,
71747183 pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
71757184
7185+ forward_htlcs : Mutex :: new ( forward_htlcs) ,
71767186 outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
71777187 id_to_peer : Mutex :: new ( id_to_peer) ,
71787188 fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
0 commit comments