@@ -493,6 +493,18 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
493
493
is_connected : bool ,
494
494
}
495
495
496
+ impl < Signer : ChannelSigner > PeerState < Signer > {
497
+ /// Indicates that a peer meets the criteria where we're ok to remove it from our storage.
498
+ /// If true is passed for `require_disconnected`, the function will return false if we haven't
499
+ /// disconnected from the node already, ie. `PeerState::is_connected` is set to `true`.
500
+ fn ok_to_remove ( & self , require_disconnected : bool ) -> bool {
501
+ if require_disconnected && self . is_connected {
502
+ return false
503
+ }
504
+ self . channel_by_id . len ( ) == 0
505
+ }
506
+ }
507
+
496
508
/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
497
509
/// actually ours and not some duplicate HTLC sent to us by a node along the route.
498
510
///
@@ -1749,12 +1761,6 @@ where
1749
1761
. collect ( )
1750
1762
}
1751
1763
1752
- // Indicates that a peer meets the criteria where we're ok to disconnect the peer and remove it
1753
- // from our storage
1754
- fn ok_to_disconnected ( & self , peer : & PeerState < <SP :: Target as SignerProvider >:: Signer > ) -> bool {
1755
- peer. channel_by_id . len ( ) == 0
1756
- }
1757
-
1758
1764
/// Helper function that issues the channel close events
1759
1765
fn issue_channel_close_events ( & self , channel : & Channel < <SP :: Target as SignerProvider >:: Signer > , closure_reason : ClosureReason ) {
1760
1766
let mut pending_events_lock = self . pending_events . lock ( ) . unwrap ( ) ;
@@ -3547,8 +3553,7 @@ where
3547
3553
3548
3554
true
3549
3555
} ) ;
3550
- let peer_should_be_removed = !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0 ;
3551
- if peer_should_be_removed {
3556
+ if peer_state. ok_to_remove ( true ) {
3552
3557
pending_peers_awaiting_removal. push ( counterparty_node_id) ;
3553
3558
}
3554
3559
}
@@ -3570,7 +3575,7 @@ where
3570
3575
// have no channels to the peer.
3571
3576
let remove_entry = {
3572
3577
let peer_state = entry. get ( ) . lock ( ) . unwrap ( ) ;
3573
- ! peer_state. is_connected && peer_state . channel_by_id . len ( ) == 0
3578
+ peer_state. ok_to_remove ( true )
3574
3579
} ;
3575
3580
if remove_entry {
3576
3581
entry. remove_entry ( ) ;
@@ -6328,9 +6333,8 @@ where
6328
6333
fn peer_disconnected ( & self , counterparty_node_id : & PublicKey , no_connection_possible : bool ) {
6329
6334
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( & self . total_consistency_lock , & self . persistence_notifier ) ;
6330
6335
let mut failed_channels = Vec :: new ( ) ;
6331
- let mut no_channels_remain = true ;
6332
6336
let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
6333
- {
6337
+ let remove_peer = {
6334
6338
log_debug ! ( self . logger, "Marking channels with {} disconnected and generating channel_updates. We believe we {} make future connections to this peer." ,
6335
6339
log_pubkey!( counterparty_node_id) , if no_connection_possible { "cannot" } else { "can" } ) ;
6336
6340
if let Some ( peer_state_mutex) = per_peer_state. get ( counterparty_node_id) {
@@ -6343,8 +6347,6 @@ where
6343
6347
update_maps_on_chan_removal ! ( self , chan) ;
6344
6348
self . issue_channel_close_events ( chan, ClosureReason :: DisconnectedPeer ) ;
6345
6349
return false ;
6346
- } else {
6347
- no_channels_remain = false ;
6348
6350
}
6349
6351
true
6350
6352
} ) ;
@@ -6374,9 +6376,10 @@ where
6374
6376
} ) ;
6375
6377
debug_assert ! ( peer_state. is_connected, "A disconnected peer cannot disconnect" ) ;
6376
6378
peer_state. is_connected = false ;
6377
- }
6378
- }
6379
- if no_channels_remain {
6379
+ peer_state. ok_to_remove ( true )
6380
+ } else { true }
6381
+ } ;
6382
+ if remove_peer {
6380
6383
per_peer_state. remove ( counterparty_node_id) ;
6381
6384
}
6382
6385
mem:: drop ( per_peer_state) ;
@@ -6978,7 +6981,7 @@ where
6978
6981
for ( _, peer_state_mutex) in per_peer_state. iter ( ) {
6979
6982
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
6980
6983
let peer_state = & mut * peer_state_lock;
6981
- if !self . ok_to_disconnected ( peer_state ) {
6984
+ if !peer_state . ok_to_remove ( false ) {
6982
6985
peers_to_searialize_count += 1 ;
6983
6986
}
6984
6987
number_of_channels += peer_state. channel_by_id . len ( ) ;
@@ -7035,7 +7038,7 @@ where
7035
7038
for ( peer_pubkey, peer_state_mutex) in per_peer_state. iter ( ) {
7036
7039
let peer_state_lock= peer_state_mutex. lock ( ) . unwrap ( ) ;
7037
7040
let peer_state = & * peer_state_lock;
7038
- if !self . ok_to_disconnected ( peer_state ) {
7041
+ if !peer_state . ok_to_remove ( false ) {
7039
7042
peer_pubkey. write ( writer) ?;
7040
7043
peer_state. latest_features . write ( writer) ?;
7041
7044
}
0 commit comments