From 239ed54b8b700c5879e1274e950b1d370d8362ff 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 | 38 +++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 4faae954423..776cb8943b5 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -1280,6 +1280,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. // @@ -1741,6 +1744,7 @@ impl ChannelContext where SP::Target: SignerProvider { signer_pending_revoke_and_ack: false, signer_pending_commitment_update: false, signer_pending_funding: false, + signer_pending_channel_ready: false, #[cfg(debug_assertions)] @@ -1970,6 +1974,7 @@ impl ChannelContext where SP::Target: SignerProvider { signer_pending_revoke_and_ack: false, 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`. @@ -5339,7 +5344,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); @@ -5708,7 +5713,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, @@ -5747,7 +5752,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 { @@ -6654,19 +6659,23 @@ impl Channel where 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 { + self.context.signer_pending_channel_ready = false; + 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 } } @@ -9486,6 +9495,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch signer_pending_revoke_and_ack: false, signer_pending_commitment_update: false, signer_pending_funding: false, + signer_pending_channel_ready: false, pending_update_fee, holding_cell_update_fee,