Skip to content

Commit 30a2665

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 30a2665

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
@@ -152,6 +152,51 @@ impl_writeable_tlv_based_enum_upgradable!(ClosureReason,
152152
(12, OutdatedChannelManager) => {},
153153
);
154154

155+
/// Intended destination of a failed HTLC as indicated in [`Event::HTLCHandlingFailed`].
156+
#[derive(Clone, Debug, PartialEq)]
157+
pub enum HTLCDestination {
158+
/// We tried forwarding to a channel but failed to do so. An example of such an instance is when
159+
/// there is insufficient capacity in our outbound channel.
160+
NextHopChannel {
161+
/// The `node_id` of the next node. For backwards compatibility, this field is
162+
/// marked as optional, since prior versions may not always be able to provide
163+
/// counterparty node information.
164+
node_id: Option<PublicKey>,
165+
/// The outgoing `channel_id` between us and the next node.
166+
channel_id: [u8; 32],
167+
},
168+
/// Scenario where we are unsure of the next node to forward the HTLC to.
169+
UnknownNextHop {
170+
/// Short channel id we are requesting to forward a HTLC to.
171+
requested_forward_scid: u64,
172+
},
173+
/// Failure scenario where an HTLC may have been forwarded to be intended for us,
174+
/// but is invalid for some reason, so we reject it.
175+
///
176+
/// Some of the reasons may include:
177+
/// * HTLC Timeouts
178+
/// * Expected MPP amount to claim does not equal HTLC total
179+
/// * Claimable amount does not match expected amount
180+
/// * Attempting to claim a payment without any HTLCs left over
181+
FailedPayment {
182+
/// The payment hash of the payment we attempted to process.
183+
payment_hash: PaymentHash
184+
},
185+
}
186+
187+
impl_writeable_tlv_based_enum_upgradable!(HTLCDestination,
188+
(0, NextHopChannel) => {
189+
(0, node_id, required),
190+
(2, channel_id, required),
191+
},
192+
(2, UnknownNextHop) => {
193+
(0, requested_forward_scid, required),
194+
},
195+
(4, FailedPayment) => {
196+
(0, payment_hash, required),
197+
}
198+
);
199+
155200
/// An Event which you should probably take some action in response to.
156201
///
157202
/// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
@@ -540,6 +585,19 @@ pub enum Event {
540585
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
541586
channel_type: ChannelTypeFeatures,
542587
},
588+
/// Indicates that the HTLC was accepted, but could not be processed when or after attempting to
589+
/// forward it. Some scenarios where this event may be sent include:
590+
/// * Insufficient capacity in the outbound channel
591+
/// * While waiting to forward the HTLC, the channel it is meant to be forwarded through closes
592+
/// * When forwarding for a phantom payment, the scid to forward to was invalid
593+
/// * Claiming an amount for an MPP payment that exceeds the HTLC total
594+
/// * The HTLC has timed out
595+
HTLCHandlingFailed {
596+
/// The channel over which the HTLC was received.
597+
prev_channel_id: [u8; 32],
598+
/// Destination of the HTLC that failed to be processed.
599+
failed_next_destination: HTLCDestination,
600+
},
543601
}
544602

545603
impl Writeable for Event {
@@ -684,6 +742,13 @@ impl Writeable for Event {
684742
(6, short_channel_id, option),
685743
})
686744
},
745+
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination } => {
746+
25u8.write(writer)?;
747+
write_tlv_fields!(writer, {
748+
(0, prev_channel_id, required),
749+
(2, failed_next_destination, required),
750+
})
751+
},
687752
// Note that, going forward, all new events must only write data inside of
688753
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write
689754
// data via `write_tlv_fields`.

0 commit comments

Comments
 (0)