Skip to content

Commit 0ff848a

Browse files
committed
Update Peer_diconnected code to handle the above case
- The unnoitified channel is retained post disconnection. - Also update teh Peer_connected code to handle the changes made here.
1 parent 929deda commit 0ff848a

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8863,29 +8863,45 @@ where
88638863
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
88648864
let peer_state = &mut *peer_state_lock;
88658865
let pending_msg_events = &mut peer_state.pending_msg_events;
8866-
peer_state.channel_by_id.retain(|_, phase| {
8866+
8867+
let mut remove_keys = Vec::new();
8868+
8869+
for (chan_id, phase) in peer_state.channel_by_id.iter_mut() {
88678870
let context = match phase {
88688871
ChannelPhase::Funded(chan) => {
88698872
if chan.remove_uncommitted_htlcs_and_mark_paused(&self.logger).is_ok() {
88708873
// We only retain funded channels that are not shutdown.
8871-
return true;
8874+
continue;
88728875
}
88738876
&mut chan.context
88748877
},
8875-
// Unfunded channels will always be removed.
88768878
ChannelPhase::UnfundedOutboundV1(chan) => {
8879+
if peer_state.unnotified_outbound_channel.contains_key(chan_id) {
8880+
// We also retain channel that were created but not broadcasted
8881+
// due to peer disconnecting
8882+
continue;
8883+
}
88778884
&mut chan.context
88788885
},
88798886
ChannelPhase::UnfundedInboundV1(chan) => {
88808887
&mut chan.context
88818888
},
88828889
};
8890+
88838891
// Clean up for removal.
88848892
update_maps_on_chan_removal!(self, &context);
88858893
self.issue_channel_close_events(&context, ClosureReason::DisconnectedPeer);
88868894
failed_channels.push(context.force_shutdown(false));
8887-
false
8888-
});
8895+
8896+
// Mark the channels for removal that didn't satisfy the "continue" conditions.
8897+
remove_keys.push(chan_id.clone()); // Collect keys to remove
8898+
}
8899+
8900+
// Remove channels based on the keys collected.
8901+
for key in remove_keys {
8902+
peer_state.channel_by_id.remove(&key);
8903+
}
8904+
88898905
// Note that we don't bother generating any events for pre-accept channels -
88908906
// they're not considered "channels" yet from the PoV of our events interface.
88918907
peer_state.inbound_channel_request_by_id.clear();
@@ -9034,23 +9050,28 @@ where
90349050
});
90359051
}
90369052

9037-
peer_state.unnotified_outbound_channel.clear();
9038-
9039-
peer_state.channel_by_id.iter_mut().filter_map(|(_, phase)|
9040-
if let ChannelPhase::Funded(chan) = phase { Some(chan) } else {
9041-
// Since unfunded channel maps are cleared upon disconnecting a peer, and they're not persisted
9042-
// (so won't be recovered after a crash), they shouldn't exist here and we would never need to
9043-
// worry about closing and removing them.
9044-
9053+
for (chan_id, phase) in peer_state.channel_by_id.iter_mut() {
9054+
if let ChannelPhase::Funded(chan) = phase {
9055+
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
9056+
node_id: chan.context.get_counterparty_node_id(),
9057+
msg: chan.get_channel_reestablish(&self.logger),
9058+
});
9059+
}
9060+
else if peer_state.unnotified_outbound_channel.contains_key(chan_id) {
9061+
// That means the channel is notified about just now.
9062+
continue;
9063+
}
9064+
else {
9065+
// Since unfunded channel maps are cleared upon disconnecting a peer and they're not persisted
9066+
// (so won't be recovered after a crash), they shouldn't exist here, and we would never need to
9067+
// worry about closing and removing them. However, it's important to note that if we were able to
9068+
// create a channel but failed to send the `SendOpenChannel` message, the channel information would
9069+
// be persisted.
90459070
debug_assert!(false);
9046-
None
90479071
}
9048-
).for_each(|chan| {
9049-
pending_msg_events.push(events::MessageSendEvent::SendChannelReestablish {
9050-
node_id: chan.context.get_counterparty_node_id(),
9051-
msg: chan.get_channel_reestablish(&self.logger),
9052-
});
9053-
});
9072+
}
9073+
9074+
peer_state.unnotified_outbound_channel.clear();
90549075
}
90559076

90569077
return NotifyOption::SkipPersistHandleEvents;

0 commit comments

Comments
 (0)