@@ -620,8 +620,6 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> = C
620
620
// | |
621
621
// | |__`best_block`
622
622
// | |
623
- // | |__`pending_peers_awaiting_removal`
624
- // | |
625
623
// | |__`pending_events`
626
624
// | |
627
625
// | |__`pending_background_events`
@@ -789,16 +787,6 @@ where
789
787
790
788
/// See `ChannelManager` struct-level documentation for lock order requirements.
791
789
pending_events : Mutex < Vec < events:: Event > > ,
792
- /// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
793
- /// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
794
- /// to that peer is later closed while still being disconnected (i.e. force closed), we
795
- /// therefore need to remove the peer from `peer_state` separately.
796
- /// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
797
- /// instead store such peers awaiting removal in this field, and remove them on a timer to
798
- /// limit the negative effects on parallelism as much as possible.
799
- ///
800
- /// See `ChannelManager` struct-level documentation for lock order requirements.
801
- pending_peers_awaiting_removal : Mutex < HashSet < PublicKey > > ,
802
790
/// See `ChannelManager` struct-level documentation for lock order requirements.
803
791
pending_background_events : Mutex < Vec < BackgroundEvent > > ,
804
792
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
@@ -1329,11 +1317,10 @@ macro_rules! try_chan_entry {
1329
1317
}
1330
1318
1331
1319
macro_rules! remove_channel {
1332
- ( $self: expr, $entry: expr, $peer_state : expr ) => {
1320
+ ( $self: expr, $entry: expr) => {
1333
1321
{
1334
1322
let channel = $entry. remove_entry( ) . 1 ;
1335
1323
update_maps_on_chan_removal!( $self, channel) ;
1336
- $self. add_pending_peer_to_be_removed( channel. get_counterparty_node_id( ) , $peer_state) ;
1337
1324
channel
1338
1325
}
1339
1326
}
@@ -1506,7 +1493,6 @@ where
1506
1493
per_peer_state : FairRwLock :: new ( HashMap :: new ( ) ) ,
1507
1494
1508
1495
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
1509
- pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
1510
1496
pending_background_events : Mutex :: new ( Vec :: new ( ) ) ,
1511
1497
total_consistency_lock : RwLock :: new ( ( ) ) ,
1512
1498
persistence_notifier : Notifier :: new ( ) ,
@@ -1745,7 +1731,7 @@ where
1745
1731
let ( result, is_permanent) =
1746
1732
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
1747
1733
if is_permanent {
1748
- remove_channel ! ( self , chan_entry, peer_state ) ;
1734
+ remove_channel ! ( self , chan_entry) ;
1749
1735
break result;
1750
1736
}
1751
1737
}
@@ -1756,7 +1742,7 @@ where
1756
1742
} ) ;
1757
1743
1758
1744
if chan_entry. get ( ) . is_shutdown ( ) {
1759
- let channel = remove_channel ! ( self , chan_entry, peer_state ) ;
1745
+ let channel = remove_channel ! ( self , chan_entry) ;
1760
1746
if let Ok ( channel_update) = self . get_channel_update_for_broadcast ( & channel) {
1761
1747
peer_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
1762
1748
msg : channel_update
@@ -1859,7 +1845,7 @@ where
1859
1845
} else {
1860
1846
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: HolderForceClosed ) ;
1861
1847
}
1862
- remove_channel ! ( self , chan, peer_state )
1848
+ remove_channel ! ( self , chan)
1863
1849
} else {
1864
1850
return Err ( APIError :: ChannelUnavailable { err : format ! ( "Channel with id {} not found for the passed counterparty node_id {}" , log_bytes!( * channel_id) , peer_node_id) } ) ;
1865
1851
}
@@ -1898,13 +1884,6 @@ where
1898
1884
}
1899
1885
}
1900
1886
1901
- fn add_pending_peer_to_be_removed ( & self , counterparty_node_id : PublicKey , peer_state : & mut PeerState < <SP :: Target as SignerProvider >:: Signer > ) {
1902
- let peer_should_be_removed = !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0 ;
1903
- if peer_should_be_removed {
1904
- self . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) . insert ( counterparty_node_id) ;
1905
- }
1906
- }
1907
-
1908
1887
/// Force closes a channel, immediately broadcasting the latest local transaction(s) and
1909
1888
/// rejecting new HTLCs on the given channel. Fails if `channel_id` is unknown to
1910
1889
/// the manager, or if the `counterparty_node_id` isn't the counterparty of the corresponding
@@ -3358,16 +3337,20 @@ where
3358
3337
true
3359
3338
}
3360
3339
3361
- /// Removes peers which have been been added to `pending_peers_awaiting_removal` which are
3362
- /// still disconnected and we have no channels to.
3340
+ /// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3341
+ /// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
3342
+ /// to that peer is later closed while still being disconnected (i.e. force closed), we
3343
+ /// therefore need to remove the peer from `peer_state` separately.
3344
+ /// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
3345
+ /// instead remove such peers awaiting removal through this function, which is called on a
3346
+ /// timer through `timer_tick_occurred`, passing the peers disconnected peers with no channels,
3347
+ /// to limit the negative effects on parallelism as much as possible.
3363
3348
///
3364
3349
/// Must be called without the `per_peer_state` lock acquired.
3365
- fn remove_peers_awaiting_removal ( & self ) {
3366
- let mut pending_peers_awaiting_removal = HashSet :: new ( ) ;
3367
- mem:: swap ( & mut * self . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) , & mut pending_peers_awaiting_removal) ;
3350
+ fn remove_peers_awaiting_removal ( & self , pending_peers_awaiting_removal : HashSet < PublicKey > ) {
3368
3351
if pending_peers_awaiting_removal. len ( ) > 0 {
3369
3352
let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3370
- for counterparty_node_id in pending_peers_awaiting_removal. drain ( ) {
3353
+ for counterparty_node_id in pending_peers_awaiting_removal {
3371
3354
match per_peer_state. entry ( counterparty_node_id) {
3372
3355
hash_map:: Entry :: Occupied ( entry) => {
3373
3356
// Remove the entry if the peer is still disconnected and we still
@@ -3446,6 +3429,7 @@ where
3446
3429
/// the channel.
3447
3430
/// * Expiring a channel's previous `ChannelConfig` if necessary to only allow forwarding HTLCs
3448
3431
/// with the current `ChannelConfig`.
3432
+ /// * Removing peers which have disconnected but and no longer have any channels.
3449
3433
///
3450
3434
/// Note that this may cause reentrancy through `chain::Watch::update_channel` calls or feerate
3451
3435
/// estimate fetches.
@@ -3458,6 +3442,7 @@ where
3458
3442
3459
3443
let mut handle_errors: Vec < ( Result < ( ) , _ > , _ ) > = Vec :: new ( ) ;
3460
3444
let mut timed_out_mpp_htlcs = Vec :: new ( ) ;
3445
+ let mut pending_peers_awaiting_removal = HashSet :: new ( ) ;
3461
3446
{
3462
3447
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
3463
3448
for ( counterparty_node_id, peer_state_mutex) in per_peer_state. iter ( ) {
@@ -3505,10 +3490,13 @@ where
3505
3490
3506
3491
true
3507
3492
} ) ;
3508
- self . add_pending_peer_to_be_removed ( counterparty_node_id, peer_state) ;
3493
+ let peer_should_be_removed = !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0 ;
3494
+ if peer_should_be_removed {
3495
+ pending_peers_awaiting_removal. insert ( counterparty_node_id) ;
3496
+ }
3509
3497
}
3510
3498
}
3511
- self . remove_peers_awaiting_removal ( ) ;
3499
+ self . remove_peers_awaiting_removal ( pending_peers_awaiting_removal ) ;
3512
3500
3513
3501
self . claimable_payments . lock ( ) . unwrap ( ) . claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3514
3502
if htlcs. is_empty ( ) {
@@ -4263,7 +4251,7 @@ where
4263
4251
}
4264
4252
} ;
4265
4253
peer_state. pending_msg_events . push ( send_msg_err_event) ;
4266
- let _ = remove_channel ! ( self , channel, peer_state ) ;
4254
+ let _ = remove_channel ! ( self , channel) ;
4267
4255
return Err ( APIError :: APIMisuseError { err : "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations." . to_owned ( ) } ) ;
4268
4256
}
4269
4257
@@ -4549,7 +4537,7 @@ where
4549
4537
let ( result, is_permanent) =
4550
4538
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
4551
4539
if is_permanent {
4552
- remove_channel ! ( self , chan_entry, peer_state ) ;
4540
+ remove_channel ! ( self , chan_entry) ;
4553
4541
break result;
4554
4542
}
4555
4543
}
@@ -4598,7 +4586,7 @@ where
4598
4586
// also implies there are no pending HTLCs left on the channel, so we can
4599
4587
// fully delete it from tracking (the channel monitor is still around to
4600
4588
// watch for old state broadcasts)!
4601
- ( tx, Some ( remove_channel ! ( self , chan_entry, peer_state ) ) )
4589
+ ( tx, Some ( remove_channel ! ( self , chan_entry) ) )
4602
4590
} else { ( tx, None ) }
4603
4591
} ,
4604
4592
hash_map:: Entry :: Vacant ( _) => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( format ! ( "Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}" , counterparty_node_id) , msg. channel_id ) )
@@ -5101,11 +5089,12 @@ where
5101
5089
if let Some ( peer_state_mutex) = per_peer_state. get ( & counterparty_node_id) {
5102
5090
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5103
5091
let peer_state = & mut * peer_state_lock;
5092
+ let pending_msg_events = & mut peer_state. pending_msg_events ;
5104
5093
if let hash_map:: Entry :: Occupied ( chan_entry) = peer_state. channel_by_id . entry ( funding_outpoint. to_channel_id ( ) ) {
5105
- let mut chan = remove_channel ! ( self , chan_entry, peer_state ) ;
5094
+ let mut chan = remove_channel ! ( self , chan_entry) ;
5106
5095
failed_channels. push ( chan. force_shutdown ( false ) ) ;
5107
5096
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5108
- peer_state . pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5097
+ pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5109
5098
msg : update
5110
5099
} ) ;
5111
5100
}
@@ -5115,7 +5104,7 @@ where
5115
5104
ClosureReason :: CommitmentTxConfirmed
5116
5105
} ;
5117
5106
self . issue_channel_close_events ( & chan, reason) ;
5118
- peer_state . pending_msg_events . push ( events:: MessageSendEvent :: HandleError {
5107
+ pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
5119
5108
node_id : chan. get_counterparty_node_id ( ) ,
5120
5109
action : msgs:: ErrorAction :: SendErrorMessage {
5121
5110
msg : msgs:: ErrorMessage { channel_id : chan. channel_id ( ) , data : "Channel force-closed" . to_owned ( ) }
@@ -5157,7 +5146,7 @@ where
5157
5146
{
5158
5147
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5159
5148
5160
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5149
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5161
5150
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5162
5151
let peer_state = & mut * peer_state_lock;
5163
5152
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5197,7 +5186,6 @@ where
5197
5186
}
5198
5187
}
5199
5188
} ) ;
5200
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5201
5189
}
5202
5190
}
5203
5191
@@ -5222,7 +5210,7 @@ where
5222
5210
{
5223
5211
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5224
5212
5225
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5213
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5226
5214
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5227
5215
let peer_state = & mut * peer_state_lock;
5228
5216
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5260,7 +5248,6 @@ where
5260
5248
}
5261
5249
}
5262
5250
} ) ;
5263
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5264
5251
}
5265
5252
}
5266
5253
@@ -5836,7 +5823,7 @@ where
5836
5823
let mut timed_out_htlcs = Vec :: new ( ) ;
5837
5824
{
5838
5825
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5839
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5826
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5840
5827
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5841
5828
let peer_state = & mut * peer_state_lock;
5842
5829
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5920,7 +5907,6 @@ where
5920
5907
}
5921
5908
true
5922
5909
} ) ;
5923
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5924
5910
}
5925
5911
}
5926
5912
@@ -6248,7 +6234,7 @@ where
6248
6234
6249
6235
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6250
6236
6251
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6237
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6252
6238
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
6253
6239
let peer_state = & mut * peer_state_lock;
6254
6240
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -6280,7 +6266,6 @@ where
6280
6266
}
6281
6267
retain
6282
6268
} ) ;
6283
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
6284
6269
}
6285
6270
//TODO: Also re-broadcast announcement_signatures
6286
6271
Ok ( ( ) )
@@ -6794,8 +6779,6 @@ where
6794
6779
6795
6780
write_ver_prefix ! ( writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
6796
6781
6797
- self . remove_peers_awaiting_removal ( ) ;
6798
-
6799
6782
self . genesis_hash . write ( writer) ?;
6800
6783
{
6801
6784
let best_block = self . best_block . read ( ) . unwrap ( ) ;
@@ -7620,7 +7603,6 @@ where
7620
7603
per_peer_state : FairRwLock :: new ( per_peer_state) ,
7621
7604
7622
7605
pending_events : Mutex :: new ( pending_events_read) ,
7623
- pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
7624
7606
pending_background_events : Mutex :: new ( pending_background_events_read) ,
7625
7607
total_consistency_lock : RwLock :: new ( ( ) ) ,
7626
7608
persistence_notifier : Notifier :: new ( ) ,
@@ -8105,9 +8087,6 @@ mod tests {
8105
8087
// Assert that nodes[1] is awaiting removal for nodes[0] once nodes[1] has been
8106
8088
// disconnected and the channel between has been force closed.
8107
8089
let nodes_0_per_peer_state = nodes[ 0 ] . node . per_peer_state . read ( ) . unwrap ( ) ;
8108
- let nodes_0_pending_peers_awaiting_removal = nodes[ 0 ] . node . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) ;
8109
- assert_eq ! ( nodes_0_pending_peers_awaiting_removal. len( ) , 1 ) ;
8110
- assert ! ( nodes_0_pending_peers_awaiting_removal. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
8111
8090
// Assert that nodes[1] isn't removed before `timer_tick_occurred` has been executed.
8112
8091
assert_eq ! ( nodes_0_per_peer_state. len( ) , 1 ) ;
8113
8092
assert ! ( nodes_0_per_peer_state. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
@@ -8118,7 +8097,6 @@ mod tests {
8118
8097
{
8119
8098
// Assert that nodes[1] has now been removed.
8120
8099
assert_eq ! ( nodes[ 0 ] . node. per_peer_state. read( ) . unwrap( ) . len( ) , 0 ) ;
8121
- assert_eq ! ( nodes[ 0 ] . node. pending_peers_awaiting_removal. lock( ) . unwrap( ) . len( ) , 0 ) ;
8122
8100
}
8123
8101
}
8124
8102
0 commit comments