Skip to content

Conversation

@Roasbeef
Copy link
Member

@Roasbeef Roasbeef commented Mar 17, 2024

In this commit, we add a new abstraction, the AuxLeafStore that's capable of dynamically deriving hte aux leaves for a new commitment state given the channel point and custom blob stored. The custom blob can be used by external programers to store custom information for a channel which can then be used to derive the aux leaves for each new state. All added features in this PR as optional, and if not set, don't alter the normal channel flow.

NOTE: In this PR, I've started to also commit my local go.work files, as I have some submodules change, and committing the go.work file allows the PR to build w/o any temporary commits or PRs.

TODO

  • Add custom blob to commit diff
  • Use leaf store to refresh blob for each new state
  • Thread through to the remaining resolvers

Summary by CodeRabbit

  • New Features
    • Introduced customizable script paths for channel and commitment transactions, enhancing flexibility and potential for future upgrades.
    • Added support for auxiliary tapscript leaves in commitment transactions, allowing for extended functionality.
  • Enhancements
    • Extended the wallet's capability to create commitment transactions with customizable options.
  • Refactor
    • Improved code structure related to script generation and handling of custom data blobs within channel data structures.
  • Tests
    • Expanded test coverage to include new features and enhancements, ensuring robustness and reliability.

Roasbeef added 15 commits March 12, 2024 10:17
In this commit, we rename the files as assembler.go houses the primary
interfaces/abstractions of the package. In the rest of the codebase,
this file is near uniformly called interface.go, so we rename the file
to make the repo more digestible at a scan.
In this commit, we consolidate the root bucket TLVs into a new struct.
This makes it easier to see all the new TLV fields at a glance. We also
convert TLV usage to use the new type param based APis.
This'll allow us to create a funding output that uses musig2, but uses a tapscript tweak rather than a normal BIP 86 tweak.
In most cases, we won't yet be passing a root. The option usage helps us keep the control flow mostly unchanged.
This isn't hooked up yet to the funding manager, but with this commit, we can now start to write internal unit tests that handle musig2 channels with a tapscript root.
With this commit, the channel is now aware of if it's a musig2 channel, that also has a tapscript root. We'll need to always pass in the tapscript root each time we: make the funding output, sign a new state, and also verify a new state.
In this commit, we update all the taproot scripts to also accept an
optional aux leaf. This aux leaf can be used to add more redemption
paths for advanced channels, or just as an extra commitment space.
In this commit, we add a new type alias for a blob type. This type can be used in areas where a byte slice is used to store a TLV value, which may be a fully opaque nested TLV.

We also commit our go.work file to ensure the changes that follow can always build.
In this commit, for each channel, we'll now start to store an optional custom blob. This can be used to store extra information for custom channels in an opauqe manner.
In this commit, we add a new AuxLeafStore which can be used to dynamically fetch the latest aux leaves for a given state. This is useful for custom channel types that will store some extra information in the form of a custom blob, then will use that information to derive the new leaf tapscript leaves that may be attached to reach state.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 17, 2024

Important

Auto Review Skipped

Auto reviews are disabled on base/target branches other than the default branch. Please add the base/target branch pattern to the list of additional branches to be reviewed in the settings.

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.

Walkthrough

The recent update introduces enhancements to the handling of custom data and auxiliary script paths within the Lightning Network Daemon (LND). Key changes include the addition of a customBlob for channel auxiliary data, improvements in script utility functions to support auxiliary tapscript leaves, and the introduction of functional options for commitment transaction creation. This update aims to increase the flexibility and customization capabilities of LND, particularly in script generation and channel management.

Changes

Files Change Summary
channeldb/channel.go, channeldb/channel_test.go Added customBlob field and updated serialization/deserialization methods; Updated OpenChannel to handle CustomBlob.
input/script_utils.go, input/size_test.go, input/taproot.go, input/taproot_test.go Enhanced script utilities to support AuxLeaf and auxiliary tapscript leaves; Introduced fn package usage.
lnwallet/channel.go, lnwallet/commitment.go, lnwallet/wallet.go Enhanced channel and commitment transaction handling with leafStore and createCommitOpts for auxiliary script customization.
tlv/record.go, watchtower/blob/justice_kit.go, watchtower/blob/justice_kit_test.go Added Blob type alias and updated justice kit handling with fn.None for auxiliary tapscript leaves; Added fn package usage.

