Skip to content

Commit e3325a2

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 59da806 commit e3325a2

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

lightning/src/ln/channel.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1285,10 +1285,18 @@ impl<SP: Deref> Channel<SP> where
12851285
}
12861286
}
12871287

1288-
pub fn is_resumable(&self) -> bool {
1289-
match &self.phase {
1288+
/// Should be called when the peer is disconnected. Returns true if the channel can be resumed
1289+
/// when the peer reconnects. If not, the channel must be immediately closed.
1290+
pub fn peer_disconnected_is_resumable<L: Deref>(&mut self, logger: &L) -> bool where L::Target: Logger {
1291+
match &mut self.phase {
12901292
ChannelPhase::Undefined => unreachable!(),
1291-
ChannelPhase::Funded(_) => false,
1293+
ChannelPhase::Funded(chan) => chan.remove_uncommitted_htlcs_and_mark_paused(logger).is_ok(),
1294+
// If we get disconnected and haven't yet committed to a funding
1295+
// transaction, we can replay the `open_channel` on reconnection, so don't
1296+
// bother dropping the channel here. However, if we already committed to
1297+
// the funding transaction we don't yet support replaying the funding
1298+
// handshake (and bailing if the peer rejects it), so we force-close in
1299+
// that case.
12921300
ChannelPhase::UnfundedOutboundV1(chan) => chan.is_resumable(),
12931301
ChannelPhase::UnfundedInboundV1(_) => false,
12941302
ChannelPhase::UnfundedV2(_) => false,
@@ -6165,7 +6173,7 @@ impl<SP: Deref> FundedChannel<SP> where
61656173
/// No further message handling calls may be made until a channel_reestablish dance has
61666174
/// completed.
61676175
/// May return `Err(())`, which implies [`ChannelContext::force_shutdown`] should be called immediately.
6168-
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
6176+
fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
61696177
assert!(!matches!(self.context.channel_state, ChannelState::ShutdownComplete));
61706178
if self.context.channel_state.is_pre_funded_state() {
61716179
return Err(())

lightning/src/ln/channelmanager.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -11499,26 +11499,10 @@ where
1149911499
let peer_state = &mut *peer_state_lock;
1150011500
let pending_msg_events = &mut peer_state.pending_msg_events;
1150111501
peer_state.channel_by_id.retain(|_, chan| {
11502-
match chan.as_funded_mut() {
11503-
Some(funded_chan) => {
11504-
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
11505-
if funded_chan.remove_uncommitted_htlcs_and_mark_paused(&&logger).is_ok() {
11506-
// We only retain funded channels that are not shutdown.
11507-
return true;
11508-
}
11509-
},
11510-
// If we get disconnected and haven't yet committed to a funding
11511-
// transaction, we can replay the `open_channel` on reconnection, so don't
11512-
// bother dropping the channel here. However, if we already committed to
11513-
// the funding transaction we don't yet support replaying the funding
11514-
// handshake (and bailing if the peer rejects it), so we force-close in
11515-
// that case.
11516-
None => {
11517-
if chan.is_resumable() {
11518-
return true;
11519-
}
11520-
},
11521-
};
11502+
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
11503+
if chan.peer_disconnected_is_resumable(&&logger) {
11504+
return true;
11505+
}
1152211506
// Clean up for removal.
1152311507
let context = chan.context_mut();
1152411508
let mut close_res = context.force_shutdown(false, ClosureReason::DisconnectedPeer);

0 commit comments

Comments
 (0)