Skip to content

Commit bf8a8b9

Browse files
committed
Handle fallible commitment point when getting channel_ready
Here we handle the case where our signer is pending the next commitment point when we try to send channel ready. We set a flag to remember to send this message when our signer is unblocked. This follows the same general pattern as everywhere else where we're waiting on a commitment point from the signer in order to send a message.
1 parent 3dec982 commit bf8a8b9

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

lightning/src/ln/channel.rs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
12831283
/// If we attempted to sign a cooperative close transaction but the signer wasn't ready, then this
12841284
/// will be set to `true`.
12851285
signer_pending_closing: bool,
1286+
/// Similar to [`Self::signer_pending_commitment_update`] but we're waiting to send a
1287+
/// [`msgs::ChannelReady`].
1288+
signer_pending_channel_ready: bool,
12861289

12871290
// pending_update_fee is filled when sending and receiving update_fee.
12881291
//
@@ -2127,6 +2130,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21272130
signer_pending_commitment_update: false,
21282131
signer_pending_funding: false,
21292132
signer_pending_closing: false,
2133+
signer_pending_channel_ready: false,
21302134

21312135

21322136
#[cfg(debug_assertions)]
@@ -2360,6 +2364,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23602364
signer_pending_commitment_update: false,
23612365
signer_pending_funding: false,
23622366
signer_pending_closing: false,
2367+
signer_pending_channel_ready: false,
23632368

23642369
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
23652370
// when we receive `accept_channel2`.
@@ -5981,7 +5986,7 @@ impl<SP: Deref> Channel<SP> where
59815986
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
59825987
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
59835988
self.context.monitor_pending_channel_ready = false;
5984-
Some(self.get_channel_ready())
5989+
self.get_channel_ready(logger)
59855990
} else { None };
59865991

59875992
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block_height, logger);
@@ -6104,8 +6109,11 @@ impl<SP: Deref> Channel<SP> where
61046109
let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
61056110
self.context.get_funding_signed_msg(logger, counterparty_initial_commitment_tx)
61066111
} else { None };
6107-
let channel_ready = if funding_signed.is_some() {
6108-
self.check_get_channel_ready(0, logger)
6112+
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
6113+
// funding.
6114+
let channel_ready = if self.context.signer_pending_channel_ready && !self.context.signer_pending_funding {
6115+
log_trace!(logger, "Attempting to generate pending channel_ready...");
6116+
self.get_channel_ready(logger)
61096117
} else { None };
61106118

61116119
let mut commitment_update = if self.context.signer_pending_commitment_update {
@@ -6404,7 +6412,7 @@ impl<SP: Deref> Channel<SP> where
64046412

64056413
// We have OurChannelReady set!
64066414
return Ok(ReestablishResponses {
6407-
channel_ready: Some(self.get_channel_ready()),
6415+
channel_ready: self.get_channel_ready(logger),
64086416
raa: None, commitment_update: None,
64096417
order: RAACommitmentOrder::CommitmentFirst,
64106418
shutdown_msg, announcement_sigs,
@@ -6443,7 +6451,7 @@ impl<SP: Deref> Channel<SP> where
64436451

64446452
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.holder_commitment_point.transaction_number() == 1 {
64456453
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
6446-
Some(self.get_channel_ready())
6454+
self.get_channel_ready(logger)
64476455
} else { None };
64486456

64496457
if msg.next_local_commitment_number == next_counterparty_commitment_number {
@@ -7298,14 +7306,6 @@ impl<SP: Deref> Channel<SP> where
72987306
return None;
72997307
}
73007308

7301-
// If we're still pending the signature on a funding transaction, then we're not ready to send a
7302-
// channel_ready yet.
7303-
if self.context.signer_pending_funding {
7304-
// TODO: set signer_pending_channel_ready
7305-
log_debug!(logger, "Can't produce channel_ready: the signer is pending funding.");
7306-
return None;
7307-
}
7308-
73097309
// Note that we don't include ChannelState::WaitingForBatch as we don't want to send
73107310
// channel_ready until the entire batch is ready.
73117311
let need_commitment_update = if matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(f) if f.clone().clear(FundedStateFlags::ALL.into()).is_empty()) {
@@ -7351,18 +7351,34 @@ impl<SP: Deref> Channel<SP> where
73517351
return None;
73527352
}
73537353

7354-
// TODO: when get_per_commiment_point becomes async, check if the point is
7355-
// available, if not, set signer_pending_channel_ready and return None
7354+
// If we're still pending the signature on a funding transaction, then we're not ready to send a
7355+
// channel_ready yet.
7356+
if self.context.signer_pending_funding {
7357+
log_debug!(logger, "Can't produce channel_ready: the signer is pending funding.");
7358+
// We make sure to set the channel ready flag here so that we try to
7359+
// generate a channel ready for 0conf channels once our signer unblocked
7360+
// for funding.
7361+
self.context.signer_pending_channel_ready = true;
7362+
return None;
7363+
}
73567364

7357-
Some(self.get_channel_ready())
7365+
self.get_channel_ready(logger)
73587366
}
73597367

7360-
fn get_channel_ready(&self) -> msgs::ChannelReady {
7361-
debug_assert!(self.holder_commitment_point.is_available());
7362-
msgs::ChannelReady {
7363-
channel_id: self.context.channel_id(),
7364-
next_per_commitment_point: self.holder_commitment_point.current_point(),
7365-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
7368+
fn get_channel_ready<L: Deref>(&mut self, logger: &L) -> Option<msgs::ChannelReady>
7369+
where L::Target: Logger
7370+
{
7371+
if let HolderCommitmentPoint::Available { current, .. } = self.holder_commitment_point {
7372+
self.context.signer_pending_channel_ready = false;
7373+
Some(msgs::ChannelReady {
7374+
channel_id: self.context.channel_id(),
7375+
next_per_commitment_point: current,
7376+
short_channel_id_alias: Some(self.context.outbound_scid_alias),
7377+
})
7378+
} else {
7379+
log_debug!(logger, "Not producing channel_ready: the holder commitment point is not available.");
7380+
self.context.signer_pending_channel_ready = true;
7381+
None
73667382
}
73677383
}
73687384

@@ -10149,6 +10165,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1014910165
signer_pending_commitment_update: false,
1015010166
signer_pending_funding: false,
1015110167
signer_pending_closing: false,
10168+
signer_pending_channel_ready: false,
1015210169

1015310170
pending_update_fee,
1015410171
holding_cell_update_fee,

0 commit comments

Comments
 (0)