Poem

In the code where bytes and bits do play,
A rabbit hopped, making changes gay.
🐰✨ With a blob and leaf, it danced around,
Crafting paths where new scripts are found.
"To the Lightning Network, a gift I give,
For flexibility and choice to live."
With every commit, the rabbit did strive,
To keep the magic of the code alive.

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

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.

@GeorgeTsagk GeorgeTsagk self-requested a review March 18, 2024 15:05
Comment on lines -210 to -211
// TestTaprootSenderHtlcSpend tests that all the positive and negative paths
// for the sender HTLC tapscript tree work as expected.
Copy link

@dstadulis dstadulis Mar 18, 2024

Choose a reason for hiding this comment

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

Was the removal of the comment intentional? Occurs a few times
ah I see it's moved

@dstadulis
Copy link

@coderabbitai review

Copy link
Contributor

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

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 33e0cea and 0d18ae6.
Files ignored due to path filters (4)
  • go.work is excluded by: !**/*.work
  • go.work.sum is excluded by: !**/*.sum
  • tlv/go.mod is excluded by: !**/*.mod
  • tlv/go.sum is excluded by: !**/*.sum
Files selected for processing (12)
  • channeldb/channel.go (8 hunks)
  • channeldb/channel_test.go (1 hunks)
  • input/script_utils.go (25 hunks)
  • input/size_test.go (14 hunks)
  • input/taproot.go (2 hunks)
  • input/taproot_test.go (20 hunks)
  • lnwallet/channel.go (11 hunks)
  • lnwallet/commitment.go (18 hunks)
  • lnwallet/wallet.go (4 hunks)
  • tlv/record.go (1 hunks)
  • watchtower/blob/justice_kit.go (3 hunks)
  • watchtower/blob/justice_kit_test.go (3 hunks)
Additional comments: 51
input/taproot.go (1)
  • 25-27: The introduction of AuxTapLeaf as a type alias for an optional tapscript leaf is a welcome addition, enhancing the flexibility and customization capabilities of the LND. However, it would be beneficial to add a brief documentation comment above the AuxTapLeaf declaration to explain its purpose and usage for future maintainability.
tlv/record.go (1)
  • 20-22: The introduction of Blob as a type alias for a byte slice to represent an encoded TLV stream is a straightforward and effective change, improving code readability. However, adding a brief documentation comment above the Blob declaration to explain its purpose and usage would enhance future maintainability.
watchtower/blob/justice_kit.go (4)
  • 13-13: The import of fn is justified and aligns with the PR objectives, enabling the use of modern Go features for handling optional values effectively.
  • 311-311: The TODO comment could benefit from additional context or a reference to an issue tracker to provide clarity on the required auxiliary leaf tower updates and facilitate tracking and accountability.
  • 315-315: The use of fn.None[txscript.TapLeaf]() in NewTaprootJusticeKit is appropriate and aligns with the PR objectives of integrating optional auxiliary leaves. This change enhances the flexibility and customization capabilities of the LND.
  • 423-423: The inclusion of fn.None[txscript.TapLeaf]() in ToRemoteOutputSpendInfo supports the integration of optional auxiliary leaves, consistent with the PR's objectives. This enhances the LND's flexibility and customization capabilities.
watchtower/blob/justice_kit_test.go (3)
  • 15-15: The addition of the fn package from github.com/lightningnetwork/lnd/fn introduces functional programming utilities, specifically for handling optional values. This is a modern approach to dealing with optional data and can enhance code readability and safety by avoiding nil checks.
  • 308-310: Using fn.None[txscript.TapLeaf]() as an argument for input.NewRemoteCommitScriptTree is a clean and safe way to handle optional TapLeaf values. This approach enhances code readability and safety by explicitly indicating the absence of a value, avoiding potential nil pointer dereferences.
  • 466-466: The use of fn.None[txscript.TapLeaf]() for specifying an optional TapLeaf in input.NewLocalCommitScriptTree is consistent with modern coding practices. It clearly communicates the intention and avoids the pitfalls associated with nil values, enhancing both code safety and readability.
