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

Use consistent cltv_expiry_delta in ForwardTlvs #2831

Merged

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Jan 16, 2024

When converting from CounterpartyForwardingInfo to PaymentRelay, the cltv_expiry_delta is copied. Then, when forming a blinded payment path, the value is mutated so that esoteric values don't reveal information about the path. However, the value was only used in computing PaymentConstraints and wasn't actually updated in PaymentRelay. Move the logic for modifying the cltv_expiry_delta to the conversion code to avoid this inconsistency.

When converting from CounterpartyForwardingInfo to PaymentRelay, the
cltv_expiry_delta is copied. Then, when forming a blinded payment path,
the value is mutated so that esoteric values don't reveal information
about the path. However, the value was only used in computing
PaymentConstraints and wasn't actually updated in PaymentRelay. Move the
logic for modifying the cltv_expiry_delta to the conversion code to
avoid this inconsistency.
Copy link

coderabbitai bot commented Jan 16, 2024

Walkthrough

The code update involves refining the logic for converting CounterpartyForwardingInfo to PaymentRelay, particularly with error handling and value restrictions for cltv_expiry_delta. Simplifications in router.rs include a more straightforward assignment of cltv_expiry_delta and an enhanced forwarding_info handling using a match expression and try_into() for conversions.

Changes

Files Summary
.../blinded_path/payment.rs, .../routing/router.rs Updated TryFrom trait implementation for CounterpartyForwardingInfo to PaymentRelay with improved error handling and value range checks for cltv_expiry_delta. Simplified cltv_expiry_delta assignment in router.rs and enhanced forwarding_info handling with match and try_into().

🐇✨
In the land of code, where the logic threads weave,
A rabbit hopped through, with a fix up its sleeve.
cltv_expiry now checked, errors neatly parried,
With a match and a hop, the PaymentRelay carried. 🌩️🚀

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.

@jkczyz jkczyz added this to the 0.0.120 milestone Jan 16, 2024
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: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ea5de93 and 8053aa3.
Files selected for processing (2)
  • lightning/src/blinded_path/payment.rs (1 hunks)
  • lightning/src/routing/router.rs (1 hunks)
Additional comments: 8
lightning/src/blinded_path/payment.rs (6)
  • 117-117: The implementation of the Writeable trait for ForwardTlvs, ReceiveTlvs, and BlindedPaymentTlvsRef appears to be correct and follows the expected pattern for encoding TLV streams.
  • 117-117: The implementation of the Readable trait for BlindedPaymentTlvs correctly decodes the TLV stream and handles optional and required fields appropriately.
  • 117-117: The blinded_hops function correctly chains iterators for public keys and TLV references and uses the utils::construct_blinded_hops function to construct the blinded hops.
  • 117-117: The amt_to_forward_msat function correctly calculates the amount to forward after fees and handles potential underflow with checked arithmetic.
  • 117-117: The compute_payinfo function aggregates fee and CLTV information across a path of nodes and correctly handles potential overflow and underflow with checked arithmetic.
  • 117-117: The test cases provided in the tests module cover a range of scenarios, including single-hop and multi-hop paths, and check for correct fee and CLTV delta aggregation. They appear to be comprehensive and correctly assert the expected outcomes.
lightning/src/routing/router.rs (2)
  • 117-120: The refactoring of the forwarding_info handling using match and try_into() is a good improvement for clarity and error handling.
  • 124-124: The direct assignment of cltv_expiry_delta from payment_relay after converting it to u32 simplifies the code. However, ensure that the type conversion is safe and will not cause overflow issues, given that cltv_expiry_delta is expected to be a small number in the context of payment channels.

Comment on lines +100 to +117
impl TryFrom<CounterpartyForwardingInfo> for PaymentRelay {
type Error = ();

fn try_from(info: CounterpartyForwardingInfo) -> Result<Self, ()> {
let CounterpartyForwardingInfo {
fee_base_msat, fee_proportional_millionths, cltv_expiry_delta
} = info;
Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat }

// Avoid exposing esoteric CLTV expiry deltas
let cltv_expiry_delta = match cltv_expiry_delta {
0..=40 => 40,
41..=80 => 80,
81..=144 => 144,
145..=216 => 216,
_ => return Err(()),
};

Ok(Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat })
Copy link

Choose a reason for hiding this comment

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

The error type for the TryFrom trait implementation is a unit type (). This provides no information about why the conversion failed. It would be more helpful to define a custom error type that can provide context on failure.

Additionally, the logic for adjusting cltv_expiry_delta is hard-coded with specific ranges and returns an error for any value not within those ranges. This could be problematic if the valid range of cltv_expiry_delta changes in the future or if there are legitimate cases where values outside these ranges should be allowed. Consider whether this logic could be made more flexible or if additional context should be provided for these constraints.

@codecov-commenter
Copy link

Codecov Report

Attention: 12 lines in your changes are missing coverage. Please review.

Comparison is base (ea5de93) 88.52% compared to head (8053aa3) 88.53%.

Files Patch % Lines
lightning/src/blinded_path/payment.rs 0.00% 8 Missing ⚠️
lightning/src/routing/router.rs 0.00% 4 Missing ⚠️

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2831   +/-   ##
=======================================
  Coverage   88.52%   88.53%           
=======================================
  Files         114      114           
  Lines       92090    92093    +3     
  Branches    92090    92093    +3     
=======================================
+ Hits        81526    81534    +8     
- Misses       8058     8062    +4     
+ Partials     2506     2497    -9     

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

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.

LGTM, thanks.

I don't think we need to wait for a second reviewer here. Bug or not, if we're dealing with (our) Counterparty('s)ForwardingInfo we should always be hiding the cltv expiry delta via round-ups. We should probably also be doing the same for fees, but we can figure that part out later.

@TheBlueMatt TheBlueMatt merged commit fbeb7ac into lightningdevkit:main Jan 17, 2024
15 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.

None yet

3 participants