Skip to content

Commit 11797e8

Browse files
committed
Reduce chan state logic from ChannelManager when disconnecting
After #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 313f7d9 commit 11797e8

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

0 commit comments

Comments
 (0)