Skip to content

tapdb: add implementation of supplycommit.SupplyTreeView #1507

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 11 commits into from
Jun 25, 2025

Conversation

Roasbeef
Copy link
Member

@Roasbeef Roasbeef commented May 3, 2025

In this commit, we add an implementation of the supplycommitment.SupplyTreeView interface to tapdb. This provides us with a concrete interface to be used along side #1464.

The structure is similar to the existing multi-verse trees, but we instead of scoping by proof type (issuance or transfer), we instead scope by the group key of a given asset.

Along the way we do some refactors to allow us to do atomic inserts across all the sub-trees, and also the root tree.

@levmi levmi moved this from 🆕 New to 🏗 In progress in Taproot-Assets Project Board May 5, 2025
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 7c88480 to 522c3ee Compare May 5, 2025 22:46
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch 2 times, most recently from 8d5041c to 648cb72 Compare May 5, 2025 23:02
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch 5 times, most recently from 6e0b6a4 to aa782c8 Compare May 6, 2025 01:27
@levmi levmi added this to the v0.7 milestone May 8, 2025
@levmi levmi moved this from 🏗 In progress to 👀 In review in Taproot-Assets Project Board May 8, 2025
@levmi levmi requested a review from ffranr May 13, 2025 16:39
@guggero guggero self-requested a review May 28, 2025 06:57
Comment on lines 172 to 179
txErr := bt.db.ExecTx(ctx, &writeTx, func(db BaseUniverseStore) error {
finalResults, err = insertBurnsInternal(
ctx, db, spec, burnLeaves...,
)

// TODO(roasbeef): also update the root supply tree?
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect it probably would be best to update the supply tree root here also as the TODO suggests. Within the same db transaction. If not here then where?

Copy link
Member Author

Choose a reason for hiding this comment

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

So this is done in the final PR of the series. We broke out these internal functions to add to each supply tree as the final PR uses these to update the root supply tree all at once. Just like we have methods to update an individual universe tree, but in practice we always update the multiverse trees afterwards.

The reason I isolated this logic to the state machine is to ensure that via that interface, the set of supply roots are always consistent.

@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 648cb72 to a0cdbd9 Compare June 9, 2025 23:35
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch 4 times, most recently from b5f3d5e to dd1d318 Compare June 12, 2025 01:35
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from a0cdbd9 to 414823f Compare June 12, 2025 01:35
@lightninglabs-deploy
Copy link

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

@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from dd1d318 to 87fefbc Compare June 18, 2025 01:52
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 414823f to 6c86168 Compare June 18, 2025 01:52
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 87fefbc to 570e477 Compare June 18, 2025 02:06
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 6c86168 to bab9a94 Compare June 18, 2025 02:06
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from faae3fc to 3b3420a Compare June 20, 2025 23:49
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 035e250 to 31a5fc6 Compare June 20, 2025 23:49
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 3b3420a to 8fff268 Compare June 21, 2025 00:09
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 31a5fc6 to 03eb6b0 Compare June 21, 2025 00:09
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 8fff268 to c668391 Compare June 21, 2025 00:12
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 03eb6b0 to 70c7f3b Compare June 21, 2025 00:12
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from c668391 to 8b06b69 Compare June 21, 2025 00:20
@Roasbeef Roasbeef force-pushed the asset-commitment-creator branch from 70c7f3b to 82f4ee3 Compare June 21, 2025 00:20
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 8b06b69 to b8988d3 Compare June 21, 2025 01:22
@guggero guggero changed the base branch from asset-commitment-creator to main June 23, 2025 07:35
@guggero guggero self-requested a review June 23, 2025 07:35
Copy link
Member

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

Needs a linter fix and perhaps a rebase. Otherwise looks good to me.

@ffranr
Copy link
Contributor

ffranr commented Jun 23, 2025

This patch fixes lint errors:

Subject: [PATCH] wallet: add method LndRpcWalletAnchor.DeriveNextKey()

Add `DeriveNextKey` method to `LndRpcWalletAnchor`, enabling it to
satisfy the `supplycommit.Wallet` interface.
---
Index: tapdb/universe.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/tapdb/universe.go b/tapdb/universe.go
--- a/tapdb/universe.go	(revision b8988d3a5a2c8f5a5b5c252698955c1b4b14f5cb)
+++ b/tapdb/universe.go	(date 1750685612847)
@@ -130,31 +130,6 @@
 		arg UpsertUniverseSupplyRoot) (int64, error)
 }
 