input/size_test.go (2)
  • 14-14: The introduction of the fn package and its None function for handling txscript.TapLeaf instances is a notable change. This approach simplifies the handling of optional taproot leaves by providing a clear way to represent the absence of a leaf. This is a good use of functional programming concepts to improve code readability and maintainability.
  • 855-855: The consistent use of fn.None[txscript.TapLeaf]() across various test cases for generating taproot script trees without specific leaves is a good practice. It ensures that the tests remain focused on the intended aspects of transaction and script handling without unnecessary complexity. This approach enhances the clarity and maintainability of the test code.

Also applies to: 889-889, 923-923, 990-990, 1030-1030, 1079-1079, 1121-1121, 1163-1163, 1210-1210, 1271-1271, 1317-1317, 1403-1403, 1472-1472

lnwallet/commitment.go (7)
  • 227-233: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [230-240]

The introduction of the auxLeaf parameter in the CommitScriptToSelf function enhances the flexibility of commitment transactions by allowing the inclusion of auxiliary tapscript leaves. This change aligns with the PR's objective to introduce dynamic auxiliary leaves based on custom channel information. However, it's crucial to ensure that all calls to this function throughout the codebase have been updated to include the new parameter to avoid compilation errors or unintended behavior.

  • 291-299: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [294-325]

The addition of the auxLeaf parameter in the CommitScriptToRemote function, allowing for the inclusion of auxiliary tapscript leaves in the to_remote output of commitment transactions, is a significant enhancement. This change supports the PR's goal of introducing dynamic auxiliary leaves. Similar to the previous comment, it's essential to verify that all invocations of this function have been updated accordingly.

  • 616-634: The CommitAuxLeaves struct, which stores potential auxiliary leaves for the remote and local output, is a well-structured way to manage these leaves. This struct facilitates the handling of auxiliary leaves in a structured manner, contributing to the maintainability and readability of the code.
  • 636-650: The AuxLeafStore interface is a crucial addition for fetching and refreshing auxiliary tapscript leaves based on an opaque blob. This interface abstracts the underlying mechanism for managing auxiliary leaves, promoting modularity and flexibility in the codebase. It's important to ensure that the implementation of this interface is robust and thoroughly tested, especially the FetchLeaves and RefreshBlob methods, to prevent potential issues in dynamic leaf management.
  • 729-746: The auxLeavesFromBlob function, which attempts to fetch auxiliary leaves given a blob and a leaf store, is a critical component for integrating auxiliary leaves into the commitment transaction process. This function effectively bridges the gap between the opaque blob and the auxiliary leaves needed for the commitment transaction. It's important to ensure that error handling and edge cases are adequately addressed in the implementations of the FetchLeaves method within the AuxLeafStore interface to prevent issues during runtime.
  • 964-989: The modifications in the CreateCommitTx function to include auxiliary leaves in the commitment transaction are aligned with the PR's objectives. The handling of auxLeaves to dynamically include auxiliary tapscript leaves based on the channel's state enhances the transaction's flexibility. It's crucial to ensure comprehensive testing of these changes, particularly the correct inclusion of auxiliary leaves in various commitment transaction scenarios.
  • 1324-1362: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1296-1359]

The findOutputIndexesFromRemote function's modifications to support auxiliary leaves when finding output indexes from the remote commitment transaction are a necessary extension of the auxiliary leaves feature. This function's changes ensure that the auxiliary leaves are considered when determining the output indexes, which is crucial for the correct functioning of the feature. It's recommended to add tests covering the scenarios where auxiliary leaves are present and correctly identified in the remote commitment transaction.

Would you like assistance in creating these tests or should I open a GitHub issue for tracking?

