@@ -590,7 +590,7 @@ impl DefaultResourceManager {
590590 max_accepted_htlcs,
591591 self . config . general_allocation_pct ,
592592 self . config . congestion_allocation_pct ,
593- self . config . revenue_window * self . config . reputation_multiplier as u32 ,
593+ self . config . revenue_window * self . config . reputation_multiplier . into ( ) ,
594594 self . config . reputation_multiplier ,
595595 revenue_window_weeks_avg as u8 ,
596596 timestamp_unix_secs,
@@ -607,10 +607,30 @@ impl DefaultResourceManager {
607607 /// This should be called when a channel is closing.
608608 pub fn remove_channel ( & self , channel_id : u64 ) -> Result < ( ) , ( ) > {
609609 let mut channels_lock = self . channels . lock ( ) . unwrap ( ) ;
610- channels_lock. remove ( & channel_id) ;
611610
612- // Remove slots assigned to channel being removed across all other channels.
611+ // Release bucket resources on each incoming channel for its pending HTLCs.
612+ if let Some ( removed_channel) = channels_lock. remove ( & channel_id) {
613+ for ( htlc_ref, pending_htlc) in & removed_channel. pending_htlcs {
614+ if let Some ( incoming_channel) = channels_lock. get_mut ( & htlc_ref. incoming_channel_id )
615+ {
616+ let _ = match pending_htlc. bucket {
617+ BucketAssigned :: General => incoming_channel
618+ . general_bucket
619+ . remove_htlc ( channel_id, pending_htlc. incoming_amount_msat ) ,
620+ BucketAssigned :: Congestion => incoming_channel
621+ . congestion_bucket
622+ . remove_htlc ( pending_htlc. incoming_amount_msat ) ,
623+ BucketAssigned :: Protected => incoming_channel
624+ . protected_bucket
625+ . remove_htlc ( pending_htlc. incoming_amount_msat ) ,
626+ } ;
627+ }
628+ }
629+ }
630+
631+ // Clean up pending HTLC entries and channel slots.
613632 for ( _, channel) in channels_lock. iter_mut ( ) {
633+ channel. pending_htlcs . retain ( |htlc_ref, _| htlc_ref. incoming_channel_id != channel_id) ;
614634 channel. general_bucket . remove_channel_slots ( channel_id) ;
615635 }
616636 Ok ( ( ) )
@@ -752,7 +772,7 @@ impl DefaultResourceManager {
752772 let outgoing_channel = channels_lock. get_mut ( & outgoing_channel_id) . ok_or ( ( ) ) ?;
753773
754774 let htlc_ref = HtlcRef { incoming_channel_id, htlc_id } ;
755- let pending_htlc = outgoing_channel. pending_htlcs . remove ( & htlc_ref) . ok_or ( ( ) ) ?;
775+ let pending_htlc = outgoing_channel. pending_htlcs . get ( & htlc_ref) . ok_or ( ( ) ) ?. clone ( ) ;
756776
757777 if resolved_at < pending_htlc. added_at_unix_seconds {
758778 return Err ( ( ) ) ;
@@ -765,6 +785,7 @@ impl DefaultResourceManager {
765785 settled,
766786 ) ;
767787 outgoing_channel. outgoing_reputation . add_value ( effective_fee, resolved_at) ?;
788+ outgoing_channel. pending_htlcs . remove ( & htlc_ref) . ok_or ( ( ) ) ?;
768789
769790 let incoming_channel = channels_lock. get_mut ( & incoming_channel_id) . ok_or ( ( ) ) ?;
770791 match pending_htlc. bucket {
@@ -1114,7 +1135,7 @@ mod tests {
11141135 fn test_channel ( config : & ResourceManagerConfig ) -> Channel {
11151136 Channel :: new (
11161137 0 ,
1117- 100_000 ,
1138+ 100_000_000 ,
11181139 100 ,
11191140 config. general_allocation_pct ,
11201141 config. congestion_allocation_pct ,
@@ -1718,7 +1739,7 @@ mod tests {
17181739 TestCase {
17191740 hold_time: slow_resolve,
17201741 settled: true ,
1721- expected_reputation: 0 , // effective_fee = 0 (slow unaccountable)
1742+ expected_reputation: 0 , // effective_fee = 0 (slow unaccountable)
17221743 expected_revenue: FEE_AMOUNT as i64 , // revenue increases regardless of speed
17231744 } ,
17241745 TestCase {
0 commit comments