Skip to content

Commit d6d2671

Browse files
committed
Get per commitment point for channel ready using HolderCommitmentPoint
1 parent 507a4af commit d6d2671

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

lightning/src/ln/channel.rs

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,12 +5316,7 @@ impl<SP: Deref> Channel<SP> where
53165316
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53175317
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53185318
self.context.monitor_pending_channel_ready = false;
5319-
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);
5320-
Some(msgs::ChannelReady {
5321-
channel_id: self.context.channel_id(),
5322-
next_per_commitment_point,
5323-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5324-
})
5319+
Some(self.get_channel_ready())
53255320
} else { None };
53265321

53275322
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block_height, logger);
@@ -5407,7 +5402,7 @@ impl<SP: Deref> Channel<SP> where
54075402
self.context.get_funding_signed_msg(logger).1
54085403
} else { None };
54095404
let channel_ready = if funding_signed.is_some() {
5410-
self.check_get_channel_ready(0)
5405+
self.check_get_channel_ready(0, logger)
54115406
} else { None };
54125407

54135408
log_trace!(logger, "Signer unblocked with {} commitment_update, {} funding_signed and {} channel_ready",
@@ -5619,13 +5614,8 @@ impl<SP: Deref> Channel<SP> where
56195614
}
56205615

56215616
// We have OurChannelReady set!
5622-
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);
56235617
return Ok(ReestablishResponses {
5624-
channel_ready: Some(msgs::ChannelReady {
5625-
channel_id: self.context.channel_id(),
5626-
next_per_commitment_point,
5627-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5628-
}),
5618+
channel_ready: Some(self.get_channel_ready()),
56295619
raa: None, commitment_update: None,
56305620
order: RAACommitmentOrder::CommitmentFirst,
56315621
shutdown_msg, announcement_sigs,
@@ -5664,12 +5654,7 @@ impl<SP: Deref> Channel<SP> where
56645654

56655655
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
56665656
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5667-
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);
5668-
Some(msgs::ChannelReady {
5669-
channel_id: self.context.channel_id(),
5670-
next_per_commitment_point,
5671-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5672-
})
5657+
Some(self.get_channel_ready())
56735658
} else { None };
56745659

56755660
if msg.next_local_commitment_number == next_counterparty_commitment_number {
@@ -6489,7 +6474,9 @@ impl<SP: Deref> Channel<SP> where
64896474
self.context.channel_update_status = status;
64906475
}
64916476

6492-
fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
6477+
fn check_get_channel_ready<L: Deref>(&mut self, height: u32, logger: &L) -> Option<msgs::ChannelReady>
6478+
where L::Target: Logger
6479+
{
64936480
// Called:
64946481
// * always when a new block/transactions are confirmed with the new height
64956482
// * when funding is signed with a height of 0
@@ -6541,22 +6528,42 @@ impl<SP: Deref> Channel<SP> where
65416528
false
65426529
};
65436530

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

65626569
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
@@ -6623,7 +6630,7 @@ impl<SP: Deref> Channel<SP> where
66236630
// If we allow 1-conf funding, we may need to check for channel_ready here and
66246631
// send it immediately instead of waiting for a best_block_updated call (which
66256632
// may have already happened for this block).
6626-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
6633+
if let Some(channel_ready) = self.check_get_channel_ready(height, logger) {
66276634
log_info!(logger, "Sending a channel_ready to our peer for channel {}", &self.context.channel_id);
66286635
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger);
66296636
msgs = (Some(channel_ready), announcement_sigs);
@@ -6689,7 +6696,7 @@ impl<SP: Deref> Channel<SP> where
66896696

66906697
self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
66916698

6692-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
6699+
if let Some(channel_ready) = self.check_get_channel_ready(height, logger) {
66936700
let announcement_sigs = if let Some((chain_hash, node_signer, user_config)) = chain_node_signer {
66946701
self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger)
66956702
} else { None };
@@ -7876,7 +7883,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78767883
dual_funding_channel_context: None,
78777884
};
78787885

7879-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
7886+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
78807887
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
78817888
Ok((channel, channel_monitor))
78827889
}
@@ -8165,7 +8172,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81658172
#[cfg(any(dual_funding, splicing))]
81668173
dual_funding_channel_context: None,
81678174
};
8168-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
8175+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
81698176
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
81708177

81718178
Ok((channel, funding_signed, channel_monitor))
@@ -11274,6 +11281,6 @@ mod tests {
1127411281
// Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
1127511282
node_a_chan.set_batch_ready();
1127611283
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
11277-
assert!(node_a_chan.check_get_channel_ready(0).is_some());
11284+
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1127811285
}
1127911286
}

0 commit comments

Comments
 (0)