input/taproot_test.go (6)
  • 4-6: The addition of imports for bytes and fmt is appropriate given their usage in the newly introduced tests and functions. This change aligns with the need to manipulate byte slices and format strings within the test cases.
  • 34-42: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [37-56]

The modification of newTestSenderHtlcScriptTree to accept an auxLeaf parameter and its usage within the function is a significant enhancement. This change allows for testing the behavior of HTLC script trees with auxiliary leaves, which is crucial for the new functionality introduced in the PR. However, it's important to ensure that all calls to this function throughout the test suite have been updated to include the auxLeaf parameter.

Verification successful

The verification process confirms that the modification of newTestSenderHtlcScriptTree to include an auxLeaf parameter has been correctly implemented in the codebase, with all calls to this function updated accordingly. There is only one occurrence of this function call, and it includes the required auxLeaf parameter.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all calls to newTestSenderHtlcScriptTree include the auxLeaf parameter.
ast-grep --lang go --pattern $'newTestSenderHtlcScriptTree($_, $_)'

Length of output: 155



Script:

#!/bin/bash
# Verify all occurrences of newTestSenderHtlcScriptTree, regardless of the number of parameters.
ast-grep --lang go --pattern $'newTestSenderHtlcScriptTree($$$)'

Length of output: 152

* 434-462: > 📝 **NOTE** > This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [215-457]

The refactoring of TestTaprootSenderHtlcSpend to iterate over hasAuxLeaf values and test with different configurations is a robust approach to ensure the functionality works as expected with and without auxiliary leaves. This pattern is consistent across several test functions, enhancing the test coverage for the new feature. It's crucial to maintain this pattern for all relevant tests to ensure comprehensive coverage.

  • 477-485: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [480-501]

Similar to the newTestSenderHtlcScriptTree, the newTestReceiverHtlcScriptTree function has been modified to accept an auxLeaf parameter. This consistency in handling auxiliary leaves across sender and receiver script trees is commendable and necessary for thorough testing of the new functionality. Ensure that all invocations of this function are updated accordingly.

Verification successful

The verification process confirms that the modification to include the auxLeaf parameter in the newTestReceiverHtlcScriptTree function is correctly applied in its call, as indicated by the script output. This aligns with the expectations set in the review comment.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all calls to newTestReceiverHtlcScriptTree include the auxLeaf parameter.
ast-grep --lang go --pattern $'newTestReceiverHtlcScriptTree($_, $_)'

Length of output: 159

* 975-985: > 📝 **NOTE** > This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [956-982]

The introduction of the newTestCommitScriptTree function with an auxLeaf parameter for both local and remote commit script trees is a critical update. This change allows for testing the commit script trees with auxiliary leaves, aligning with the PR's objectives to enhance flexibility and customization in channel and transaction management. It's essential to verify that the function is utilized correctly across the test suite.

Verification successful

The verification process confirms that all calls to newTestCommitScriptTree within the test suite correctly include the auxLeaf parameter, aligning with the objectives mentioned in the review comment. This ensures the function's intended use for testing commit script trees with auxiliary leaves is adhered to.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all calls to newTestCommitScriptTree include the auxLeaf parameter correctly.
ast-grep --lang go --pattern $'newTestCommitScriptTree($_, $_)'

Length of output: 253

* 1690-1710: > 📝 **NOTE** > This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1693-1716]

The newTestSecondLevelHtlcTree function's adaptation to include an auxLeaf parameter is a significant enhancement. This modification ensures that the second-level HTLC script trees can be tested with auxiliary leaves, further extending the test coverage for the new feature. Consistency in handling auxiliary leaves across different script trees demonstrates a thorough approach to testing.

Verification successful

The verification process confirms that the newTestSecondLevelHtlcTree function is indeed called with the auxLeaf parameter, as indicated by the review comment. The script output shows that all identified calls to this function include the auxLeaf parameter, aligning with the enhancement described in the review. This adaptation ensures that the second-level HTLC script trees can be tested with auxiliary leaves, demonstrating a thorough approach to testing.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that all calls to newTestSecondLevelHtlcTree include the auxLeaf parameter.
ast-grep --lang go --pattern $'newTestSecondLevelHtlcTree($_, $_)'

