@@ -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
@@ -2241,6 +2253,7 @@ where
2241
2253
for ( _cp_id, peer_state_mutex) in per_peer_state. iter ( ) {
2242
2254
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
2243
2255
let peer_state = & mut * peer_state_lock;
2256
+ // Only `Channels` in the channel_by_id map can be considered funded.
2244
2257
for ( _channel_id, channel) in peer_state. channel_by_id . iter ( ) . filter ( f) {
2245
2258
let details = ChannelDetails :: from_channel_context ( & channel. context , best_block_height,
2246
2259
peer_state. latest_features . clone ( ) , & self . fee_estimator ) ;
@@ -2309,11 +2322,15 @@ where
2309
2322
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
2310
2323
let peer_state = & mut * peer_state_lock;
2311
2324
let features = & peer_state. latest_features ;
2325
+ let chan_context_to_details = |context| {
2326
+ ChannelDetails :: from_channel_context ( context, best_block_height, features. clone ( ) , & self . fee_estimator )
2327
+ } ;
2312
2328
return peer_state. channel_by_id
2313
2329
. iter ( )
2314
- . map ( |( _, channel) |
2315
- ChannelDetails :: from_channel_context ( & channel. context , best_block_height,
2316
- features. clone ( ) , & self . fee_estimator ) )
2330
+ . map ( |( _, channel) | & channel. context )
2331
+ . chain ( peer_state. outbound_v1_channel_by_id . iter ( ) . map ( |( _, channel) | & channel. context ) )
2332
+ . chain ( peer_state. inbound_v1_channel_by_id . iter ( ) . map ( |( _, channel) | & channel. context ) )
2333
+ . map ( chan_context_to_details)
2317
2334
. collect ( ) ;
2318
2335
}
2319
2336
vec ! [ ]
@@ -7146,37 +7163,26 @@ where
7146
7163
log_debug ! ( self . logger, "Generating channel_reestablish events for {}" , log_pubkey!( counterparty_node_id) ) ;
7147
7164
7148
7165
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
7149
- for ( _cp_id , peer_state_mutex) in per_peer_state. iter ( ) {
7166
+ if let Some ( peer_state_mutex) = per_peer_state. get ( counterparty_node_id ) {
7150
7167
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
7151
7168
let peer_state = & mut * peer_state_lock;
7152
7169
let pending_msg_events = & mut peer_state. pending_msg_events ;
7153
- peer_state. channel_by_id . retain ( |_, chan| {
7154
- let retain = if chan. context . get_counterparty_node_id ( ) == * counterparty_node_id {
7155
- if !chan. context . have_received_message ( ) {
7156
- // If we created this (outbound) channel while we were disconnected from the
7157
- // peer we probably failed to send the open_channel message, which is now
7158
- // lost. We can't have had anything pending related to this channel, so we just
7159
- // drop it.
7160
- false
7161
- } else {
7162
- pending_msg_events. push ( events:: MessageSendEvent :: SendChannelReestablish {
7163
- node_id : chan. context . get_counterparty_node_id ( ) ,
7164
- msg : chan. get_channel_reestablish ( & self . logger ) ,
7165
- } ) ;
7166
- true
7167
- }
7168
- } else { true } ;
7169
- if retain && chan. context . get_counterparty_node_id ( ) != * counterparty_node_id {
7170
- 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 ) {
7171
- if let Ok ( update_msg) = self . get_channel_update_for_broadcast ( chan) {
7172
- pending_msg_events. push ( events:: MessageSendEvent :: SendChannelAnnouncement {
7173
- node_id : * counterparty_node_id,
7174
- msg, update_msg,
7175
- } ) ;
7176
- }
7177
- }
7178
- }
7179
- retain
7170
+ for ( _, chan) in peer_state. outbound_v1_channel_by_id . iter_mut ( ) {
7171
+ // We shouldn't have any pending outbound channels upon reconnect. If one exists,
7172
+ // then we probably tried to open a channel while the peer was disconnected and they
7173
+ // wouldn't have received the corresponding channel open message. We can just force-close
7174
+ // these channels.
7175
+ log_error ! ( self . logger, "Force-closing channel {}" , log_bytes!( chan. context. channel_id( ) ) ) ;
7176
+ self . issue_channel_close_events ( & chan. context , ClosureReason :: HolderForceClosed ) ;
7177
+ self . finish_force_close_channel ( chan. context . force_shutdown ( false ) ) ;
7178
+ }
7179
+ // Drop all outbound channels which we have force-closed above.
7180
+ peer_state. outbound_v1_channel_by_id . clear ( ) ;
7181
+ peer_state. channel_by_id . iter_mut ( ) . for_each ( |( _, chan) | {
7182
+ pending_msg_events. push ( events:: MessageSendEvent :: SendChannelReestablish {
7183
+ node_id : chan. context . get_counterparty_node_id ( ) ,
7184
+ msg : chan. get_channel_reestablish ( & self . logger ) ,
7185
+ } ) ;
7180
7186
} ) ;
7181
7187
}
7182
7188
//TODO: Also re-broadcast announcement_signatures
0 commit comments