From 307e0485d2e1440b8bdbcb80ade31b679c79b785 Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Mon, 6 Jan 2025 18:30:49 -0600 Subject: [PATCH] Handle tx_abort without using ChannelPhase 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::internal_tx_abort to use ChannelPhase::is_funded and a new ChannelPhase::as_unfunded_v2_mut method. --- lightning/src/ln/channel.rs | 8 ++++++++ lightning/src/ln/channelmanager.rs | 9 ++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index c5ec6ce8c..bdadcb75f 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -1180,6 +1180,14 @@ impl<'a, SP: Deref> ChannelPhase where None } } + + pub fn as_unfunded_v2_mut(&mut self) -> Option<&mut PendingV2Channel> { + if let ChannelPhase::UnfundedV2(channel) = self { + Some(channel) + } else { + None + } + } } /// Contains all state common to unfunded inbound/outbound channels. diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 5026c820b..8d39784ef 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -8425,9 +8425,9 @@ This indicates a bug inside LDK. Please report this error at https://github.com/ match peer_state.channel_by_id.entry(msg.channel_id) { hash_map::Entry::Occupied(mut chan_phase_entry) => { let channel_phase = chan_phase_entry.get_mut(); - let tx_constructor = match channel_phase { - ChannelPhase::UnfundedV2(chan) => &mut chan.interactive_tx_constructor, - ChannelPhase::Funded(_) => { + let tx_constructor = match channel_phase.as_unfunded_v2_mut() { + Some(chan) => &mut chan.interactive_tx_constructor, + None => if channel_phase.is_funded() { // TODO(splicing)/TODO(RBF): We'll also be doing interactive tx construction // for a "ChannelPhase::Funded" when we want to bump the fee on an interactively // constructed funding tx or during splicing. For now we send an error as we would @@ -8437,8 +8437,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/ splicing and RBF attempts of interactive funding transactions are not supported yet so \ we don't have any negotiation in progress".into(), )), chan_phase_entry) - } - ChannelPhase::UnfundedInboundV1(_) | ChannelPhase::UnfundedOutboundV1(_) => { + } else { try_chan_phase_entry!(self, peer_state, Err(ChannelError::Warn( "Got an unexpected tx_abort message: This is an unfunded channel created with V1 channel \ establishment".into(),