Skip to content

lnwallet: fix closechannel for P2TR external addr#9223

Merged
guggero merged 2 commits into
lightningnetwork:masterfrom
starius:close-to-external-p2tr
Oct 30, 2024
Merged

lnwallet: fix closechannel for P2TR external addr#9223
guggero merged 2 commits into
lightningnetwork:masterfrom
starius:close-to-external-p2tr

Conversation

@starius
Copy link
Copy Markdown
Collaborator

@starius starius commented Oct 25, 2024

Change Description

Customize the itest with the type of external delivery address. Test with P2TR address type in addition to P2WKPH.

Steps to Test

make itest icase='coop close with external delivery'

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Oct 25, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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>, please review it.
    • Generate unit testing code 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@starius starius force-pushed the close-to-external-p2tr branch from ab0b961 to e86e4af Compare October 25, 2024 23:33
@starius starius marked this pull request as ready for review October 26, 2024 00:51
@starius starius changed the title [WIP] itest: coop_close_with_external_delivery with p2tr itest: coop_close_with_external_delivery with p2tr Oct 26, 2024
@starius starius changed the title itest: coop_close_with_external_delivery with p2tr lnwallet: fix closechannel for P2TR external addr Oct 26, 2024
@guggero guggero self-requested a review October 28, 2024 07:52
Copy link
Copy Markdown
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! LGTM 🎉

Comment thread lnwallet/interface.go Outdated
Comment thread itest/lnd_coop_close_external_delivery_test.go Outdated
Comment thread lnwallet/interface.go Outdated
walletAddr, err := wallet.AddressInfo(addr)
if err != nil {
// If the error is that the address can't be found, it is not
// an error, as callers can use the .Option() method to get an
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure I follow here, could you elaborate a bit? If there's no internal key associated shouldn't we return an error?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I changed the comment:

        walletAddr, err := wallet.AddressInfo(addr)
        if err != nil {
                // If the error is that the address can't be found, it is not
                // an error. This happens when any channel which is not a custom
                // taproot channel is cooperatively closed to an external P2TR
                // address. In this case there is no internal key associated
                // with the address. Callers can use the .Option() method to get
                // an option value.

})
ht.Run("set delivery address at close", func(t *testing.T) {

ht.Run("set P2WPKH delivery address at close", func(t *testing.T) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice tests - can we add success := ht.Run and stop the test if !success? this is helpful for future development as it's easier to debug that way.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks! Done:

        ok := ht.Run("set P2WPKH delivery address at open", func(t *testing.T) {
                tt := ht.Subtest(t)
                testCoopCloseWithExternalDeliveryImpl(
                        tt, true, lnrpc.AddressType_UNUSED_WITNESS_PUBKEY_HASH,
                )
        })
        // Abort the test if failed.
        if !ok {
                return
        }

...

If the delivery address is P2TR, function InternalKeyForAddr checks its
existance in the wallet to return internal key for it in case it is a custom
taproot channel. It used to return the error returned by wallet.AddressInfo.
The error is now ignored if it is ErrAddressNotFound error. This fixes
"lncli closechannel --delivery_addr <external p2tr address" case.
Customize the itest with the type of external delivery address. Test with P2TR
address type in addition to P2WKPH.
@starius starius force-pushed the close-to-external-p2tr branch from e86e4af to 4b87072 Compare October 28, 2024 14:13
@saubyk saubyk added this to the v0.19.0 milestone Oct 29, 2024
Copy link
Copy Markdown
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

A few more questions then this should be good to go!

})
ht.Run("set delivery address at close", func(t *testing.T) {
// Abort the test if failed.
if !ok {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🙏

Comment thread lnwallet/interface.go

walletAddr, err := wallet.AddressInfo(addr)
if err != nil {
// If the error is that the address can't be found, it is not
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

Comment thread lnwallet/interface.go
// an error. This happens when any channel which is not a custom
// taproot channel is cooperatively closed to an external P2TR
// address. In this case there is no internal key associated
// with the address. Callers can use the .Option() method to get
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Q: how can the callers get the value, don't we return a none option here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I copy-pasted this part of the comment from the check below:

        // No wallet addr. No error, but we'll return an nil error value here,
        // as callers can use the .Option() method to get an option value.
        if walletAddr == nil {
                return none, nil
        }

I propose to modify it in both cases to the following: "callers can inspect the returned option to determine if it has a value".

cc @guggero

Comment thread lnwallet/interface.go
// an option value.
var managerErr waddrmgr.ManagerError
if errors.As(err, &managerErr) &&
managerErr.ErrorCode == waddrmgr.ErrAddressNotFound {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't have the full pic here, but just wanna point out that there probably should have a dedicated method to do this job since this method is named InternalKeyForAddr and the caller would expect an error when there's no internal key. In addition, we are fixing the case "non-custom tap channel closed to an external addr", then what about "custom tap chan closed but no internal key found" case? Previously we'd have an error but now it's ignored. Is that handled elsewhere? cc @guggero

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We only need the internal key in case of a custom (tap) channel. So for a normal taproot channel, we don't want this to error out and return a None. If we also return a None for custom (tap) channels, then that is fine here, as it will lead to an error elsewhere.
So the fix is good, we basically shouldn't error out for anything that can happen during normal operation (and specifying an external shutdown address definitely is normal operation).

Basically the

if walletAddr == nil {
		return none, nil
}

further below is what we thought would happen if a key wasn't found. But apparently an error is returned, so we need this special case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cool thanks for the explanation!

Copy link
Copy Markdown
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

LGTM 💃

@guggero guggero modified the milestones: v0.19.0, v0.18.4 Oct 30, 2024
@guggero guggero merged commit 22c3cc5 into lightningnetwork:master Oct 30, 2024
@starius starius deleted the close-to-external-p2tr branch October 30, 2024 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants