@@ -620,6 +620,8 @@ 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
+ // | |
623
625
// | |__`pending_events`
624
626
// | |
625
627
// | |__`pending_background_events`
@@ -787,6 +789,16 @@ where
787
789
788
790
/// See `ChannelManager` struct-level documentation for lock order requirements.
789
791
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 > > ,
790
802
/// See `ChannelManager` struct-level documentation for lock order requirements.
791
803
pending_background_events : Mutex < Vec < BackgroundEvent > > ,
792
804
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
@@ -1317,10 +1329,11 @@ macro_rules! try_chan_entry {
1317
1329
}
1318
1330
1319
1331
macro_rules! remove_channel {
1320
- ( $self: expr, $entry: expr) => {
1332
+ ( $self: expr, $entry: expr, $peer_state : expr ) => {
1321
1333
{
1322
1334
let channel = $entry. remove_entry( ) . 1 ;
1323
1335
update_maps_on_chan_removal!( $self, channel) ;
1336
+ $self. add_pending_peer_to_be_removed( channel. get_counterparty_node_id( ) , $peer_state) ;
1324
1337
channel
1325
1338
}
1326
1339
}
@@ -1493,6 +1506,7 @@ where
1493
1506
per_peer_state : FairRwLock :: new ( HashMap :: new ( ) ) ,
1494
1507
1495
1508
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
1509
+ pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
1496
1510
pending_background_events : Mutex :: new ( Vec :: new ( ) ) ,
1497
1511
total_consistency_lock : RwLock :: new ( ( ) ) ,
1498
1512
persistence_notifier : Notifier :: new ( ) ,
@@ -1731,7 +1745,7 @@ where
1731
1745
let ( result, is_permanent) =
1732
1746
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
1733
1747
if is_permanent {
1734
- remove_channel ! ( self , chan_entry) ;
1748
+ remove_channel ! ( self , chan_entry, peer_state ) ;
1735
1749
break result;
1736
1750
}
1737
1751
}
@@ -1742,7 +1756,7 @@ where
1742
1756
} ) ;
1743
1757
1744
1758
if chan_entry. get ( ) . is_shutdown ( ) {
1745
- let channel = remove_channel ! ( self , chan_entry) ;
1759
+ let channel = remove_channel ! ( self , chan_entry, peer_state ) ;
1746
1760
if let Ok ( channel_update) = self . get_channel_update_for_broadcast ( & channel) {
1747
1761
peer_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
1748
1762
msg : channel_update
@@ -1845,7 +1859,7 @@ where
1845
1859
} else {
1846
1860
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: HolderForceClosed ) ;
1847
1861
}
1848
- remove_channel ! ( self , chan)
1862
+ remove_channel ! ( self , chan, peer_state )
1849
1863
} else {
1850
1864
return Err ( APIError :: ChannelUnavailable { err : format ! ( "Channel with id {} not found for the passed counterparty node_id {}" , log_bytes!( * channel_id) , peer_node_id) } ) ;
1851
1865
}
@@ -1884,6 +1898,13 @@ where
1884
1898
}
1885
1899
}
1886
1900
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
+
1887
1908
/// Force closes a channel, immediately broadcasting the latest local transaction(s) and
1888
1909
/// rejecting new HTLCs on the given channel. Fails if `channel_id` is unknown to
1889
1910
/// the manager, or if the `counterparty_node_id` isn't the counterparty of the corresponding
@@ -3337,6 +3358,34 @@ where
3337
3358
true
3338
3359
}
3339
3360
3361
+ /// Removes peers which have been been added to `pending_peers_awaiting_removal` which are
3362
+ /// still disconnected and we have no channels to.
3363
+ ///
3364
+ /// 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) ;
3368
+ if pending_peers_awaiting_removal. len ( ) > 0 {
3369
+ let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3370
+ for counterparty_node_id in pending_peers_awaiting_removal. drain ( ) {
3371
+ match per_peer_state. entry ( counterparty_node_id) {
3372
+ hash_map:: Entry :: Occupied ( entry) => {
3373
+ // Remove the entry if the peer is still disconnected and we still
3374
+ // have no channels to the peer.
3375
+ let remove_entry = {
3376
+ let peer_state = entry. get ( ) . lock ( ) . unwrap ( ) ;
3377
+ !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0
3378
+ } ;
3379
+ if remove_entry {
3380
+ entry. remove_entry ( ) ;
3381
+ }
3382
+ } ,
3383
+ hash_map:: Entry :: Vacant ( _) => { /* The PeerState has already been removed */ }
3384
+ }
3385
+ }
3386
+ }
3387
+ }
3388
+
3340
3389
#[ cfg( any( test, feature = "_test_utils" ) ) ]
3341
3390
/// Process background events, for functional testing
3342
3391
pub fn test_process_background_events ( & self ) {
@@ -3415,13 +3464,14 @@ where
3415
3464
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
3416
3465
let peer_state = & mut * peer_state_lock;
3417
3466
let pending_msg_events = & mut peer_state. pending_msg_events ;
3467
+ let counterparty_node_id = * counterparty_node_id;
3418
3468
peer_state. channel_by_id . retain ( |chan_id, chan| {
3419
3469
let chan_needs_persist = self . update_channel_fee ( chan_id, chan, new_feerate) ;
3420
3470
if chan_needs_persist == NotifyOption :: DoPersist { should_persist = NotifyOption :: DoPersist ; }
3421
3471
3422
3472
if let Err ( e) = chan. timer_check_closing_negotiation_progress ( ) {
3423
3473
let ( needs_close, err) = convert_chan_err ! ( self , e, chan, chan_id) ;
3424
- handle_errors. push ( ( Err ( err) , * counterparty_node_id) ) ;
3474
+ handle_errors. push ( ( Err ( err) , counterparty_node_id) ) ;
3425
3475
if needs_close { return false ; }
3426
3476
}
3427
3477
@@ -3455,8 +3505,10 @@ where
3455
3505
3456
3506
true
3457
3507
} ) ;
3508
+ self . add_pending_peer_to_be_removed ( counterparty_node_id, peer_state) ;
3458
3509
}
3459
3510
}
3511
+ self . remove_peers_awaiting_removal ( ) ;
3460
3512
3461
3513
self . claimable_payments . lock ( ) . unwrap ( ) . claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3462
3514
if htlcs. is_empty ( ) {
@@ -4211,7 +4263,7 @@ where
4211
4263
}
4212
4264
} ;
4213
4265
peer_state. pending_msg_events . push ( send_msg_err_event) ;
4214
- let _ = remove_channel ! ( self , channel) ;
4266
+ let _ = remove_channel ! ( self , channel, peer_state ) ;
4215
4267
return Err ( APIError :: APIMisuseError { err : "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations." . to_owned ( ) } ) ;
4216
4268
}
4217
4269
@@ -4497,7 +4549,7 @@ where
4497
4549
let ( result, is_permanent) =
4498
4550
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
4499
4551
if is_permanent {
4500
- remove_channel ! ( self , chan_entry) ;
4552
+ remove_channel ! ( self , chan_entry, peer_state ) ;
4501
4553
break result;
4502
4554
}
4503
4555
}
@@ -4546,7 +4598,7 @@ where
4546
4598
// also implies there are no pending HTLCs left on the channel, so we can
4547
4599
// fully delete it from tracking (the channel monitor is still around to
4548
4600
// watch for old state broadcasts)!
4549
- ( tx, Some ( remove_channel ! ( self , chan_entry) ) )
4601
+ ( tx, Some ( remove_channel ! ( self , chan_entry, peer_state ) ) )
4550
4602
} else { ( tx, None ) }
4551
4603
} ,
4552
4604
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 ) )
@@ -5049,12 +5101,11 @@ where
5049
5101
if let Some ( peer_state_mutex) = per_peer_state. get ( & counterparty_node_id) {
5050
5102
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5051
5103
let peer_state = & mut * peer_state_lock;
5052
- let pending_msg_events = & mut peer_state. pending_msg_events ;
5053
5104
if let hash_map:: Entry :: Occupied ( chan_entry) = peer_state. channel_by_id . entry ( funding_outpoint. to_channel_id ( ) ) {
5054
- let mut chan = remove_channel ! ( self , chan_entry) ;
5105
+ let mut chan = remove_channel ! ( self , chan_entry, peer_state ) ;
5055
5106
failed_channels. push ( chan. force_shutdown ( false ) ) ;
5056
5107
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5057
- pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5108
+ peer_state . pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5058
5109
msg : update
5059
5110
} ) ;
5060
5111
}
@@ -5064,7 +5115,7 @@ where
5064
5115
ClosureReason :: CommitmentTxConfirmed
5065
5116
} ;
5066
5117
self . issue_channel_close_events ( & chan, reason) ;
5067
- pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
5118
+ peer_state . pending_msg_events . push ( events:: MessageSendEvent :: HandleError {
5068
5119
node_id : chan. get_counterparty_node_id ( ) ,
5069
5120
action : msgs:: ErrorAction :: SendErrorMessage {
5070
5121
msg : msgs:: ErrorMessage { channel_id : chan. channel_id ( ) , data : "Channel force-closed" . to_owned ( ) }
@@ -5106,7 +5157,7 @@ where
5106
5157
{
5107
5158
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5108
5159
5109
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5160
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5110
5161
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5111
5162
let peer_state = & mut * peer_state_lock;
5112
5163
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5146,6 +5197,7 @@ where
5146
5197
}
5147
5198
}
5148
5199
} ) ;
5200
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5149
5201
}
5150
5202
}
5151
5203
@@ -5170,7 +5222,7 @@ where
5170
5222
{
5171
5223
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5172
5224
5173
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5225
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5174
5226
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5175
5227
let peer_state = & mut * peer_state_lock;
5176
5228
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5208,6 +5260,7 @@ where
5208
5260
}
5209
5261
}
5210
5262
} ) ;
5263
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5211
5264
}
5212
5265
}
5213
5266
@@ -5783,7 +5836,7 @@ where
5783
5836
let mut timed_out_htlcs = Vec :: new ( ) ;
5784
5837
{
5785
5838
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5786
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5839
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5787
5840
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5788
5841
let peer_state = & mut * peer_state_lock;
5789
5842
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5867,6 +5920,7 @@ where
5867
5920
}
5868
5921
true
5869
5922
} ) ;
5923
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5870
5924
}
5871
5925
}
5872
5926
@@ -6194,7 +6248,7 @@ where
6194
6248
6195
6249
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6196
6250
6197
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6251
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6198
6252
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
6199
6253
let peer_state = & mut * peer_state_lock;
6200
6254
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -6226,6 +6280,7 @@ where
6226
6280
}
6227
6281
retain
6228
6282
} ) ;
6283
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
6229
6284
}
6230
6285
//TODO: Also re-broadcast announcement_signatures
6231
6286
Ok ( ( ) )
@@ -6739,6 +6794,8 @@ where
6739
6794
6740
6795
write_ver_prefix ! ( writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
6741
6796
6797
+ self . remove_peers_awaiting_removal ( ) ;
6798
+
6742
6799
self . genesis_hash . write ( writer) ?;
6743
6800
{
6744
6801
let best_block = self . best_block . read ( ) . unwrap ( ) ;
@@ -7563,6 +7620,7 @@ where
7563
7620
per_peer_state : FairRwLock :: new ( per_peer_state) ,
7564
7621
7565
7622
pending_events : Mutex :: new ( pending_events_read) ,
7623
+ pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
7566
7624
pending_background_events : Mutex :: new ( pending_background_events_read) ,
7567
7625
total_consistency_lock : RwLock :: new ( ( ) ) ,
7568
7626
persistence_notifier : Notifier :: new ( ) ,
@@ -8026,6 +8084,44 @@ mod tests {
8026
8084
}
8027
8085
}
8028
8086
8087
+ #[ test]
8088
+ fn test_drop_disconnected_peers_when_removing_channels ( ) {
8089
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8090
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8091
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8092
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8093
+
8094
+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
8095
+
8096
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
8097
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
8098
+
8099
+ nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 1 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
8100
+ check_closed_broadcast ! ( nodes[ 0 ] , true ) ;
8101
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8102
+ check_closed_event ! ( nodes[ 0 ] , 1 , ClosureReason :: HolderForceClosed ) ;
8103
+
8104
+ {
8105
+ // Assert that nodes[1] is awaiting removal for nodes[0] once nodes[1] has been
8106
+ // disconnected and the channel between has been force closed.
8107
+ 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
+ // Assert that nodes[1] isn't removed before `timer_tick_occurred` has been executed.
8112
+ assert_eq ! ( nodes_0_per_peer_state. len( ) , 1 ) ;
8113
+ assert ! ( nodes_0_per_peer_state. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
8114
+ }
8115
+
8116
+ nodes[ 0 ] . node . timer_tick_occurred ( ) ;
8117
+
8118
+ {
8119
+ // Assert that nodes[1] has now been removed.
8120
+ 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
+ }
8123
+ }
8124
+
8029
8125
#[ test]
8030
8126
fn bad_inbound_payment_hash ( ) {
8031
8127
// Add coverage for checking that a user-provided payment hash matches the payment secret.
0 commit comments