Skip to content

Refactor BlindedPath construction utils #3248

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

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Aug 16, 2024

When constructing a BlindedPath, the construct_blinded_hops utility function requires two iterators: one for public keys and another for TLVs. The first is used by the construct_keys_callback function while the second is used in a callback passed to that function.

There are few disadvantages to this:

  • The iterators are assumed to be the same length otherwise there will be a panic.
  • Iterators over the same data cannot have one be over mutable references.
  • The Option<Destination> parameter is not relevant as it is only used for constructing the full onion.

This PR refactors the utility functions such that only a single iterator is used. This is useful as an iterator over mutable TLVs can be used to add padding, in particular for payment paths when adding dummy hops. See #3177 (comment).

Copy link

codecov bot commented Aug 16, 2024

Codecov Report

Attention: Patch coverage is 98.41270% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.79%. Comparing base (bbfa15e) to head (76c9fda).
Report is 45 commits behind head on main.

Files Patch % Lines
lightning/src/blinded_path/utils.rs 97.67% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3248      +/-   ##
==========================================
- Coverage   89.82%   89.79%   -0.03%     
==========================================
  Files         125      125              
  Lines      102867   102882      +15     
  Branches   102867   102882      +15     
==========================================
- Hits        92398    92388      -10     
- Misses       7753     7777      +24     
- Partials     2716     2717       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@arik-so
Copy link
Contributor

arik-so commented Aug 20, 2024

Needs a rebase.

jkczyz added 7 commits August 20, 2024 16:00
When constructing a BlindedPath, utils::construct_blinded_hops uses two
iterators. However, this makes it difficult to pad blinded hops to equal
sizes without allocating a vector or cloning data. Refactor the
construct_keys_callback utility function so that is can be used with an
Iterator with different Item types. This allows using a single Iterator
of tuples while still supporting its use only with pubkeys.
Instead of accepting iterators yielding PublicKey by reference in
utils::construct_keys_callback, take iterators yielding values since
these implement Copy and need to be copied anyway. This will help avoid
a situation when padding where both a reference and mutable reference
are needed.
When constructing a BlindedPath, an Option<Destination> parameter to
construct_keys_callback is never passed. Make a separate utility
function that doesn't take this parameter. This allows for using the
build_keys_helper macro with a Iterator yielding items other than
PublicKey.
When constructing a blinded path, two iterators are used: one for the
pubkeys and another for Writeable TLVs. The first iterator is used in
the build_keys_helper utility function while the second is used inside
of a callback. Update this utility to work on any type that can be
borrowed as a PublicKey. This allows for using a single iterator of
tuples, which is necessary for padding the hops without additional
allocations and clones.
Instead of using separate iterators for pubkeys and TLVs, use an
iterator that yields a tuple of them. This allows for holding a mutable
reference to the TLVs such that they can be padded. With two iterators,
the pubkey iterator would have a reference to the ForwardNode slice when
constructing a payment path. However, this would prevent holding mutable
references in the TLVs iterator.
Copy link
Member

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

@dunxen dunxen merged commit cf2fa9d into lightningdevkit:main Aug 27, 2024
20 of 21 checks passed
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.

6 participants