Skip to content

Commit 036871a

Browse files
committed
Reduce chan state logic from ChannelManager when disconnecting
After lightningdevkit#3513 we have a bit more encapsulation of channel logic in channel.rs with channelmanager.rs needing a bit less knowledge of which specific state a channel is in. This continues that trend slightly when a peer disconnects.
1 parent 0c6cb6c commit 036871a

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,9 +1278,18 @@ impl<SP: Deref> Channel<SP> where
12781278
}
12791279
}
12801280

1281-
pub fn is_resumable(&self) -> bool {
1281+
/// Should be called when the peer is disconnected. Returns true if the channel can be resumed
1282+
/// when the peer reconnects (via [`Self::peer_connected_get_handshake`]). If not, the channel
1283+
/// must be immediately closed.
1284+
pub fn peer_disconnected_is_resumable<L: Deref>(&mut self, logger: &L) -> bool where L::Target: Logger {
12821285
match self {
1283-
Channel::Funded(_) => false,
1286+
Channel::Funded(chan) => chan.remove_uncommitted_htlcs_and_mark_paused(logger).is_ok(),
1287+
// If we get disconnected and haven't yet committed to a funding
1288+
// transaction, we can replay the `open_channel` on reconnection, so don't
1289+
// bother dropping the channel here. However, if we already committed to
1290+
// the funding transaction we don't yet support replaying the funding
1291+
// handshake (and bailing if the peer rejects it), so we force-close in
1292+
// that case.
12841293
Channel::UnfundedOutboundV1(chan) => chan.is_resumable(),
12851294
Channel::UnfundedInboundV1(_) => false,
12861295
Channel::UnfundedV2(_) => false,
@@ -6050,7 +6059,7 @@ impl<SP: Deref> FundedChannel<SP> where
60506059
/// No further message handling calls may be made until a channel_reestablish dance has
60516060
/// completed.
60526061
/// May return `Err(())`, which implies [`ChannelContext::force_shutdown`] should be called immediately.
6053-
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
6062+
fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
60546063
assert!(!matches!(self.context.channel_state, ChannelState::ShutdownComplete));
60556064
if self.context.channel_state.is_pre_funded_state() {
60566065
return Err(())

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11473,26 +11473,10 @@ where
1147311473
let peer_state = &mut *peer_state_lock;
1147411474
let pending_msg_events = &mut peer_state.pending_msg_events;
1147511475
peer_state.channel_by_id.retain(|_, phase| {
11476-
match phase.as_funded_mut() {
11477-
Some(chan) => {
11478-
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
11479-
if chan.remove_uncommitted_htlcs_and_mark_paused(&&logger).is_ok() {
11480-
// We only retain funded channels that are not shutdown.
11481-
return true;
11482-
}
11483-
},
11484-
// If we get disconnected and haven't yet committed to a funding
11485-
// transaction, we can replay the `open_channel` on reconnection, so don't
11486-
// bother dropping the channel here. However, if we already committed to
11487-
// the funding transaction we don't yet support replaying the funding
11488-
// handshake (and bailing if the peer rejects it), so we force-close in
11489-
// that case.
11490-
None => {
11491-
if phase.is_resumable() {
11492-
return true;
11493-
}
11494-
},
11495-
};
11476+
let logger = WithChannelContext::from(&self.logger, &phase.context(), None);
11477+
if phase.peer_disconnected_is_resumable(&&logger) {
11478+
return true;
11479+
}
1149611480
// Clean up for removal.
1149711481
let context = phase.context_mut();
1149811482
let mut close_res = context.force_shutdown(false, ClosureReason::DisconnectedPeer);

0 commit comments

Comments
 (0)