@@ -691,6 +691,18 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
691
691
self . outbound_v1_channel_by_id . contains_key ( channel_id) ||
692
692
self . inbound_v1_channel_by_id . contains_key ( channel_id)
693
693
}
694
+
695
+ /// Returns a bool indicating if the given `channel_id` matches a channel we have with this peer
696
+ /// that is in one of our pending (unfunded) channel maps.
697
+ ///
698
+ /// NOTE: Although V1 established channels will always have a `temporary_channel_id` if they're
699
+ /// in `(outbound/inbound)_v1_channel_by_id`, we use the more general `channel_id` as V2
700
+ /// established channels will have a fixed `channel_id` already after the `accept_channel2`
701
+ /// message is sent/received.
702
+ fn has_pending_channel ( & self , channel_id : & [ u8 ; 32 ] ) -> bool {
703
+ self . outbound_v1_channel_by_id . contains_key ( channel_id) ||
704
+ self . inbound_v1_channel_by_id . contains_key ( channel_id)
705
+ }
694
706
}
695
707
696
708
/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
@@ -2239,6 +2251,7 @@ where
2239
2251
for ( _cp_id, peer_state_mutex) in per_peer_state. iter ( ) {
2240
2252
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
2241
2253
let peer_state = & mut * peer_state_lock;
2254
+ // Only `Channels` in the channel_by_id map can be considered funded.
2242
2255
for ( _channel_id, channel) in peer_state. channel_by_id . iter ( ) . filter ( f) {
2243
2256
let details = ChannelDetails :: from_channel_context ( & channel. context , best_block_height,
2244
2257
peer_state. latest_features . clone ( ) , & self . fee_estimator ) ;
@@ -2307,11 +2320,15 @@ where
2307
2320
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
2308
2321
let peer_state = & mut * peer_state_lock;
2309
2322
let features = & peer_state. latest_features ;
2323
+ let chan_context_to_details = |context| {
2324
+ ChannelDetails :: from_channel_context ( context, best_block_height, features. clone ( ) , & self . fee_estimator )
2325
+ } ;
2310
2326
return peer_state. channel_by_id
2311
2327
. iter ( )
2312
- . map ( |( _, channel) |
2313
- ChannelDetails :: from_channel_context ( & channel. context , best_block_height,
2314
- features. clone ( ) , & self . fee_estimator ) )
2328
+ . map ( |( _, channel) | & channel. context )
2329
+ . chain ( peer_state. outbound_v1_channel_by_id . iter ( ) . map ( |( _, channel) | & channel. context ) )
2330
+ . chain ( peer_state. inbound_v1_channel_by_id . iter ( ) . map ( |( _, channel) | & channel. context ) )
2331
+ . map ( chan_context_to_details)
2315
2332
. collect ( ) ;
2316
2333
}
2317
2334
vec ! [ ]
@@ -7186,37 +7203,26 @@ where
7186
7203
log_debug ! ( self . logger, "Generating channel_reestablish events for {}" , log_pubkey!( counterparty_node_id) ) ;
7187
7204
7188
7205
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
7189
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
7206
+ if let Some ( peer_state_mutex) = per_peer_state. get ( counterparty_node_id ) {
7190
7207
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
7191
7208
let peer_state = & mut * peer_state_lock;
7192
7209
let pending_msg_events = & mut peer_state. pending_msg_events ;
7193
- peer_state. channel_by_id . retain ( |_, chan| {
7194
- let retain = if chan. context . get_counterparty_node_id ( ) == * counterparty_node_id {
7195
- if !chan. context . have_received_message ( ) {
7196
- // If we created this (outbound) channel while we were disconnected from the
7197
- // peer we probably failed to send the open_channel message, which is now
7198
- // lost. We can't have had anything pending related to this channel, so we just
7199
- // drop it.
7200
- false
7201
- } else {
7202
- pending_msg_events. push ( events:: MessageSendEvent :: SendChannelReestablish {
7203
- node_id : chan. context . get_counterparty_node_id ( ) ,
7204
- msg : chan. get_channel_reestablish ( & self . logger ) ,
7205
- } ) ;
7206
- true
7207
- }
7208
- } else { true } ;
7209
- if retain && chan. context . get_counterparty_node_id ( ) != * counterparty_node_id {
7210
- if let Some ( msg) = chan. get_signed_channel_announcement ( & self . node_signer , self . genesis_hash . clone ( ) , self . best_block . read ( ) . unwrap ( ) . height ( ) , & self . default_configuration ) {
7211
- if let Ok ( update_msg) = self . get_channel_update_for_broadcast ( chan) {
7212
- pending_msg_events. push ( events:: MessageSendEvent :: SendChannelAnnouncement {
7213
- node_id : * counterparty_node_id,
7214
- msg, update_msg,
7215
- } ) ;
7216
- }
7217
- }
7218
- }
7219
- retain
7210
+ for ( _, chan) in peer_state. outbound_v1_channel_by_id . iter_mut ( ) {
7211
+ // We shouldn't have any pending outbound channels upon reconnect. If one exists,
7212
+ // then we probably tried to open a channel while the peer was disconnected and they
7213
+ // wouldn't have received the corresponding channel open message. We can just force-close
7214
+ // these channels.
7215
+ log_error ! ( self . logger, "Force-closing channel {}" , log_bytes!( chan. context. channel_id( ) ) ) ;
7216
+ self . issue_channel_close_events ( & chan. context , ClosureReason :: HolderForceClosed ) ;
7217
+ self . finish_force_close_channel ( chan. context . force_shutdown ( false ) ) ;
7218
+ }
7219
+ // Drop all outbound channels which we have force-closed above.
7220
+ peer_state. outbound_v1_channel_by_id . clear ( ) ;
7221
+ peer_state. channel_by_id . iter_mut ( ) . for_each ( |( _, chan) | {
7222
+ pending_msg_events. push ( events:: MessageSendEvent :: SendChannelReestablish {
7223
+ node_id : chan. context . get_counterparty_node_id ( ) ,
7224
+ msg : chan. get_channel_reestablish ( & self . logger ) ,
7225
+ } ) ;
7220
7226
} ) ;
7221
7227
}
7222
7228
//TODO: Also re-broadcast announcement_signatures
0 commit comments