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

util: Adds Into<PaymentHash> for PaymentPreimage #2916

Merged

Conversation

sr-gi
Copy link
Contributor

@sr-gi sr-gi commented Feb 29, 2024

Currently, there is no way to build a PaymentHash from a PaymentPreimage (apart from doing it manually). This seems like a useful interface to have for downstream users.

Copy link

coderabbitai bot commented Feb 29, 2024

Walkthrough

The update refines the import structure in lightning/src/ln/mod.rs, now utilizing Sha256 and Hash from bitcoin::hashes. Additionally, it implements the Into<PaymentHash> trait for PaymentPreimage, enabling seamless conversion to enhance payment processing capabilities.

Changes

File Path Change Summary
lightning/src/ln/mod.rs Updated imports for Sha256 and Hash from bitcoin::hashes. Implemented Into<PaymentHash> for PaymentPreimage.

🐇✨
In the realm of code, where secrets hide,
New paths are forged, with hashes as guide.
From preimage to hash, a leap so bold,
A tale of transformation, gracefully told.
Through lines of change, our journey's spun,
In the glow of bytes, a new dawn begun.
🌟🐾

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cfb4391 and 8c1ce0a.
Files selected for processing (1)
  • lightning/src/ln/mod.rs (3 hunks)
Additional comments: 1
lightning/src/ln/mod.rs (1)
  • 89-89: The imports from bitcoin::hashes are correctly added and used in the implementation of the Into<PaymentHash> trait for PaymentPreimage. This aligns with the PR objectives to facilitate the conversion between PaymentPreimage and PaymentHash.

@@ -34,6 +34,7 @@ pub mod channel;
#[cfg(not(fuzzing))]
pub(crate) mod channel;

use bitcoin::secp256k1::ThirtyTwoByteHash;
Copy link

Choose a reason for hiding this comment

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

The import of ThirtyTwoByteHash from bitcoin::secp256k1 is added, but it appears to be unused in the provided code changes. Ensure that this import is necessary for the current implementation or future extensions. If it's not used, consider removing it to keep the code clean and maintainable.

Comment on lines 115 to 118
impl Into<PaymentHash> for PaymentPreimage {
fn into(self) -> PaymentHash {
PaymentHash(Sha256::hash(&self.0).into_32())
}
Copy link

Choose a reason for hiding this comment

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

The implementation of the Into<PaymentHash> trait for PaymentPreimage is concise and leverages the Sha256::hash function to perform the conversion. This is a straightforward and efficient way to achieve the desired functionality. However, consider adding documentation comments to explain the conversion process for future maintainers and users of the code. Enhancing documentation can significantly improve code readability and maintainability.

+ /// Converts a `PaymentPreimage` into a `PaymentHash` by hashing the preimage with SHA256.
  impl Into<PaymentHash> for PaymentPreimage {
      fn into(self) -> PaymentHash {
          PaymentHash(Sha256::hash(&self.0).into_32())
      }
  }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
impl Into<PaymentHash> for PaymentPreimage {
fn into(self) -> PaymentHash {
PaymentHash(Sha256::hash(&self.0).into_32())
}
/// Converts a `PaymentPreimage` into a `PaymentHash` by hashing the preimage with SHA256.
impl Into<PaymentHash> for PaymentPreimage {
fn into(self) -> PaymentHash {
PaymentHash(Sha256::hash(&self.0).into_32())
}

@codecov-commenter
Copy link

codecov-commenter commented Feb 29, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 89.30%. Comparing base (affe557) to head (8c1ce0a).
Report is 15 commits behind head on main.

❗ Current head 8c1ce0a differs from pull request most recent head d2ffcbc. Consider uploading reports for the commit d2ffcbc to get more accurate results

Files Patch % Lines
lightning/src/ln/mod.rs 0.00% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2916      +/-   ##
==========================================
+ Coverage   89.12%   89.30%   +0.18%     
==========================================
  Files         117      117              
  Lines       94456    96484    +2028     
  Branches    94456    96484    +2028     
==========================================
+ Hits        84180    86163    +1983     
- Misses       7800     7868      +68     
+ Partials     2476     2453      -23     

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

This seems like a useful interface to have for downstream users
@sr-gi sr-gi force-pushed the 2024-02-29-payment-preimage-hash branch from 8c1ce0a to d2ffcbc Compare February 29, 2024 20:41
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cfb4391 and d2ffcbc.
Files selected for processing (1)
  • lightning/src/ln/mod.rs (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • lightning/src/ln/mod.rs

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.

Thanks!

@TheBlueMatt
Copy link
Collaborator

This is super trivial, it just hashes once...gonna go ahead and land this.

@TheBlueMatt TheBlueMatt merged commit bf3bc42 into lightningdevkit:main Feb 29, 2024
12 of 16 checks passed
@tnull
Copy link
Contributor

tnull commented Mar 1, 2024

Post-merge nit: the std library docs state that implementing Into should be avoided:

One should avoid implementing Into and implement From instead. Implementing From automatically provides one with an implementation of Into thanks to the blanket implementation in the standard library.

Fixed in #2918.

@sr-gi
Copy link
Contributor Author

sr-gi commented Mar 1, 2024

Post-merge nit: the std library docs state that implementing Into should be avoided:

One should avoid implementing Into and implement From instead. Implementing From automatically provides one with an implementation of Into thanks to the blanket implementation in the standard library.

Fixed in #2918.

I always get this the other way around :/

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