Skip to content

Commit 74a0b48

Browse files
committed
Get per commitment point for channel ready using HolderCommitmentPoint
1 parent 889e178 commit 74a0b48

File tree

1 file changed

+46
-40
lines changed

1 file changed

+46
-40
lines changed

lightning/src/ln/channel.rs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5306,12 +5306,7 @@ impl<SP: Deref> Channel<SP> where
53065306
assert!(!self.context.is_outbound() || self.context.minimum_depth == Some(0),
53075307
"Funding transaction broadcast by the local client before it should have - LDK didn't do it!");
53085308
self.context.monitor_pending_channel_ready = false;
5309-
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);
5310-
Some(msgs::ChannelReady {
5311-
channel_id: self.context.channel_id(),
5312-
next_per_commitment_point,
5313-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5314-
})
5309+
Some(self.get_channel_ready())
53155310
} else { None };
53165311

53175312
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, best_block_height, logger);
@@ -5397,7 +5392,7 @@ impl<SP: Deref> Channel<SP> where
53975392
self.context.get_funding_signed_msg(logger).1
53985393
} else { None };
53995394
let channel_ready = if funding_signed.is_some() {
5400-
self.check_get_channel_ready(0)
5395+
self.check_get_channel_ready(0, logger)
54015396
} else { None };
54025397

54035398
log_trace!(logger, "Signer unblocked with {} commitment_update, {} funding_signed and {} channel_ready",
@@ -5609,13 +5604,8 @@ impl<SP: Deref> Channel<SP> where
56095604
}
56105605

