Skip to content

Commit 48b93b8

Browse files
committed
Remove ChannelPhase::Unfunded*V1 pattern matches
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 methods to use methods on ChannelPhase for obtaining the appropriate V1 channel types.
1 parent d60109a commit 48b93b8

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

lightning/src/ln/channel.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,34 @@ impl<'a, SP: Deref> ChannelPhase<SP> where
11851185
}
11861186
}
11871187

1188+
pub fn as_unfunded_outbound_v1_mut(&mut self) -> Option<&mut OutboundV1Channel<SP>> {
1189+
if let ChannelPhase::UnfundedOutboundV1(channel) = self {
1190+
Some(channel)
1191+
} else {
1192+
None
1193+
}
1194+
}
1195+
1196+
pub fn is_unfunded_outbound_v1(&self) -> bool {
1197+
matches!(self, ChannelPhase::UnfundedOutboundV1(_))
1198+
}
1199+
1200+
pub fn into_unfunded_outbound_v1(self) -> Result<OutboundV1Channel<SP>, Self> {
1201+
if let ChannelPhase::UnfundedOutboundV1(channel) = self {
1202+
Ok(channel)
1203+
} else {
1204+
Err(self)
1205+
}
1206+
}
1207+
1208+
pub fn into_unfunded_inbound_v1(self) -> Result<InboundV1Channel<SP>, Self> {
1209+
if let ChannelPhase::UnfundedInboundV1(channel) = self {
1210+
Ok(channel)
1211+
} else {
1212+
Err(self)
1213+
}
1214+
}
1215+
11881216
pub fn as_unfunded_v2(&self) -> Option<&PendingV2Channel<SP>> {
11891217
if let ChannelPhase::UnfundedV2(channel) = self {
11901218
Some(channel)

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5035,13 +5035,15 @@ where
50355035
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
50365036
let peer_state = &mut *peer_state_lock;
50375037
let funding_txo;
5038-
let (mut chan, msg_opt) = match peer_state.channel_by_id.remove(&temporary_channel_id) {
5039-
Some(ChannelPhase::UnfundedOutboundV1(mut chan)) => {
5038+
let (mut chan, msg_opt) = match peer_state.channel_by_id.remove(&temporary_channel_id)
5039+
.map(ChannelPhase::into_unfunded_outbound_v1)
5040+
{
5041+
Some(Ok(mut chan)) => {
50405042
macro_rules! close_chan { ($err: expr, $api_err: expr, $chan: expr) => { {
50415043
let counterparty;
50425044
let err = if let ChannelError::Close((msg, reason)) = $err {
50435045
let channel_id = $chan.context.channel_id();
5044-
counterparty = chan.context.get_counterparty_node_id();
5046+
counterparty = $chan.context.get_counterparty_node_id();
50455047
let shutdown_res = $chan.context.force_shutdown(false, reason);
50465048
MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, shutdown_res, None)
50475049
} else { unreachable!(); };
@@ -5070,7 +5072,7 @@ where
50705072
}
50715073
}
50725074
},
5073-
Some(phase) => {
5075+
Some(Err(phase)) => {
50745076
peer_state.channel_by_id.insert(temporary_channel_id, phase);
50755077
return Err(APIError::APIMisuseError {
50765078
err: format!(
@@ -8018,12 +8020,12 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80188020
let peer_state = &mut *peer_state_lock;
80198021
match peer_state.channel_by_id.entry(msg.common_fields.temporary_channel_id) {
80208022
hash_map::Entry::Occupied(mut phase) => {
8021-
match phase.get_mut() {
8022-
ChannelPhase::UnfundedOutboundV1(chan) => {
8023+
match phase.get_mut().as_unfunded_outbound_v1_mut() {
8024+
Some(chan) => {
80238025
try_chan_phase_entry!(self, peer_state, chan.accept_channel(msg, &self.default_configuration.channel_handshake_limits, &peer_state.latest_features), phase);
80248026
(chan.context.get_value_satoshis(), chan.context.get_funding_redeemscript().to_p2wsh(), chan.context.get_user_id())
80258027
},
8026-
_ => {
8028+
None => {
80278029
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got an unexpected accept_channel message from peer with counterparty_node_id {}", counterparty_node_id), msg.common_fields.temporary_channel_id));
80288030
}
80298031
}
@@ -8055,8 +8057,10 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80558057
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
80568058
let peer_state = &mut *peer_state_lock;
80578059
let (mut chan, funding_msg_opt, monitor) =
8058-
match peer_state.channel_by_id.remove(&msg.temporary_channel_id) {
8059-
Some(ChannelPhase::UnfundedInboundV1(inbound_chan)) => {
8060+
match peer_state.channel_by_id.remove(&msg.temporary_channel_id)
8061+
.map(ChannelPhase::into_unfunded_inbound_v1)
8062+
{
8063+
Some(Ok(inbound_chan)) => {
80608064
let logger = WithChannelContext::from(&self.logger, &inbound_chan.context, None);
80618065
match inbound_chan.funding_created(msg, best_block, &self.signer_provider, &&logger) {
80628066
Ok(res) => res,
@@ -8072,7 +8076,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80728076
},
80738077
}
80748078
},
8075-
Some(mut phase) => {
8079+
Some(Err(mut phase)) => {
80768080
let err_msg = format!("Got an unexpected funding_created message from peer with counterparty_node_id {}", counterparty_node_id);
80778081
let err = ChannelError::close(err_msg);
80788082
return Err(convert_chan_phase_err!(self, peer_state, err, &mut phase, &msg.temporary_channel_id).1);
@@ -8151,8 +8155,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
81518155
let peer_state = &mut *peer_state_lock;
81528156
match peer_state.channel_by_id.entry(msg.channel_id) {
81538157
hash_map::Entry::Occupied(chan_phase_entry) => {
8154-
if matches!(chan_phase_entry.get(), ChannelPhase::UnfundedOutboundV1(_)) {
8155-
let chan = if let ChannelPhase::UnfundedOutboundV1(chan) = chan_phase_entry.remove() { chan } else { unreachable!() };
8158+
if chan_phase_entry.get().is_unfunded_outbound_v1() {
8159+
let chan = if let Ok(chan) = chan_phase_entry.remove().into_unfunded_outbound_v1() { chan } else { unreachable!() };
81568160
let logger = WithContext::from(
81578161
&self.logger,
81588162
Some(chan.context.get_counterparty_node_id()),

0 commit comments

Comments
 (0)