@@ -1616,14 +1616,13 @@ where
1616
1616
}
1617
1617
1618
1618
fn list_channels_with_filter < Fn : FnMut ( & ( & [ u8 ; 32 ] , & Channel < <SP :: Target as SignerProvider >:: Signer > ) ) -> bool + Copy > ( & self , f : Fn ) -> Vec < ChannelDetails > {
1619
- let mut res = Vec :: new ( ) ;
1620
1619
// Allocate our best estimate of the number of channels we have in the `res`
1621
1620
// Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
1622
1621
// a scid or a scid alias, and the `id_to_peer` shouldn't be used outside
1623
1622
// of the ChannelMonitor handling. Therefore reallocations may still occur, but is
1624
1623
// unlikely as the `short_to_chan_info` map often contains 2 entries for
1625
1624
// the same channel.
1626
- res. reserve ( self . short_to_chan_info . read ( ) . unwrap ( ) . len ( ) ) ;
1625
+ let mut res = Vec :: with_capacity ( self . short_to_chan_info . read ( ) . unwrap ( ) . len ( ) ) ;
1627
1626
{
1628
1627
let best_block_height = self . best_block . read ( ) . unwrap ( ) . height ( ) ;
1629
1628
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
@@ -3401,38 +3400,6 @@ where
3401
3400
true
3402
3401
}
3403
3402
3404
- /// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3405
- /// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
3406
- /// to that peer is later closed while still being disconnected (i.e. force closed), we
3407
- /// therefore need to remove the peer from `peer_state` separately.
3408
- /// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
3409
- /// instead remove such peers awaiting removal through this function, which is called on a
3410
- /// timer through `timer_tick_occurred`, passing the peers disconnected peers with no channels,
3411
- /// to limit the negative effects on parallelism as much as possible.
3412
- ///
3413
- /// Must be called without the `per_peer_state` lock acquired.
3414
- fn remove_peers_awaiting_removal ( & self , pending_peers_awaiting_removal : HashSet < PublicKey > ) {
3415
- if pending_peers_awaiting_removal. len ( ) > 0 {
3416
- let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3417
- for counterparty_node_id in pending_peers_awaiting_removal {
3418
- match per_peer_state. entry ( counterparty_node_id) {
3419
- hash_map:: Entry :: Occupied ( entry) => {
3420
- // Remove the entry if the peer is still disconnected and we still
3421
- // have no channels to the peer.
3422
- let remove_entry = {
3423
- let peer_state = entry. get ( ) . lock ( ) . unwrap ( ) ;
3424
- !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0
3425
- } ;
3426
- if remove_entry {
3427
- entry. remove_entry ( ) ;
3428
- }
3429
- } ,
3430
- hash_map:: Entry :: Vacant ( _) => { /* The PeerState has already been removed */ }
3431
- }
3432
- }
3433
- }
3434
- }
3435
-
3436
3403
#[ cfg( any( test, feature = "_test_utils" ) ) ]
3437
3404
/// Process background events, for functional testing
3438
3405
pub fn test_process_background_events ( & self ) {
@@ -3506,7 +3473,7 @@ where
3506
3473
3507
3474
let mut handle_errors: Vec < ( Result < ( ) , _ > , _ ) > = Vec :: new ( ) ;
3508
3475
let mut timed_out_mpp_htlcs = Vec :: new ( ) ;
3509
- let mut pending_peers_awaiting_removal = HashSet :: new ( ) ;
3476
+ let mut pending_peers_awaiting_removal = Vec :: new ( ) ;
3510
3477
{
3511
3478
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
3512
3479
for ( counterparty_node_id, peer_state_mutex) in per_peer_state. iter ( ) {
@@ -3556,11 +3523,37 @@ where
3556
3523
} ) ;
3557
3524
let peer_should_be_removed = !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0 ;
3558
3525
if peer_should_be_removed {
3559
- pending_peers_awaiting_removal. insert ( counterparty_node_id) ;
3526
+ pending_peers_awaiting_removal. push ( counterparty_node_id) ;
3527
+ }
3528
+ }
3529
+ }
3530
+
3531
+ // When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3532
+ // `per_peer_state` is not removed by the `peer_disconnected` function. If the channels
3533
+ // of to that peer is later closed while still being disconnected (i.e. force closed),
3534
+ // we therefore need to remove the peer from `peer_state` separately.
3535
+ // To avoid having to take the `per_peer_state` `write` lock once the channels are
3536
+ // closed, we instead remove such peers awaiting removal here on a timer, to limit the
3537
+ // negative effects on parallelism as much as possible.
3538
+ if pending_peers_awaiting_removal. len ( ) > 0 {
3539
+ let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3540
+ for counterparty_node_id in pending_peers_awaiting_removal {
3541
+ match per_peer_state. entry ( counterparty_node_id) {
3542
+ hash_map:: Entry :: Occupied ( entry) => {
3543
+ // Remove the entry if the peer is still disconnected and we still
3544
+ // have no channels to the peer.
3545
+ let remove_entry = {
3546
+ let peer_state = entry. get ( ) . lock ( ) . unwrap ( ) ;
3547
+ !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0
3548
+ } ;
3549
+ if remove_entry {
3550
+ entry. remove_entry ( ) ;
3551
+ }
3552
+ } ,
3553
+ hash_map:: Entry :: Vacant ( _) => { /* The PeerState has already been removed */ }
3560
3554
}
3561
3555
}
3562
3556
}
3563
- self . remove_peers_awaiting_removal ( pending_peers_awaiting_removal) ;
3564
3557
3565
3558
self . claimable_payments . lock ( ) . unwrap ( ) . claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3566
3559
if htlcs. is_empty ( ) {
0 commit comments