Length of output: 154

channeldb/channel_test.go (1)
  • 361-361: The addition of the CustomBlob field to the OpenChannel struct in the createTestChannelState function is a significant change. This field allows for the storage of custom information related to channels, enhancing flexibility and customization for developers working with LND.
lnwallet/wallet.go (6)
  • 1458-1467: The introduction of createCommitOpts and defaultCommitOpts provides a structured way to handle options for creating commitment transactions. This approach enhances readability and maintainability by encapsulating options within a struct.
  • 1469-1471: The CreateCommitOpt type definition as a functional option is a good practice. It allows for flexible and extensible configuration of commitment transactions without changing the function signature.
  • 1479-1490: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1482-1513]

The CreateCommitmentTxns function has been modified to accept functional options for creating commitment transactions. This change improves the function's flexibility and extensibility by allowing additional parameters to be passed without altering the function's signature. However, ensure that all calls to this function throughout the codebase have been updated to use the new functional options pattern.

  • 1482-1487: The pattern of using functional options (CreateCommitOpt) for configuring the creation of commitment transactions is a solid design choice. It enhances code readability and maintainability by allowing for more flexible function invocation.
  • 1479-1490: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1482-1513]

The addition of leaseExpiry and opts parameters to CreateCommitmentTxns function indicates an enhancement in the functionality related to commitment transactions. Ensure that the handling of leaseExpiry and the application of options through opts are thoroughly tested, especially in scenarios where leaseExpiry impacts the transaction's validity or behavior.

  • 1479-1490: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1482-1513]

The CreateCommitmentTxns function now includes handling for leaseExpiry and functional options (opts). This enhancement allows for more flexible and dynamic creation of commitment transactions. It's important to ensure that all existing and new calls to this function are correctly updated to utilize these new capabilities.

input/script_utils.go (7)
  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3050-3058]

Looks good, correctly implements single tweak generation.

  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3060-3084]

The TweakPubKey function correctly tweaks a public key using a commit point. It's essential for the security of off-chain contracts in the Lightning Network.

  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3086-3093]

The TweakPubKeyWithTweak function provides flexibility by allowing direct tweak bytes input, which is useful for various operations in the Lightning Network.

  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3095-3108]

The TweakPrivKey function correctly tweaks a private key, which is crucial for deriving the correct private key corresponding to the tweaked public key in the Lightning Network.

  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3110-3142]

The DeriveRevocationPubkey function correctly derives the revocation public key, a key component in the penalty mechanism of the Lightning Network.

  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3144-3158]

The DeriveRevocationPrivKey function correctly derives the revocation private key, enabling a node to claim funds from a counterparty who broadcasts a revoked state.

  • 673-684: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [3160-3164]

The ComputeCommitmentPoint function correctly generates a commitment point from a secret, essential for key randomization in the Lightning Network.

channeldb/channel.go (5)
  • 242-242: The customBlob field has been added to the chanAuxData struct.

This addition allows for the storage of custom information related to channels, enhancing flexibility and extensibility.

  • 245-273: The encode method in chanAuxData has been updated to include the serialization of the customBlob field.

The method correctly appends the customBlob to the TLV records if present, ensuring the custom data is serialized along with other channel data.

  • 276-310: The decode method in chanAuxData has been updated to include the deserialization of the customBlob field.

The method correctly extracts the customBlob from the TLV stream if present, ensuring the custom data is deserialized along with other channel data.

  • 327-329: The toOpenChan method in chanAuxData now includes the assignment of the customBlob field to the OpenChannel struct.

This ensures that the custom data is correctly transferred from the auxiliary data structure to the main channel structure.

  • 359-363: The newChanAuxDataFromChan function has been updated to include the customBlob field.

This ensures that the custom data is correctly included when creating a new chanAuxData instance from an OpenChannel struct.

