Skip to content

Commit 8a5c6fa

Browse files
TheBlueMattAditya Sharma
authored and
Aditya Sharma
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 a2d1724 commit 8a5c6fa

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,
@@ -6117,7 +6125,7 @@ impl<SP: Deref> FundedChannel<SP> where
61176125
/// No further message handling calls may be made until a channel_reestablish dance has
61186126
/// completed.
61196127
/// May return `Err(())`, which implies [`ChannelContext::force_shutdown`] should be called immediately.
6120-
pub fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
6128+
fn remove_uncommitted_htlcs_and_mark_paused<L: Deref>(&mut self, logger: &L) -> Result<(), ()> where L::Target: Logger {
61216129
assert!(!matches!(self.context.channel_state, ChannelState::ShutdownComplete));
61226130
if self.context.channel_state.is_pre_funded_state() {
61236131
return Err(())

lightning/src/ln/channelmanager.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -11528,26 +11528,10 @@ where
1152811528
let peer_state = &mut *peer_state_lock;
1152911529
let pending_msg_events = &mut peer_state.pending_msg_events;
1153011530
peer_state.channel_by_id.retain(|_, chan| {
11531-
match chan.as_funded_mut() {
11532-
Some(funded_chan) => {
11533-
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
11534-
if funded_chan.remove_uncommitted_htlcs_and_mark_paused(&&logger).is_ok() {
11535-
// We only retain funded channels that are not shutdown.
11536-
return true;
11537-
}
11538-
},
11539-
// If we get disconnected and haven't yet committed to a funding
11540-
// transaction, we can replay the `open_channel` on reconnection, so don't
11541-
// bother dropping the channel here. However, if we already committed to
11542-
// the funding transaction we don't yet support replaying the funding
11543-
// handshake (and bailing if the peer rejects it), so we force-close in
11544-
// that case.
11545-
None => {
11546-
if chan.is_resumable() {
11547-
return true;
11548-
}
11549-
},
11550-
};
11531+
let logger = WithChannelContext::from(&self.logger, &chan.context(), None);
11532+
if chan.peer_disconnected_is_resumable(&&logger) {
11533+
return true;
11534+
}
1155111535
// Clean up for removal.
1155211536
let context = chan.context_mut();
1155311537
let mut close_res = context.force_shutdown(false, ClosureReason::DisconnectedPeer);

0 commit comments

Comments
 (0)