Skip to content

Commit ce91315

Browse files
committed
Expose the outgoing HTLC's CLTV expiry in Event::HTLCIntercepted
In the previous commit our interception documentation noted that in some cases devs may wish to validate the CLTV expiry of HTLCs before forwarding. Here we enable that by exposing the next-hop's CLTV value in `Event::HTLCIntercepted`
1 parent a0723ad commit ce91315

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

lightning/src/events/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,13 @@ pub enum Event {
12871287
/// [`Self::HTLCIntercepted::inbound_amount_msat`]) or subtract it as required. Further,
12881288
/// LDK will not stop you from forwarding more than you received.
12891289
expected_outbound_amount_msat: u64,
1290+
/// The block height at which the forwarded HTLC sent to our peer will time out. In
1291+
/// practice, LDK will refuse to forward an HTLC several blocks before this height (as if
1292+
/// we attempted to forward an HTLC at this height we'd run some risk that our peer
1293+
/// force-closes the channel immediately).
1294+
///
1295+
/// This will only be `None` for events generated or serialized by LDK 0.2 or prior.
1296+
outgoing_htlc_expiry_block_height: Option<u32>,
12901297
},
12911298
/// Used to indicate that an output which you should know how to spend was confirmed on chain
12921299
/// and is now spendable.
@@ -2017,11 +2024,13 @@ impl Writeable for Event {
20172024
inbound_amount_msat,
20182025
expected_outbound_amount_msat,
20192026
intercept_id,
2027+
outgoing_htlc_expiry_block_height,
20202028
} => {
20212029
6u8.write(writer)?;
20222030
let intercept_scid = InterceptNextHop::FakeScid { requested_next_hop_scid };
20232031
write_tlv_fields!(writer, {
20242032
(0, intercept_id, required),
2033+
(1, outgoing_htlc_expiry_block_height, option),
20252034
(2, intercept_scid, required),
20262035
(4, payment_hash, required),
20272036
(6, inbound_amount_msat, required),
@@ -2526,8 +2535,10 @@ impl MaybeReadable for Event {
25262535
InterceptNextHop::FakeScid { requested_next_hop_scid: 0 };
25272536
let mut inbound_amount_msat = 0;
25282537
let mut expected_outbound_amount_msat = 0;
2538+
let mut outgoing_htlc_expiry_block_height = None;
25292539
read_tlv_fields!(reader, {
25302540
(0, intercept_id, required),
2541+
(1, outgoing_htlc_expiry_block_height, option),
25312542
(2, requested_next_hop_scid, required),
25322543
(4, payment_hash, required),
25332544
(6, inbound_amount_msat, required),
@@ -2542,6 +2553,7 @@ impl MaybeReadable for Event {
25422553
inbound_amount_msat,
25432554
expected_outbound_amount_msat,
25442555
intercept_id,
2556+
outgoing_htlc_expiry_block_height,
25452557
}))
25462558
},
25472559
7u8 => {

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,6 +3409,7 @@ fn create_htlc_intercepted_event(
34093409
inbound_amount_msat,
34103410
expected_outbound_amount_msat: pending_add.forward_info.outgoing_amt_msat,
34113411
intercept_id,
3412+
outgoing_htlc_expiry_block_height: Some(pending_add.forward_info.outgoing_cltv_value),
34123413
})
34133414
}
34143415

lightning/src/ln/payment_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,17 +2277,20 @@ fn do_test_intercepted_payment(test: InterceptTest) {
22772277
// Check that we generate the PaymentIntercepted event when an intercept forward is detected.
22782278
let events = nodes[1].node.get_and_clear_pending_events();
22792279
assert_eq!(events.len(), 1);
2280+
let expected_cltv = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 1;
22802281
let (intercept_id, outbound_amt) = match events[0] {
22812282
crate::events::Event::HTLCIntercepted {
22822283
intercept_id,
22832284
expected_outbound_amount_msat,
22842285
payment_hash,
22852286
inbound_amount_msat,
22862287
requested_next_hop_scid: short_channel_id,
2288+
outgoing_htlc_expiry_block_height,
22872289
} => {
22882290
assert_eq!(payment_hash, hash);
22892291
assert_eq!(inbound_amount_msat, route.get_total_amount() + route.get_total_fees());
22902292
assert_eq!(short_channel_id, intercept_scid);
2293+
assert_eq!(outgoing_htlc_expiry_block_height.unwrap(), expected_cltv);
22912294
(intercept_id, expected_outbound_amount_msat)
22922295
},
22932296
_ => panic!(),
@@ -2356,6 +2359,7 @@ fn do_test_intercepted_payment(test: InterceptTest) {
23562359
assert_eq!(events.len(), 1);
23572360
SendEvent::from_event(events.remove(0))
23582361
};
2362+
assert_eq!(payment_event.msgs[0].cltv_expiry, expected_cltv);
23592363
nodes[2].node.handle_update_add_htlc(node_b_id, &payment_event.msgs[0]);
23602364
let commitment = &payment_event.commitment_msg;
23612365
do_commitment_signed_dance(&nodes[2], &nodes[1], commitment, false, true);

0 commit comments

Comments
 (0)