Skip to content

Commit 6138779

Browse files
committed
ln+events: deprecate UnknownNextPeer in HTLCHandlingType
This variant of HTLCHandlingType contains infromation about the failure cause along with its type - as an UnknownNextPeer is just an InvalidForward that has the failure type UnknownNextPeer. This commit deprecates the variant's use, while still writing it to disk to allow the option to downgrade.
1 parent 30b1c3f commit 6138779

File tree

6 files changed

+56
-16
lines changed

6 files changed

+56
-16
lines changed

lightning-liquidity/src/lsps2/service.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,7 @@ where
882882
/// or if the payment queue is empty
883883
///
884884
/// [`Event::HTLCHandlingFailed`]: lightning::events::Event::HTLCHandlingFailed
885-
pub fn htlc_handling_failed(
886-
&self, handling_type: HTLCHandlingType,
887-
) -> Result<(), APIError> {
885+
pub fn htlc_handling_failed(&self, handling_type: HTLCHandlingType) -> Result<(), APIError> {
888886
if let HTLCHandlingType::ForwardFailed { channel_id, .. } = handling_type {
889887
let peer_by_channel_id = self.peer_by_channel_id.read().unwrap();
890888
if let Some(counterparty_node_id) = peer_by_channel_id.get(&channel_id) {

lightning/src/events/mod.rs

+45-3
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,16 @@ pub enum HTLCHandlingType {
479479
channel_id: ChannelId,
480480
},
481481
/// Scenario where we are unsure of the next node to forward the HTLC to.
482+
///
483+
/// Deprecated: will only be used in versions before LDK v0.2.0.
482484
UnknownNextHop {
483485
/// Short channel id we are requesting to forward an HTLC to.
484486
requested_forward_scid: u64,
485487
},
486488
/// We couldn't forward to the outgoing scid. An example would be attempting to send a duplicate
487489
/// intercept HTLC.
490+
///
491+
/// In LDK v0.2.0 and greater, this variant replaces [`Self::UnknownNextHop`].
488492
InvalidForward {
489493
/// Short channel id we are requesting to forward an HTLC to.
490494
requested_forward_scid: u64
@@ -1777,10 +1781,27 @@ impl Writeable for Event {
17771781
},
17781782
&Event::HTLCHandlingFailed { ref prev_channel_id, ref handling_type, ref handling_failure } => {
17791783
25u8.write(writer)?;
1784+
1785+
// The [`HTLCHandlingType::UnknownNextPeer`] variant is deprecated, but we want to
1786+
// continue writing it to allow downgrading. Detect the case where we're
1787+
// representing it as [`HTLCHandlingType::InvalidForward`] and
1788+
// [`LocalHTLCFailureReason::UnknownNextHop`] and write the old variant instead.
1789+
let downgradable_type = match (handling_type, handling_failure) {
1790+
(HTLCHandlingType::InvalidForward { requested_forward_scid },
1791+
Some(HTLCHandlingFailureReason::Local { reason }))
1792+
if *reason == LocalHTLCFailureReason::UnknownNextPeer =>
1793+
{
1794+
HTLCHandlingType::UnknownNextHop {
1795+
requested_forward_scid: *requested_forward_scid,
1796+
}
1797+
}
1798+
_ => handling_type.clone()
1799+
};
1800+
17801801
write_tlv_fields!(writer, {
17811802
(0, prev_channel_id, required),
17821803
(1, handling_failure, option),
1783-
(2, handling_type, required),
1804+
(2, downgradable_type, required),
17841805
})
17851806
},
17861807
&Event::BumpTransaction(ref event)=> {
@@ -2232,11 +2253,32 @@ impl MaybeReadable for Event {
22322253
(1, handling_failure, option),
22332254
(2, handling_type_opt, upgradable_required),
22342255
});
2235-
Ok(Some(Event::HTLCHandlingFailed {
2256+
2257+
let mut event = Event::HTLCHandlingFailed {
22362258
prev_channel_id,
22372259
handling_type: _init_tlv_based_struct_field!(handling_type_opt, upgradable_required),
22382260
handling_failure,
2239-
}))
2261+
};
2262+
2263+
// The [`HTLCHandlingType::UnknownNextPeer`] variant is deprecated, but we
2264+
// continue writing it to allow downgrading. If it was written, upgrade
2265+
// it to its new representation of [`HTLCHandlingType::InvalidForward`] and
2266+
// [`LocalHTLCFailureReason::UnknownNextHop`]. This will cover both the case
2267+
// where we have a legacy event
2268+
match event {
2269+
Event::HTLCHandlingFailed { ref handling_type, .. } => {
2270+
if let HTLCHandlingType::UnknownNextHop { requested_forward_scid } = handling_type {
2271+
event = Event::HTLCHandlingFailed {
2272+
prev_channel_id,
2273+
handling_type: HTLCHandlingType::InvalidForward { requested_forward_scid: *requested_forward_scid },
2274+
handling_failure: Some(LocalHTLCFailureReason::UnknownNextPeer.into()),
2275+
}
2276+
}
2277+
}
2278+
_ => panic!("HTLCHandlingFailed wrong type")
2279+
}
2280+
2281+
Ok(Some(event))
22402282
};
22412283
f()
22422284
},

lightning/src/ln/blinded_payment_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ fn do_forward_fail_in_process_pending_htlc_fwds(check: ProcessPendingHTLCsCheck,
626626

627627
$curr_node.node.process_pending_htlc_forwards();
628628
expect_htlc_handling_failed_destinations!($curr_node.node.get_and_clear_pending_events(),
629-
vec![HTLCHandlingType::UnknownNextHop { requested_forward_scid: $failed_scid }]);
629+
vec![HTLCHandlingType::InvalidForward { requested_forward_scid: $failed_scid }]);
630630
$curr_node.node.process_pending_htlc_forwards();
631631
},
632632
}
@@ -725,7 +725,7 @@ fn do_blinded_intercept_payment(intercept_node_fails: bool) {
725725

726726
if intercept_node_fails {
727727
nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
728-
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCHandlingType::UnknownNextHop { requested_forward_scid: intercept_scid }]);
728+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCHandlingType::InvalidForward { requested_forward_scid: intercept_scid }]);
729729
nodes[1].node.process_pending_htlc_forwards();
730730
check_added_monitors!(&nodes[1], 1);
731731
fail_blinded_htlc_backwards(payment_hash, 1, &[&nodes[0], &nodes[1]], false);

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5730,7 +5730,7 @@ where
57305730
});
57315731

