Skip to content

Commit b592686

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 c74ca29 commit b592686

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

lightning/src/ln/channel.rs

Lines changed: 33 additions & 25 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
//
@@ -2129,6 +2132,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
21292132
signer_pending_commitment_update: false,
21302133
signer_pending_funding: false,
21312134
signer_pending_closing: false,
2135+
signer_pending_channel_ready: false,
21322136

21332137

21342138
#[cfg(debug_assertions)]
@@ -2362,6 +2366,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
23622366
signer_pending_commitment_update: false,
23632367
signer_pending_funding: false,
23642368
signer_pending_closing: false,
2369+
signer_pending_channel_ready: false,
23652370

23662371
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
23672372
// 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,23 @@ 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
7356-
7357-
Some(self.get_channel_ready())
7354+
self.get_channel_ready(logger)
73587355
}
73597356

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),
7357+
fn get_channel_ready<L: Deref>(
7358+
&mut self, logger: &L
7359+
) -> Option<msgs::ChannelReady> where L::Target: Logger {
7360+
if let HolderCommitmentPoint::Available { current, .. } = self.holder_commitment_point {
7361+
self.context.signer_pending_channel_ready = false;
7362+
Some(msgs::ChannelReady {
7363+
channel_id: self.context.channel_id(),
7364+
next_per_commitment_point: current,
7365+
short_channel_id_alias: Some(self.context.outbound_scid_alias),
7366+
})
7367+
} else {
7368+
log_debug!(logger, "Not producing channel_ready: the holder commitment point is not available.");
7369+
self.context.signer_pending_channel_ready = true;
7370+
None
73667371
}
73677372
}
73687373

@@ -8519,7 +8524,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
85198524
holder_commitment_point,
85208525
};
85218526

8522-
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
8527+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some()
8528+
|| channel.context.signer_pending_channel_ready;
85238529
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
85248530
Ok((channel, channel_monitor))
85258531
}
@@ -8788,7 +8794,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
87888794
interactive_tx_signing_session: None,
87898795
holder_commitment_point,
87908796
};
8791-
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
8797+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some()
8798+
|| channel.context.signer_pending_channel_ready;
87928799
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
87938800

87948801
Ok((channel, funding_signed, channel_monitor))
@@ -10153,6 +10160,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1015310160
signer_pending_commitment_update: false,
1015410161
signer_pending_funding: false,
1015510162
signer_pending_closing: false,
10163+
signer_pending_channel_ready: false,
1015610164

1015710165
pending_update_fee,
1015810166
holding_cell_update_fee,

0 commit comments

Comments
 (0)