Skip to content

Commit f27515d

Browse files
committed
Add missing unfunded 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 e9001aa commit f27515d

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,7 @@ where
22462246
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
22472247
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
22482248
let peer_state = &mut *peer_state_lock;
2249+
// Only `Channels` in the channel_by_id map can be considered funded.
22492250
for (_channel_id, channel) in peer_state.channel_by_id.iter().filter(f) {
22502251
let details = ChannelDetails::from_channel_context(&channel.context, best_block_height,
22512252
peer_state.latest_features.clone(), &self.fee_estimator);
@@ -2314,11 +2315,15 @@ where
23142315
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
23152316
let peer_state = &mut *peer_state_lock;
23162317
let features = &peer_state.latest_features;
2318+
let chan_context_to_details = |context| {
2319+
ChannelDetails::from_channel_context(context, best_block_height, features.clone(), &self.fee_estimator)
2320+
};
23172321
return peer_state.channel_by_id
23182322
.iter()
2319-
.map(|(_, channel)|
2320-
ChannelDetails::from_channel_context(&channel.context, best_block_height,
2321-
features.clone(), &self.fee_estimator))
2323+
.map(|(_, channel)| &channel.context)
2324+
.chain(peer_state.outbound_v1_channel_by_id.iter().map(|(_, channel)| &channel.context))
2325+
.chain(peer_state.inbound_v1_channel_by_id.iter().map(|(_, channel)| &channel.context))
2326+
.map(chan_context_to_details)
23222327
.collect();
23232328
}
23242329
vec![]
@@ -7226,37 +7231,20 @@ where
72267231
log_debug!(self.logger, "Generating channel_reestablish events for {}", log_pubkey!(counterparty_node_id));
72277232

72287233
let per_peer_state = self.per_peer_state.read().unwrap();
7229-
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
7234+
if let Some(peer_state_mutex) = per_peer_state.get(counterparty_node_id) {
72307235
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
72317236
let peer_state = &mut *peer_state_lock;
72327237
let pending_msg_events = &mut peer_state.pending_msg_events;
7233-
peer_state.channel_by_id.retain(|_, chan| {
7234-
let retain = if chan.context.get_counterparty_node_id() == *counterparty_node_id {
7235-
if !chan.context.have_received_message() {
7236-
// If we created this (outbound) channel while we were disconnected from the
7237-
// peer we probably failed to send the open_channel message, which is now
7238-
// lost. We can't have had anything pending related to this channel, so we just
7239-
// drop it.
7240-
false
7241-
} else {
7242-
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
7243-
node_id: chan.context.get_counterparty_node_id(),
7244-
msg: chan.get_channel_reestablish(&self.logger),
7245-
});
7246-
true
7247-
}
7248-
} else { true };
7249-
if retain && chan.context.get_counterparty_node_id() != *counterparty_node_id {
7250-
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) {
7251-
if let Ok(update_msg) = self.get_channel_update_for_broadcast(chan) {
7252-
pending_msg_events.push(events::MessageSendEvent::SendChannelAnnouncement {
7253-
node_id: *counterparty_node_id,
7254-
msg, update_msg,
7255-
});
7256-
}
7257-
}
7258-
}
7259-
retain
7238+
7239+
// Since unfunded channel maps are cleared upon disconnecting a peer, and they're not persisted
7240+
// (so won't be recovered after a crash) we don't need to bother closing unfunded channels and
7241+
// clearing their maps here. Instead we can just send queue channel_reestablish messages for
7242+
// channels in the channel_by_id map.
7243+
peer_state.channel_by_id.iter_mut().for_each(|(_, chan)| {
7244+
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
7245+
node_id: chan.context.get_counterparty_node_id(),
7246+
msg: chan.get_channel_reestablish(&self.logger),
7247+
});
72607248
});
72617249
}
72627250
//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
@@ -2861,13 +2861,6 @@ macro_rules! get_chan_reestablish_msgs {
28612861
panic!("Unexpected event")
28622862
}
28632863
}
2864-
for chan in $src_node.node.list_channels() {
2865-
if chan.is_public && chan.counterparty.node_id != $dst_node.node.get_our_node_id() {
2866-
if let Some(scid) = chan.short_channel_id {
2867-
assert!(announcements.remove(&scid));
2868-
}
2869-
}
2870-
}
28712864
assert!(announcements.is_empty());
28722865
res
28732866
}

0 commit comments

Comments
 (0)