@@ -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.
@@ -1347,10 +1359,11 @@ macro_rules! try_chan_entry {
1347
1359
}
1348
1360
1349
1361
macro_rules! remove_channel {
1350
- ( $self: expr, $entry: expr) => {
1362
+ ( $self: expr, $entry: expr, $peer_state : expr ) => {
1351
1363
{
1352
1364
let channel = $entry. remove_entry( ) . 1 ;
1353
1365
update_maps_on_chan_removal!( $self, channel) ;
1366
+ $self. add_pending_peer_to_be_removed( channel. get_counterparty_node_id( ) , $peer_state) ;
1354
1367
channel
1355
1368
}
1356
1369
}
@@ -1523,6 +1536,7 @@ where
1523
1536
per_peer_state : FairRwLock :: new ( HashMap :: new ( ) ) ,
1524
1537
1525
1538
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
1539
+ pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
1526
1540
pending_background_events : Mutex :: new ( Vec :: new ( ) ) ,
1527
1541
total_consistency_lock : RwLock :: new ( ( ) ) ,
1528
1542
persistence_notifier : Notifier :: new ( ) ,
@@ -1789,7 +1803,7 @@ where
1789
1803
let ( result, is_permanent) =
1790
1804
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
1791
1805
if is_permanent {
1792
- remove_channel ! ( self , chan_entry) ;
1806
+ remove_channel ! ( self , chan_entry, peer_state ) ;
1793
1807
break result;
1794
1808
}
1795
1809
}
@@ -1800,7 +1814,7 @@ where
1800
1814
} ) ;
1801
1815
1802
1816
if chan_entry. get ( ) . is_shutdown ( ) {
1803
- let channel = remove_channel ! ( self , chan_entry) ;
1817
+ let channel = remove_channel ! ( self , chan_entry, peer_state ) ;
1804
1818
if let Ok ( channel_update) = self . get_channel_update_for_broadcast ( & channel) {
1805
1819
peer_state. pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
1806
1820
msg : channel_update
@@ -1903,7 +1917,7 @@ where
1903
1917
} else {
1904
1918
self . issue_channel_close_events ( chan. get ( ) , ClosureReason :: HolderForceClosed ) ;
1905
1919
}
1906
- remove_channel ! ( self , chan)
1920
+ remove_channel ! ( self , chan, peer_state )
1907
1921
} else {
1908
1922
return Err ( APIError :: ChannelUnavailable { err : format ! ( "Channel with id {} not found for the passed counterparty node_id {}" , log_bytes!( * channel_id) , peer_node_id) } ) ;
1909
1923
}
@@ -1942,6 +1956,13 @@ where
1942
1956
}
1943
1957
}
1944
1958
1959
+ fn add_pending_peer_to_be_removed ( & self , counterparty_node_id : PublicKey , peer_state : & mut PeerState < <SP :: Target as SignerProvider >:: Signer > ) {
1960
+ let peer_should_be_removed = !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0 ;
1961
+ if peer_should_be_removed {
1962
+ self . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) . insert ( counterparty_node_id) ;
1963
+ }
1964
+ }
1965
+
1945
1966
/// Force closes a channel, immediately broadcasting the latest local transaction(s) and
1946
1967
/// rejecting new HTLCs on the given channel. Fails if `channel_id` is unknown to
1947
1968
/// the manager, or if the `counterparty_node_id` isn't the counterparty of the corresponding
@@ -3421,6 +3442,34 @@ where
3421
3442
true
3422
3443
}
3423
3444
3445
+ /// Removes peers which have been been added to `pending_peers_awaiting_removal` which are
3446
+ /// still disconnected and we have no channels to.
3447
+ ///
3448
+ /// Must be called without the `per_peer_state` lock acquired.
3449
+ fn remove_peers_awaiting_removal ( & self ) {
3450
+ let mut pending_peers_awaiting_removal = HashSet :: new ( ) ;
3451
+ mem:: swap ( & mut * self . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) , & mut pending_peers_awaiting_removal) ;
3452
+ if pending_peers_awaiting_removal. len ( ) > 0 {
3453
+ let mut per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3454
+ for counterparty_node_id in pending_peers_awaiting_removal. drain ( ) {
3455
+ match per_peer_state. entry ( counterparty_node_id) {
3456
+ hash_map:: Entry :: Occupied ( entry) => {
3457
+ // Remove the entry if the peer is still disconnected and we still
3458
+ // have no channels to the peer.
3459
+ let remove_entry = {
3460
+ let peer_state = entry. get ( ) . lock ( ) . unwrap ( ) ;
3461
+ !peer_state. is_connected && peer_state. channel_by_id . len ( ) == 0
3462
+ } ;
3463
+ if remove_entry {
3464
+ entry. remove_entry ( ) ;
3465
+ }
3466
+ } ,
3467
+ hash_map:: Entry :: Vacant ( _) => { /* The PeerState has already been removed */ }
3468
+ }
3469
+ }
3470
+ }
3471
+ }
3472
+
3424
3473
#[ cfg( any( test, feature = "_test_utils" ) ) ]
3425
3474
/// Process background events, for functional testing
3426
3475
pub fn test_process_background_events ( & self ) {
@@ -3499,13 +3548,14 @@ where
3499
3548
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
3500
3549
let peer_state = & mut * peer_state_lock;
3501
3550
let pending_msg_events = & mut peer_state. pending_msg_events ;
3551
+ let counterparty_node_id = * counterparty_node_id;
3502
3552
peer_state. channel_by_id . retain ( |chan_id, chan| {
3503
3553
let chan_needs_persist = self . update_channel_fee ( chan_id, chan, new_feerate) ;
3504
3554
if chan_needs_persist == NotifyOption :: DoPersist { should_persist = NotifyOption :: DoPersist ; }
3505
3555
3506
3556
if let Err ( e) = chan. timer_check_closing_negotiation_progress ( ) {
3507
3557
let ( needs_close, err) = convert_chan_err ! ( self , e, chan, chan_id) ;
3508
- handle_errors. push ( ( Err ( err) , * counterparty_node_id) ) ;
3558
+ handle_errors. push ( ( Err ( err) , counterparty_node_id) ) ;
3509
3559
if needs_close { return false ; }
3510
3560
}
3511
3561
@@ -3539,8 +3589,10 @@ where
3539
3589
3540
3590
true
3541
3591
} ) ;
3592
+ self . add_pending_peer_to_be_removed ( counterparty_node_id, peer_state) ;
3542
3593
}
3543
3594
}
3595
+ self . remove_peers_awaiting_removal ( ) ;
3544
3596
3545
3597
self . claimable_payments . lock ( ) . unwrap ( ) . claimable_htlcs . retain ( |payment_hash, ( _, htlcs) | {
3546
3598
if htlcs. is_empty ( ) {
@@ -4295,7 +4347,7 @@ where
4295
4347
}
4296
4348
} ;
4297
4349
peer_state. pending_msg_events . push ( send_msg_err_event) ;
4298
- let _ = remove_channel ! ( self , channel) ;
4350
+ let _ = remove_channel ! ( self , channel, peer_state ) ;
4299
4351
return Err ( APIError :: APIMisuseError { err : "Please use accept_inbound_channel_from_trusted_peer_0conf to accept channels with zero confirmations." . to_owned ( ) } ) ;
4300
4352
}
4301
4353
@@ -4581,7 +4633,7 @@ where
4581
4633
let ( result, is_permanent) =
4582
4634
handle_monitor_update_res ! ( self , update_res, chan_entry. get_mut( ) , RAACommitmentOrder :: CommitmentFirst , chan_entry. key( ) , NO_UPDATE ) ;
4583
4635
if is_permanent {
4584
- remove_channel ! ( self , chan_entry) ;
4636
+ remove_channel ! ( self , chan_entry, peer_state ) ;
4585
4637
break result;
4586
4638
}
4587
4639
}
@@ -4630,7 +4682,7 @@ where
4630
4682
// also implies there are no pending HTLCs left on the channel, so we can
4631
4683
// fully delete it from tracking (the channel monitor is still around to
4632
4684
// watch for old state broadcasts)!
4633
- ( tx, Some ( remove_channel ! ( self , chan_entry) ) )
4685
+ ( tx, Some ( remove_channel ! ( self , chan_entry, peer_state ) ) )
4634
4686
} else { ( tx, None ) }
4635
4687
} ,
4636
4688
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 ) )
@@ -5133,12 +5185,11 @@ where
5133
5185
if let Some ( peer_state_mutex) = per_peer_state. get ( & counterparty_node_id) {
5134
5186
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5135
5187
let peer_state = & mut * peer_state_lock;
5136
- let pending_msg_events = & mut peer_state. pending_msg_events ;
5137
5188
if let hash_map:: Entry :: Occupied ( chan_entry) = peer_state. channel_by_id . entry ( funding_outpoint. to_channel_id ( ) ) {
5138
- let mut chan = remove_channel ! ( self , chan_entry) ;
5189
+ let mut chan = remove_channel ! ( self , chan_entry, peer_state ) ;
5139
5190
failed_channels. push ( chan. force_shutdown ( false ) ) ;
5140
5191
if let Ok ( update) = self . get_channel_update_for_broadcast ( & chan) {
5141
- pending_msg_events. push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5192
+ peer_state . pending_msg_events . push ( events:: MessageSendEvent :: BroadcastChannelUpdate {
5142
5193
msg : update
5143
5194
} ) ;
5144
5195
}
@@ -5148,7 +5199,7 @@ where
5148
5199
ClosureReason :: CommitmentTxConfirmed
5149
5200
} ;
5150
5201
self . issue_channel_close_events ( & chan, reason) ;
5151
- pending_msg_events. push ( events:: MessageSendEvent :: HandleError {
5202
+ peer_state . pending_msg_events . push ( events:: MessageSendEvent :: HandleError {
5152
5203
node_id : chan. get_counterparty_node_id ( ) ,
5153
5204
action : msgs:: ErrorAction :: SendErrorMessage {
5154
5205
msg : msgs:: ErrorMessage { channel_id : chan. channel_id ( ) , data : "Channel force-closed" . to_owned ( ) }
@@ -5190,7 +5241,7 @@ where
5190
5241
{
5191
5242
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5192
5243
5193
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5244
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5194
5245
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5195
5246
let peer_state = & mut * peer_state_lock;
5196
5247
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5230,6 +5281,7 @@ where
5230
5281
}
5231
5282
}
5232
5283
} ) ;
5284
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5233
5285
}
5234
5286
}
5235
5287
@@ -5254,7 +5306,7 @@ where
5254
5306
{
5255
5307
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5256
5308
5257
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5309
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5258
5310
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5259
5311
let peer_state = & mut * peer_state_lock;
5260
5312
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5292,6 +5344,7 @@ where
5292
5344
}
5293
5345
}
5294
5346
} ) ;
5347
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5295
5348
}
5296
5349
}
5297
5350
@@ -5873,7 +5926,7 @@ where
5873
5926
let mut timed_out_htlcs = Vec :: new ( ) ;
5874
5927
{
5875
5928
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
5876
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5929
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
5877
5930
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
5878
5931
let peer_state = & mut * peer_state_lock;
5879
5932
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -5957,6 +6010,7 @@ where
5957
6010
}
5958
6011
true
5959
6012
} ) ;
6013
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
5960
6014
}
5961
6015
}
5962
6016
@@ -6284,7 +6338,7 @@ where
6284
6338
6285
6339
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
6286
6340
6287
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6341
+ for ( cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
6288
6342
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
6289
6343
let peer_state = & mut * peer_state_lock;
6290
6344
let pending_msg_events = & mut peer_state. pending_msg_events ;
@@ -6316,6 +6370,7 @@ where
6316
6370
}
6317
6371
retain
6318
6372
} ) ;
6373
+ self . add_pending_peer_to_be_removed ( * cp_id, peer_state) ;
6319
6374
}
6320
6375
//TODO: Also re-broadcast announcement_signatures
6321
6376
Ok ( ( ) )
@@ -6829,6 +6884,8 @@ where
6829
6884
6830
6885
write_ver_prefix ! ( writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
6831
6886
6887
+ self . remove_peers_awaiting_removal ( ) ;
6888
+
6832
6889
self . genesis_hash . write ( writer) ?;
6833
6890
{
6834
6891
let best_block = self . best_block . read ( ) . unwrap ( ) ;
@@ -7654,6 +7711,7 @@ where
7654
7711
per_peer_state : FairRwLock :: new ( per_peer_state) ,
7655
7712
7656
7713
pending_events : Mutex :: new ( pending_events_read) ,
7714
+ pending_peers_awaiting_removal : Mutex :: new ( HashSet :: new ( ) ) ,
7657
7715
pending_background_events : Mutex :: new ( pending_background_events_read) ,
7658
7716
total_consistency_lock : RwLock :: new ( ( ) ) ,
7659
7717
persistence_notifier : Notifier :: new ( ) ,
@@ -8117,6 +8175,44 @@ mod tests {
8117
8175
}
8118
8176
}
8119
8177
8178
+ #[ test]
8179
+ fn test_drop_disconnected_peers_when_removing_channels ( ) {
8180
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
8181
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
8182
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
8183
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
8184
+
8185
+ let chan = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
8186
+
8187
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
8188
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
8189
+
8190
+ nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & chan. 2 , & nodes[ 1 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
8191
+ check_closed_broadcast ! ( nodes[ 0 ] , true ) ;
8192
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
8193
+ check_closed_event ! ( nodes[ 0 ] , 1 , ClosureReason :: HolderForceClosed ) ;
8194
+
8195
+ {
8196
+ // Assert that nodes[1] is awaiting removal for nodes[0] once nodes[1] has been
8197
+ // disconnected and the channel between has been force closed.
8198
+ let nodes_0_per_peer_state = nodes[ 0 ] . node . per_peer_state . read ( ) . unwrap ( ) ;
8199
+ let nodes_0_pending_peers_awaiting_removal = nodes[ 0 ] . node . pending_peers_awaiting_removal . lock ( ) . unwrap ( ) ;
8200
+ assert_eq ! ( nodes_0_pending_peers_awaiting_removal. len( ) , 1 ) ;
8201
+ assert ! ( nodes_0_pending_peers_awaiting_removal. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
8202
+ // Assert that nodes[1] isn't removed before `timer_tick_occurred` has been executed.
8203
+ assert_eq ! ( nodes_0_per_peer_state. len( ) , 1 ) ;
8204
+ assert ! ( nodes_0_per_peer_state. get( & nodes[ 1 ] . node. get_our_node_id( ) ) . is_some( ) ) ;
8205
+ }
8206
+
8207
+ nodes[ 0 ] . node . timer_tick_occurred ( ) ;
8208
+
8209
+ {
8210
+ // Assert that nodes[1] has now been removed.
8211
+ assert_eq ! ( nodes[ 0 ] . node. per_peer_state. read( ) . unwrap( ) . len( ) , 0 ) ;
8212
+ assert_eq ! ( nodes[ 0 ] . node. pending_peers_awaiting_removal. lock( ) . unwrap( ) . len( ) , 0 ) ;
8213
+ }
8214
+ }
8215
+
8120
8216
#[ test]
8121
8217
fn bad_inbound_payment_hash ( ) {
8122
8218
// Add coverage for checking that a user-provided payment hash matches the payment secret.
0 commit comments