Skip to content

Commit 18a8e6b

Browse files
TheBlueMattwaterson
authored andcommitted
Handling for sign_counterparty_commitment failing during normal op
If sign_counterparty_commitment fails (i.e. because the signer is temporarily disconnected), this really indicates that we should retry the message sending later, rather than force-closing the channel (which probably won't even work if the signer is missing). Here we add initial handling of sign_counterparty_commitment failing during normal channel operation, setting a new flag in `ChannelContext` which indicates we should retry sending the commitment update later. We don't yet add any ability to do that retry.
1 parent 448b191 commit 18a8e6b

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

lightning/src/ln/channel.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,14 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
729729
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
730730
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
731731

732+
/// If we went to send a commitment update (ie some messages then [`msgs::CommitmentSigned`])
733+
/// but our signer (initially) refused to give us a signature, we should retry at some point in
734+
/// the future when the signer indicates it may have a signature for us.
735+
///
736+
/// This flag is set in such a case. Note that we don't need to persist this as we'll end up
737+
/// setting it again as a side-effect of [`Channel::channel_reestablish`].
738+
signer_pending_commitment_update: bool,
739+
732740
// pending_update_fee is filled when sending and receiving update_fee.
733741
//
734742
// Because it follows the same commitment flow as HTLCs, `FeeUpdateState` is either `Outbound`
@@ -3071,8 +3079,8 @@ impl<SP: Deref> Channel<SP> where
30713079
self.context.monitor_pending_revoke_and_ack = true;
30723080
if need_commitment && (self.context.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
30733081
// If we were going to send a commitment_signed after the RAA, go ahead and do all
3074-
// the corresponding HTLC status updates so that get_last_commitment_update
3075-
// includes the right HTLCs.
3082+
// the corresponding HTLC status updates so that
3083+
// get_last_commitment_update_for_send includes the right HTLCs.
30763084
self.context.monitor_pending_commitment_signed = true;
30773085
let mut additional_update = self.build_commitment_no_status_check(logger);
30783086
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
@@ -3446,9 +3454,10 @@ impl<SP: Deref> Channel<SP> where
34463454
// cells) while we can't update the monitor, so we just return what we have.
34473455
if require_commitment {
34483456
self.context.monitor_pending_commitment_signed = true;
3449-
// When the monitor updating is restored we'll call get_last_commitment_update(),
3450-
// which does not update state, but we're definitely now awaiting a remote revoke
3451-
// before we can step forward any more, so set it here.
3457+
// When the monitor updating is restored we'll call
3458+
// get_last_commitment_update_for_send(), which does not update state, but we're
3459+
// definitely now awaiting a remote revoke before we can step forward any more, so
3460+
// set it here.
34523461
let mut additional_update = self.build_commitment_no_status_check(logger);
34533462
// build_commitment_no_status_check may bump latest_monitor_id but we want them to be
34543463
// strictly increasing by one, so decrement it here.
@@ -3750,9 +3759,11 @@ impl<SP: Deref> Channel<SP> where
37503759
Some(self.get_last_revoke_and_ack())
37513760
} else { None };
37523761
let commitment_update = if self.context.monitor_pending_commitment_signed {
3753-
self.mark_awaiting_response();
3754-
Some(self.get_last_commitment_update(logger))
3762+
self.get_last_commitment_update_for_send(logger).ok()
37553763
} else { None };
3764+
if commitment_update.is_some() {
3765+
self.mark_awaiting_response();
3766+
}
37563767

37573768
self.context.monitor_pending_revoke_and_ack = false;
37583769
self.context.monitor_pending_commitment_signed = false;
@@ -3813,7 +3824,8 @@ impl<SP: Deref> Channel<SP> where
38133824
}
38143825
}
38153826

3816-
fn get_last_commitment_update<L: Deref>(&self, logger: &L) -> msgs::CommitmentUpdate where L::Target: Logger {
3827+
/// Gets the last commitment update for immediate sending to our peer.
3828+
fn get_last_commitment_update_for_send<L: Deref>(&mut self, logger: &L) -> Result<msgs::CommitmentUpdate, ()> where L::Target: Logger {
38173829
let mut update_add_htlcs = Vec::new();
38183830
let mut update_fulfill_htlcs = Vec::new();
38193831
let mut update_fail_htlcs = Vec::new();
@@ -3872,10 +3884,17 @@ impl<SP: Deref> Channel<SP> where
38723884
log_trace!(logger, "Regenerated latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
38733885
&self.context.channel_id(), if update_fee.is_some() { " update_fee," } else { "" },
38743886
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
3875-
msgs::CommitmentUpdate {
3887+
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger).map(|(cu, _)| cu) {
3888+
self.context.signer_pending_commitment_update = false;
3889+
update
3890+
} else {
3891+
self.context.signer_pending_commitment_update = true;
3892+
return Err(());
3893+
};
3894+
Ok(msgs::CommitmentUpdate {
38763895
update_add_htlcs, update_fulfill_htlcs, update_fail_htlcs, update_fail_malformed_htlcs, update_fee,
3877-
commitment_signed: self.send_commitment_no_state_update(logger).expect("It looks like we failed to re-generate a commitment_signed we had previously sent?").0,
3878-
}
3896+
commitment_signed,
3897+
})
38793898
}
38803899

38813900
/// Gets the `Shutdown` message we should send our peer on reconnect, if any.
@@ -4055,7 +4074,7 @@ impl<SP: Deref> Channel<SP> where
40554074
Ok(ReestablishResponses {
40564075
channel_ready, shutdown_msg, announcement_sigs,
40574076
raa: required_revoke,
4058-
commitment_update: Some(self.get_last_commitment_update(logger)),
4077+
commitment_update: self.get_last_commitment_update_for_send(logger).ok(),
40594078
order: self.context.resend_order.clone(),
40604079
})
40614080
}
@@ -5413,7 +5432,7 @@ impl<SP: Deref> Channel<SP> where
54135432
}
54145433

54155434
let res = ecdsa.sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.context.secp_ctx)
5416-
.map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
5435+
.map_err(|_| ChannelError::Ignore("Failed to get signatures for new commitment_signed".to_owned()))?;
54175436
signature = res.0;
54185437
htlc_signatures = res.1;
54195438

@@ -5726,6 +5745,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
57265745
monitor_pending_failures: Vec::new(),
57275746
monitor_pending_finalized_fulfills: Vec::new(),
57285747

5748+
signer_pending_commitment_update: false,
5749+
57295750
#[cfg(debug_assertions)]
57305751
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
57315752
#[cfg(debug_assertions)]
@@ -6373,6 +6394,8 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
63736394
monitor_pending_failures: Vec::new(),
63746395
monitor_pending_finalized_fulfills: Vec::new(),
63756396

6397+
signer_pending_commitment_update: false,
6398+
63766399
#[cfg(debug_assertions)]
63776400
holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
63786401
#[cfg(debug_assertions)]
@@ -7459,6 +7482,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
74597482
monitor_pending_failures,
74607483
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
74617484

7485+
signer_pending_commitment_update: false,
7486+
74627487
pending_update_fee,
74637488
holding_cell_update_fee,
74647489
next_holder_htlc_id,

0 commit comments

Comments
 (0)