Skip to content

Commit 052428e

Browse files
committed
Rewrite ChannelManager::peer_disconnected
Exposing ChannelPhase in ChannelManager has led to verbose match statements, which need to be modified each time a ChannelPhase is added. Making ChannelPhase an implementation detail of Channel would help avoid this. As a step in this direction, update ChannelManager::peer_disconnected to use ChannelPhase::as_funded_mut and a new ChannelPhase::is_resumable method.
1 parent 599da4b commit 052428e

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,15 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
12331233
ChannelPhase::UnfundedV2(_) => None,
12341234
}
12351235
}
1236+
1237+
pub fn is_resumable(&self) -> bool {
1238+
match self {
1239+
ChannelPhase::Funded(_) => false,
1240+
ChannelPhase::UnfundedOutboundV1(chan) => chan.is_resumable(),
1241+
ChannelPhase::UnfundedInboundV1(_) => false,
1242+
ChannelPhase::UnfundedV2(_) => false,
1243+
}
1244+
}
12361245
}
12371246

12381247
/// Contains all state common to unfunded inbound/outbound channels.

lightning/src/ln/channelmanager.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11483,32 +11483,28 @@ where
1148311483
let peer_state = &mut *peer_state_lock;
1148411484
let pending_msg_events = &mut peer_state.pending_msg_events;
1148511485
peer_state.channel_by_id.retain(|_, phase| {
11486-
let context = match phase {
11487-
ChannelPhase::Funded(chan) => {
11486+
match phase.as_funded_mut() {
11487+
Some(chan) => {
1148811488
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
1148911489
if chan.remove_uncommitted_htlcs_and_mark_paused(&&logger).is_ok() {
1149011490
// We only retain funded channels that are not shutdown.
1149111491
return true;
1149211492
}
11493-
&mut chan.context
1149411493
},
1149511494
// If we get disconnected and haven't yet committed to a funding
1149611495
// transaction, we can replay the `open_channel` on reconnection, so don't
1149711496
// bother dropping the channel here. However, if we already committed to
1149811497
// the funding transaction we don't yet support replaying the funding
1149911498
// handshake (and bailing if the peer rejects it), so we force-close in
1150011499
// that case.
11501-
ChannelPhase::UnfundedOutboundV1(chan) if chan.is_resumable() => return true,
11502-
ChannelPhase::UnfundedOutboundV1(chan) => &mut chan.context,
11503-
// Unfunded inbound channels will always be removed.
11504-
ChannelPhase::UnfundedInboundV1(chan) => {
11505-
&mut chan.context
11506-
},
11507-
ChannelPhase::UnfundedV2(chan) => {
11508-
&mut chan.context
11500+
None => {
11501+
if phase.is_resumable() {
11502+
return true;
11503+
}
1150911504
},
1151011505
};
1151111506
// Clean up for removal.
11507+
let context = phase.context_mut();
1151211508
let mut close_res = context.force_shutdown(false, ClosureReason::DisconnectedPeer);
1151311509
locked_close_channel!(self, peer_state, &context, close_res);
1151411510
failed_channels.push(close_res);

0 commit comments

Comments
 (0)