Skip to content

Commit d189cf0

Browse files
committed
Get per commitment point for channel ready using HolderCommitmentPoint
1 parent 7a115d7 commit d189cf0

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

lightning/src/ln/channel.rs

+42-40
Original file line numberDiff line numberDiff line change
@@ -5315,12 +5315,7 @@ impl<SP: Deref> Channel<SP> where
53155315
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53165316
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53175317
self.context.monitor_pending_channel_ready = false;
5318-
let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number(), &self.context.secp_ctx);
5319-
Some(msgs::ChannelReady {
5320-
channel_id: self.context.channel_id(),
5321-
next_per_commitment_point,
5322-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5323-
})
5318+
Some(self.get_channel_ready())
53245319
} else { None };
53255320

53265321
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block_height, logger);
@@ -5406,7 +5401,7 @@ impl<SP: Deref> Channel<SP> where
54065401
self.context.get_funding_signed_msg(logger).1
54075402
} else { None };
54085403
let channel_ready = if funding_signed.is_some() {
5409-
self.check_get_channel_ready(0)
5404+
self.check_get_channel_ready(0, logger)
54105405
} else { None };
54115406

54125407
log_trace!(logger, "Signer unblocked with {} commitment_update, {} funding_signed and {} channel_ready",
@@ -5618,13 +5613,8 @@ impl<SP: Deref> Channel<SP> where
56185613
}
56195614

56205615
// We have OurChannelReady set!
5621-
let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number(), &self.context.secp_ctx);
56225616
return Ok(ReestablishResponses {
5623-
channel_ready: Some(msgs::ChannelReady {
5624-
channel_id: self.context.channel_id(),
5625-
next_per_commitment_point,
5626-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5627-
}),
5617+
channel_ready: Some(self.get_channel_ready()),
56285618
raa: None, commitment_update: None,
56295619
order: RAACommitmentOrder::CommitmentFirst,
56305620
shutdown_msg, announcement_sigs,
@@ -5663,12 +5653,7 @@ impl<SP: Deref> Channel<SP> where
56635653

56645654
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
56655655
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5666-
let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.holder_commitment_point.transaction_number(), &self.context.secp_ctx);
5667-
Some(msgs::ChannelReady {
5668-
channel_id: self.context.channel_id(),
5669-
next_per_commitment_point,
5670-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5671-
})
5656+
Some(self.get_channel_ready())
56725657
} else { None };
56735658

56745659
if msg.next_local_commitment_number == next_counterparty_commitment_number {
@@ -6488,7 +6473,9 @@ impl<SP: Deref> Channel<SP> where
64886473
self.context.channel_update_status = status;
64896474
}
64906475

6491-
fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
6476+
fn check_get_channel_ready<L: Deref>(&mut self, height: u32, logger: &L) -> Option<msgs::ChannelReady>
6477+
where L::Target: Logger
6478+
{
64926479
// Called:
64936480
// * always when a new block/transactions are confirmed with the new height
64946481
// * when funding is signed with a height of 0
@@ -6508,6 +6495,8 @@ impl<SP: Deref> Channel<SP> where
65086495
// If we're still pending the signature on a funding transaction, then we're not ready to send a
65096496
// channel_ready yet.
65106497
if self.context.signer_pending_funding {
6498+
// TODO: set signer_pending_channel_ready
6499+
log_debug!(logger, "Can't produce channel_ready: the signer is pending funding.");
65116500
return None;
65126501
}
65136502

@@ -6540,22 +6529,35 @@ impl<SP: Deref> Channel<SP> where
65406529
false
65416530
};
65426531

6543-
if need_commitment_update {
6544-
if !self.context.channel_state.is_monitor_update_in_progress() {
6545-
if !self.context.channel_state.is_peer_disconnected() {
6546-
let next_per_commitment_point =
6547-
self.context.holder_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
6548-
return Some(msgs::ChannelReady {
6549-
channel_id: self.context.channel_id,
6550-
next_per_commitment_point,
6551-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
6552-
});
6553-
}
6554-
} else {
6555-
self.context.monitor_pending_channel_ready = true;
6556-
}
6532+
if !need_commitment_update {
6533+
log_debug!(logger, "Not producing channel_ready: we do not need a commitment update");
6534+
return None;
6535+
}
6536+
6537+
if self.context.channel_state.is_monitor_update_in_progress() {
6538+
log_debug!(logger, "Not producing channel_ready: a monitor update is in progress. Setting monitor_pending_channel_ready.");
6539+
self.context.monitor_pending_channel_ready = true;
6540+
return None;
6541+
}
6542+
6543+
if self.context.channel_state.is_peer_disconnected() {
6544+
log_debug!(logger, "Not producing channel_ready: the peer is disconnected.");
6545+
return None;
6546+
}
6547+
6548+
// TODO: when get_per_commiment_point becomes async, check if the point is
6549+
// available, if not, set signer_pending_channel_ready and return None
6550+
6551+
Some(self.get_channel_ready())
6552+
}
6553+
6554+
fn get_channel_ready(&self) -> msgs::ChannelReady {
6555+
debug_assert!(self.context.holder_commitment_point.is_available());
6556+
msgs::ChannelReady {
6557+
channel_id: self.context.channel_id(),
6558+
next_per_commitment_point: self.context.holder_commitment_point.current_point(),
6559+
short_channel_id_alias: Some(self.context.outbound_scid_alias),
65576560
}
6558-
None
65596561
}
65606562

65616563
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
@@ -6622,7 +6624,7 @@ impl<SP: Deref> Channel<SP> where
66226624
// If we allow 1-conf funding, we may need to check for channel_ready here and
66236625
// send it immediately instead of waiting for a best_block_updated call (which
66246626
// may have already happened for this block).
6625-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
6627+
if let Some(channel_ready) = self.check_get_channel_ready(height, logger) {
66266628
log_info!(logger, "Sending a channel_ready to our peer for channel {}", &self.context.channel_id);
66276629
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger);
66286630
msgs = (Some(channel_ready), announcement_sigs);
@@ -6688,7 +6690,7 @@ impl<SP: Deref> Channel<SP> where
66886690

66896691
self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
66906692

6691-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
6693+
if let Some(channel_ready) = self.check_get_channel_ready(height, logger) {
66926694
let announcement_sigs = if let Some((chain_hash, node_signer, user_config)) = chain_node_signer {
66936695
self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger)
66946696
} else { None };
@@ -7873,7 +7875,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78737875
dual_funding_channel_context: None,
78747876
};
78757877

7876-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
7878+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
78777879
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
78787880
Ok((channel, channel_monitor))
78797881
}
@@ -8162,7 +8164,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81628164
#[cfg(any(dual_funding, splicing))]
81638165
dual_funding_channel_context: None,
81648166
};
8165-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
8167+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
81668168
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
81678169

81688170
Ok((channel, funding_signed, channel_monitor))
@@ -11269,6 +11271,6 @@ mod tests {
1126911271
// Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
1127011272
node_a_chan.set_batch_ready();
1127111273
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
11272-
assert!(node_a_chan.check_get_channel_ready(0).is_some());
11274+
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1127311275
}
1127411276
}

0 commit comments

Comments
 (0)