Skip to content

Commit b00a161

Browse files
TheBlueMattwaterson
authored andcommitted
Handle retrying sign_counterparty_commitment failures
If sign_counterparty_commitment fails (i.e. because the signer is temporarily disconnected), this really indicates that we should retry the message sending which required the signature later, rather than force-closing the channel (which probably won't even work if the signer is missing). This commit adds initial retrying of failures, specifically regenerating commitment updates, attempting to re-sign the `CommitmentSigned` message, and sending it to our peers if we succed.
1 parent 3dcb345 commit b00a161

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ pub(super) struct MonitorRestoreUpdates {
515515
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
516516
}
517517

518+
/// The return value of `signer_maybe_unblocked`
519+
pub(super) struct SignerResumeUpdates {
520+
pub commitment_update: Option<msgs::CommitmentUpdate>,
521+
pub funding_signed: Option<msgs::FundingSigned>,
522+
pub funding_created: Option<msgs::FundingCreated>,
523+
}
524+
518525
/// The return value of `channel_reestablish`
519526
pub(super) struct ReestablishResponses {
520527
pub channel_ready: Option<msgs::ChannelReady>,
@@ -3816,6 +3823,21 @@ impl<SP: Deref> Channel<SP> where
38163823
Ok(())
38173824
}
38183825

3826+
/// Indicates that the signer may have some signatures for us, so we should retry if we're
3827+
/// blocked.
3828+
pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
3829+
let commitment_update = if self.context.signer_pending_commitment_update {
3830+
None
3831+
} else { None };
3832+
let funding_signed = None;
3833+
let funding_created = None;
3834+
SignerResumeUpdates {
3835+
commitment_update,
3836+
funding_signed,
3837+
funding_created,
3838+
}
3839+
}
3840+
38193841
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
38203842
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);
38213843
let per_commitment_secret = self.context.holder_signer.as_ref().release_commitment_secret(self.context.cur_holder_commitment_transaction_number + 2);

lightning/src/ln/channelmanager.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6787,6 +6787,63 @@ where
67876787
has_update
67886788
}
67896789

6790+
/// When a call to a [`ChannelSigner`] method returns an error, this indicates that the signer
6791+
/// is (temporarily) unavailable, and the operation should be retried later.
6792+
///
6793+
/// This method allows for that retry - either checking for any signer-pending messages to be
6794+
/// attempted in every channel, or in the specifically provided channel.
6795+
///
6796+
/// [`ChannelSigner`]: crate::sign::ChannelSigner
6797+
#[cfg(test)] // This is only implemented for one signer method, and should be private until we
6798+
// actually finish implementing it fully.
6799+
pub fn signer_unblocked(&self, channel_opt: Option<(PublicKey, ChannelId)>) {
6800+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6801+
6802+
let unblock_chan = |phase: &mut ChannelPhase<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| {
6803+
if let ChannelPhase::Funded(chan) = phase {
6804+
let msgs = chan.signer_maybe_unblocked(&self.logger);
6805+
let node_id = phase.context().get_counterparty_node_id();
6806+
if let Some(updates) = msgs.commitment_update {
6807+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
6808+
node_id,
6809+
updates,
6810+
});
6811+
}
6812+
if let Some(msg) = msgs.funding_signed {
6813+
pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
6814+
node_id,
6815+
msg,
6816+
});
6817+
}
6818+
if let Some(msg) = msgs.funding_created {
6819+
pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
6820+
node_id,
6821+
msg,
6822+
});
6823+
}
6824+
}
6825+
};
6826+
6827+
let per_peer_state = self.per_peer_state.read().unwrap();
6828+
if let Some((counterparty_node_id, channel_id)) = channel_opt {
6829+
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
6830+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6831+
let peer_state = &mut *peer_state_lock;
6832+
if let Some(chan) = peer_state.channel_by_id.get_mut(&channel_id) {
6833+
unblock_chan(chan, &mut peer_state.pending_msg_events);
6834+
}
6835+
}
6836+
} else {
6837+
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
6838+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
6839+
let peer_state = &mut *peer_state_lock;
6840+
for (_, chan) in peer_state.channel_by_id.iter_mut() {
6841+
unblock_chan(chan, &mut peer_state.pending_msg_events);
6842+
}
6843+
}
6844+
}
6845+
}
6846+
67906847
/// Check whether any channels have finished removing all pending updates after a shutdown
67916848
/// exchange and can now send a closing_signed.
67926849
/// Returns whether any closing_signed messages were generated.

0 commit comments

Comments
 (0)