Skip to content

Commit 360b8a8

Browse files
committed
Add HTLCHandlingFailed event
Adds a HTLCHandlingFailed that expresses failure by our node to process a specific HTLC. A HTLCDestination enum is defined to express the possible cases that causes the handling to fail.
1 parent 5cca9a0 commit 360b8a8

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ pub fn do_test<Out: Output>(data: &[u8], underlying_out: Out) {
860860
events::Event::PendingHTLCsForwardable { .. } => {
861861
nodes[$node].process_pending_htlc_forwards();
862862
},
863+
events::Event::HTLCHandlingFailed { .. } => {},
863864
_ => if out.may_fail.load(atomic::Ordering::Acquire) {
864865
return;
865866
} else {

lightning/src/util/events.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,19 @@ pub enum Event {
540540
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
541541
channel_type: ChannelTypeFeatures,
542542
},
543+
/// Indicates that the HTLC was accepted, but could not be processed when or after attempting to
544+
/// forward it. Some scenarios where this event may be sent include:
545+
/// 1. While waiting to forward the HTLC, the channel it is meant to be forwarded through closes
546+
/// 2. When forwarding for a phantom payment, the scid to forward to was invalid
547+
/// 3. Attempting to claim a payment without any HTLCs left over
548+
/// 4. Claiming an amount for an MPP payment that exceeds the HTLC total
549+
/// 5. The HTLC has timed out
550+
HTLCHandlingFailed {
551+
/// The channel over which the HTLC was received.
552+
prev_channel_id: [u8; 32],
553+
/// Destination of the HTLC that failed to be processed.
554+
failed_next_destination: HTLCDestination,
555+
},
543556
}
544557

545558
impl Writeable for Event {
@@ -684,6 +697,13 @@ impl Writeable for Event {
684697
(6, short_channel_id, option),
685698
})
686699
},
700+
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination } => {
701+
25u8.write(writer)?;
702+
write_tlv_fields!(writer, {
703+
(0, prev_channel_id, required),
704+
(2, failed_next_destination, required),
705+
})
706+
},
687707
// Note that, going forward, all new events must only write data inside of
688708
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
689709
// data via `write_tlv_fields`.
@@ -1178,3 +1198,48 @@ impl<T: EventHandler> EventHandler for Arc<T> {
11781198
self.deref().handle_event(event)
11791199
}
11801200
}
1201+
1202+
/// Intended destination of a failed HTLC as indicated in [`Event::HTLCHandlingFailed`].
1203+
#[derive(Clone, Debug, PartialEq)]
1204+
pub enum HTLCDestination {
1205+
/// We tried forwarding to a channel, but failed to do so. An example of such an instance
1206+
/// is when a channel closes while we were waiting to forward to it.
1207+
NextHopChannel {
1208+
/// The node_id of the next node. For backwards compatibility, this field is
1209+
/// marked as optional, since prior versions may not always be able to provide
1210+
/// counterparty node information.
1211+
node_id: Option<PublicKey>,
1212+
/// The outgoing channel_id between us and the next node.
1213+
channel_id: [u8; 32],
1214+
},
1215+
/// Scenario where we are unsure of the next node to forward the HTLC to.
1216+
UnknownNextHop {
1217+
/// Short channel id we are requesting to forward a HTLC to.
1218+
requested_forward_scid: u64,
1219+
},
1220+
/// Failure scenario where an HTLC may have been forwarded to be intended for us,
1221+
/// but is invalid for some reason, so we reject it.
1222+
///
1223+
/// Some of the reasons may include:
1224+
/// 1. HTLC Timeouts
1225+
/// 2. Expected MPP amount to claim does not equal HTLC total
1226+
/// 3. Claimable amount does not match expected amount
1227+
/// 4. Attempting to claim a payment without any HTLCs left over
1228+
FailedPayment {
1229+
/// The payment hash of the payment we attempted to process.
1230+
payment_hash: PaymentHash
1231+
},
1232+
}
1233+
1234+
impl_writeable_tlv_based_enum_upgradable!(HTLCDestination,
1235+
(0, NextHopChannel) => {
1236+
(0, node_id, required),
1237+
(2, channel_id, required),
1238+
},
1239+
(2, UnknownNextHop) => {
1240+
(0, requested_forward_scid, required),
1241+
},
1242+
(4, FailedPayment) => {
1243+
(0, payment_hash, required),
1244+
}
1245+
);

0 commit comments

Comments
 (0)