-// specifierToIdentifier converts an asset.Specifier into a universe.Identifier
-// for a specific proof type.
-//
-// NOTE: This makes an assumption that only specifiers with a group key are
-// valid for the ignore and burn proof types.
-func specifierToIdentifier(spec asset.Specifier,
-	proofType universe.ProofType) (universe.Identifier, error) {
-
-	var id universe.Identifier
-
-	// The specifier must have a group key to be able to be used within the
-	// ignore or burn tree context.
-	requireGroupKey := proofType == universe.ProofTypeIgnore ||
-		proofType == universe.ProofTypeBurn
-	if requireGroupKey && !spec.HasGroupPubKey() {
-		return id, fmt.Errorf("group key must be set for proof type %v",
-			proofType)
-	}
-
-	id.GroupKey = spec.UnwrapGroupKeyToPtr()
-	id.ProofType = proofType
-
-	return id, nil
-}
-
 // getUniverseTreeSum retrieves the sum of a universe tree specified by its
 // identifier.
 func getUniverseTreeSum(ctx context.Context, db BatchedUniverseTree,
Index: tapdb/supply_tree.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/tapdb/supply_tree.go b/tapdb/supply_tree.go
--- a/tapdb/supply_tree.go	(revision b8988d3a5a2c8f5a5b5c252698955c1b4b14f5cb)
+++ b/tapdb/supply_tree.go	(date 1750685675569)
@@ -278,7 +278,8 @@
 
 	groupKey, err := assetSpec.UnwrapGroupKeyOrErr()
 	if err != nil {
-		return nil, fmt.Errorf("group key must be specified for mint supply: %w", err)
+		return nil, fmt.Errorf("group key must be specified for "+
+			"mint supply: %w", err)
 	}
 	subNs := subTreeNamespace(groupKey, supplycommit.MintTreeType)
 
@@ -361,9 +362,8 @@
 
 	groupKey, err := spec.UnwrapGroupKeyOrErr()
 	if err != nil {
-		return nil, fmt.Errorf(
-			"group key must be specified for supply updates: %w", err,
-		)
+		return nil, fmt.Errorf("group key must be specified for "+
+			"supply updates: %w", err)
 	}
 
 	// Group updates by their sub-tree type, this'll simplify the logic

@ffranr
Copy link
Contributor

ffranr commented Jun 24, 2025

If helpful, I cleaned up the linter errors and rebased the branch here: https://github.com/ffranr/taproot-assets/tree/rebased-universe-supply-trees

Roasbeef added 2 commits June 24, 2025 17:17
In this commit, we add new namespace routines for the supply tree. We'll use these to modify exactly which namespace we use for the ignore and burn trees. Rather than store using the old universe namespace, we choose to roll a new set of namespaces, as these trees are specific to a given group key.
…space directly

In this commit, as a prep for the new universe supply tree routines, we
refactor the InsertBurns method to rely on a new internal helper
function. We also change the namespace we store things under to use the
new supply tree namespaces.

Finally, we fix a bug that would use different roots for the merkle tree
proofs after insertion. We'll now ensure that the merkle tree proofs all
use the same merkle root.
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from b8988d3 to 85109d9 Compare June 25, 2025 00:27
Roasbeef added 9 commits June 24, 2025 17:51
…pace

Similar to the last commit, in this commit, we refactor the AddTuples
method to use a new helper function. We then start to use the new supply
tree namespace just like we did for the burn tree.
…ce directly

In this commit, we update the helper functions that the ignore and burn tree use to take the namespace directly instead of the universe.Identifier. This makes the functions more flexible, as they don't assume a namespace derivation.
In this commit, we break up the singular universeUpsertProofLeaf
function into two: one for inserting into the universe tree, and one for
taking the updated universe root to insert into the multi-verse tree.
In this commit, we add a new migration for the universe supply trees.
Rather than use the multi-verse pointers (which are namespaced by proof
type), we instead move to use a new set of smt tree pointers, that are
scoped by the group key instead.

We'll use these later to keep track of the new supply sub-trees, and
also root supply tree. We also add a new proof type of mint_supply, to
distinguish from the normal issuance type.
In this commit, we add a concrete implementation of SupplyTreeStore in
tapdb. This is similar to the structs we used to manage the multiverse
tree, which is scoped by proof type. Instead, the supply trees are
scoped by group key.

We can use this to stage a series of updates for a group key (mint,
ignore, burn), and insert into a supply tree in a single atomic
transaction. We also expose methods to fetch each sub supply tree as
well.
In this commit, we add tests for the supply tree store. We use rapid to ensure that we can handle any combiniation of events when we go to update them in the tree.
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from 85109d9 to 1ab7129 Compare June 25, 2025 00:52
@Roasbeef
Copy link
Member Author

Merging as the unit test failure is unrelated:

--- FAIL: TestBatchedAssetIssuance (86.69s)
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/001/tmp.db
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/002/tmp.db
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/003/tmp.db
    --- FAIL: TestBatchedAssetIssuance/minting_with_cancellation (61.28s)
        planter_test.go:312: 
            	Error Trace:	/home/runner/work/taproot-assets/taproot-assets/tapgarden/planter_test.go:312
            	            				/home/runner/work/taproot-assets/taproot-assets/tapgarden/planter_test.go:1312
            	            				/home/runner/work/taproot-assets/taproot-assets/tapgarden/planter_test.go:2048
            	Error:      	Condition never satisfied
            	Test:       	TestBatchedAssetIssuance/minting_with_cancellation
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/004/tmp.db
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/005/tmp.db
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/006/tmp.db
    test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuance1396529981/007/tmp.db

@Roasbeef Roasbeef merged commit e9bc43e into lightninglabs:main Jun 25, 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 Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

5 participants