57325732
let reason = HTLCFailReason::from_failure_code(LocalHTLCFailureReason::UnknownNextPeer);
5733-
let destination = HTLCHandlingType::UnknownNextHop { requested_forward_scid: short_channel_id };
5733+
let destination = HTLCHandlingType::InvalidForward { requested_forward_scid: short_channel_id };
57345734
self.fail_htlc_backwards_internal(&htlc_source, &payment.forward_info.payment_hash, &reason, destination);
57355735
} else { unreachable!() } // Only `PendingHTLCRouting::Forward`s are intercepted
57365736

@@ -5749,7 +5749,7 @@ where
57495749
node_id: Some(*outgoing_counterparty_node_id),
57505750
channel_id: *outgoing_channel_id,
57515751
},
5752-
None => HTLCHandlingType::UnknownNextHop {
5752+
None => HTLCHandlingType::InvalidForward {
57535753
requested_forward_scid: outgoing_scid,
57545754
},
57555755
}

lightning/src/ln/onion_route_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ fn test_onion_failure() {
549549
bogus_route.paths[0].hops[1].short_channel_id -= 1;
550550
let short_channel_id = bogus_route.paths[0].hops[1].short_channel_id;
551551
run_onion_failure_test("unknown_next_peer", 100, &nodes, &bogus_route, &payment_hash, &payment_secret, |_| {}, ||{}, true, Some(LocalHTLCFailureReason::UnknownNextPeer),
552-
Some(NetworkUpdate::ChannelFailure{short_channel_id, is_permanent:true}), Some(short_channel_id), Some(HTLCHandlingType::UnknownNextHop { requested_forward_scid: short_channel_id }));
552+
Some(NetworkUpdate::ChannelFailure{short_channel_id, is_permanent:true}), Some(short_channel_id), Some(HTLCHandlingType::InvalidForward { requested_forward_scid: short_channel_id }));
553553

