Skip to content

Commit 6be9aec

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 71ea826 commit 6be9aec

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

lightning/src/ln/async_signer_tests.rs

+1-1
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

+62-13
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,9 @@ 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+
/// Similar to [`Self::signer_pending_commitment_update`]: indicates that we've deferred sending a
750+
/// `revoke_and_ack`, and should do so once the signer has become unblocked.
751+
signer_pending_revoke_and_ack: bool,
747752
/// Similar to [`Self::signer_pending_commitment_update`] but we're waiting to send either a
748753
/// [`msgs::FundingCreated`] or [`msgs::FundingSigned`] depending on if this channel is
749754
/// outbound or inbound.
@@ -3143,6 +3148,7 @@ impl<SP: Deref> Channel<SP> where
31433148
self.context.cur_holder_commitment_transaction_number -= 1;
31443149
// Note that if we need_commitment & !AwaitingRemoteRevoke we'll call
31453150
// build_commitment_no_status_check() next which will reset this to RAAFirst.
3151+
log_debug!(logger, "setting resend_order to CommitmentFirst");
31463152
self.context.resend_order = RAACommitmentOrder::CommitmentFirst;
31473153

31483154
if (self.context.channel_state & ChannelState::MonitorUpdateInProgress as u32) != 0 {
@@ -3160,8 +3166,8 @@ impl<SP: Deref> Channel<SP> where
31603166
self.context.latest_monitor_update_id = monitor_update.update_id;
31613167
monitor_update.updates.append(&mut additional_update.updates);
31623168
}
3163-
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state but awaiting a monitor update resolution to reply.",
3164-
&self.context.channel_id);
3169+
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updated HTLC state and set sequence to {} but awaiting a monitor update resolution to reply.",
3170+
&self.context.channel_id, self.context.cur_holder_commitment_transaction_number);
31653171
return Ok(self.push_ret_blockable_mon_update(monitor_update));
31663172
}
31673173

@@ -3177,8 +3183,8 @@ impl<SP: Deref> Channel<SP> where
31773183
true
31783184
} else { false };
31793185

3180-
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and responding with{} a revoke_and_ack.",
3181-
&self.context.channel_id(), if need_commitment_signed { " our own commitment_signed and" } else { "" });
3186+
log_debug!(logger, "Received valid commitment_signed from peer in channel {}, updating HTLC state and set sequence to {}; responding with{} a revoke_and_ack.",
3187+
&self.context.channel_id(), self.context.cur_holder_commitment_transaction_number, if need_commitment_signed { " our own commitment_signed and" } else { "" });
31823188
self.monitor_updating_paused(true, need_commitment_signed, false, Vec::new(), Vec::new(), Vec::new());
31833189
return Ok(self.push_ret_blockable_mon_update(monitor_update));
31843190
}
@@ -3828,7 +3834,7 @@ impl<SP: Deref> Channel<SP> where
38283834
}
38293835

38303836
let raa = if self.context.monitor_pending_revoke_and_ack {
3831-
Some(self.get_last_revoke_and_ack())
3837+
self.get_last_revoke_and_ack(logger)
38323838
} else { None };
38333839
let commitment_update = if self.context.monitor_pending_commitment_signed {
38343840
self.get_last_commitment_update_for_send(logger).ok()
@@ -3837,13 +3843,25 @@ impl<SP: Deref> Channel<SP> where
38373843
self.mark_awaiting_response();
38383844
}
38393845

3846+
if self.context.monitor_pending_commitment_signed && commitment_update.is_none() {
3847+
log_debug!(logger, "Monitor was pending_commitment_signed with no commitment update available; setting signer_pending_commitment_update = true");
3848+
self.context.signer_pending_commitment_update = true;
3849+
}
3850+
if self.context.monitor_pending_revoke_and_ack && raa.is_none() {
3851+
log_debug!(logger, "Monitor was pending_revoke_and_ack with no RAA available; setting signer_pending_revoke_and_ack = true");
3852+
self.context.signer_pending_revoke_and_ack = true;
3853+
}
3854+
38403855
self.context.monitor_pending_revoke_and_ack = false;
38413856
self.context.monitor_pending_commitment_signed = false;
3857+
38423858
let order = self.context.resend_order.clone();
3843-
log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA, with {} first",
3859+
log_debug!(logger, "Restored monitor updating in channel {} resulting in {}{} commitment update and {} RAA{}",
38443860
&self.context.channel_id(), if funding_broadcastable.is_some() { "a funding broadcastable, " } else { "" },
38453861
if commitment_update.is_some() { "a" } else { "no" }, if raa.is_some() { "an" } else { "no" },
3846-
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
3862+
if commitment_update.is_some() && raa.is_some() {
3863+
match order { RAACommitmentOrder::CommitmentFirst => ", with commitment first", RAACommitmentOrder::RevokeAndACKFirst => ", with RAA first"}
3864+
} else { "" });
38473865
MonitorRestoreUpdates {
38483866
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, funding_broadcastable, channel_ready, announcement_sigs
38493867
}
@@ -3890,6 +3908,9 @@ impl<SP: Deref> Channel<SP> where
38903908
let commitment_update = if self.context.signer_pending_commitment_update {
38913909
self.get_last_commitment_update_for_send(logger).ok()
38923910
} else { None };
3911+
let raa = if self.context.signer_pending_revoke_and_ack {
3912+
self.get_last_revoke_and_ack(logger)
3913+
} else { None };
38933914
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
38943915
self.context.get_funding_signed_msg(logger).1
38953916
} else { None };
@@ -3899,24 +3920,48 @@ impl<SP: Deref> Channel<SP> where
38993920
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
39003921
self.context.get_funding_created_msg(logger)
39013922
} else { None };
3923+
let order = self.context.resend_order.clone();
3924+
3925+
log_debug!(logger, "Signing unblocked in channel {} at sequence {} resulting in {} commitment update, {} RAA{}, {} funding signed, and {} funding created",
3926+
&self.context.channel_id(), self.context.cur_holder_commitment_transaction_number,
3927+
if commitment_update.is_some() { "a" } else { "no" },
3928+
if raa.is_some() { "an" } else { "no" },
3929+
if commitment_update.is_some() && raa.is_some() {
3930+
if order == RAACommitmentOrder::CommitmentFirst { " (commitment first)" } else { " (RAA first)" }
3931+
} else { "" },
3932+
if funding_signed.is_some() { "a" } else { "no" },
3933+
if funding_created.is_some() { "a" } else { "no" });
3934+
39023935
SignerResumeUpdates {
39033936
commitment_update,
3937+
raa,
3938+
order,
39043939
funding_signed,
39053940
funding_created,
39063941
channel_ready,
39073942
}
39083943
}
39093944

