Skip to content

Commit e9b5f25

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 99e4db3 commit e9b5f25

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lightning/src/ln/channel.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,14 @@ pub(super) struct MonitorRestoreUpdates {
532532
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
533533
}
534534

535+
/// The return value of `signer_maybe_unblocked`
536+
#[allow(unused)]
537+
pub(super) struct SignerResumeUpdates {
538+
pub commitment_update: Option<msgs::CommitmentUpdate>,
539+
pub funding_signed: Option<msgs::FundingSigned>,
540+
pub funding_created: Option<msgs::FundingCreated>,
541+
}
542+
535543
/// The return value of `channel_reestablish`
536544
pub(super) struct ReestablishResponses {
537545
pub channel_ready: Option<msgs::ChannelReady>,
@@ -3895,6 +3903,22 @@ impl<SP: Deref> Channel<SP> where
38953903
Ok(())
38963904
}
38973905

3906+
/// Indicates that the signer may have some signatures for us, so we should retry if we're
3907+
/// blocked.
3908+
#[allow(unused)]
3909+
pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
3910+
let commitment_update = if self.context.signer_pending_commitment_update {
3911+
self.get_last_commitment_update_for_send(logger).ok()
3912+
} else { None };
3913+
let funding_signed = None;
3914+
let funding_created = None;
3915+
SignerResumeUpdates {
3916+
commitment_update,
3917+
funding_signed,
3918+
funding_created,
3919+
}
3920+
}
3921+
38983922
fn get_last_revoke_and_ack(&self) -> msgs::RevokeAndACK {
38993923
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);
39003924
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
@@ -6987,6 +6987,63 @@ where
69876987
has_update
69886988
}
69896989

6990+
/// When a call to a [`ChannelSigner`] method returns an error, this indicates that the signer
6991+
/// is (temporarily) unavailable, and the operation should be retried later.
6992+
///
6993+
/// This method allows for that retry - either checking for any signer-pending messages to be
6994+
/// attempted in every channel, or in the specifically provided channel.
6995+
///
6996+
/// [`ChannelSigner`]: crate::sign::ChannelSigner
6997+
#[cfg(test)] // This is only implemented for one signer method, and should be private until we
6998+
// actually finish implementing it fully.
6999+
pub fn signer_unblocked(&self, channel_opt: Option<(PublicKey, ChannelId)>) {
7000+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
7001+
7002+
let unblock_chan = |phase: &mut ChannelPhase<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| {
7003+
if let ChannelPhase::Funded(chan) = phase {
7004+
let msgs = chan.signer_maybe_unblocked(&self.logger);
7005+
let node_id = phase.context().get_counterparty_node_id();
7006+
if let Some(updates) = msgs.commitment_update {
7007+
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
7008+
node_id,
7009+
updates,
7010+
});
7011+
}
7012+
if let Some(msg) = msgs.funding_signed {
7013+
pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
7014+
node_id,
7015+
msg,
7016+
});
7017+
}
7018+
if let Some(msg) = msgs.funding_created {
7019+
pending_msg_events.push(events::MessageSendEvent::SendFundingCreated {
7020+
node_id,
7021+
msg,
7022+
});
7023+
}
7024+
}
7025+
};
7026+
7027+
let per_peer_state = self.per_peer_state.read().unwrap();
7028+
if let Some((counterparty_node_id, channel_id)) = channel_opt {
7029+
if let Some(peer_state_mutex) = per_peer_state.get(&counterparty_node_id) {
7030+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7031+
let peer_state = &mut *peer_state_lock;
7032+
if let Some(chan) = peer_state.channel_by_id.get_mut(&channel_id) {
7033+
unblock_chan(chan, &mut peer_state.pending_msg_events);
7034+
}
7035+
}
7036+
} else {
7037+
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
7038+
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
7039+
let peer_state = &mut *peer_state_lock;
7040+
for (_, chan) in peer_state.channel_by_id.iter_mut() {
7041+
unblock_chan(chan, &mut peer_state.pending_msg_events);
7042+
}
7043+
}
7044+
}
7045+
}
7046+
69907047
/// Check whether any channels have finished removing all pending updates after a shutdown
69917048
/// exchange and can now send a closing_signed.
69927049
/// Returns whether any closing_signed messages were generated.

0 commit comments

Comments
 (0)