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

Fail request_refund_payment for unsupported chain #2917

Merged

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Feb 29, 2024

If a Refund has an unsupported chain, ChannelManager should not send an invoice as it can't be paid on that chain. Instead, return an error when calling ChannelManager::request_refund_payment for such refunds.

Copy link

coderabbitai bot commented Feb 29, 2024

Walkthrough

The recent updates focus on enhancing security and consistency in the handling of chains within the refund process and offer creation in a Lightning Network implementation. Specifically, these changes ensure that operations related to refunds and offers are conducted on the correct blockchain network by verifying the chain hash. If a mismatch is detected, the process is halted, and an error is returned, thereby preventing potential issues arising from chain incompatibility.

Changes

Files Change Summary
.../ln/channelmanager.rs Added chain hash check in refund process to ensure chain compatibility, returning an error if mismatched.
.../ln/offers_tests.rs Introduced tests for invoice creation and sending with unsupported chains in refund scenarios.
.../offers/offer.rs
.../offers/refund.rs
Added methods (clear_chains, clear_chain) to clear chain information in offers and refunds.

🐇✨
In the realm of code where the lightning plays,
A rabbit hopped through the blockchain's maze.
With a flick and a hop, it mended the links,
Ensuring no chain in disarray sinks.
"To secure and to safeguard," it whispered with glee,
As it bounded away, "Let the transactions be free!"
🌟🐾

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.
    • Open a follow-up GitHub issue for this discussion.
  • 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 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: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cfb4391 and 25761b9.
Files selected for processing (4)
  • lightning/src/ln/channelmanager.rs (1 hunks)
  • lightning/src/ln/offers_tests.rs (2 hunks)
  • lightning/src/offers/offer.rs (1 hunks)
  • lightning/src/offers/refund.rs (1 hunks)
Additional comments: 6
lightning/src/ln/offers_tests.rs (3)
  • 43-43: The addition of the Network import from bitcoin::network::constants is appropriate for the tests that follow, which involve specifying blockchain networks (e.g., Signet) to simulate scenarios with unsupported chains.
  • 671-695: The test fails_creating_invoice_request_for_unsupported_chain correctly simulates a scenario where an invoice request is made for an offer that specifies an unsupported chain (Network::Signet). The test setup, execution, and assertion that an Err(Bolt12SemanticError::UnsupportedChain) is returned are all correctly implemented.
  • 697-725: The test fails_sending_invoice_with_unsupported_chain_for_refund accurately simulates a scenario where a refund request is made for a refund that specifies an unsupported chain (Network::Signet). The setup, execution, and the assertion that an Err(Bolt12SemanticError::UnsupportedChain) is returned are correctly implemented, ensuring that the system behaves as expected when dealing with refunds on unsupported chains.
lightning/src/offers/refund.rs (1)
  • 305-308: The addition of the clear_chain method in the RefundBuilder struct allows for resetting the chain field to None. This change is consistent with the PR's objective to enhance the handling of refunds on unsupported chains by providing a way to clear previously set chain information. The method is correctly implemented, following Rust's ownership and borrowing rules, and it returns self for chaining calls, aligning with the builder pattern used throughout the file.

However, it's important to ensure that this method is used appropriately in the context where a refund's chain needs to be validated or reset. Additionally, consider adding unit tests specifically for this method to verify its behavior in various scenarios, such as when the chain is initially set and then cleared.

lightning/src/offers/offer.rs (1)
  • 342-345: The addition of the clear_chains method to the OfferBuilder struct allows for resetting the chains field to None. This method enhances the flexibility of the OfferBuilder by enabling the removal of previously set chains, which could be particularly useful in testing scenarios or when adjusting an offer to match specific requirements. The implementation is straightforward and correctly sets the chains field to None, ensuring that no blockchain chains are associated with the offer being built. This change aligns with the PR's objective of improving the handling of refunds on unsupported chains by providing a mechanism to clear the chains field in offers, likely for testing purposes.
lightning/src/ln/channelmanager.rs (1)
  • 7901-7901: Following the chain hash check, the method proceeds with creating an inbound payment if the chain hashes match. This control flow is logical and maintains the integrity of the refund process by ensuring that only valid refunds proceed to the invoice creation stage. It's crucial to verify that the error handling for the new chain hash mismatch scenario is integrated into the broader error handling strategy of the ChannelManager, ensuring a seamless user experience and clear error messaging.

lightning/src/ln/channelmanager.rs Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Feb 29, 2024

Codecov Report

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

Project coverage is 89.30%. Comparing base (670b41a) to head (228e72c).
Report is 15 commits behind head on main.

Files Patch % Lines
lightning/src/ln/offers_tests.rs 95.91% 2 Missing ⚠️

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2917      +/-   ##
==========================================
+ Coverage   89.11%   89.30%   +0.19%     
==========================================
  Files         117      117              
  Lines       95029    95929     +900     
  Branches    95029    95929     +900     
==========================================
+ Hits        84685    85670     +985     
+ Misses       7856     7785      -71     
+ Partials     2488     2474      -14     

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

@jkczyz jkczyz force-pushed the 2024-02-refund-unsupported-chain branch from 25761b9 to 27f7731 Compare February 29, 2024 21:45
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 27f7731.
Files selected for processing (4)
  • lightning/src/ln/channelmanager.rs (3 hunks)
  • lightning/src/ln/offers_tests.rs (2 hunks)
  • lightning/src/offers/offer.rs (1 hunks)
  • lightning/src/offers/refund.rs (1 hunks)
Files skipped from review as they are similar to previous changes (4)
  • lightning/src/ln/channelmanager.rs
  • lightning/src/ln/offers_tests.rs
  • lightning/src/offers/offer.rs
  • lightning/src/offers/refund.rs

TheBlueMatt
TheBlueMatt previously approved these changes Mar 7, 2024
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, but do we want to land #2903 first to avoid rebase?

@jkczyz
Copy link
Contributor Author

jkczyz commented Mar 8, 2024

LGTM, but do we want to land #2903 first to avoid rebase?

Yeah, let's wait for that to land. Rebasing this would be easier.

@jkczyz
Copy link
Contributor Author

jkczyz commented Mar 8, 2024

Rebased and resolved merge conflicts with #2903.

If a Refund has an unsupported chain, ChannelManager should not send an
invoice as it can't be paid on that chain. Instead, return an error when
calling ChannelManager::request_refund_payment for such refunds.
@jkczyz jkczyz force-pushed the 2024-02-refund-unsupported-chain branch from da3623b to 228e72c Compare March 8, 2024 21:57
@jkczyz
Copy link
Contributor Author

jkczyz commented Mar 8, 2024

Dropped Refund::clear_chain since the chain can be simply overridden unlike in Offer where there can be multiple chains.

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.

The only non-test code here is a trivial if chain != self.chain -> err so I'm not sure why this would be worth having anyone else look at.

@TheBlueMatt TheBlueMatt merged commit aa334d5 into lightningdevkit:main Mar 12, 2024
13 of 16 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