Skip to content

Eliminate invalid Hop/InboundOnionPayload combinations #3598

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

Conversation

arik-so
Copy link
Contributor

@arik-so arik-so commented Feb 13, 2025

We previously had nested enums of InboundOnionPayload inside of Hop with impossible combinations such as Receive data inside Forward variants and vice versa. Now this is no longer possible, we will be able to better represent decoded information for inner (Trampoline) onions.

Builds on top of #3595.

@arik-so arik-so changed the title Eliminate invalid state combinations for Hop/InboundOnionPayload combinations Eliminate invalid Hop/InboundOnionPayload combinations Feb 13, 2025
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

New commits basically LGTM (I assume the [expound] tag on the commits is a note to yourself to add more to the commit messages?)

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

Nice refactor!

Comment on lines 5889 to 5903
onion_utils::Hop::Receive(hop_data) => {
let current_height: u32 = self.best_block.read().unwrap().height;
match create_recv_pending_htlc_info(hop_data,
match create_recv_pending_htlc_info(msgs::InboundOnionPayload::Receive(hop_data),
incoming_shared_secret, payment_hash, outgoing_amt_msat,
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
current_height)
{
Ok(info) => phantom_receives.push((
prev_short_channel_id, prev_counterparty_node_id, prev_funding_outpoint,
prev_channel_id, prev_user_channel_id, vec![(info, prev_htlc_id)]
)),
Err(InboundHTLCErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
}
},
onion_utils::Hop::BlindedReceive(hop_data) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you can extract hop_data like

let hop_data = match next_hop {
	onion_utils::Hop::Receive(hop_data) => msgs::InboundOnionPayload::Receive(hop_data),
	onion_utils::Hop::BlindedReceive(hop_data) => msgs::InboundOnionPayload::BlindedReceive(hop_data),
	_ => panic!()
};

and avoid repeating the code here. Maybe other areas where this commit could be DRY'd as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there are some other places where the repetition is just one or two lines, so I thought drying those would actually result in more code (which should be called lint imo), but perhaps there's a better way I didn't see

@arik-so
Copy link
Contributor Author

arik-so commented Feb 13, 2025

Thanks! As I clean this up (and yes, expound on the comments ;) ) the biggest thing I think I'd like you to take a look at is the modification of your blinded payment unit test, because we're detecting an invalid state earlier and consequently, returning a different error code.

@valentinewallace
Copy link
Contributor

Blinded path tests changes LGTM!

In an upcoming commit, we will eliminate various invalid state
combinations between `Hop` and `InboundOnionPayload` enums. To do so,
rather than nesting one within the other, we will instead have them
both referring to the same structs, with variant-dependent supplemental
data. This requires pulling each variant's data into its own type.
@arik-so arik-so force-pushed the arik/trampoline/inbound-prefactors-02 branch from 05bbdb9 to c1ade72 Compare February 14, 2025 07:54
@arik-so arik-so added the weekly goal Someone wants to land this this week label Feb 14, 2025
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Please squash and let's land this!

@@ -322,15 +322,27 @@ where
// TODO: If this is potentially a phantom payment we should decode the phantom payment
// onion here and check it.

let inbound_onion_payload = match hop {
Copy link
Collaborator

Choose a reason for hiding this comment

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

super nit: move this up to right under the match so its clear from context why the last arm is unreachable.

Now that each `InboundOnionPayload` variant corresponds to its own
struct, we can reference these same types inside `Hop` and thereby
avoid nesting that allowed invalid combinations, and instead store
supplemental data as each variant calls for.
@arik-so arik-so force-pushed the arik/trampoline/inbound-prefactors-02 branch from c1ade72 to 38284a0 Compare February 14, 2025 18:10
@TheBlueMatt TheBlueMatt merged commit 1613f87 into lightningdevkit:main Feb 14, 2025
24 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weekly goal Someone wants to land this this week
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants