Skip to content

(2/3) Add Enum for HTLCHandlingFailed Reasons #3601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 16, 2025
Merged
10 changes: 7 additions & 3 deletions lightning/src/ln/async_payments_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::ln::msgs::{
BaseMessageHandler, ChannelMessageHandler, MessageSendEvent, OnionMessageHandler,
};
use crate::ln::offers_tests;
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
use crate::ln::onion_utils::LocalHTLCFailureReason;
Copy link
Contributor

@joostjager joostjager Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is just so intense. Getting nervous of reviewing so many trivial changes. I wonder if it is worth to:

  • Do the renames from err_code to reason separately in a non-review commit
  • Introduce the LocalHTLCFailureReason enum, still unused
  • Have a commit that contains just mechanical search/replaces that needs no review. Possibly list all the replaces that were applied.
  • Then finish with a commit with the semi-manual leftovers.

Maybe it's too much of an ask though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a shot at splitting this up 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is incredible how much more reviewable this change makes your PR. Thanks so much! The anxiety is gone 😅

use crate::ln::outbound_payment::PendingOutboundPayment;
use crate::ln::outbound_payment::Retry;
use crate::offers::invoice_request::InvoiceRequest;
Expand Down Expand Up @@ -179,7 +179,10 @@ fn invalid_keysend_payment_secret() {
assert_eq!(updates_2_1.update_fail_malformed_htlcs.len(), 1);
let update_malformed = &updates_2_1.update_fail_malformed_htlcs[0];
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(
update_malformed.failure_code,
LocalHTLCFailureReason::InvalidOnionBlinding.failure_code()
);
nodes[1]
.node
.handle_update_fail_malformed_htlc(nodes[2].node.get_our_node_id(), update_malformed);
Expand All @@ -196,7 +199,8 @@ fn invalid_keysend_payment_secret() {
&nodes[0],
payment_hash,
false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]),
PaymentFailedConditions::new()
.expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]),
);
}

Expand Down
47 changes: 23 additions & 24 deletions lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use crate::ln::inbound_payment::ExpandedKey;
use crate::ln::msgs;
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, UnsignedGossipMessage, MessageSendEvent};
use crate::ln::onion_payment;
use crate::ln::onion_utils;
use crate::ln::onion_utils::INVALID_ONION_BLINDING;
use crate::ln::onion_utils::{self, LocalHTLCFailureReason};
use crate::ln::outbound_payment::{Retry, IDEMPOTENCY_TIMEOUT_TICKS};
use crate::offers::invoice::UnsignedBolt12Invoice;
use crate::offers::nonce::Nonce;
Expand Down Expand Up @@ -118,7 +117,7 @@ pub fn fail_blinded_htlc_backwards(
match i {
0 => {
let mut payment_failed_conditions = PaymentFailedConditions::new()
.expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]);
.expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]);
if retry_expected {
payment_failed_conditions = payment_failed_conditions.retry_expected();
}
Expand All @@ -137,7 +136,7 @@ pub fn fail_blinded_htlc_backwards(
assert_eq!(blinded_node_updates.update_fail_malformed_htlcs.len(), 1);
let update_malformed = &blinded_node_updates.update_fail_malformed_htlcs[0];
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(update_malformed.failure_code, LocalHTLCFailureReason::InvalidOnionBlinding.failure_code());
nodes[i-1].node.handle_update_fail_malformed_htlc(nodes[i].node.get_our_node_id(), update_malformed);
do_commitment_signed_dance(&nodes[i-1], &nodes[i], &blinded_node_updates.commitment_signed, true, false);
}
Expand Down Expand Up @@ -437,11 +436,11 @@ fn do_forward_checks_failure(check: ForwardCheckFail, intro_fails: bool) {
match check {
ForwardCheckFail::ForwardPayloadEncodedAsReceive => {
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(0x4000 | 22, &[0; 0]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionPayload, &[0; 0]));
}
_ => {
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]));
}
};
return
Expand Down Expand Up @@ -469,20 +468,20 @@ fn do_forward_checks_failure(check: ForwardCheckFail, intro_fails: bool) {

let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
let update_malformed = &mut updates.update_fail_malformed_htlcs[0];
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(update_malformed.failure_code, LocalHTLCFailureReason::InvalidOnionBlinding.failure_code());
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);

// Ensure the intro node will properly blind the error if its downstream node failed to do so.
update_malformed.sha256_of_onion = [1; 32];
update_malformed.failure_code = INVALID_ONION_BLINDING ^ 1;
update_malformed.failure_code = LocalHTLCFailureReason::InvalidOnionBlinding.failure_code() ^ 1;
nodes[1].node.handle_update_fail_malformed_htlc(nodes[2].node.get_our_node_id(), update_malformed);
do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, true, false);

let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]));
}

#[test]
Expand Down Expand Up @@ -534,7 +533,7 @@ fn failed_backwards_to_intro_node() {
let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
let mut update_malformed = &mut updates.update_fail_malformed_htlcs[0];
// Check that the final node encodes its failure correctly.
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(update_malformed.failure_code, LocalHTLCFailureReason::InvalidOnionBlinding.failure_code());
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);

// Modify such the final hop does not correctly blind their error so we can ensure the intro node
Expand All @@ -547,7 +546,7 @@ fn failed_backwards_to_intro_node() {
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]));
}

