Skip to content
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

Clean up create_onion_keys_callback_generic #3668

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

TheBlueMatt
Copy link
Collaborator

It should return an iterator, not rely on callbacks (I think it predates impl Iterator lol) and doesn't need to return a Result.

There's no reason for anything outside of `onion_utils` to call an
`_inner` method, so it shouldn't be pub.
Onion keys construction can only fail if we have a key which is
the inverse of a hash containing a public key which we generated
(even indirectly). Because we prefer to panic if our RNG creates an
insecure key (as it indicates operating is incredibly unsafe), we
thus don't actually need to return an error from
`construct_onion_keys_generic_callback`, which we clean up here.
The onion keys building logic is rather ancient and predates the
`-> impl Trait` syntax in Rust, and thus used a callback. Here we
move it to an `impl Iterator` which is much Rustier and cleans up
the code a bit.
Now that `create_onion_keys_generic` returns an iterator rather
than relying on a callback, making `process_onion_failure_inner`
chain the non-trampoline and trampoline iterators is trivial.

We do so here, avoiding the extra vec allocation.
@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Mar 14, 2025

👋 Thanks for assigning @arik-so as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@ldk-reviews-bot
Copy link

🔔 1st Reminder

Hey @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

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.

Not sure how strongly we feel about tests passing in intermediate commits blocking PRs. Feel free to land IMO

@@ -2163,8 +2149,7 @@ mod tests {
let route = Route { paths: vec![path], route_params: None };

let onion_keys =
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key())
.unwrap();
super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key());
Copy link
Contributor

Choose a reason for hiding this comment

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

This diff and below in 7f22871 belong in a prior commit

@ldk-reviews-bot
Copy link

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@arik-so arik-so self-requested a review March 18, 2025 00:19
)
.expect("Route we used spontaneously grew invalid keys in the middle of it?");
// if we have Trampoline hops, the blinded hops are part of the inner Trampoline onion
let nontrampoline_bp =
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not a blinding_point, it's the blinded_tail. Also, you think it makes sense to unify the terminology between non_trampoline_ and/or outer?

let unblinded_hops_iter = hops.iter().map(|h| (h.node_pubkey(), Some(h)));
let blinded_pks_iter = blinded_tail
let unblinded_hops = hops.iter().map(|h| (h.node_pubkey(), Some(h)));
let blinded_pks = blinded_tail
.map(|t| t.hops.iter())
Copy link
Contributor

Choose a reason for hiding this comment

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

eh, variable names are cheap, we can call these pubkeys. It'll help readers coming from code bases where the counterpart is private_key and not secret_key.

inner_session_priv.expect("Trampoline hops always have an inner session priv");
Some(construct_onion_keys_generic(secp_ctx, hops, blinded_tail, inner_session_priv).map(
|(shared_secret, _, _, route_hop_option, _)| {
(route_hop_option.map(|th| ErrorHop::TrampolineHop(th)), shared_secret)
},
Copy link
Contributor

Choose a reason for hiding this comment

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

if we're renaming this, let's also do the closure parameter rh, or leave the outer as trampoline_hop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants