Skip to content

Commit d761469

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 e915c6b commit d761469

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

lightning/src/util/events.rs

Lines changed: 70 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
@@ -581,6 +597,24 @@ pub enum Event {
581597
/// now + 5*time_forwardable).
582598
time_forwardable: Duration,
583599
},
600+
/// Used to indicate that we've intercepted an HTLC forward.
601+
HTLCIntercepted {
602+
/// A id to help LDK identify which HTLC is being forwarded or failed.
603+
intercept_id: InterceptId,
604+
/// The fake scid that was programmed as the next hop's scid.
605+
requested_next_hop_scid: u64,
606+
/// The payment hash used for this HTLC.
607+
payment_hash: PaymentHash,
608+
/// How many msats were received on the inbound edge of this HTLC.
609+
inbound_amount_msat: u64,
610+
/// How many msats the payer intended to route to the next node. Depending on the reason you are
611+
/// intercepting this payment, you might take a fee by forwarding less than this amount.
612+
///
613+
/// Note that LDK will NOT check that expected fees were factored into this value. You MUST
614+
/// check that whatever fee you want has been included here or subtract it as required. Further,
615+
/// LDK will NOT stop you from forwarding more than you received.
616+
expected_outbound_amount_msat: u64,
617+
},
584618
/// Used to indicate that an output which you should know how to spend was confirmed on chain
585619
/// and is now spendable.
586620
/// Such an output will *not* ever be spent by rust-lightning, and are not at risk of your
@@ -819,6 +853,17 @@ impl Writeable for Event {
819853
(0, WithoutLength(outputs), required),
820854
});
821855
},
856+
&Event::HTLCIntercepted { requested_next_hop_scid, payment_hash, inbound_amount_msat, expected_outbound_amount_msat, intercept_id } => {
857+
6u8.write(writer)?;
858+
let intercept_scid = InterceptNextHop::FakeScid { requested_next_hop_scid };
859+
write_tlv_fields!(writer, {
860+
(0, intercept_id, required),
861+
(2, intercept_scid, required),
862+
(4, payment_hash, required),
863+
(6, inbound_amount_msat, required),
864+
(8, expected_outbound_amount_msat, required),
865+
});
866+
}
822867
&Event::PaymentForwarded { fee_earned_msat, prev_channel_id, claim_from_onchain_tx, next_channel_id } => {
823868
7u8.write(writer)?;
824869
write_tlv_fields!(writer, {
@@ -1042,6 +1087,30 @@ impl MaybeReadable for Event {
10421087
};
10431088
f()
10441089
},
1090+
6u8 => {
1091+
let mut payment_hash = PaymentHash([0; 32]);
1092+
let mut intercept_id = InterceptId([0; 32]);
1093+
let mut requested_next_hop_scid = InterceptNextHop::FakeScid { requested_next_hop_scid: 0 };
1094+
let mut inbound_amount_msat = 0;
1095+
let mut expected_outbound_amount_msat = 0;
1096+
read_tlv_fields!(reader, {
1097+
(0, intercept_id, required),
1098+
(2, requested_next_hop_scid, required),
1099+
(4, payment_hash, required),
1100+
(6, inbound_amount_msat, required),
1101+
(8, expected_outbound_amount_msat, required),
1102+
});
1103+
let next_scid = match requested_next_hop_scid {
1104+
InterceptNextHop::FakeScid { requested_next_hop_scid: scid } => scid
1105+
};
1106+
Ok(Some(Event::HTLCIntercepted {
1107+
payment_hash,
1108+
requested_next_hop_scid: next_scid,
1109+
inbound_amount_msat,
1110+
expected_outbound_amount_msat,
1111+
intercept_id,
1112+
}))
1113+
},
10451114
7u8 => {
10461115
let f = || {
10471116
let mut fee_earned_msat = None;

0 commit comments

Comments
 (0)