Skip to content

Commit 329175b

Browse files
committed
Fix commitment signed
When monitor updating is restored, we may have a situation where the signer still has not returned a signature for the counterparty commitment. If this is the case, note that the _signer_ is still pending a commitment update event though the _monitor_ is not. Similarly, only allow the revoke-and-ack to be generated in the case that we're not still pending the counterparty commitment signature. If the monitor was pending a revoke-and-ack and the commitment update is not available, then note that _signer_ is still pending a revoke-and-ack. Ensure that we honor the ordering specified by the channel.
1 parent c20f6aa commit 329175b

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

lightning/src/ln/async_signer_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn test_async_commitment_signature_for_peer_disconnect() {
344344
dst.node.peer_disconnected(&src.node.get_our_node_id());
345345
let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]);
346346
reconnect_args.send_channel_ready = (false, false);
347-
reconnect_args.pending_raa = (true, false);
347+
reconnect_args.pending_raa = (false, false);
348348
reconnect_nodes(reconnect_args);
349349

350350
// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.

lightning/src/ln/channel.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ pub(super) struct MonitorRestoreUpdates {
518518
/// The return value of `signer_maybe_unblocked`
519519
pub(super) struct SignerResumeUpdates {
520520
pub commitment_update: Option<msgs::CommitmentUpdate>,
521+
pub raa: Option<msgs::RevokeAndACK>,
522+
pub order: RAACommitmentOrder,
521523
pub funding_signed: Option<msgs::FundingSigned>,
522524
pub funding_created: Option<msgs::FundingCreated>,
523525
pub channel_ready: Option<msgs::ChannelReady>,
@@ -744,6 +746,7 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
744746
/// This flag is set in such a case. Note that we don't need to persist this as we'll end up
745747
/// setting it again as a side-effect of [`Channel::channel_reestablish`].
746748
signer_pending_commitment_update: bool,
749+
signer_pending_revoke_and_ack: bool,
747750
/// Similar to [`Self::signer_pending_commitment_update`] but we're waiting to send either a
748751
/// [`msgs::FundingCreated`] or [`msgs::FundingSigned`] depending on if this channel is
749752
/// outbound or inbound.
@@ -3143,6 +3146,7 @@ impl<SP: Deref> Channel<SP> where
31433146
self.context.cur_holder_commitment_transaction_number -= 1;
31443147
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
31453148
// build_commitment_no_status_check() next which will reset this to RAAFirst.
3149+
log_debug!(logger, "setting resend_order to CommitmentFirst");
31463150
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
31473151

31483152
if (self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0 {
@@ -3828,7 +3832,7 @@ impl<SP: Deref> Channel<SP> where
38283832
}
38293833

38303834
let raa = if self.context.monitor_pending_revoke_and_ack {
3831-
Some(self.get_last_revoke_and_ack())
3835+
self.get_last_revoke_and_ack(logger)
38323836
} else { None };
38333837
let commitment_update = if self.context.monitor_pending_commitment_signed {
38343838
self.get_last_commitment_update_for_send(logger).ok()
@@ -3837,13 +3841,25 @@ impl<SP: Deref> Channel<SP> where
38373841
self.mark_awaiting_response();
38383842
}
38393843

3844+
if self.context.monitor_pending_commitment_signed && commitment_update.is_none() {
3845+
log_debug!(logger, "Monitor was pending_commitment_signed with no commitment update available; setting signer_pending_commitment_update = true");
3846+
self.context.signer_pending_commitment_update = true;
3847+
}
3848+
if self.context.monitor_pending_revoke_and_ack && raa.is_none() {
3849+
log_debug!(logger, "Monitor was pending_revoke_and_ack with no RAA available; setting signer_pending_revoke_and_ack = true");
3850+
self.context.signer_pending_revoke_and_ack = true;
3851+
}
3852+
38403853
self.context.monitor_pending_revoke_and_ack = false;
38413854
self.context.monitor_pending_commitment_signed = false;
3855+
38423856
let order = self.context.resend_order.clone();
3843-
log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA, with {} first",
3857+
log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA{}",
38443858
&self.context.channel_id(), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
38453859
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
3846-
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
3860+
if commitment_update.is_some() && raa.is_some() {
3861+
match order { RAACommitmentOrder::CommitmentFirst => ", with commitment first", RAACommitmentOrder::RevokeAndACKFirst => ", with RAA first"}
3862+
} else { "" });
38473863
MonitorRestoreUpdates {
38483864
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
38493865
}
@@ -3890,6 +3906,9 @@ impl<SP: Deref> Channel<SP> where
38903906
let commitment_update = if self.context.signer_pending_commitment_update {
38913907
self.get_last_commitment_update_for_send(logger).ok()
38923908
} else { None };
3909+
let raa = if self.context.signer_pending_revoke_and_ack {
3910+
self.get_last_revoke_and_ack(logger)
3911+
} else { None };
38933912
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
38943913
self.context.get_funding_signed_msg(logger).1
38953914
} else { None };
@@ -3899,24 +3918,48 @@ impl<SP: Deref> Channel<SP> where
38993918
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
39003919
self.context.get_funding_created_msg(logger)
39013920
} else { None };
3921+
let order = self.context.resend_order.clone();
3922+
3923+
log_debug!(logger, "Signing unblocked in channel {} at sequence {} resulting in {} commitment update, {} RAA{}, {} funding signed, and {} funding created",
3924+
&self.context.channel_id(), self.context.cur_holder_commitment_transaction_number,
3925+
if commitment_update.is_some() { "a" } else { "no" },
3926+
if raa.is_some() { "an" } else { "no" },
3927+
if commitment_update.is_some() && raa.is_some() {
3928+
if order == RAACommitmentOrder::CommitmentFirst { " (commitment first)" } else { " (RAA first)" }
3929+
} else { "" },
3930+
if funding_signed.is_some() { "a" } else { "no" },
3931+
if funding_created.is_some() { "a" } else { "no" });
3932+
39023933
SignerResumeUpdates {
39033934
commitment_update,
3935+
raa,
3936+
order,
39043937
funding_signed,
39053938
funding_created,
39063939
channel_ready,
39073940
}
39083941
}
39093942