56115606
// We have OurChannelReady set!
5612-
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);
56135607
return Ok(ReestablishResponses {
5614-
channel_ready: Some(msgs::ChannelReady {
5615-
channel_id: self.context.channel_id(),
5616-
next_per_commitment_point,
5617-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5618-
}),
5608+
channel_ready: Some(self.get_channel_ready()),
56195609
raa: None, commitment_update: None,
56205610
order: RAACommitmentOrder::CommitmentFirst,
56215611
shutdown_msg, announcement_sigs,
@@ -5654,12 +5644,7 @@ impl<SP: Deref> Channel<SP> where
56545644

56555645
let channel_ready = if msg.next_local_commitment_number == 1 && INITIAL_COMMITMENT_NUMBER - self.context.holder_commitment_point.transaction_number() == 1 {
56565646
// We should never have to worry about MonitorUpdateInProgress resending ChannelReady
5657-
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);
5658-
Some(msgs::ChannelReady {
5659-
channel_id: self.context.channel_id(),
5660-
next_per_commitment_point,
5661-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
5662-
})
5647+
Some(self.get_channel_ready())
56635648
} else { None };
56645649

56655650
if msg.next_local_commitment_number == next_counterparty_commitment_number {
@@ -6479,7 +6464,9 @@ impl<SP: Deref> Channel<SP> where
64796464
self.context.channel_update_status = status;
64806465
}
64816466

6482-
fn check_get_channel_ready(&mut self, height: u32) -> Option<msgs::ChannelReady> {
6467+
fn check_get_channel_ready<L: Deref>(&mut self, height: u32, logger: &L) -> Option<msgs::ChannelReady>
6468+
where L::Target: Logger
6469+
{
64836470
// Called:
64846471
// * always when a new block/transactions are confirmed with the new height
64856472
// * when funding is signed with a height of 0
@@ -6531,22 +6518,41 @@ impl<SP: Deref> Channel<SP> where
65316518
false
65326519
};
65336520

6534-
if need_commitment_update {
6535-
if !self.context.channel_state.is_monitor_update_in_progress() {
6536-
if !self.context.channel_state.is_peer_disconnected() {
6537-
let next_per_commitment_point =
6538-
self.context.holder_signer.as_ref().get_per_commitment_point(INITIAL_COMMITMENT_NUMBER - 1, &self.context.secp_ctx);
6539-
return Some(msgs::ChannelReady {
6540-
channel_id: self.context.channel_id,
6541-
next_per_commitment_point,
6542-
short_channel_id_alias: Some(self.context.outbound_scid_alias),
6543-
});
6544-
}
6545-
} else {
6546-
self.context.monitor_pending_channel_ready = true;
6547-
}
6521+
if !need_commitment_update {
6522+
log_debug!(logger, "Not producing channel_ready: we do not need a commitment update");
6523+
return None;
6524+
}
6525+
6526+
if self.context.channel_state.is_monitor_update_in_progress() {
6527+
log_debug!(logger, "Not producing channel_ready: a monitor update is in progress. Setting monitor_pending_channel_ready.");
6528+
self.context.monitor_pending_channel_ready = true;
6529+
return None;
6530+
}
6531+
6532+
if self.context.channel_state.is_peer_disconnected() {
6533+
log_debug!(logger, "Not producing channel_ready: the peer is disconnected.");
6534+
return None;
6535+
}
6536+
6537+
if self.context.signer_pending_funding {
6538+
// TODO: set signer_pending_channel_ready
6539+
log_debug!(logger, "Can't produce channel_ready: the signer is pending funding.");
6540+
return None;
6541+
}
6542+
6543+
// TODO: when get_per_commiment_point becomes async, check if the point is
6544+
// available, if not, set signer_pending_channel_ready and return None
6545+
6546+
Some(self.get_channel_ready())
6547+
}
6548+
6549+
fn get_channel_ready(&self) -> msgs::ChannelReady {
6550+
debug_assert!(self.context.holder_commitment_point.is_available());
6551+
msgs::ChannelReady {
6552+
channel_id: self.context.channel_id(),
6553+
next_per_commitment_point: self.context.holder_commitment_point.current_point(),
6554+
short_channel_id_alias: Some(self.context.outbound_scid_alias),
65486555
}
6549-
None
65506556
}
65516557

65526558
/// When a transaction is confirmed, we check whether it is or spends the funding transaction
@@ -6613,7 +6619,7 @@ impl<SP: Deref> Channel<SP> where
66136619
// If we allow 1-conf funding, we may need to check for channel_ready here and
66146620
// send it immediately instead of waiting for a best_block_updated call (which
66156621
// may have already happened for this block).
6616-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
6622+
if let Some(channel_ready) = self.check_get_channel_ready(height, logger) {
66176623
log_info!(logger, "Sending a channel_ready to our peer for channel {}", &self.context.channel_id);
66186624
let announcement_sigs = self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger);
66196625
msgs = (Some(channel_ready), announcement_sigs);
@@ -6679,7 +6685,7 @@ impl<SP: Deref> Channel<SP> where
66796685

66806686
self.context.update_time_counter = cmp::max(self.context.update_time_counter, highest_header_time);
66816687

6682-
if let Some(channel_ready) = self.check_get_channel_ready(height) {
6688+
if let Some(channel_ready) = self.check_get_channel_ready(height, logger) {
66836689
let announcement_sigs = if let Some((chain_hash, node_signer, user_config)) = chain_node_signer {
66846690
self.get_announcement_sigs(node_signer, chain_hash, user_config, height, logger)
66856691
} else { None };
@@ -7864,7 +7870,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
78647870
dual_funding_channel_context: None,
78657871
};
78667872

7867-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
7873+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
78687874
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
78697875
Ok((channel, channel_monitor))
78707876
}
@@ -8153,7 +8159,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
81538159
#[cfg(any(dual_funding, splicing))]
81548160
dual_funding_channel_context: None,
81558161
};
8156-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
8162+
let need_channel_ready = channel.check_get_channel_ready(0, logger).is_some();
81578163
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
81588164

81598165
Ok((channel, funding_signed, channel_monitor))
@@ -11260,6 +11266,6 @@ mod tests {
1126011266
// Clear the ChannelState::WaitingForBatch only when called by ChannelManager.
1126111267
node_a_chan.set_batch_ready();
1126211268
assert_eq!(node_a_chan.context.channel_state, ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY));
11263-
assert!(node_a_chan.check_get_channel_ready(0).is_some());
11269+
assert!(node_a_chan.check_get_channel_ready(0, &&logger).is_some());
1126411270
}
1126511271
}

0 commit comments

Comments
 (0)