Skip to content

Commit 8c82665

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 78283a0 commit 8c82665

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lightning/src/ln/channel.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,10 +1343,13 @@ impl<Signer: Sign> Channel<Signer> {
13431343
}), Some(monitor_update))
13441344
}
13451345

1346-
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitorUpdate>), ChannelError> where L::Target: Logger {
1346+
pub fn get_update_fulfill_htlc_and_commit<L: Deref>(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage, logger: &L) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitorUpdate>), (ChannelError, ChannelMonitorUpdate)> where L::Target: Logger {
13471347
match self.get_update_fulfill_htlc(htlc_id, payment_preimage, logger) {
13481348
(Some(update_fulfill_htlc), Some(mut monitor_update)) => {
1349-
let (commitment, mut additional_update) = self.send_commitment_no_status_check(logger)?;
1349+
let (commitment, mut additional_update) = match self.send_commitment_no_status_check(logger) {
1350+
Err(e) => return Err((e, monitor_update)),
1351+
Ok(res) => res
1352+
};
13501353
// send_commitment_no_status_check may bump latest_monitor_id but we want them to be
13511354
// strictly increasing by one, so decrement it here.
13521355
self.latest_monitor_update_id = monitor_update.update_id;

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,16 +2722,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
27222722
};
27232723

27242724
if let hash_map::Entry::Occupied(mut chan) = channel_state.by_id.entry(chan_id) {
2725-
let was_frozen_for_monitor = chan.get().is_awaiting_monitor_update();
27262725
match chan.get_mut().get_update_fulfill_htlc_and_commit(prev_hop.htlc_id, payment_preimage, &self.logger) {
27272726
Ok((msgs, monitor_option)) => {
27282727
if let Some(monitor_update) = monitor_option {
27292728
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2730-
if was_frozen_for_monitor {
2731-
assert!(msgs.is_none());
2732-
} else {
2733-
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())));
2734-
}
2729+
return Err(Some((
2730+
chan.get().get_counterparty_node_id(),
2731+
handle_monitor_err!(self, e, channel_state, chan, RAACommitmentOrder::CommitmentFirst, false, msgs.is_some()).unwrap_err(),
2732+
)));
27352733
}
27362734
}
27372735
if let Some((msg, commitment_signed)) = msgs {
@@ -2751,16 +2749,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
27512749
}
27522750
return Ok(())
27532751
},
2754-
Err(e) => {
2755-
// TODO: Do something with e?
2756-
// This should only occur if we are claiming an HTLC at the same time as the
2757-
// HTLC is being failed (eg because a block is being connected and this caused
2758-
// an HTLC to time out). This should, of course, only occur if the user is the
2759-
// one doing the claiming (as it being a part of a peer claim would imply we're
2760-
// about to lose funds) and only if the lock in claim_funds was dropped as a
2761-
// previous HTLC was failed (thus not for an MPP payment).
2762-
debug_assert!(false, "This shouldn't be reachable except in absurdly rare cases between monitor updates and HTLC timeouts: {:?}", e);
2763-
return Err(None)
2752+
Err((e, monitor_update)) => {
2753+
if let Err(e) = self.chain_monitor.update_channel(chan.get().get_funding_txo().unwrap(), monitor_update) {
2754+
log_error!(self.logger, "Critical error: failed to update channel monitor with preimage {:?}: {:?}",
2755+
payment_preimage, e);
2756+
}
2757+
let counterparty_node_id = chan.get().get_counterparty_node_id();
2758+
let (drop, res) = convert_chan_err!(self, e, channel_state.short_to_id, chan.get_mut(), &chan_id);
2759+
if drop {
2760+
chan.remove_entry();
2761+
}
2762+
return Err(Some((counterparty_node_id, res)));
27642763
},
27652764
}
27662765
} else { unreachable!(); }

0 commit comments

Comments
 (0)