@@ -595,8 +595,6 @@ pub type SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L> = C
595
595
// | |
596
596
// | |__`best_block`
597
597
// | |
598
- // | |__`pending_peers_awaiting_removal`
599
- // | |
600
598
// | |__`pending_events`
601
599
// | |
602
600
// | |__`pending_background_events`
@@ -764,16 +762,6 @@ where
764
762
765
763
/// See `ChannelManager` struct-level documentation for lock order requirements.
766
764
pending_events : Mutex < Vec < events:: Event > > ,
767
- /// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
768
- /// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
769
- /// to that peer is later closed while still being disconnected (i.e. force closed), we
770
- /// therefore need to remove the peer from `peer_state` separately.
771
- /// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
772
- /// instead store such peers awaiting removal in this field, and remove them on a timer to
773
- /// limit the negative effects on parallelism as much as possible.
774
- ///
775
- /// See `ChannelManager` struct-level documentation for lock order requirements.
776
- pending_peers_awaiting_removal : Mutex < HashSet < PublicKey > > ,
777
765
/// See `ChannelManager` struct-level documentation for lock order requirements.
778
766
pending_background_events : Mutex < Vec < BackgroundEvent > > ,
779
767
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
@@ -1304,11 +1292,10 @@ macro_rules! try_chan_entry {
1304
1292
}
1305
1293
1306
1294
macro_rules! remove_channel {
1307
- ( $self: expr, $entry: expr, $peer_state : expr ) => {
1295
+ ( $self: expr, $entry: expr) => {
1308
1296
{
1309
1297
let channel = $entry. remove_entry( ) . 1 ;
1310
1298
update_maps_on_chan_removal!( $self, channel) ;
1311
- $self. add_pending_peer_to_be_removed( channel. get_counterparty_node_id( ) , $peer_state) ;
1312
1299
channel
1313
1300
}
1314
1301
}
@@ -1481,7 +1468,6 @@ where
1481
1468
per_peer_state : FairRwLock :: new ( HashMap :: new ( ) ) ,
1482
1469
1483
1470
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
1484
- pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
1485
1471
pending_background_events : Mutex :: new ( Vec :: new ( ) ) ,
1486
1472
total_consistency_lock : RwLock :: new ( ( ) ) ,
1487
1473
persistence_notifier : Notifier :: new ( ) ,
@@ -1720,7 +1706,7 @@ where
1720
1706
let ( result, is_permanent) =
1721
1707
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
1722
1708
if is_permanent {
1723
- remove_channel ! ( self , chan_entry, peer_state ) ;
1709
+ remove_channel ! ( self , chan_entry) ;
1724
1710
break result;
1725
1711
}
1726
1712
}
@@ -1731,7 +1717,7 @@ where
1731
1717
} ) ;
1732
1718
1733
1719
if chan_entry. get ( ) . is_shutdown ( ) {
1734
- let channel = remove_channel ! ( self , chan_entry, peer_state ) ;
1720
+ let channel = remove_channel ! ( self , chan_entry) ;
1735
1721
if let Ok ( channel_update) = self . get_channel_update_for_broadcast ( & channel) {
1736
1722
peer_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
1737
1723
msg : channel_update
@@ -1834,7 +1820,7 @@ where
1834
1820
} else {
1835
1821
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: HolderForceClosed ) ;
1836
1822
}
1837
- remove_channel ! ( self , chan, peer_state )
1823
+ remove_channel ! ( self , chan)
1838
1824
} else {
1839
1825
return Err ( APIError :: ChannelUnavailable { err : format ! ( "Channel with id {} not found for the passed counterparty node_id {}" , log_bytes!( * channel_id) , peer_node_id) } ) ;
1840
1826
}
@@ -1873,13 +1859,6 @@ where
1873
1859
}
1874
1860
}
1875
1861
1876
- fn add_pending_peer_to_be_removed ( & self , counterparty_node_id : PublicKey , peer_state : & mut PeerState < <SP :: Target as SignerProvider >:: Signer > ) {
1877
- let peer_should_be_removed = !peer_state. connected && peer_state. channel_by_id . len ( ) == 0 ;
1878
- if peer_should_be_removed {
1879
- self . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) . insert ( counterparty_node_id) ;
1880
- }
1881
- }
1882
-
1883
1862
/// Force closes a channel, immediately broadcasting the latest local transaction(s) and
1884
1863
/// rejecting new HTLCs on the given channel. Fails if `channel_id` is unknown to
1885
1864
/// the manager, or if the `counterparty_node_id` isn't the counterparty of the corresponding
@@ -3304,16 +3283,20 @@ where
3304
3283
true
3305
3284
}
3306
3285
3307
- /// Removes peers which have been been added to `pending_peers_awaiting_removal` which are
3308
- /// still disconnected and we have no channels to.
3286
+ /// When a peer disconnects but still has channels, the peer's `peer_state` entry in the
3287
+ /// `per_peer_state` is not removed by the `peer_disconnected` function. If the channels of
3288
+ /// to that peer is later closed while still being disconnected (i.e. force closed), we
3289
+ /// therefore need to remove the peer from `peer_state` separately.
3290
+ /// To avoid having to take the `per_peer_state` `write` lock once the channels are closed, we
3291
+ /// instead remove such peers awaiting removal through this function, which is called on a
3292
+ /// timer through `timer_tick_occurred`, passing the peers disconnected peers with no channels,
3293
+ /// to limit the negative effects on parallelism as much as possible.
3309
3294
///
3310
3295
/// Must be called without the `per_peer_state` lock acquired.
3311
- fn remove_peers_awaiting_removal ( & self ) {
3312
- let mut pending_peers_awaiting_removal = HashSet :: new ( ) ;
3313
- mem:: swap ( & mut * self . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) , & mut pending_peers_awaiting_removal) ;
3296
+ fn remove_peers_awaiting_removal ( & self , pending_peers_awaiting_removal : HashSet < PublicKey > ) {
3314
3297
if pending_peers_awaiting_removal. len ( ) > 0 {
3315
3298
let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3316
- for counterparty_node_id in pending_peers_awaiting_removal. drain ( ) {
3299
+ for counterparty_node_id in pending_peers_awaiting_removal {
3317
3300
match per_peer_state. entry ( counterparty_node_id) {
3318
3301
hash_map:: Entry :: Occupied ( entry) => {
3319
3302
// Remove the entry if the peer is still disconnected and we still
@@ -3392,6 +3375,7 @@ where
3392
3375
/// the channel.
3393
3376
/// * Expiring a channel's previous `ChannelConfig` if necessary to only allow forwarding HTLCs
3394
3377
/// with the current `ChannelConfig`.
3378
+ /// * Removing peers which have disconnected but and no longer have any channels.
3395
3379
///
3396
3380
/// Note that this may cause reentrancy through `chain::Watch::update_channel` calls or feerate
3397
3381
/// estimate fetches.
@@ -3404,6 +3388,7 @@ where
3404
3388
3405
3389
let mut handle_errors: Vec < ( Result < ( ) , _ > , _ ) > = Vec :: new ( ) ;
3406
3390
let mut timed_out_mpp_htlcs = Vec :: new ( ) ;
3391
+ let mut pending_peers_awaiting_removal = HashSet :: new ( ) ;
3407
3392
{
3408
3393
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
3409
3394
for ( counterparty_node_id, peer_state_mutex) in per_peer_state. iter ( ) {
@@ -3451,10 +3436,13 @@ where
3451
3436
3452
3437
true
3453
3438
} ) ;
3454
- self . add_pending_peer_to_be_removed ( counterparty_node_id, peer_state) ;
3439
+ let peer_should_be_removed = !peer_state. connected && peer_state. channel_by_id . len ( ) == 0 ;
3440
+ if peer_should_be_removed {
3441
+ pending_peers_awaiting_removal. insert ( counterparty_node_id) ;
3442
+ }
3455
3443
}
3456
3444
}
3457
- self . remove_peers_awaiting_removal ( ) ;
3445
+ self . remove_peers_awaiting_removal ( pending_peers_awaiting_removal ) ;
3458
3446
3459
3447
self . claimable_payments . lock ( ) . unwrap ( ) . claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3460
3448
if htlcs. is_empty ( ) {
@@ -4190,7 +4178,7 @@ where
4190
4178
}
4191
4179
} ;
4192
4180
peer_state. pending_msg_events . push ( send_msg_err_event) ;
4193
- let _ = remove_channel ! ( self , channel, peer_state ) ;
4181
+ let _ = remove_channel ! ( self , channel) ;
4194
4182
return Err ( APIError :: APIMisuseError { err : "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations." . to_owned ( ) } ) ;
4195
4183
}
4196
4184
@@ -4476,7 +4464,7 @@ where
4476
4464
let ( result, is_permanent) =
4477
4465
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
4478
4466
if is_permanent {
4479
- remove_channel ! ( self , chan_entry, peer_state ) ;
4467
+ remove_channel ! ( self , chan_entry) ;
4480
4468
break result;
4481
4469
}
4482
4470
}
@@ -4525,7 +4513,7 @@ where
4525
4513
// also implies there are no pending HTLCs left on the channel, so we can
4526
4514
// fully delete it from tracking (the channel monitor is still around to
4527
4515
// watch for old state broadcasts)!
4528
- ( tx, Some ( remove_channel ! ( self , chan_entry, peer_state ) ) )
4516
+ ( tx, Some ( remove_channel ! ( self , chan_entry) ) )
4529
4517
} else { ( tx, None ) }
4530
4518
} ,
4531
4519
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 ) )
@@ -5028,11 +5016,12 @@ where
5028
5016
if let Some ( peer_state_mutex) = per_peer_state. get ( & counterparty_node_id) {
5029
5017
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5030
5018
let peer_state = & mut * peer_state_lock;
5019
+ let pending_msg_events = & mut peer_state. pending_msg_events ;
5031
5020
if let hash_map:: Entry :: Occupied ( chan_entry) = peer_state. channel_by_id . entry ( funding_outpoint. to_channel_id ( ) ) {
5032
- let mut chan = remove_channel ! ( self , chan_entry, peer_state ) ;
5021
+ let mut chan = remove_channel ! ( self , chan_entry) ;
5033
5022
failed_channels. push ( chan. force_shutdown ( false ) ) ;
5034
5023
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5035
- peer_state . pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5024
+ pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5036
5025
msg : update
5037
5026
} ) ;
5038
5027
}
@@ -5042,7 +5031,7 @@ where
5042
5031
ClosureReason :: CommitmentTxConfirmed
5043
5032
} ;
5044
5033
self . issue_channel_close_events ( & chan, reason) ;
5045
- peer_state . pending_msg_events . push ( events:: MessageSendEvent :: HandleError {
5034
+ pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
5046
5035
node_id : chan. get_counterparty_node_id ( ) ,
5047
5036
action : msgs:: ErrorAction :: SendErrorMessage {
5048
5037
msg : msgs:: ErrorMessage { channel_id : chan. channel_id ( ) , data : "Channel force-closed" . to_owned ( ) }
@@ -5084,7 +5073,7 @@ where
5084
5073
{
5085
5074
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5086
5075
5087
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5076
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5088
5077
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5089
5078
let peer_state = & mut * peer_state_lock;
5090
5079
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5124,7 +5113,6 @@ where
5124
5113
}
5125
5114
}
5126
5115
} ) ;
5127
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5128
5116
}
5129
5117
}
5130
5118
@@ -5149,7 +5137,7 @@ where
5149
5137
{
5150
5138
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5151
5139
5152
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5140
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5153
5141
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5154
5142
let peer_state = & mut * peer_state_lock;
5155
5143
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5187,7 +5175,6 @@ where
5187
5175
}
5188
5176
}
5189
5177
} ) ;
5190
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5191
5178
}
5192
5179
}
5193
5180
@@ -5751,7 +5738,7 @@ where
5751
5738
let mut timed_out_htlcs = Vec :: new ( ) ;
5752
5739
{
5753
5740
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5754
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5741
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5755
5742
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5756
5743
let peer_state = & mut * peer_state_lock;
5757
5744
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5835,7 +5822,6 @@ where
5835
5822
}
5836
5823
true
5837
5824
} ) ;
5838
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5839
5825
}
5840
5826
}
5841
5827
@@ -6161,7 +6147,7 @@ where
6161
6147
6162
6148
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6163
6149
6164
- for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6150
+ for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6165
6151
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
6166
6152
let peer_state = & mut * peer_state_lock;
6167
6153
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -6193,7 +6179,6 @@ where
6193
6179
}
6194
6180
retain
6195
6181
} ) ;
6196
- self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
6197
6182
}
6198
6183
//TODO: Also re-broadcast announcement_signatures
6199
6184
Ok ( ( ) )
@@ -6707,8 +6692,6 @@ where
6707
6692
6708
6693
write_ver_prefix ! ( writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
6709
6694
6710
- self . remove_peers_awaiting_removal ( ) ;
6711
-
6712
6695
self . genesis_hash . write ( writer) ?;
6713
6696
{
6714
6697
let best_block = self . best_block . read ( ) . unwrap ( ) ;
@@ -7530,7 +7513,6 @@ where
7530
7513
per_peer_state : FairRwLock :: new ( per_peer_state) ,
7531
7514
7532
7515
pending_events : Mutex :: new ( pending_events_read) ,
7533
- pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
7534
7516
pending_background_events : Mutex :: new ( pending_background_events_read) ,
7535
7517
total_consistency_lock : RwLock :: new ( ( ) ) ,
7536
7518
persistence_notifier : Notifier :: new ( ) ,
@@ -8019,9 +8001,6 @@ mod tests {
8019
8001
// Assert that nodes[1] is awaiting removal for nodes[0] once nodes[1] has been
8020
8002
// disconnected and the channel between has been force closed.
8021
8003
let nodes_0_per_peer_state = nodes[ 0 ] . node . per_peer_state . read ( ) . unwrap ( ) ;
8022
- let nodes_0_pending_peers_awaiting_removal = nodes[ 0 ] . node . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) ;
8023
- assert_eq ! ( nodes_0_pending_peers_awaiting_removal. len( ) , 1 ) ;
8024
- assert ! ( nodes_0_pending_peers_awaiting_removal. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
8025
8004
// Assert that nodes[1] isn't removed before `timer_tick_occurred` has been executed.
8026
8005
assert_eq ! ( nodes_0_per_peer_state. len( ) , 1 ) ;
8027
8006
assert ! ( nodes_0_per_peer_state. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
@@ -8032,7 +8011,6 @@ mod tests {
8032
8011
{
8033
8012
// Assert that nodes[1] has now been removed.
8034
8013
assert_eq ! ( nodes[ 0 ] . node. per_peer_state. read( ) . unwrap( ) . len( ) , 0 ) ;
8035
- assert_eq ! ( nodes[ 0 ] . node. pending_peers_awaiting_removal. lock( ) . unwrap( ) . len( ) , 0 ) ;
8036
8014
}
8037
8015
}
8038
8016
0 commit comments