enum ProcessPendingHTLCsCheck {
Expand Down Expand Up @@ -655,20 +654,20 @@ fn do_forward_fail_in_process_pending_htlc_fwds(check: ProcessPendingHTLCsCheck,

let mut updates = get_htlc_update_msgs!(nodes[2], nodes[1].node.get_our_node_id());
let update_malformed = &mut updates.update_fail_malformed_htlcs[0];
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(update_malformed.failure_code, LocalHTLCFailureReason::InvalidOnionBlinding.failure_code());
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);

// Ensure the intro node will properly blind the error if its downstream node failed to do so.
update_malformed.sha256_of_onion = [1; 32];
update_malformed.failure_code = INVALID_ONION_BLINDING ^ 1;
update_malformed.failure_code = LocalHTLCFailureReason::InvalidOnionBlinding.failure_code() ^ 1;
nodes[1].node.handle_update_fail_malformed_htlc(nodes[2].node.get_our_node_id(), update_malformed);
do_commitment_signed_dance(&nodes[1], &nodes[2], &updates.commitment_signed, true, false);

let mut updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]));
}

#[test]
Expand Down Expand Up @@ -1042,7 +1041,7 @@ fn do_multi_hop_receiver_fail(check: ReceiveCheckFail) {
assert_eq!(updates_2_1.update_fail_malformed_htlcs.len(), 1);
let update_malformed = &updates_2_1.update_fail_malformed_htlcs[0];
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(update_malformed.failure_code, LocalHTLCFailureReason::InvalidOnionBlinding.failure_code());
nodes[1].node.handle_update_fail_malformed_htlc(nodes[2].node.get_our_node_id(), update_malformed);
do_commitment_signed_dance(&nodes[1], &nodes[2], &updates_2_1.commitment_signed, true, false);

Expand All @@ -1064,7 +1063,7 @@ fn do_multi_hop_receiver_fail(check: ReceiveCheckFail) {
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates_1_0.update_fail_htlcs[0]);
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates_1_0.commitment_signed, false, false);
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]));
}

#[test]
Expand Down Expand Up @@ -1131,7 +1130,7 @@ fn blinded_path_retries() {
assert_eq!(updates.update_fail_malformed_htlcs.len(), 1);
let update_malformed = &updates.update_fail_malformed_htlcs[0];
assert_eq!(update_malformed.sha256_of_onion, [0; 32]);
assert_eq!(update_malformed.failure_code, INVALID_ONION_BLINDING);
assert_eq!(update_malformed.failure_code, LocalHTLCFailureReason::InvalidOnionBlinding.failure_code());
$intro_node.node.handle_update_fail_malformed_htlc(nodes[3].node.get_our_node_id(), update_malformed);
do_commitment_signed_dance(&$intro_node, &nodes[3], &updates.commitment_signed, true, false);

Expand Down Expand Up @@ -1251,7 +1250,7 @@ fn min_htlc() {
nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
do_commitment_signed_dance(&nodes[0], &nodes[1], &updates.commitment_signed, false, false);
expect_payment_failed_conditions(&nodes[0], payment_hash, false,
PaymentFailedConditions::new().expected_htlc_error_data(INVALID_ONION_BLINDING, &[0; 32]));
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionBlinding, &[0; 32]));
}

#[test]
Expand Down Expand Up @@ -1446,7 +1445,7 @@ fn fails_receive_tlvs_authentication() {
commitment_signed_dance!(nodes[0], nodes[1], update_fail.commitment_signed, false);
expect_payment_failed_conditions(
&nodes[0], payment_hash, true,
PaymentFailedConditions::new().expected_htlc_error_data(0x4000 | 22, &[]),
PaymentFailedConditions::new().expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionPayload, &[]),
);
}

Expand Down Expand Up @@ -1728,7 +1727,8 @@ fn route_blinding_spec_test_vector() {
match onion_payment::decode_incoming_update_add_htlc_onion(
&eve_update_add, &eve_node_signer, &logger, &secp_ctx
) {
Err(HTLCFailureMsg::Malformed(msg)) => assert_eq!(msg.failure_code, INVALID_ONION_BLINDING),
Err((HTLCFailureMsg::Malformed(msg), _)) => assert_eq!(msg.failure_code,
LocalHTLCFailureReason::InvalidOnionBlinding.failure_code()),
_ => panic!("Unexpected error")
}
}
Expand Down Expand Up @@ -2160,7 +2160,7 @@ fn do_test_trampoline_single_hop_receive(success: bool) {
}
{
let payment_failed_conditions = PaymentFailedConditions::new()
.expected_htlc_error_data(0x4000 | 22, &[0; 0]);
.expected_htlc_error_data(LocalHTLCFailureReason::InvalidOnionPayload, &[0; 0]);
expect_payment_failed_conditions(&nodes[0], payment_hash, true, payment_failed_conditions);
}
}
Expand Down Expand Up @@ -2453,10 +2453,9 @@ fn test_trampoline_forward_rejection() {
do_commitment_signed_dance(&nodes[0], &nodes[1], &unblinded_node_updates.commitment_signed, false, false);
}
{
// Expect a PERM|10 (unknown_next_peer) error while we are unable to route forwarding
// Trampoline payments.
// Expect UnknownNextPeer error while we are unable to route forwarding Trampoline payments.
let payment_failed_conditions = PaymentFailedConditions::new()
.expected_htlc_error_data(0x4000 | 10, &[0; 0]);
.expected_htlc_error_data(LocalHTLCFailureReason::UnknownNextPeer, &[0; 0]);
expect_payment_failed_conditions(&nodes[0], payment_hash, false, payment_failed_conditions);
}
}
Loading
Loading