3910-
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
3945+
fn get_last_revoke_and_ack<L: Deref>(&mut self, logger: &L) -> Option<msgs::RevokeAndACK> where L::Target: Logger {
3946+
if self.context.signer_pending_commitment_update {
3947+
log_debug!(logger, "Can't generate revoke-and-ack in channel {} while pending commitment update",
3948+
self.context.channel_id());
3949+
return None;
3950+
}
3951+
3952+
log_debug!(logger, "Regenerated last revoke-and-ack in channel {} for next per-commitment point sequence number {}, releasing secret for {}",
3953+
&self.context.channel_id(), self.context.cur_holder_commitment_transaction_number,
3954+
self.context.cur_holder_commitment_transaction_number + 2);
3955+
self.context.signer_pending_revoke_and_ack = false;
39113956
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);
39123957
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);
3913-
msgs::RevokeAndACK {
3958+
Some(msgs::RevokeAndACK {
39143959
channel_id: self.context.channel_id,
39153960
per_commitment_secret,
39163961
next_per_commitment_point,
39173962
#[cfg(taproot)]
39183963
next_local_nonce: None,
3919-
}
3964+
})
39203965
}
39213966

39223967
/// Gets the last commitment update for immediate sending to our peer.
@@ -3976,8 +4021,8 @@ impl<SP: Deref> Channel<SP> where
39764021
})
39774022
} else { None };
39784023

3979-
log_trace!(logger, "Regenerated latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
3980-
&self.context.channel_id(), if update_fee.is_some() { " update_fee," } else { "" },
4024+
log_debug!(logger, "Regenerated latest commitment update in channel {} at {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
4025+
&self.context.channel_id(), self.context.cur_holder_commitment_transaction_number, if update_fee.is_some() { " update_fee," } else { "" },
39814026
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
39824027
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger).map(|(cu, _)| cu) {
39834028
self.context.signer_pending_commitment_update = false;
@@ -4112,7 +4157,7 @@ impl<SP: Deref> Channel<SP> where
41124157
self.context.monitor_pending_revoke_and_ack = true;
41134158
None
41144159
} else {
4115-
Some(self.get_last_revoke_and_ack())
4160+
self.get_last_revoke_and_ack(logger)
41164161
}
41174162
} else {
41184163
return Err(ChannelError::Close("Peer attempted to reestablish channel with a very old local commitment transaction".to_owned()));
@@ -5447,6 +5492,7 @@ impl<SP: Deref> Channel<SP> where
54475492
self.context.pending_update_fee = None;
54485493
}
54495494
}
5495+
log_debug!(logger, "setting resend_order to RevokeAndACKFirst");
54505496
self.context.resend_order = RAACommitmentOrder::RevokeAndACKFirst;
54515497

54525498
let (mut htlcs_ref, counterparty_commitment_tx) =
@@ -5841,6 +5887,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
58415887
monitor_pending_finalized_fulfills: Vec::new(),
58425888

58435889
signer_pending_commitment_update: false,
5890+
signer_pending_revoke_and_ack: false,
58445891
signer_pending_funding: false,
58455892

58465893
#[cfg(debug_assertions)]
@@ -6463,6 +6510,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
64636510
monitor_pending_finalized_fulfills: Vec::new(),
64646511

64656512
signer_pending_commitment_update: false,
6513+
signer_pending_revoke_and_ack: false,
64666514
signer_pending_funding: false,
64676515

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

75337581
signer_pending_commitment_update: false,
7582+
signer_pending_revoke_and_ack: false,
75347583
signer_pending_funding: false,
75357584

75367585
pending_update_fee,

lightning/src/ln/channelmanager.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -6803,12 +6803,19 @@ 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+
match (msgs.commitment_update, msgs.raa) {
6807+
(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::CommitmentFirst => {
6808+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs { node_id, updates: cu });
6809+
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK { node_id, msg: raa });
6810+
},
6811+
(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::RevokeAndACKFirst => {
6812+
pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK { node_id, msg: raa });
6813+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs { node_id, updates: cu });
6814+
},
6815+
(Some(cu), _) => pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs { node_id, updates: cu }),
6816+
(_, Some(raa)) => pending_msg_events.push(events::MessageSendEvent::SendRevokeAndACK { node_id, msg: raa }),
6817+
(_, _) => (),
6818+
};
68126819
if let Some(msg) = msgs.funding_signed {
68136820
pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
68146821
node_id,

0 commit comments

Comments
 (0)