Skip to content

Commit 4a14deb

Browse files
committed
lightning: add HTLCHandlingFailure to HTLCHandlingFailed event
1 parent 1b281f1 commit 4a14deb

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

lightning/src/events/mod.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,26 @@ pub enum InboundChannelFunds {
627627
DualFunded,
628628
}
629629

630+
/// Used to describe the types of failures that may occur when handling HTLCs.
631+
#[derive(Clone, Debug, PartialEq, Eq)]
632+
pub enum HTLCHandlingFailure {
633+
/// The HTLC was failed by the local node.
634+
Local {
635+
/// The BOLT 04 failure code providing a specific failure reason.
636+
failure_code: u16
637+
},
638+
/// The HTLC was failed by the remote downstream node. The specific reason for failure is not
639+
/// known because it is encrypted.
640+
Remote,
641+
}
642+
643+
impl_writeable_tlv_based_enum!(HTLCHandlingFailure,
644+
(0, Local) => {
645+
(0, failure_code, required)
646+
},
647+
(1, Remote) => {},
648+
);
649+
630650
/// An Event which you should probably take some action in response to.
631651
///
632652
/// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
@@ -1441,6 +1461,10 @@ pub enum Event {
14411461
prev_channel_id: ChannelId,
14421462
/// Destination of the HTLC that failed to be processed.
14431463
failed_next_destination: HTLCDestination,
1464+
/// The cause of the processing failure.
1465+
///
1466+
/// This field will be `None` for objects serialized prior to LDK 0.1.1.
1467+
reason: Option<HTLCHandlingFailure>,
14441468
},
14451469
/// Indicates that a transaction originating from LDK needs to have its fee bumped. This event
14461470
/// requires confirmed external funds to be readily available to spend.
@@ -1743,10 +1767,11 @@ impl Writeable for Event {
17431767
(8, path.blinded_tail, option),
17441768
})
17451769
},
1746-
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination } => {
1770+
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination, ref reason} => {
17471771
25u8.write(writer)?;
17481772
write_tlv_fields!(writer, {
17491773
(0, prev_channel_id, required),
1774+
(1, reason, option),
17501775
(2, failed_next_destination, required),
17511776
})
17521777
},
@@ -2190,13 +2215,16 @@ impl MaybeReadable for Event {
21902215
let mut f = || {
21912216
let mut prev_channel_id = ChannelId::new_zero();
21922217
let mut failed_next_destination_opt = UpgradableRequired(None);
2218+
let mut reason = None;
21932219
read_tlv_fields!(reader, {
21942220
(0, prev_channel_id, required),
2221+
(1, reason, option),
21952222
(2, failed_next_destination_opt, upgradable_required),
21962223
});
21972224
Ok(Some(Event::HTLCHandlingFailed {
21982225
prev_channel_id,
21992226
failed_next_destination: _init_tlv_based_struct_field!(failed_next_destination_opt, upgradable_required),
2227+
reason,
22002228
}))
22012229
};
22022230
f()

lightning/src/ln/channelmanager.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
3232
use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::{secp256k1, Sequence, Weight};
3434

35-
use crate::events::FundingInfo;
35+
use crate::events::{FundingInfo, HTLCHandlingFailure};
3636
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext};
3737
use crate::blinded_path::NodeIdLookUp;
3838
use crate::blinded_path::message::{BlindedMessagePath, MessageForwardNode};
@@ -336,6 +336,15 @@ pub(super) enum HTLCFailureMsg {
336336
Malformed(msgs::UpdateFailMalformedHTLC),
337337
}
338338

339+
impl From<&HTLCFailureMsg> for HTLCHandlingFailure {
340+
fn from(value: &HTLCFailureMsg) -> Self {
341+
match value {
342+
HTLCFailureMsg::Relay(_) => HTLCHandlingFailure::Remote,
343+
HTLCFailureMsg::Malformed(msg) => HTLCHandlingFailure::Local{ failure_code: msg.failure_code },
344+
}
345+
}
346+
}
347+
339348
/// Stores whether we can't forward an HTLC or relevant forwarding info
340349
#[derive(Clone)] // See FundedChannel::revoke_and_ack for why, tl;dr: Rust bug
341350
pub(super) enum PendingHTLCStatus {
@@ -5720,6 +5729,7 @@ where
57205729
);
57215730
self.forward_htlcs_without_forward_event(&mut [pending_forwards]);
57225731
for (htlc_fail, htlc_destination) in htlc_fails.drain(..) {
5732+
let handling_fail = (&htlc_fail).into();
57235733
let failure = match htlc_fail {
57245734
HTLCFailureMsg::Relay(fail_htlc) => HTLCForwardInfo::FailHTLC {
57255735
htlc_id: fail_htlc.htlc_id,
@@ -5735,6 +5745,7 @@ where
57355745
self.pending_events.lock().unwrap().push_back((events::Event::HTLCHandlingFailed {
57365746
prev_channel_id: incoming_channel_id,
57375747
failed_next_destination: htlc_destination,
5748+
reason: Some(handling_fail),
57385749
}, None));
57395750
}
57405751
}
@@ -6926,6 +6937,7 @@ where
69266937
pending_events.push_back((events::Event::HTLCHandlingFailed {
69276938
prev_channel_id: *channel_id,
69286939
failed_next_destination: destination,
6940+
reason: Some(onion_error.into()),
69296941
}, None));
69306942
},
69316943
}

lightning/src/ln/onion_utils.rs

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use crate::blinded_path::BlindedHop;
1111
use crate::crypto::chacha20::ChaCha20;
1212
use crate::crypto::streams::ChaChaReader;
13+
use crate::events::HTLCHandlingFailure;
1314
use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS;
1415
use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
1516
use crate::ln::msgs;
@@ -1243,6 +1244,15 @@ enum HTLCFailReasonRepr {
12431244
Reason { failure_code: u16, data: Vec<u8> },
12441245
}
12451246

1247+
impl From<&HTLCFailReason> for HTLCHandlingFailure {
1248+
fn from(value: &HTLCFailReason) -> Self {
1249+
match value.0 {
1250+
HTLCFailReasonRepr::LightningError { .. } => Self::Remote,
1251+
HTLCFailReasonRepr::Reason { failure_code, .. } => Self::Local { failure_code },
1252+
}
1253+
}
1254+
}
1255+
12461256
impl core::fmt::Debug for HTLCFailReason {
12471257
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
12481258
match self.0 {

0 commit comments

Comments
 (0)