From ef5830a3c309890bf0294f592fac57180cf07b75 Mon Sep 17 00:00:00 2001 From: optout <13562139+optout21@users.noreply.github.com> Date: Mon, 16 Sep 2024 17:03:52 +0200 Subject: [PATCH] Preserve ChannelContext.funding_transaction for the later lifecycle of the channel --- lightning/src/ln/channel.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 46b35a9403..d24c05a26b 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -1383,6 +1383,8 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { /// The transaction which funds this channel. Note that for manually-funded channels (i.e., /// is_manual_broadcast is true) this will be a dummy empty transaction. funding_transaction: Option, + /// Rememeber whether the funding transaction has been broadcast + funding_transaction_broadcast: bool, /// This flag indicates that it is the user's responsibility to validated and broadcast the /// funding transaction. is_manual_broadcast: bool, @@ -1791,6 +1793,7 @@ impl ChannelContext where SP::Target: SignerProvider { channel_type_features: channel_type.clone() }, funding_transaction: None, + funding_transaction_broadcast: false, is_batch_funding: None, counterparty_cur_commitment_point: Some(open_channel_fields.first_per_commitment_point), @@ -2024,6 +2027,7 @@ impl ChannelContext where SP::Target: SignerProvider { channel_type_features: channel_type.clone() }, funding_transaction: None, + funding_transaction_broadcast: false, is_batch_funding: None, counterparty_cur_commitment_point: None, @@ -3445,13 +3449,22 @@ impl ChannelContext where SP::Target: SignerProvider { } } + /// Returns funding_transaction unless it has been broadcast already + pub fn funding_transaction_unless_broadcast(&self) -> Option { + if !self.funding_transaction_broadcast { + self.funding_transaction.clone() + } else { + None + } + } + /// Returns the transaction if there is a pending funding transaction that is yet to be /// broadcast. /// /// Note that if [`Self::is_manual_broadcast`] is true the transaction will be a dummy /// transaction. pub fn unbroadcasted_funding(&self) -> Option { - self.if_unbroadcasted_funding(|| self.funding_transaction.clone()) + self.if_unbroadcasted_funding(|| self.funding_transaction_unless_broadcast()) } /// Returns the transaction ID if there is a pending funding transaction that is yet to be @@ -5336,7 +5349,9 @@ impl Channel where (matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) || matches!(self.context.channel_state, ChannelState::ChannelReady(_))) { - self.context.funding_transaction.take() + let res = self.context.funding_transaction_unless_broadcast(); + self.context.funding_transaction_broadcast = true; + res } else { None }; // That said, if the funding transaction is already confirmed (ie we're active with a // minimum_depth over 0) don't bother re-broadcasting the confirmed funding tx. @@ -6594,7 +6609,9 @@ impl Channel where // Because deciding we're awaiting initial broadcast spuriously could result in // funds-loss (as we don't have a monitor, but have the funding transaction confirmed), // we hard-assert here, even in production builds. - if self.context.is_outbound() { assert!(self.context.funding_transaction.is_some()); } + if self.context.is_outbound() { + assert!(self.context.funding_transaction_unless_broadcast().is_some()); + } assert!(self.context.monitor_pending_channel_ready); assert_eq!(self.context.latest_monitor_update_id, 0); return true; @@ -7740,7 +7757,9 @@ impl OutboundV1Channel where SP::Target: SignerProvider { self.context.minimum_depth = Some(COINBASE_MATURITY); } + debug_assert!(self.context.funding_transaction.is_none()); self.context.funding_transaction = Some(funding_transaction); + self.context.funding_transaction_broadcast = false; self.context.is_batch_funding = Some(()).filter(|_| is_batch_funding); let funding_created = self.get_funding_created_msg(logger); @@ -8874,6 +8893,7 @@ impl Writeable for Channel where SP::Target: SignerProvider { self.context.channel_transaction_parameters.write(writer)?; self.context.funding_transaction.write(writer)?; + self.context.funding_transaction_broadcast.write(writer)?; self.context.counterparty_cur_commitment_point.write(writer)?; self.context.counterparty_prev_commitment_point.write(writer)?; @@ -9214,6 +9234,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch let mut channel_parameters: ChannelTransactionParameters = Readable::read(reader)?; let funding_transaction: Option = Readable::read(reader)?; + let funding_transaction_broadcast: bool = Readable::read(reader)?; let counterparty_cur_commitment_point = Readable::read(reader)?; @@ -9542,6 +9563,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch channel_transaction_parameters: channel_parameters, funding_transaction, + funding_transaction_broadcast, is_batch_funding, counterparty_cur_commitment_point,