Skip to content

Commit

Permalink
Handle fallible commitment point when getting channel_ready
Browse files Browse the repository at this point in the history
  • Loading branch information
alecchendev committed Jun 10, 2024
1 parent e132d4b commit 1e5b210
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,9 @@ pub(super) struct ChannelContext<SP: Deref> 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.
//
Expand Down Expand Up @@ -1681,6 +1684,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {

signer_pending_commitment_update: false,
signer_pending_funding: false,
signer_pending_channel_ready: false,


#[cfg(debug_assertions)]
Expand Down Expand Up @@ -1909,6 +1913,7 @@ impl<SP: Deref> ChannelContext<SP> 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`.
Expand Down Expand Up @@ -5135,7 +5140,7 @@ impl<SP: Deref> Channel<SP> 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);
Expand Down Expand Up @@ -5438,7 +5443,7 @@ impl<SP: Deref> Channel<SP> 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,
Expand Down Expand Up @@ -5477,7 +5482,7 @@ impl<SP: Deref> Channel<SP> 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 {
Expand Down Expand Up @@ -6368,24 +6373,27 @@ impl<SP: Deref> Channel<SP> 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<L: Deref>(&mut self, logger: &L) -> Option<msgs::ChannelReady>
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
}
}

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

0 comments on commit 1e5b210

Please sign in to comment.