Skip to content

Commit 2aa2da6

Browse files
committed
Allow sending RAA upon unblocking the signer
1 parent c761a59 commit 2aa2da6

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,10 @@ pub(super) struct MonitorRestoreUpdates {
906906
#[allow(unused)]
907907
pub(super) struct SignerResumeUpdates {
908908
pub commitment_update: Option<msgs::CommitmentUpdate>,
909+
pub revoke_and_ack: Option<msgs::RevokeAndACK>,
909910
pub funding_signed: Option<msgs::FundingSigned>,
910911
pub channel_ready: Option<msgs::ChannelReady>,
912+
pub order: RAACommitmentOrder,
911913
}
912914

913915
/// The return value of `channel_reestablish`
@@ -1215,6 +1217,14 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
12151217
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
12161218
monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
12171219

1220+
/// If we went to send a revoke_and_ack but our signer was unable to give us a signature,
1221+
/// we should retry at some point in the future when the signer indicates it may have a
1222+
/// signature for us.
1223+
///
1224+
/// This may also be used to make sure we send a `revoke_and_ack` after a `commitment_signed`
1225+
/// if we need to maintain ordering of messages, but are pending the signer on a previous
1226+
/// message.
1227+
signer_pending_revoke_and_ack: bool,
12181228
/// If we went to send a commitment update (ie some messages then [`msgs::CommitmentSigned`])
12191229
/// but our signer (initially) refused to give us a signature, we should retry at some point in
12201230
/// the future when the signer indicates it may have a signature for us.
@@ -1683,6 +1693,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
16831693
monitor_pending_finalized_fulfills: Vec::new(),
16841694
monitor_pending_update_adds: Vec::new(),
16851695

1696+
signer_pending_revoke_and_ack: false,
16861697
signer_pending_commitment_update: false,
16871698
signer_pending_funding: false,
16881699

@@ -1908,6 +1919,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19081919
monitor_pending_finalized_fulfills: Vec::new(),
19091920
monitor_pending_update_adds: Vec::new(),
19101921

1922+
signer_pending_revoke_and_ack: false,
19111923
signer_pending_commitment_update: false,
19121924
signer_pending_funding: false,
19131925

@@ -5373,11 +5385,6 @@ impl<SP: Deref> Channel<SP> where
53735385
/// blocked.
53745386
#[cfg(async_signing)]
53755387
pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
5376-
let commitment_update = if self.context.signer_pending_commitment_update {
5377-
log_trace!(logger, "Attempting to generate pending commitment update...");
5378-
self.context.signer_pending_commitment_update = false;
5379-
self.get_last_commitment_update_for_send(logger).ok()
5380-
} else { None };
53815388
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
53825389
log_trace!(logger, "Attempting to generate pending funding signed...");
53835390
self.context.signer_pending_funding = false;
@@ -5387,15 +5394,42 @@ impl<SP: Deref> Channel<SP> where
53875394
self.check_get_channel_ready(0, logger)
53885395
} else { None };
53895396

5390-
log_trace!(logger, "Signer unblocked with {} commitment_update, {} funding_signed and {} channel_ready",
5397+
let mut commitment_update = if self.context.signer_pending_commitment_update {
5398+
log_trace!(logger, "Attempting to generate pending commitment update...");
5399+
self.context.signer_pending_commitment_update = false;
5400+
self.get_last_commitment_update_for_send(logger).ok()
5401+
} else { None };
5402+
let mut revoke_and_ack = if self.context.signer_pending_revoke_and_ack {
5403+
log_trace!(logger, "Attempting to generate pending revoke and ack...");
5404+
self.context.signer_pending_revoke_and_ack = false;
5405+
Some(self.get_last_revoke_and_ack())
5406+
} else { None };
5407+
5408+
if self.context.resend_order == RAACommitmentOrder::CommitmentFirst
5409+
&& self.context.signer_pending_commitment_update && revoke_and_ack.is_some() {
5410+
log_trace!(logger, "Signer unblocked for revoke and ack, but unable to send due to resend order, waiting on signer for commitment update");
5411+
self.context.signer_pending_revoke_and_ack = true;
5412+
revoke_and_ack = None;
5413+
}
5414+
if self.context.resend_order == RAACommitmentOrder::RevokeAndACKFirst
5415+
&& self.context.signer_pending_revoke_and_ack && commitment_update.is_some() {
5416+
log_trace!(logger, "Signer unblocked for commitment update, but unable to send due to resend order, waiting on signer for revoke and ack");
5417+
self.context.signer_pending_commitment_update = true;
5418+
commitment_update = None;
5419+
}
5420+
5421+
log_trace!(logger, "Signer unblocked with {} commitment_update, {} revoke_and_ack, {} funding_signed and {} channel_ready",
53915422
if commitment_update.is_some() { "a" } else { "no" },
5423+
if revoke_and_ack.is_some() { "a" } else { "no" },
53925424
if funding_signed.is_some() { "a" } else { "no" },
53935425
if channel_ready.is_some() { "a" } else { "no" });
53945426

53955427
SignerResumeUpdates {
53965428
commitment_update,
5429+
revoke_and_ack,
53975430
funding_signed,
53985431
channel_ready,
5432+
order: self.context.resend_order.clone(),
53995433
}
54005434
}
54015435

@@ -9272,6 +9306,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
92729306
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
92739307
monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or(Vec::new()),
92749308

9309+
signer_pending_revoke_and_ack: false,
92759310
signer_pending_commitment_update: false,
92769311
signer_pending_funding: false,
92779312

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8196,11 +8196,26 @@ where
81968196
match phase {
81978197
ChannelPhase::Funded(chan) => {
81988198
let msgs = chan.signer_maybe_unblocked(&self.logger);
8199-
if let Some(updates) = msgs.commitment_update {
8200-
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
8201-
node_id,
8202-
updates,
8203-
});
8199+
let cu_msg = msgs.commitment_update.map(|updates| events::MessageSendEvent::UpdateHTLCs {
8200+
node_id,
8201+
updates,
8202+
});
8203+
let raa_msg = msgs.revoke_and_ack.map(|msg| events::MessageSendEvent::SendRevokeAndACK {
8204+
node_id,
8205+
msg,
8206+
});
8207+
match (cu_msg, raa_msg) {
8208+
(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::CommitmentFirst => {
8209+
pending_msg_events.push(cu);
8210+
pending_msg_events.push(raa);
8211+
},
8212+
(Some(cu), Some(raa)) if msgs.order == RAACommitmentOrder::RevokeAndACKFirst => {
8213+
pending_msg_events.push(raa);
8214+
pending_msg_events.push(cu);
8215+
},
8216+
(Some(cu), _) => pending_msg_events.push(cu),
8217+
(_, Some(raa)) => pending_msg_events.push(raa),
8218+
(_, _) => {},
82048219
}
82058220
if let Some(msg) = msgs.funding_signed {
82068221
pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {

0 commit comments

Comments
 (0)