Skip to content

Commit e64af01

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 8058a60 commit e64af01

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

lightning/src/ln/channel.rs

+33-25
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`.
@@ -5997,7 +6002,7 @@ impl<SP: Deref> Channel<SP> where
59976002
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
59986003
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
59996004
self.context.monitor_pending_channel_ready = false;
6000-
Some(self.get_channel_ready())
6005+
self.get_channel_ready(logger)
60016006
} else { None };
60026007

60036008
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block_height, logger);
@@ -6120,8 +6125,11 @@ impl<SP: Deref> Channel<SP> where
61206125
let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
61216126
self.context.get_funding_signed_msg(logger, counterparty_initial_commitment_tx)
61226127
} else { None };
6123-
let channel_ready = if funding_signed.is_some() {
6124-
self.check_get_channel_ready(0, logger)
6128+
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
6129+
// funding.
6130+
let channel_ready = if self.context.signer_pending_channel_ready && !self.context.signer_pending_funding {
6131+
log_trace!(logger, "Attempting to generate pending channel_ready...");
6132+
self.get_channel_ready(logger)
61256133
} else { None };
61266134

61276135
let mut commitment_update = if self.context.signer_pending_commitment_update {
@@ -6428,7 +6436,7 @@ impl<SP: Deref> Channel<SP> where
64286436

64296437
// We have OurChannelReady set!
64306438
return Ok(ReestablishResponses {
6431-
channel_ready: Some(self.get_channel_ready()),
6439+
channel_ready: self.get_channel_ready(logger),
64326440
raa: None, commitment_update: None,
64336441
order: RAACommitmentOrder::CommitmentFirst,
64346442
shutdown_msg, announcement_sigs,
@@ -6467,7 +6475,7 @@ impl<SP: Deref> Channel<SP> where
64676475

64686476
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.holder_commitment_point.transaction_number() == 1 {
64696477
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
6470-
Some(self.get_channel_ready())
6478+
self.get_channel_ready(logger)
64716479
} else { None };
64726480

64736481
if msg.next_local_commitment_number == next_counterparty_commitment_number {
@@ -7322,14 +7330,6 @@ impl<SP: Deref> Channel<SP> where
73227330
return None;
73237331
}
73247332

7325-
// If we're still pending the signature on a funding transaction, then we're not ready to send a
7326-
// channel_ready yet.
7327-
if self.context.signer_pending_funding {
7328-
// TODO: set signer_pending_channel_ready
7329-
log_debug!(logger, "Can't produce channel_ready: the signer is pending funding.");
7330-
return None;
7331-
}
7332-
73337333
// Note that we don't include ChannelState::WaitingForBatch as we don't want to send
73347334
// channel_ready until the entire batch is ready.
73357335
let need_commitment_update = if matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(f) if f.clone().clear(FundedStateFlags::ALL.into()).is_empty()) {
@@ -7375,18 +7375,23 @@ impl<SP: Deref> Channel<SP> where
73757375
return None;
73767376
}
73777377

7378-
// TODO: when get_per_commiment_point becomes async, check if the point is
7379-
// available, if not, set signer_pending_channel_ready and return None
7380-
7381-
Some(self.get_channel_ready())
7378+
self.get_channel_ready(logger)
73827379
}
73837380

7384-
fn get_channel_ready(&self) -> msgs::ChannelReady {
7385-
debug_assert!(self.holder_commitment_point.is_available());
7386-
msgs::ChannelReady {
7387-
channel_id: self.context.channel_id(),
7388-
next_per_commitment_point: self.holder_commitment_point.current_point(),
7389-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
7381+
fn get_channel_ready<L: Deref>(
7382+
&mut self, logger: &L
7383+
) -> Option<msgs::ChannelReady> where L::Target: Logger {
7384+
if let HolderCommitmentPoint::Available { current, .. } = self.holder_commitment_point {
7385+
self.context.signer_pending_channel_ready = false;
7386+
Some(msgs::ChannelReady {
7387+
channel_id: self.context.channel_id(),
7388+
next_per_commitment_point: current,
7389+
short_channel_id_alias: Some(self.context.outbound_scid_alias),
7390+
})
7391+
} else {
7392+
log_debug!(logger, "Not producing channel_ready: the holder commitment point is not available.");
7393+
self.context.signer_pending_channel_ready = true;
7394+
None
73907395
}
73917396
}
73927397

@@ -8549,7 +8554,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
85498554
holder_commitment_point,
85508555
};
85518556

8552-
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
8557+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some()
8558+
|| channel.context.signer_pending_channel_ready;
85538559
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
85548560
Ok((channel, channel_monitor))
85558561
}
@@ -8818,7 +8824,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
88188824
interactive_tx_signing_session: None,
88198825
holder_commitment_point,
88208826
};
8821-
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
8827+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some()
8828+
|| channel.context.signer_pending_channel_ready;
88228829
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
88238830

88248831
Ok((channel, funding_signed, channel_monitor))
@@ -10182,6 +10189,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
1018210189
signer_pending_commitment_update: false,
1018310190
signer_pending_funding: false,
1018410191
signer_pending_closing: false,
10192+
signer_pending_channel_ready: false,
1018510193

1018610194
pending_update_fee,
1018710195
holding_cell_update_fee,

0 commit comments

Comments
 (0)