Skip to content

Store HumanReadableNames in-object rather than on the heap #3733

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 1 commit into from
Apr 17, 2025

Conversation

TheBlueMatt
Copy link
Collaborator

Since the full encoded domain name of an HRN cannot exceed the maximum length of a DNS name (255 octets), there's not a lot of reason to store the user and domain parts of an HRN on the heap via two Strings.

Instead, here, we store one byte array with the maximum size of both labels as well as the length of the user and domain parts.

Because we're now avoiding heap allocations this also implies making HumanReadableName::new take the user and domain parts by reference as &strs, rather than by value as Strings.

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Apr 12, 2025

👋 Thanks for assigning @valentinewallace 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.

Basically LGTM

Ok(user) => user,
Err(_) => return Err(DecodeError::InvalidValue),
};

let mut domain_bytes = [0; 255];
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to not use the 231 value mentioned above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It would make the below read_exact call able to panic if user_len is greater than 231 bytes. Seems simpler to expand stack a bit than bother checking it.

@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.

@valentinewallace
Copy link
Contributor

LGTM, feel free to squash

@ldk-reviews-bot
Copy link

✅ Added second reviewer: @jkczyz

jkczyz
jkczyz previously approved these changes Apr 16, 2025
Since the full encoded domain name of an HRN cannot exceed the
maximum length of a DNS name (255 octets), there's not a lot of
reason to store the `user` and `domain` parts of an HRN on the heap
via two `String`s.

Instead, here, we store one byte array with the maximum size of
both labels as well as the length of the `user` and `domain` parts.

Because we're now avoiding heap allocations this also implies
making `HumanReadableName::new` take the `user` and `domain` parts
by reference as `&str`s, rather than by value as `String`s.
@TheBlueMatt
Copy link
Collaborator Author

Just fixed the spelling issue:

$ git diff-tree -U1 7365ed8f6 7535a6078
diff --git a/lightning/src/onion_message/dns_resolution.rs b/lightning/src/onion_message/dns_resolution.rs
index 58237bc29..df08787ad 100644
--- a/lightning/src/onion_message/dns_resolution.rs
+++ b/lightning/src/onion_message/dns_resolution.rs
@@ -190,3 +190,3 @@ const REQUIRED_EXTRA_LEN: usize = ".user._bitcoin-payment.".len() + 1;
 /// If you intend to handle non-ASCII `user` or `domain` parts, you must handle [Homograph Attacks]
-/// and do punycode en-/de-coding yourself. This struc will always handle only plain ASCII `user`
+/// and do punycode en-/de-coding yourself. This struct will always handle only plain ASCII `user`
 /// and `domain` parts.

Copy link

codecov bot commented Apr 16, 2025

Codecov Report

Attention: Patch coverage is 75.00000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 89.83%. Comparing base (83e9e80) to head (7535a60).
Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/onion_message/dns_resolution.rs 75.00% 1 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3733      +/-   ##
==========================================
+ Coverage   89.12%   89.83%   +0.70%     
==========================================
  Files         156      156              
  Lines      123514   129420    +5906     
  Branches   123514   129420    +5906     
==========================================
+ Hits       110086   116263    +6177     
+ Misses      10749    10545     -204     
+ Partials     2679     2612      -67     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TheBlueMatt TheBlueMatt merged commit c6921fa into lightningdevkit:main Apr 17, 2025
26 of 27 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.

4 participants