Skip to content

Commit a6f2c12

Browse files
committed
Add missing pending channel maps checks in ChannelManager
One of a series of follow-up commits to address some issues found in PR 2077, where we split channels up into different maps and structs depending on phase in their life.
1 parent a358ba2 commit a6f2c12

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,18 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
691691
self.outbound_v1_channel_by_id.contains_key(channel_id) ||
692692
self.inbound_v1_channel_by_id.contains_key(channel_id)
693693
}
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+
}
694706
}
695707

696708
/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
@@ -2241,6 +2253,7 @@ where
22412253
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
22422254
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
22432255
let peer_state = &mut *peer_state_lock;
2256+
// Only `Channels` in the channel_by_id map can be considered funded.
22442257
for (_channel_id, channel) in peer_state.channel_by_id.iter().filter(f) {
22452258
let details = ChannelDetails::from_channel_context(&channel.context, best_block_height,
22462259
peer_state.latest_features.clone(), &self.fee_estimator);
@@ -2309,11 +2322,15 @@ where
23092322
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
23102323
let peer_state = &mut *peer_state_lock;
23112324
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+
};
23122328
return peer_state.channel_by_id
23132329
.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)
23172334
.collect();
23182335
}
23192336
vec![]
@@ -7146,37 +7163,26 @@ where
71467163
log_debug!(self.logger, "Generating channel_reestablish events for {}", log_pubkey!(counterparty_node_id));
71477164

71487165
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) {
71507167
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
71517168
let peer_state = &mut *peer_state_lock;
71527169
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+
});
71807186
});
71817187
}
71827188
//TODO: Also re-broadcast announcement_signatures

lightning/src/ln/functional_test_utils.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,13 +2828,6 @@ macro_rules! get_chan_reestablish_msgs {
28282828
panic!("Unexpected event")
28292829
}
28302830
}
2831-
for chan in $src_node.node.list_channels() {
2832-
if chan.is_public && chan.counterparty.node_id != $dst_node.node.get_our_node_id() {
2833-
if let Some(scid) = chan.short_channel_id {
2834-
assert!(announcements.remove(&scid));
2835-
}
2836-
}
2837-
}
28382831
assert!(announcements.is_empty());
28392832
res
28402833
}

0 commit comments

Comments
 (0)