Skip to content

Commit c641a51

Browse files
Add HTLCIntercepted event
Used in upcoming commit(s) so users can intercept forwarded HTLCs Co-authored-by: John Cantrell <johncantrell97@gmail.com> Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
1 parent 53e6c9f commit c641a51

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

lightning/src/util/events.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use crate::chain::keysinterface::SpendableOutputDescriptor;
1818
#[cfg(anchors)]
1919
use crate::ln::chan_utils::HTLCOutputInCommitment;
20-
use crate::ln::channelmanager::PaymentId;
20+
use crate::ln::channelmanager::{InterceptId, PaymentId};
2121
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
2222
use crate::ln::features::ChannelTypeFeatures;
2323
use crate::ln::msgs;
@@ -288,6 +288,22 @@ pub enum BumpTransactionEvent {
288288
},
289289
}
290290

291+
/// Will be used in [`Event::HTLCIntercepted`] to identify the next hop in the HTLC's path.
292+
/// Currently only used in serialization for the sake of maintaining compatibility. More variants
293+
/// will be added for general-purpose HTLC forward intercepts as well as trampoline forward
294+
/// intercepts in upcoming work.
295+
enum InterceptNextHop {
296+
FakeScid {
297+
requested_next_hop_scid: u64,
298+
},
299+
}
300+
301+
impl_writeable_tlv_based_enum!(InterceptNextHop,
302+
(0, FakeScid) => {
303+
(0, requested_next_hop_scid, required),
304+
};
305+
);
306+
291307
/// An Event which you should probably take some action in response to.
292308
///
293309
/// Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
@@ -566,6 +582,20 @@ pub enum Event {
566582
/// now + 5*time_forwardable).
567583
time_forwardable: Duration,
568584
},
585+
/// Used to indicate that we've intercepted an HTLC forward.
586+
HTLCIntercepted {
587+
/// The fake scid that was programmed as the next hop's scid.
588+
requested_next_hop_scid: u64,
589+
/// The payment hash used for this HTLC.
590+
payment_hash: PaymentHash,
591+
/// How many msats are to be received on the inbound edge of this HTLC.
592+
inbound_amount_msat: u64,
593+
/// How many msats the payer intended to route to the next node. Depending on the reason you are
594+
/// intercepting this payment, you might take a fee by forwarding less than this amount.
595+
expected_outbound_amount_msat: u64,
596+
/// A id to help LDK identify which HTLC is being forwarded or failed.
597+
intercept_id: InterceptId
598+
},
569599
/// Used to indicate that an output which you should know how to spend was confirmed on chain
570600
/// and is now spendable.
571601
/// Such an output will *not* ever be spent by rust-lightning, and are not at risk of your
@@ -802,6 +832,17 @@ impl Writeable for Event {
802832
(0, WithoutLength(outputs), required),
803833
});
804834
},
835+
&Event::HTLCIntercepted { requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat, intercept_id } => {
836+
6u8.write(writer)?;
837+
let intercept_scid = InterceptNextHop::FakeScid { requested_next_hop_scid };
838+
write_tlv_fields!(writer, {
839+
(0, intercept_scid, required),
840+
(2, payment_hash, required),
841+
(4, inbound_amount_msat, required),
842+
(6, expected_outbound_amount_msat, required),
843+
(8, intercept_id, required)
844+
});
845+
}
805846
&Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id } => {
806847
7u8.write(writer)?;
807848
write_tlv_fields!(writer, {
@@ -1015,6 +1056,30 @@ impl MaybeReadable for Event {
10151056
};
10161057
f()
10171058
},
1059+
6u8 => {
1060+
let mut payment_hash = PaymentHash([0; 32]);
1061+
let mut intercept_id = InterceptId([0; 32]);
1062+
let mut requested_next_hop_scid = InterceptNextHop::FakeScid { requested_next_hop_scid: 0 };
1063+
let mut inbound_amount_msat = 0;
1064+
let mut expected_outbound_amount_msat = 0;
1065+
read_tlv_fields!(reader, {
1066+
(0, requested_next_hop_scid, required),
1067+
(2, payment_hash, required),
1068+
(4, inbound_amount_msat, required),
1069+
(6, expected_outbound_amount_msat, required),
1070+
(8, intercept_id, required)
1071+
});
1072+
let next_scid = match requested_next_hop_scid {
1073+
InterceptNextHop::FakeScid { requested_next_hop_scid: scid } => scid
1074+
};
1075+
Ok(Some(Event::HTLCIntercepted {
1076+
payment_hash,
1077+
requested_next_hop_scid: next_scid,
1078+
inbound_amount_msat,
1079+
expected_outbound_amount_msat,
1080+
intercept_id,
1081+
}))
1082+
},
10181083
7u8 => {
10191084
let f = || {
10201085
let mut fee_earned_msat = None;

0 commit comments

Comments
 (0)