3910-
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3943+
fn get_last_revoke_and_ack<L: Deref>(&mut self, logger: &L) -> Option<msgs::RevokeAndACK> where L::Target: Logger {
3944+
if self.context.signer_pending_commitment_update {
3945+
log_debug!(logger, "Can't generate revoke-and-ack in channel {} while pending commitment update",
3946+
self.context.channel_id());
3947+
return None;
3948+
}
3949+
3950+
log_debug!(logger, "Regenerated last revoke-and-ack in channel {} for next per-commitment point sequence number {}, releasing secret for {}",
3951+
&self.context.channel_id(), self.context.cur_holder_commitment_transaction_number,
3952+
self.context.cur_holder_commitment_transaction_number + 2);
3953+
self.context.signer_pending_revoke_and_ack = false;
39113954
let next_per_commitment_point = self.context.holder_signer.as_ref().get_per_commitment_point(self.context.cur_holder_commitment_transaction_number, &self.context.secp_ctx);
39123955
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3913-
msgs::RevokeAndACK {
3956+
Some(msgs::RevokeAndACK {
39143957
channel_id: self.context.channel_id,
39153958
per_commitment_secret,
39163959
next_per_commitment_point,
39173960
#[cfg(taproot)]
39183961
next_local_nonce: None,
3919-
}
3962+
})
39203963
}
39213964

39223965
/// Gets the last commitment update for immediate sending to our peer.
@@ -4112,7 +4155,7 @@ impl<SP: Deref> Channel<SP> where
41124155
self.context.monitor_pending_revoke_and_ack = true;
41134156
None
41144157
} else {
4115-
Some(self.get_last_revoke_and_ack())
4158+
self.get_last_revoke_and_ack(logger)
41164159
}
41174160
} else {
41184161
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old local commitment transaction".to_owned()));
@@ -5447,6 +5490,7 @@ impl<SP: Deref> Channel<SP> where
54475490
self.context.pending_update_fee = None;
54485491
}
54495492
}
5493+
log_debug!(logger, "setting resend_order to RevokeAndACKFirst");
54505494
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
54515495

54525496
let (mut htlcs_ref, counterparty_commitment_tx) =
@@ -5841,6 +5885,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
58415885
monitor_pending_finalized_fulfills: Vec::new(),
58425886

58435887
signer_pending_commitment_update: false,
5888+
signer_pending_revoke_and_ack: false,
58445889
signer_pending_funding: false,
58455890

58465891
#[cfg(debug_assertions)]
@@ -6463,6 +6508,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
64636508
monitor_pending_finalized_fulfills: Vec::new(),
64646509

64656510
signer_pending_commitment_update: false,
6511+
signer_pending_revoke_and_ack: false,
64666512
signer_pending_funding: false,
64676513

64686514
#[cfg(debug_assertions)]
@@ -7531,6 +7577,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
75317577
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
75327578

75337579
signer_pending_commitment_update: false,
7580+
signer_pending_revoke_and_ack: false,
75347581
signer_pending_funding: false,
75357582

75367583
pending_update_fee,

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6803,12 +6803,20 @@ where
68036803
let node_id = phase.context().get_counterparty_node_id();
68046804
if let ChannelPhase::Funded(chan) = phase {
68056805
let msgs = chan.signer_maybe_unblocked(&self.logger);
6806-
if let Some(updates) = msgs.commitment_update {
6807-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
6808-
node_id,
6809-
updates,
6810-
});
6811-
}
6806+
let mut evs = match (msgs.commitment_update, msgs.raa) {
6807+
(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::CommitmentFirst => vec![
6808+
events::MessageSendEvent::UpdateHTLCs { node_id, updates: cu },
6809+
events::MessageSendEvent::SendRevokeAndACK { node_id, msg: raa },
6810+
],
6811+
(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::RevokeAndACKFirst => vec![
6812+
events::MessageSendEvent::SendRevokeAndACK { node_id, msg: raa },
6813+
events::MessageSendEvent::UpdateHTLCs { node_id, updates: cu },
6814+
],
6815+
(Some(cu), _) => vec![events::MessageSendEvent::UpdateHTLCs { node_id, updates: cu }],
6816+
(_, Some(raa)) => vec![events::MessageSendEvent::SendRevokeAndACK { node_id, msg: raa }],
6817+
(_, _) => vec![],
6818+
};
6819+
pending_msg_events.append(&mut evs);
68126820
if let Some(msg) = msgs.funding_signed {
68136821
pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
68146822
node_id,

0 commit comments

Comments
 (0)