lnwallet/channel.go (8)
  • 1258-1260: The addition of the leafStore field to the LightningChannel struct is a key part of integrating the AuxLeafStore. This change allows the LightningChannel to retrieve extra tapscript leaves for special custom channel types. Ensure that the AuxLeafStore interface is well-defined and that its methods are implemented correctly to avoid runtime errors.
  • 1337-1337: The leafStore field is also added to the channelOpts struct, which is consistent with the addition to the LightningChannel struct. This ensures that the custom leaf store can be passed through the channel options during initialization. It's important to verify that all code paths that create channelOpts instances properly handle this new field.
  • 1369-1374: The WithLeafStore function is a well-structured way to specify a custom leaf store for the channel. This approach follows the functional options pattern, which is a good practice for extending functionality without breaking existing code. Ensure that this function is used wherever a LightningChannel is initialized with a custom leaf store.
  • 1417-1426: The initialization of LightningChannel now includes the leafStore from options, which is a necessary change to support the new functionality. Additionally, passing the leafStore to the NewCommitmentBuilder function is crucial for integrating the custom leaf retrieval into the commitment transaction generation process. Ensure that the NewCommitmentBuilder function properly utilizes the leafStore.
  • 2484-2484: The use of fn.None[txscript.TapLeaf]() in the CommitScriptToSelf call suggests that the auxiliary leaves are optional for certain script generation calls. This is a good approach to maintain backward compatibility and flexibility. However, ensure that the fn.None and fn.Some functions are well-tested, especially in the context of optional types in Go.
  • 5824-5824: The modification to include leafStore in the findOutputIndexesFromRemote function call is crucial for ensuring that auxiliary leaves are considered when finding output indexes. This change integrates the custom leaf retrieval into a critical part of the channel state management. Verify that the findOutputIndexesFromRemote function correctly handles the leafStore.
  • 7129-7129: The use of fn.None[txscript.TapLeaf]() in the TaprootSecondLevelScriptTree call is consistent with the optional nature of auxiliary leaves. This approach allows for flexibility in script tree generation. Ensure that the TaprootSecondLevelScriptTree function is prepared to handle cases where auxiliary leaves are not provided.
  • 7374-7374: This is a duplicate of the previous comment regarding the use of fn.None[txscript.TapLeaf]() in the TaprootSecondLevelScriptTree call. Ensure that the handling of optional auxiliary leaves is consistent across all relevant function calls.

@saubyk saubyk requested a review from guggero March 19, 2024 01:19
Copy link
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.

Cool cool cool, I can see where things are going, very exciting. Did a first pass only as this is still in draft. But looking pretty, good, just need to finish drawing the owl :)

