Skip to content

Commit

Permalink
Preserve ChannelContext.funding_transaction for the later lifecycle o…
Browse files Browse the repository at this point in the history
…f the channel
  • Loading branch information
optout21 committed Sep 16, 2024
1 parent 02973ea commit 2e925ee
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,8 @@ pub(super) struct ChannelContext<SP: Deref> 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<Transaction>,
/// 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,
Expand Down Expand Up @@ -1791,6 +1793,7 @@ impl<SP: Deref> ChannelContext<SP> 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),
Expand Down Expand Up @@ -2024,6 +2027,7 @@ impl<SP: Deref> ChannelContext<SP> 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,
Expand Down Expand Up @@ -3445,13 +3449,22 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
}
}

/// Returns funding_transaction unless it has been broadcast already
pub fn funding_transaction_unless_broadcast(&self) -> Option<Transaction> {
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<Transaction> {
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
Expand Down Expand Up @@ -5336,7 +5349,9 @@ impl<SP: Deref> Channel<SP> 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.
Expand Down Expand Up @@ -6594,7 +6609,9 @@ impl<SP: Deref> Channel<SP> 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;
Expand Down Expand Up @@ -7740,7 +7757,9 @@ impl<SP: Deref> OutboundV1Channel<SP> 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);
Expand Down Expand Up @@ -8874,6 +8893,7 @@ impl<SP: Deref> Writeable for Channel<SP> 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)?;
Expand Down Expand Up @@ -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<Transaction> = Readable::read(reader)?;
let funding_transaction_broadcast: bool = Readable::read(reader)?;

let counterparty_cur_commitment_point = Readable::read(reader)?;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2e925ee

Please sign in to comment.