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

Open
wants to merge 12 commits into
base: asset-commitment-creator
Choose a base branch
from

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 2 times, most recently from d786104 to af5f8f5 Compare May 5, 2025 23:15
Roasbeef added 9 commits May 5, 2025 16:46
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.
…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.
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch from af5f8f5 to 64516c9 Compare May 5, 2025 23:54
Roasbeef added 2 commits May 5, 2025 16:59
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 64516c9 to 6e0b6a4 Compare May 6, 2025 01:19
@Roasbeef Roasbeef force-pushed the universe-supply-trees branch 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
@lightninglabs-deploy
Copy link

@ffranr: review reminder

@guggero guggero self-requested a review May 28, 2025 06:57
id, err := specifierToIdentifier(spec, universe.ProofTypeBurn)
if err != nil {
return lfn.Err[lfn.Option[uint64]](err)
groupKey := spec.UnwrapGroupKeyToPtr()
Copy link
Contributor

Choose a reason for hiding this comment

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

another possibility: spec.UnwrapGroupKeyOrErr

Comment on lines +172 to +178
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?

Comment on lines +74 to +77
// To insert the universe leaf below, we'll need both the db the
// outpoint to be ignored. primary key for the asset genesis,
// and also the raw bytes of
assetGenID, err := db.FetchGenesisIDByAssetID(
Copy link
Contributor

Choose a reason for hiding this comment

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

unfinished comment

scriptKey := ignoreTup.ScriptKey
err = db.UpsertUniverseLeaf(ctx, UpsertUniverseLeaf{
AssetGenesisID: assetGenID,
ScriptKeyBytes: scriptKey.SchnorrSerialized(), //nolint:lll
Copy link
Contributor

Choose a reason for hiding this comment

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

//nolint:lll unnecessary here

Comment on lines +28 to +33
// addTuplesInternal performs the insertion of ignore tuples within a database
// transaction. It also updates the main supply tree with the new ignore
// sub-tree root.
//
// NOTE: This function must be called within a database transaction.
func addTuplesInternal(ctx context.Context, db BaseUniverseStore,
Copy link
Contributor

Choose a reason for hiding this comment

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

this in the doc comment is not currently true for this function:

It also updates the main supply tree with the new ignore
// sub-tree root.

Comment on lines +171 to 174
txErr := it.db.ExecTx(ctx, &writeTx, func(db BaseUniverseStore) error {
finalResults, err = addTuplesInternal(ctx, db, spec, tuples...)
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.

consider updating supply tree root here, similar to burn tree insertion


namespace := id.String()
namespace string, proofTypeStr string, groupKey *btcec.PublicKey,
key universe.LeafKey, leaf *universe.Leaf,
Copy link
Contributor

Choose a reason for hiding this comment

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

this commit doesn't compile, perhaps we should squash into another commit?

-- Reference to the root supply tree this leaf belongs to.
supply_root_id BIGINT NOT NULL REFERENCES universe_supply_roots(id) ON DELETE CASCADE,

-- The type of sub-tree this leaf represents (issuance, burn, ignore).
Copy link
Contributor

Choose a reason for hiding this comment

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

issuence should be mint_supply in this comment?

LeafNodeNamespace: subNs,
})
if err != nil {
fmt.Println("sub tree: ", subTreeType.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

fmt.Println left over from debugging?

Comment on lines +3 to +4
INSERT INTO proof_types (proof_type) VALUES ('burn'), ('ignore'), ('mint_supply')
ON CONFLICT (proof_type) DO NOTHING;
Copy link
Contributor

Choose a reason for hiding this comment

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

We also add a new proof type of mint_supply, to distinguish from the normal issuance type.

Is the new proof type (mint_supply) necessary here? I thought we already added supply commit specific prefixes to the relevant namespaces? If true, then I think we could just reuse the issuance proof type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

4 participants