type AuxLeafStore interface {
// FetchLeaves attempts to fetch the auxiliary leaves for the
// commitment.
FetchLeaves(chanPoint wire.OutPoint,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I understand this method signature. Why would you pass in the auxBlob? Isn't that what contains the aux leaves already? So it would be more of a parse/extract than a fetch? Or what am I misunderstanding?

Copy link
Collaborator

Choose a reason for hiding this comment

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

From lines 636-637

// AuxLeafStore is used to optionally fetch auxiliary tapscript leaves for the
// commitment transaction given an opaque blob.

So seems like it's expected to provide the blob. I do see your argument tho, perhaps worth renaming something here

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeh I can probably rename things a bit, it's more of a parse/extract, although the implementor could also store that information, but my latest thinking is that it's derived on the fly.

I think the "shape" of these interfaces will likely change a bit as we start on the concrete implementations elsewhere.

The layer of indirection here is that lnd doesn't know exactly what's in the blob, but given the blob and the commitment it can obtain the aux tap leaves.

Copy link
Collaborator

@GeorgeTsagk GeorgeTsagk left a comment

Choose a reason for hiding this comment

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

Good stuff 🧑‍🦲 🥕 🥕 🧑‍🦱 , really looking forward to the tests for 0d18ae6

type AuxLeafStore interface {
// FetchLeaves attempts to fetch the auxiliary leaves for the
// commitment.
FetchLeaves(chanPoint wire.OutPoint,
Copy link
Collaborator

Choose a reason for hiding this comment

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

From lines 636-637

// AuxLeafStore is used to optionally fetch auxiliary tapscript leaves for the
// commitment transaction given an opaque blob.

So seems like it's expected to provide the blob. I do see your argument tho, perhaps worth renaming something here

@dstadulis
Copy link

Fixes lightninglabs/taproot-assets#849

Roasbeef added 14 commits March 30, 2024 17:24
This type is useful when one wants to encode an integer as an underlying BigSize record. It wraps any integer, then handles the transformation into and out of the BigSize encoding on disk.
This'll be useful for custom channel types that want to store extra information that'll be useful to help handle channel revocation cases.
This may be useful for custom channel types that base everything off the index (a global value) rather than the output index (can change with each state).
This blob can be used to store funding specific information.
In this commit, we add some useful type definitions for the aux leaf, and a helper function to cut down on some line noise.
In this commit, we add a TLV blob to the PaymentDescriptor struct. We also now thread through this value from the UpdateAddHTLC message to the PaymentDescriptor mapping, and the other way around.
We'll need this later on to ensure we can always interact with the new aux blobs at all stages of commitment transaction construction.
In addition to the leaves for the commitment outputs, we'll also need to interact with leaves for all the HTLC outputs as well.
When constructing or validating a commitment transaction, we'll need to be able to obtain the aux leaves when:
  * we're making a new state
  * we're making a resolution for an existing state
  * we're handling an on chain breach

The three top level methods now handle these three cases.

The final method `ApplyHtlcView` will be used when we've made a new state, and need to derive the opaque blob that we'll store in the db for that respective commitment.
In this commit, we also add the custom TLV blob to the internal commitment struct that we use within the in-memory commitment linked list.

This'll be useful to ensure that we're tracking the current blob for our in memory commitment for when we need to write it to disk.
In this commit, we start to thread thru the new aux tap leaf structures to all relevant areas. This includes: commitment outputs, resolution creation, breach handling, and also HTLC scripts.

Given the aux leaf store, and a struct that describes the current commitment, we can obtain the CommitAuxLeaves struct, then use that to derive the aux leaves for the commitment outputs and also HTLCs as well.

When restoring commitments and pay descs from disk, we'll store the aux leaves as custom TLV blobs on disk, then use the aux leaf store to map back to the concrete aux leaves when needed.
@Roasbeef Roasbeef marked this pull request as ready for review April 2, 2024 03:11
@Roasbeef
Copy link
Member Author

Roasbeef commented Apr 2, 2024

Pushed up some more commits, tentatively removing from draft. Commits need a good bit of clean up and consolidation though. After the next PR, will be able to go back and meld some commits into themselves.

@guggero guggero self-requested a review April 2, 2024 15:52
@GeorgeTsagk GeorgeTsagk self-requested a review April 4, 2024 15:37
Copy link
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.

Looks very good! Was able to hook everything up in litd (with some small tweaks, see inline comments).
Still working on integrating everything, so will probably have more comments at a later date.

Copy link
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.

Did a deep dive while starting on the implementation for the aux leaf store.
Have a couple of questions around the method signatures of the aux leaf store.

@guggero
Copy link
Collaborator

guggero commented Apr 8, 2024

@Roasbeef I think we also need error return values on the interface methods of the aux leaf store, otherwise it's hard to communicate if something goes wrong (and we definitely don't want to burn the assets by returning fn.None() when something goes wrong).

I implemented that in this branch: https://github.com/lightningnetwork/lnd/tree/custom-channels-integration (see last 4 commits, I also hooked up the aux leaf store everywhere in those commits, feel free to take over).

@lightninglabs-deploy
Copy link
Collaborator

@GeorgeTsagk: review reminder
@Roasbeef, remember to re-request review from reviewers when ready

@guggero
Copy link
Collaborator

guggero commented Apr 24, 2024

Replacement version of this PR with all current review comments applied and rebased on the 0-19-staging branch created here: #8684

@guggero guggero closed this Apr 24, 2024
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.

[feature]: add AuxLeafStore for dynamic aux leaves in lnd

5 participants