Skip to content

Commit 204246b

Browse files
committed
Introduce ChannelPhase enum
We introduce the `ChannelPhase` enum which will contain the different channel structs wrapped by each of its variants so that we can place these within a single `channel_by_id` map in `peer_state` in the following commits. This will reduce the number of map lookup operations we need to do in `ChannelManager`'s various methods. It will also make certain channel counting logic easier to reason about with less risk of forgetting to modify logic when new channels structs are introduced for V2 channel establishment.
1 parent 1e3df7f commit 204246b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lightning/src/ln/channel.rs

+29
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,35 @@ impl_writeable_tlv_based!(PendingChannelMonitorUpdate, {
605605
(0, update, required),
606606
});
607607

608+
/// The `ChannelPhase` enum describes the current phase in life of a lightning channel with each of
609+
/// its variants containing an appropriate channel struct.
610+
pub(super) enum ChannelPhase<SP: Deref> where SP::Target: SignerProvider {
611+
UnfundedOutboundV1(OutboundV1Channel<SP>),
612+
UnfundedInboundV1(InboundV1Channel<SP>),
613+
Funded(Channel<SP>),
614+
}
615+
616+
impl<'a, SP: Deref> ChannelPhase<SP> where
617+
SP::Target: SignerProvider,
618+
<SP::Target as SignerProvider>::Signer: ChannelSigner,
619+
{
620+
pub fn context(&'a self) -> &'a ChannelContext<SP> {
621+
match self {
622+
ChannelPhase::Funded(chan) => &chan.context,
623+
ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
624+
ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
625+
}
626+
}
627+
628+
pub fn context_mut(&'a mut self) -> &'a mut ChannelContext<SP> {
629+
match self {
630+
ChannelPhase::Funded(ref mut chan) => &mut chan.context,
631+
ChannelPhase::UnfundedOutboundV1(ref mut chan) => &mut chan.context,
632+
ChannelPhase::UnfundedInboundV1(ref mut chan) => &mut chan.context,
633+
}
634+
}
635+
}
636+
608637
/// Contains all state common to unfunded inbound/outbound channels.
609638
pub(super) struct UnfundedChannelContext {
610639
/// A counter tracking how many ticks have elapsed since this unfunded channel was

0 commit comments

Comments
 (0)