Skip to content

mssmt: add new Copy method and InsertMany to optimize slightly #1467

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

Merged
merged 2 commits into from
Apr 22, 2025

Conversation

Roasbeef
Copy link
Member

In this commit, we add a new Copy method that takes as an arg another mssmt.Tree and inserts all the keys+leaves from the target tree into the source tree. We then optimize a bit by adding an InsertMany method that will call insert N times in the same TreeStoreUpdateTx. This doesn't yet implement any fancy optimizations to ensure the root is only updated once as #1347 does.

This was spun off a work unit related to #1464.

@Roasbeef Roasbeef requested review from a team, Copilot, ffranr and GeorgeTsagk and removed request for a team April 12, 2025 02:23
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

mssmt/tree.go:450

  • The type assertion to *BranchNode may panic if the underlying type is not as expected. Consider checking the type before casting to ensure safe failure handling.
rootBranch := currentRoot.(*BranchNode)

if err != nil {
return err
}
rootBranch := currentRoot.(*BranchNode)
Copy link
Preview

Copilot AI Apr 12, 2025

Choose a reason for hiding this comment

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

The direct type assertion of currentRoot to *BranchNode can lead to a runtime panic if the type is not correct. It is recommended to perform a type check to handle unexpected types gracefully.

Suggested change
rootBranch := currentRoot.(*BranchNode)
rootBranch, ok := currentRoot.(*BranchNode)
if !ok {
return fmt.Errorf("expected currentRoot to be of type *BranchNode, got %T", currentRoot)
}

Copilot uses AI. Check for mistakes.

@coveralls
Copy link

coveralls commented Apr 12, 2025

Pull Request Test Coverage Report for Build 14483858596

Details

  • 152 of 244 (62.3%) changed or added relevant lines in 2 files are covered.
  • 22 unchanged lines in 6 files lost coverage.
  • Overall coverage increased (+0.1%) to 28.51%

Changes Missing Coverage Covered Lines Changed/Added Lines %
mssmt/tree.go 76 112 67.86%
mssmt/compacted_tree.go 76 132 57.58%
Files with Coverage Reduction New Missed Lines %
address/address.go 2 69.55%
asset/asset.go 2 50.6%
asset/mock.go 3 64.72%
commitment/tap.go 4 71.82%
tapchannel/aux_leaf_signer.go 5 43.08%
tapgarden/caretaker.go 6 68.63%
Totals Coverage Status
Change from base Build 14474635473: 0.1%
Covered Lines: 26122
Relevant Lines: 91624

💛 - Coveralls

Copy link
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Some of the comments are missing punctuation, but no big deal.

This commit introduces a new `Copy` method to both the `FullTree` and
`CompactedTree` implementations of the MS-SMT. This method allows copying
all key-value pairs from a source tree to a target tree, assuming the
target tree is initially empty.

The `Copy` method is implemented differently for each tree type:

- For `FullTree`, the method recursively traverses the tree, collecting all
  non-empty leaf nodes along with their keys. It then inserts these leaves
  into the target tree.

- For `CompactedTree`, the method similarly traverses the tree, collecting
  all non-empty compacted leaf nodes along with their keys. It then inserts
  these leaves into the target tree.

A new test case, `TestTreeCopy`, is added to verify the correctness of
the `Copy` method for both tree types, including copying between
different tree types (FullTree to CompactedTree and vice versa). The
test case generates a set of random leaves, inserts them into a source
tree, copies the source tree to a target tree, and then verifies that
the target tree contains the same leaves as the source tree.
This commit introduces the InsertMany method to both the FullTree and
CompactedTree implementations of the MS-SMT. This method allows for the
insertion of multiple leaf nodes in a single database transaction,
improving efficiency when adding multiple leaves at once.

The InsertMany method is added to the Tree interface and implemented in
both FullTree and CompactedTree. The implementation includes sum
overflow checks before each insertion and updates the root within the
transaction for consistency.

A new test case, TestInsertMany, is added to verify the functionality of the
InsertMany method in both FullTree and CompactedTree. The test inserts a
random set of leaves using InsertMany and verifies the resulting root and
retrieved leaves.

The Copy method in both FullTree and CompactedTree is updated to use
InsertMany for efficiency when copying leaves to the target tree.
Copy link
Contributor

@ffranr ffranr left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Member

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

Lgtm!

@GeorgeTsagk
Copy link
Member

pending this

@Roasbeef Roasbeef merged commit bf6ffa7 into lightninglabs:main Apr 22, 2025
17 of 18 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Taproot-Assets Project Board Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

4 participants