554554
let short_channel_id = channels[1].0.contents.short_channel_id;
555555
let amt_to_forward = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
@@ -1751,7 +1751,7 @@ fn test_phantom_failure_modified_cltv() {
17511751
expect_pending_htlcs_forwardable!(nodes[1]);
17521752
expect_htlc_handling_failed_destinations!(
17531753
nodes[1].node.get_and_clear_pending_events(),
1754-
&[HTLCHandlingType::UnknownNextHop { requested_forward_scid: phantom_scid }]
1754+
&[HTLCHandlingType::InvalidForward { requested_forward_scid: phantom_scid }]
17551755
);
17561756
check_added_monitors(&nodes[1], 1);
17571757

@@ -1800,7 +1800,7 @@ fn test_phantom_failure_expires_too_soon() {
18001800
expect_pending_htlcs_forwardable!(nodes[1]);
18011801
expect_htlc_handling_failed_destinations!(
18021802
nodes[1].node.get_and_clear_pending_events(),
1803-
&[HTLCHandlingType::UnknownNextHop { requested_forward_scid: phantom_scid }]
1803+
&[HTLCHandlingType::InvalidForward { requested_forward_scid: phantom_scid }]
18041804
);
18051805
check_added_monitors(&nodes[1], 1);
18061806

@@ -1905,7 +1905,7 @@ fn do_test_phantom_dust_exposure_failure(multiplier_dust_limit: bool) {
19051905
expect_pending_htlcs_forwardable!(nodes[1]);
19061906
expect_htlc_handling_failed_destinations!(
19071907
nodes[1].node.get_and_clear_pending_events(),
1908-
&[HTLCHandlingType::UnknownNextHop { requested_forward_scid: phantom_scid }]
1908+
&[HTLCHandlingType::InvalidForward { requested_forward_scid: phantom_scid }]
19091909
);
19101910
check_added_monitors(&nodes[1], 1);
19111911

lightning/src/ln/payment_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1939,7 +1939,7 @@ fn do_test_intercepted_payment(test: InterceptTest) {
19391939
if test == InterceptTest::Fail {
19401940
// Ensure we can fail the intercepted payment back.
19411941
nodes[1].node.fail_intercepted_htlc(intercept_id).unwrap();
1942-
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCHandlingType::UnknownNextHop { requested_forward_scid: intercept_scid }]);
1942+
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCHandlingType::InvalidForward { requested_forward_scid: intercept_scid }]);
19431943
nodes[1].node.process_pending_htlc_forwards();
19441944
let update_fail = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
19451945
check_added_monitors!(&nodes[1], 1);
@@ -3406,7 +3406,7 @@ fn test_threaded_payment_retries() {
34063406
nodes[1].node.process_pending_htlc_forwards();
34073407
expect_htlc_handling_failed_destinations!(
34083408
nodes[1].node.get_and_clear_pending_events(),
3409-
&[HTLCHandlingType::UnknownNextHop { requested_forward_scid: route.paths[0].hops[1].short_channel_id }]
3409+
&[HTLCHandlingType::InvalidForward { requested_forward_scid: route.paths[0].hops[1].short_channel_id }]
34103410
);
34113411
check_added_monitors(&nodes[1], 1);
34123412

0 commit comments

Comments
 (0)