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

[custom channels 1/5]: extract PART1 from mega staging branch #9030

Merged
merged 22 commits into from
Aug 28, 2024

Conversation

guggero
Copy link
Collaborator

@guggero guggero commented Aug 23, 2024

Extracts part 1 from #8960.

Copy link
Contributor

coderabbitai bot commented Aug 23, 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>.
    • 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.
    • @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 as 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.

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

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.

Copy link

Pull reviewers stats

Stats of the last 30 days for lnd:

User Total reviews Time to review Total comments
yyforyongyu
🥇
29
▀▀▀
19h 14m
100
▀▀
guggero
🥈
20
▀▀
12h 52m
48
Roasbeef
🥉
14
14d 6h 59m
▀▀▀
56
carlaKC
10
1d 14h 32m
46
bhandras
9
2h 38m
5
ProofOfKeags
5
16d 4h 24m
▀▀▀▀
86
▀▀
ziggie1984
5
1d 4h 20m
17
Crypt-iQ
4
1d 20h 44m
11
bitromortac
4
2d 3h 3m
29
morehouse
3
1h 5m
1
ellemouton
3
1h 30m
11
hieblmi
2
5d 23h 6m
5
GeorgeTsagk
1
20h 3m
1

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

I think it's good to go. If you decide to address any of the comments it'd be great to address them in new commits, otherwise feel free to ignore them.

channeldb/channel.go Outdated Show resolved Hide resolved
channeldb/channel.go Outdated Show resolved Hide resolved
input/taproot.go Outdated Show resolved Hide resolved
type CommitDiffAuxInput struct {
// ChannelState is the static channel information of the channel this
// commitment transaction relates to.
ChannelState *channeldb.OpenChannel
Copy link
Member

Choose a reason for hiding this comment

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

yikes - guess we'll figure out how to not reference big struct in future PRs...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it's not optimal. But we're re-creating quite a bit of the channel logic in tapd, so we need access to a lot of fields on this struct. We could inline the fields we need into this new CommitDiffAuxInput struct instead, but that would just grow the diff with a lot of fields that would be copied.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yikes++. It's times like these when I wish we could distinguish between immutable and mutable pointers like rust. Personally I think that copying over the fields is the right thing to do if what you're doing is read-only.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, I've added a new struct AuxChanState that the required fields are copied into.

// lets us skip sending the entire transaction over, instead we'll just
// send signatures.
commitSort := auxResult.CommitSortFunc.UnwrapOr(DefaultCommitSort)
err = commitSort(commitTx, cltvs, htlcIndexes)
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite follow the need to sort based on the htlc index - since HtlcIndex is monotonically increasing, the sort should have no effect unless we are sorting descendingly in tap chan?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If there are multiple HTLCs with the same amount, same CLTV timeout and same payment hash (e.g. MPP shards with same split amount), they look identical on the BTC level (exact same script and amount), so the sort order of them is "fungible" (e.g. it doesn't matter which comes first in the transaction).
But on the Taproot Asset level it matters, because the actual asset leaves that are committed to each HTLC might be different. So we need to know exactly what output index a specific HTLC is located at in the final on-chain transaction. Therefore we need to take the HTLC index as an additional sorting indicator.

I didn't want to put any asset specific comments into the lnd code. But I added a bit more rationale to the documentation of the CommitSortFunc comment.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation!

Copy link
Collaborator

Choose a reason for hiding this comment

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

I must be missing something: Bolt 3 specifies that scriptpubkey is part of the sort order and afaik the tapscript root affects that, doesn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are correct. We're basically sorting by amount and scriptpubkey and the tapscript root affects that. But because the actual custom channel tapscript leaf depends on the output index and the output index changes depending on the leaf, we have a potentially infinite recursion.
So we decided to break with Bolt3 and just do a single iteration of sort using the fields mentioned above, then add the extra leaves even though that causes the transaction not to be BIP-069 sorted anymore.
IMO this is fine as both parties of custom channels will be able to deterministically do that. The only slight downside is that such transactions would be identifiable as custom channel transactions if they ever went on chain.

lnwallet/commitment.go Show resolved Hide resolved
lnwallet/channel.go Show resolved Hide resolved
lnwallet/mock.go Show resolved Hide resolved
@guggero
Copy link
Collaborator Author

guggero commented Aug 26, 2024

Thanks a lot for the review, @yyforyongyu! I pushed 4 fixup commits. I'll rebase/squash them shortly before merging, there might be more after @ProofOfKeags' review.

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

ACK on the new commits.

channeldb/channel.go Show resolved Hide resolved
channeldb/channel.go Show resolved Hide resolved
// lets us skip sending the entire transaction over, instead we'll just
// send signatures.
commitSort := auxResult.CommitSortFunc.UnwrapOr(DefaultCommitSort)
err = commitSort(commitTx, cltvs, htlcIndexes)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation!

Copy link
Collaborator

@ProofOfKeags ProofOfKeags left a comment

Choose a reason for hiding this comment

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

Just one major question regarding the sorting stuff. Otherwise looks fine.

channeldb/channel.go Outdated Show resolved Hide resolved
input/taproot.go Show resolved Hide resolved
type CommitDiffAuxInput struct {
// ChannelState is the static channel information of the channel this
// commitment transaction relates to.
ChannelState *channeldb.OpenChannel
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yikes++. It's times like these when I wish we could distinguish between immutable and mutable pointers like rust. Personally I think that copying over the fields is the right thing to do if what you're doing is read-only.

// lets us skip sending the entire transaction over, instead we'll just
// send signatures.
commitSort := auxResult.CommitSortFunc.UnwrapOr(DefaultCommitSort)
err = commitSort(commitTx, cltvs, htlcIndexes)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I must be missing something: Bolt 3 specifies that scriptpubkey is part of the sort order and afaik the tapscript root affects that, doesn't it?

channeldb/channel.go Show resolved Hide resolved
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, 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.
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 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 add some useful type definitions for the aux leaf.
@guggero guggero force-pushed the extract-part1-from-staging-branch branch from dbeeb37 to 4a23771 Compare August 28, 2024 09:56
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.
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.
Roasbeef and others added 6 commits August 28, 2024 13:22
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).
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.
@guggero guggero force-pushed the extract-part1-from-staging-branch branch from 4a23771 to 9dfbde7 Compare August 28, 2024 11:30
@guggero guggero merged commit 5c3a8e9 into master Aug 28, 2024
27 of 33 checks passed
@guggero guggero deleted the extract-part1-from-staging-branch branch August 28, 2024 13:13
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