Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a reason to HTLCHandlingFailed event #3601

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,26 @@ pub enum InboundChannelFunds {
DualFunded,
}

/// Used to describe the types of failures that may occur when handling HTLCs.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HTLCHandlingFailure {
/// The HTLC was failed by the local node.
Local {
/// The BOLT 04 failure code providing a specific failure reason.
failure_code: u16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I admit I'm not super excited about exposing the failure code as-is for two reasons - (a) we have very different constraints on the code - we may not always want to give the sender all the details about a failure (eg if the sender used a private channel but we didn't mean to expose it we might pretend the channel doesnt exist) vs what we want to tell the user about a failure (basically everything), (b) its not particularly easy to parse - users have to go read the BOLT then map that to what is actually happening in LDK code, if we had a big enum here we'd be able to provide good docs linking to config knobs and reasons why failures happened.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, will take a look at adding a more detailed enum or adding to HTLCDestination 👍

},
/// The HTLC was failed by the remote downstream node. The specific reason for failure is not
/// known because it is encrypted.
Remote,
}

impl_writeable_tlv_based_enum!(HTLCHandlingFailure,
(0, Local) => {
(0, failure_code, required)
},
(1, Remote) => {},
);

/// An Event which you should probably take some action in response to.
///
/// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
Expand Down Expand Up @@ -1441,6 +1461,10 @@ pub enum Event {
prev_channel_id: ChannelId,
/// Destination of the HTLC that failed to be processed.
failed_next_destination: HTLCDestination,
/// The cause of the processing failure.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its worth noting that HTLCDestination has some "reason"-like attributes (I meant to mention this in the issue, but apparently forgot), they're just incomplete and too broad. Up to you if you want to try to add more details there vs adding a new enum.

///
/// This field will be `None` for objects serialized prior to LDK 0.1.1.
reason: Option<HTLCHandlingFailure>,
},
/// Indicates that a transaction originating from LDK needs to have its fee bumped. This event
/// requires confirmed external funds to be readily available to spend.
Expand Down Expand Up @@ -1743,10 +1767,11 @@ impl Writeable for Event {
(8, path.blinded_tail, option),
})
},
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination } => {
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination, ref reason} => {
25u8.write(writer)?;
write_tlv_fields!(writer, {
(0, prev_channel_id, required),
(1, reason, option),
(2, failed_next_destination, required),
})
},
Expand Down Expand Up @@ -2190,13 +2215,16 @@ impl MaybeReadable for Event {
let mut f = || {
let mut prev_channel_id = ChannelId::new_zero();
let mut failed_next_destination_opt = UpgradableRequired(None);
let mut reason = None;
read_tlv_fields!(reader, {
(0, prev_channel_id, required),
(1, reason, option),
(2, failed_next_destination_opt, upgradable_required),
});
Ok(Some(Event::HTLCHandlingFailed {
prev_channel_id,
failed_next_destination: _init_tlv_based_struct_field!(failed_next_destination_opt, upgradable_required),
reason,
}))
};
f()
Expand Down
14 changes: 13 additions & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
use bitcoin::secp256k1::Secp256k1;
use bitcoin::{secp256k1, Sequence, Weight};

use crate::events::FundingInfo;
use crate::events::{FundingInfo, HTLCHandlingFailure};
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext};
use crate::blinded_path::NodeIdLookUp;
use crate::blinded_path::message::{BlindedMessagePath, MessageForwardNode};
Expand Down Expand Up @@ -336,6 +336,15 @@ pub(super) enum HTLCFailureMsg {
Malformed(msgs::UpdateFailMalformedHTLC),
}

impl From<&HTLCFailureMsg> for HTLCHandlingFailure {
fn from(value: &HTLCFailureMsg) -> Self {
match value {
HTLCFailureMsg::Relay(_) => HTLCHandlingFailure::Remote,
HTLCFailureMsg::Malformed(msg) => HTLCHandlingFailure::Local{ failure_code: msg.failure_code },
}
}
}

/// Stores whether we can't forward an HTLC or relevant forwarding info
#[derive(Clone)] // See FundedChannel::revoke_and_ack for why, tl;dr: Rust bug
pub(super) enum PendingHTLCStatus {
Expand Down Expand Up @@ -5720,6 +5729,7 @@ where
);
self.forward_htlcs_without_forward_event(&mut [pending_forwards]);
for (htlc_fail, htlc_destination) in htlc_fails.drain(..) {
let handling_fail = (&htlc_fail).into();
let failure = match htlc_fail {
HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
htlc_id: fail_htlc.htlc_id,
Expand All @@ -5735,6 +5745,7 @@ where
self.pending_events.lock().unwrap().push_back((events::Event::HTLCHandlingFailed {
prev_channel_id: incoming_channel_id,
failed_next_destination: htlc_destination,
reason: Some(handling_fail),
}, None));
}
}
Expand Down Expand Up @@ -6926,6 +6937,7 @@ where
pending_events.push_back((events::Event::HTLCHandlingFailed {
prev_channel_id: *channel_id,
failed_next_destination: destination,
reason: Some(onion_error.into()),
}, None));
},
}
Expand Down
10 changes: 10 additions & 0 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use crate::blinded_path::BlindedHop;
use crate::crypto::chacha20::ChaCha20;
use crate::crypto::streams::ChaChaReader;
use crate::events::HTLCHandlingFailure;
use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS;
use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
use crate::ln::msgs;
Expand Down Expand Up @@ -1243,6 +1244,15 @@ enum HTLCFailReasonRepr {
Reason { failure_code: u16, data: Vec<u8> },
}

impl From<&HTLCFailReason> for HTLCHandlingFailure {
fn from(value: &HTLCFailReason) -> Self {
match value.0 {
HTLCFailReasonRepr::LightningError { .. } => Self::Remote,
HTLCFailReasonRepr::Reason { failure_code, .. } => Self::Local { failure_code },
}
}
}

impl core::fmt::Debug for HTLCFailReason {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
match self.0 {
Expand Down
Loading