Skip to content

Commit f06f9d1

Browse files
committed
Fail channel if we can't sign a new commitment tx during HTLC claim
Previously, we could fail to generate a new commitment transaction but it simply indicated we had gone to doule-claim an HTLC. Now that double-claims are returned instead as Ok(None), we should handle the error case and fail the channel, as the only way to hit the error case is if key derivation failed or the user refused to sign the new commitment transaction. This also resolves an issue where we wouldn't inform our ChannelMonitor of the new payment preimage in case we failed to fetch a signature for the new commitment transaction.
1 parent c09104f commit f06f9d1

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Readable for ChannelMonitorUpdate {
114114
}
115115

116116
/// An error enum representing a failure to persist a channel monitor update.
117-
#[derive(Clone, Debug)]
117+
#[derive(Clone, Copy, Debug, PartialEq)]
118118
pub enum ChannelMonitorUpdateErr {
119119
/// Used to indicate a temporary failure (eg connection to a watchtower or remote backup of
120120
/// our state failed, but is expected to succeed at some point in the future).

lightning/src/ln/channel.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,10 +1374,13 @@ impl<Signer: Sign> Channel<Signer> {
13741374
}
13751375
}
13761376

1377-
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> Result<UpdateFulfillCommitFetch, ChannelError> where L::Target: Logger {
1377+
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> Result<UpdateFulfillCommitFetch, (ChannelError, ChannelMonitorUpdate)> where L::Target: Logger {
13781378
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
13791379
UpdateFulfillFetch::NewClaim { mut monitor_update, msg: Some(update_fulfill_htlc) } => {
1380-
let (commitment, mut additional_update) = self.send_commitment_no_status_check(logger)?;
1380+
let (commitment, mut additional_update) = match self.send_commitment_no_status_check(logger) {
1381+
Err(e) => return Err((e, monitor_update)),
1382+
Ok(res) => res
1383+
};
13811384
// send_commitment_no_status_check may bump latest_monitor_id but we want them to be
13821385
// strictly increasing by one, so decrement it here.
13831386
self.latest_monitor_update_id = monitor_update.update_id;

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use util::events::{EventHandler, EventsProvider, MessageSendEvent, MessageSendEv
5757
use util::{byte_utils, events};
5858
use util::ser::{Readable, ReadableArgs, MaybeReadable, Writeable, Writer};
5959
use util::chacha20::{ChaCha20, ChaChaReader};
60-
use util::logger::Logger;
60+
use util::logger::{Logger, Level};
6161
use util::errors::APIError;
6262

6363
use prelude::*;
@@ -2679,16 +2679,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
26792679
};
26802680

26812681
if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(chan_id) {
2682-
let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
26832682
match chan.get_mut().get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger) {
26842683
Ok(msgs_monitor_option) => {
26852684
if let UpdateFulfillCommitFetch::NewClaim { msgs, monitor_update } = msgs_monitor_option {
26862685
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2687-
if was_frozen_for_monitor {
2688-
assert!(msgs.is_none());
2689-
} else {
2690-
return Err(Some((chan.get().get_counterparty_node_id(), handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err())));
2691-
}
2686+
log_given_level!(self.logger, if e == ChannelMonitorUpdateErr::PermanentFailure { Level::Error } else { Level::Debug },
2687+
"Failed to update channel monitor with preimage {:?}: {:?}",
2688+
payment_preimage, e);
2689+
return Err(Some((
2690+
chan.get().get_counterparty_node_id(),
2691+
handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err(),
2692+
)));
26922693
}
26932694
if let Some((msg, commitment_signed)) = msgs {
26942695
log_debug!(self.logger, "Claiming funds for HTLC with preimage {} resulted in a commitment_signed for channel {}",
@@ -2708,16 +2709,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
27082709
}
27092710
return Ok(())
27102711
},
2711-
Err(e) => {
2712-
// TODO: Do something with e?
2713-
// This should only occur if we are claiming an HTLC at the same time as the
2714-
// HTLC is being failed (eg because a block is being connected and this caused
2715-
// an HTLC to time out). This should, of course, only occur if the user is the
2716-
// one doing the claiming (as it being a part of a peer claim would imply we're
2717-
// about to lose funds) and only if the lock in claim_funds was dropped as a
2718-
// previous HTLC was failed (thus not for an MPP payment).
2719-
debug_assert!(false, "This shouldn't be reachable except in absurdly rare cases between monitor updates and HTLC timeouts: {:?}", e);
2720-
return Err(None)
2712+
Err((e, monitor_update)) => {
2713+
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2714+
log_given_level!(self.logger, if e == ChannelMonitorUpdateErr::PermanentFailure { Level::Error } else { Level::Info },
2715+
"Failed to update channel monitor with preimage {:?} immediately prior to force-close: {:?}",
2716+
payment_preimage, e);
2717+
}
2718+
let counterparty_node_id = chan.get().get_counterparty_node_id();
2719+
let (drop, res) = convert_chan_err!(self, e, channel_state.short_to_id, chan.get_mut(), &chan_id);
2720+
if drop {
2721+
chan.remove_entry();
2722+
}
2723+
return Err(Some((counterparty_node_id, res)));
27212724
},
27222725
}
27232726
} else { unreachable!(); }

0 commit comments

Comments
 (0)