Skip to content

Commit 0c4410b

Browse files
committed
Allow failing revoke_and_ack if commitment point is not available
1 parent 8830e1c commit 0c4410b

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

lightning/src/ln/channel.rs

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5363,16 +5363,21 @@ impl<SP: Deref> Channel<SP> where
53635363
}
53645364

53655365
let mut raa = if self.context.monitor_pending_revoke_and_ack {
5366-
Some(self.get_last_revoke_and_ack())
5366+
self.get_last_revoke_and_ack(logger)
53675367
} else { None };
5368-
let commitment_update = if self.context.monitor_pending_commitment_signed {
5368+
let mut commitment_update = if self.context.monitor_pending_commitment_signed {
53695369
self.get_last_commitment_update_for_send(logger).ok()
53705370
} else { None };
53715371
if self.context.resend_order == RAACommitmentOrder::CommitmentFirst
53725372
&& self.context.signer_pending_commitment_update && raa.is_some() {
53735373
self.context.signer_pending_revoke_and_ack = true;
53745374
raa = None;
53755375
}
5376+
if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
5377+
&& self.context.signer_pending_revoke_and_ack && commitment_update.is_some() {
5378+
self.context.signer_pending_commitment_update = true;
5379+
commitment_update = None;
5380+
}
53765381

53775382
if commitment_update.is_some() {
53785383
self.mark_awaiting_response();
@@ -5443,6 +5448,10 @@ impl<SP: Deref> Channel<SP> where
54435448
/// blocked.
54445449
#[cfg(async_signing)]
54455450
pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
5451+
if !self.context.holder_commitment_point.is_available() {
5452+
log_trace!(logger, "Attempting to update holder per-commitment point...");
5453+
self.context.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
5454+
}
54465455
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
54475456
self.context.get_funding_signed_msg(logger).1
54485457
} else { None };
@@ -5456,7 +5465,7 @@ impl<SP: Deref> Channel<SP> where
54565465
} else { None };
54575466
let mut revoke_and_ack = if self.context.signer_pending_revoke_and_ack {
54585467
log_trace!(logger, "Attempting to generate pending revoke and ack...");
5459-
Some(self.get_last_revoke_and_ack())
5468+
self.get_last_revoke_and_ack(logger)
54605469
} else { None };
54615470

54625471
if self.context.resend_order == RAACommitmentOrder::CommitmentFirst
@@ -5488,19 +5497,35 @@ impl<SP: Deref> Channel<SP> where
54885497
}
54895498
}
54905499

5491-
fn get_last_revoke_and_ack(&mut self) -> msgs::RevokeAndACK {
5492-
debug_assert!(self.context.holder_commitment_point.transaction_number() <= INITIAL_COMMITMENT_NUMBER + 2);
5493-
// TODO: handle non-available case when get_per_commitment_point becomes async
5494-
debug_assert!(self.context.holder_commitment_point.is_available());
5495-
let next_per_commitment_point = self.context.holder_commitment_point.current_point();
5500+
fn get_last_revoke_and_ack<L: Deref>(&mut self, _logger: &L) -> Option<msgs::RevokeAndACK> where L::Target: Logger {
5501+
debug_assert!(self.context.holder_commitment_point.transaction_number() <= INITIAL_COMMITMENT_NUMBER - 2);
5502+
self.context.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, _logger);
54965503
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.holder_commitment_point.transaction_number() + 2);
5497-
self.context.signer_pending_revoke_and_ack = false;
5498-
msgs::RevokeAndACK {
5499-
channel_id: self.context.channel_id,
5500-
per_commitment_secret,
5501-
next_per_commitment_point,
5502-
#[cfg(taproot)]
5503-
next_local_nonce: None,
5504+
if let HolderCommitmentPoint::Available { transaction_number: _, current, next: _ } = self.context.holder_commitment_point {
5505+
self.context.signer_pending_revoke_and_ack = false;
5506+
Some(msgs::RevokeAndACK {
5507+
channel_id: self.context.channel_id,
5508+
per_commitment_secret,
5509+
next_per_commitment_point: current,
5510+
#[cfg(taproot)]
5511+
next_local_nonce: None,
5512+
})
5513+
} else {
5514+
#[cfg(not(async_signing))] {
5515+
panic!("Holder commitment point must be Available when generating revoke_and_ack");
5516+
}
5517+
#[cfg(async_signing)] {
5518+
// Technically if we're at HolderCommitmentPoint::PendingNext,
5519+
// we have a commitment point ready to send in an RAA, however we
5520+
// choose to wait since if we send RAA now, we could get another
5521+
// CS before we have any commitment point available. Blocking our
5522+
// RAA here is a convenient way to make sure that post-funding
5523+
// we're only ever waiting on one commitment point at a time.
5524+
log_trace!(_logger, "Last revoke-and-ack pending in channel {} for sequence {} because the next per-commitment point is not available",
5525+
&self.context.channel_id(), self.context.holder_commitment_point.transaction_number());
5526+
self.context.signer_pending_revoke_and_ack = true;
5527+
None
5528+
}
55045529
}
55055530
}
55065531

@@ -5708,7 +5733,7 @@ impl<SP: Deref> Channel<SP> where
57085733
self.context.monitor_pending_revoke_and_ack = true;
57095734
None
57105735
} else {
5711-
Some(self.get_last_revoke_and_ack())
5736+
self.get_last_revoke_and_ack(logger)
57125737
}
57135738
} else {
57145739
debug_assert!(false, "All values should have been handled in the four cases above");
@@ -5735,7 +5760,7 @@ impl<SP: Deref> Channel<SP> where
57355760
} else { None };
57365761

57375762
if msg.next_local_commitment_number == next_counterparty_commitment_number {
5738-
if required_revoke.is_some() {
5763+
if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
57395764
log_debug!(logger, "Reconnected channel {} with only lost outbound RAA", &self.context.channel_id());
57405765
} else {
57415766
log_debug!(logger, "Reconnected channel {} with no loss", &self.context.channel_id());
@@ -5748,7 +5773,7 @@ impl<SP: Deref> Channel<SP> where
57485773
order: self.context.resend_order.clone(),
57495774
})
57505775
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
5751-
if required_revoke.is_some() {
5776+
if required_revoke.is_some() || self.context.signer_pending_revoke_and_ack {
57525777
log_debug!(logger, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx", &self.context.channel_id());
57535778
} else {
57545779
log_debug!(logger, "Reconnected channel {} with only lost remote commitment tx", &self.context.channel_id());
@@ -5762,7 +5787,14 @@ impl<SP: Deref> Channel<SP> where
57625787
order: self.context.resend_order.clone(),
57635788
})
57645789
} else {
5765-
let commitment_update = self.get_last_commitment_update_for_send(logger).ok();
5790+
let commitment_update = if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
5791+
&& self.context.signer_pending_revoke_and_ack {
5792+
log_trace!(logger, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx, but unable to send due to resend order, waiting on signer for revoke and ack", &self.context.channel_id());
5793+
self.context.signer_pending_commitment_update = true;
5794+
None
5795+
} else {
5796+
self.get_last_commitment_update_for_send(logger).ok()
5797+
};
57665798
let raa = if self.context.resend_order == RAACommitmentOrder::CommitmentFirst
57675799
&& self.context.signer_pending_commitment_update && required_revoke.is_some() {
57685800
log_trace!(logger, "Reconnected channel {} with lost outbound RAA and lost remote commitment tx, but unable to send due to resend order, waiting on signer for commitment update", &self.context.channel_id());

0 commit comments

Comments
 (0)