From 07991fa90fe658e4064396ea5bca9013144010c0 Mon Sep 17 00:00:00 2001 From: Alec Chen Date: Thu, 6 Jun 2024 18:15:30 -0500 Subject: [PATCH] Handle fallible commitment point when getting channel_ready --- lightning/src/ln/channel.rs | 39 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 2ca644f52e..b02f4b2f66 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -1221,6 +1221,9 @@ pub(super) struct ChannelContext where SP::Target: SignerProvider { /// [`msgs::FundingCreated`] or [`msgs::FundingSigned`] depending on if this channel is /// outbound or inbound. signer_pending_funding: bool, + /// Similar to [`Self::signer_pending_commitment_update`] but we're waiting to send a + /// [`msgs::ChannelReady`]. + signer_pending_channel_ready: bool, // pending_update_fee is filled when sending and receiving update_fee. // @@ -1681,6 +1684,7 @@ impl ChannelContext where SP::Target: SignerProvider { signer_pending_commitment_update: false, signer_pending_funding: false, + signer_pending_channel_ready: false, #[cfg(debug_assertions)] @@ -1909,6 +1913,7 @@ impl ChannelContext where SP::Target: SignerProvider { signer_pending_commitment_update: false, signer_pending_funding: false, + signer_pending_channel_ready: false, // We'll add our counterparty's `funding_satoshis` to these max commitment output assertions // when we receive `accept_channel2`. @@ -5135,7 +5140,7 @@ impl Channel where assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0), "Funding transaction broadcast by the local client before it should have - LDK didn't do it!"); self.context.monitor_pending_channel_ready = false; - Some(self.get_channel_ready()) + self.get_channel_ready(logger) } else { None }; let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block_height, logger); @@ -5438,7 +5443,7 @@ impl Channel where // We have OurChannelReady set! return Ok(ReestablishResponses { - channel_ready: Some(self.get_channel_ready()), + channel_ready: self.get_channel_ready(logger), raa: None, commitment_update: None, order: RAACommitmentOrder::CommitmentFirst, shutdown_msg, announcement_sigs, @@ -5477,7 +5482,7 @@ impl Channel where let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 { // We should never have to worry about MonitorUpdateInProgress resending ChannelReady - Some(self.get_channel_ready()) + self.get_channel_ready(logger) } else { None }; if msg.next_local_commitment_number == next_counterparty_commitment_number { @@ -6368,24 +6373,27 @@ impl Channel where } if self.context.signer_pending_funding { - // TODO: set signer_pending_channel_ready log_debug!(logger, "Can't produce channel_ready: the signer is pending funding."); + self.context.signer_pending_channel_ready = true; return None; } - // TODO: when get_per_commiment_point becomes async, check if the point is - // available, if not, set signer_pending_channel_ready and return None - - Some(self.get_channel_ready()) + self.get_channel_ready(logger) } - fn get_channel_ready(&self) -> msgs::ChannelReady { - debug_assert!(self.context.holder_commitment_point.is_available()); - msgs::ChannelReady { - channel_id: self.context.channel_id(), - next_per_commitment_point: self.context.holder_commitment_point.current_point() - .expect("TODO"), - short_channel_id_alias: Some(self.context.outbound_scid_alias), + fn get_channel_ready(&mut self, logger: &L) -> Option + where L::Target: Logger + { + if let HolderCommitmentPoint::Available { current, .. } = self.context.holder_commitment_point { + Some(msgs::ChannelReady { + channel_id: self.context.channel_id(), + next_per_commitment_point: current, + short_channel_id_alias: Some(self.context.outbound_scid_alias), + }) + } else { + log_debug!(logger, "Not producing channel_ready: the holder commitment point is not available."); + self.context.signer_pending_channel_ready = true; + None } } @@ -9314,6 +9322,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch signer_pending_commitment_update: false, signer_pending_funding: false, + signer_pending_channel_ready: false, pending_update_fee, holding_cell_update_fee,