Skip to content

Commit 716053b

Browse files
Roasbeefguggero
authored andcommitted
universe: rename RootNode to MultiverseRootNode
In this commit, we rename RootNode to MultiverseRootNode as we can return multiple root types. We also modify the query to use the new `FetchMultiverseRoot` method as well.
1 parent 93546f7 commit 716053b

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

tapdb/multiverse.go

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,48 @@ const (
3232
transferMultiverseNS = "multiverse-transfer"
3333
)
3434

35+
var (
36+
// ErrNoMultiverseRoot is returned when no universe root is found for
37+
// the target proof type.
38+
ErrNoMultiverseRoot = errors.New("no multiverse root found")
39+
)
40+
3541
type (
3642
BaseUniverseRoot = sqlc.UniverseRootsRow
3743

3844
UniverseRootsParams = sqlc.UniverseRootsParams
45+
46+
// MultiverseRoot is the root of a multiverse tree. Two trees exist:
47+
// issuance and transfers.
48+
MultiverseRoot = sqlc.FetchMultiverseRootRow
49+
50+
// MultiverseLeaf is a leaf in a multiverse.
51+
MultiverseLeaf = sqlc.QueryMultiverseLeavesRow
52+
53+
// QueryMultiverseLeaves is used to query for a set of leaves based on
54+
// the proof type and asset ID (or group key)
55+
QueryMultiverseLeaves = sqlc.QueryMultiverseLeavesParams
3956
)
4057

4158
// BaseMultiverseStore is used to interact with a set of base universe
4259
// roots, also known as a multiverse.
4360
type BaseMultiverseStore interface {
4461
BaseUniverseStore
4562

63+
// UniverseRoots returns the set of active universe roots for a given
64+
// Multiverse type.
4665
UniverseRoots(ctx context.Context,
4766
params UniverseRootsParams) ([]BaseUniverseRoot, error)
67+
68+
// QueryMultiverseLeaves is used to query for the set of leaves that
69+
// reside in a multiverse tree.
70+
QueryMultiverseLeaves(ctx context.Context,
71+
arg QueryMultiverseLeaves) ([]MultiverseLeaf, error)
72+
73+
// FetchMultiverseRoot returns the root of the multiverse tree for a
74+
// given target namespace (proof type in this case).
75+
FetchMultiverseRoot(ctx context.Context,
76+
proofNamespace string) (MultiverseRoot, error)
4877
}
4978

5079
// BaseMultiverseOptions is the set of options for multiverse queries.
@@ -504,40 +533,53 @@ func namespaceForProof(proofType universe.ProofType) (string, error) {
504533
}
505534
}
506535

507-
// RootNode returns the root multiverse node for the given proof type.
508-
func (b *MultiverseStore) RootNode(ctx context.Context,
509-
proofType universe.ProofType) (*universe.MultiverseRoot, error) {
536+
// MultiverseRootNode returns the root multiverse node for the given proof
537+
// type.
538+
func (b *MultiverseStore) MultiverseRootNode(ctx context.Context,
539+
proofType universe.ProofType) (fn.Option[universe.MultiverseRoot],
540+
error) {
541+
542+
none := fn.None[universe.MultiverseRoot]()
510543

511544
multiverseNS, err := namespaceForProof(proofType)
512545
if err != nil {
513-
return nil, err
546+
return none, err
514547
}
515548

516-
var rootNode *universe.MultiverseRoot
549+
var rootNode universe.MultiverseRoot
517550

518551
readTx := NewBaseUniverseReadTx()
519552
dbErr := b.db.ExecTx(ctx, &readTx, func(db BaseMultiverseStore) error {
520-
multiverseTree := mssmt.NewCompactedTree(
521-
newTreeStoreWrapperTx(db, multiverseNS),
522-
)
553+
multiverseRoot, err := db.FetchMultiverseRoot(ctx, multiverseNS)
554+
switch {
555+
case errors.Is(err, sql.ErrNoRows):
556+
return ErrNoMultiverseRoot
523557

524-
multiverseRoot, err := multiverseTree.Root(ctx)
558+
case err != nil:
559+
return err
560+
}
561+
562+
nodeHash, err := newKey(multiverseRoot.MultiverseRootHash[:])
525563
if err != nil {
526564
return err
527565
}
528566

529-
rootNode = &universe.MultiverseRoot{
530-
Node: multiverseRoot,
567+
smtRoot := mssmt.NewComputedBranch(
568+
nodeHash, uint64(multiverseRoot.MultiverseRootSum),
569+
)
570+
571+
rootNode = universe.MultiverseRoot{
572+
Node: smtRoot,
531573
ProofType: proofType,
532574
}
533575

534576
return nil
535577
})
536578
if dbErr != nil {
537-
return nil, dbErr
579+
return none, dbErr
538580
}
539581

540-
return rootNode, nil
582+
return fn.Some(rootNode), nil
541583
}
542584

543585
// UniverseRootNode returns the Universe root node for the given asset ID.

tapdb/universe.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ type (
5050

5151
// DeleteMultiverseLeaf is used to delete a multiverse leaf.
5252
DeleteMultiverseLeaf = sqlc.DeleteMultiverseLeafParams
53-
54-
// QueryMultiverseLeaves is used to query for a set of leaves based on
55-
// the proof type and asset ID (or group key)
56-
QueryMultiverseLeaves = sqlc.QueryMultiverseLeavesParams
57-
58-
// MultiverseLeaf is a leaf in a multiverse.
59-
MultiverseLeaf = sqlc.QueryMultiverseLeavesRow
6053
)
6154

6255
// BaseUniverseStore is the main interface for the Taproot Asset universe store.
@@ -117,11 +110,6 @@ type BaseUniverseStore interface {
117110
// DeleteMultiverseLeaf deletes a multiverse leaf from the database.
118111
DeleteMultiverseLeaf(ctx context.Context,
119112
arg DeleteMultiverseLeaf) error
120-
121-
// QueryMultiverseLeaves is used to query for the set of leaves that
122-
// reside in a multiverse tree.
123-
QueryMultiverseLeaves(ctx context.Context,
124-
arg QueryMultiverseLeaves) ([]MultiverseLeaf, error)
125113
}
126114

127115
// BaseUniverseStoreOptions is the set of options for universe tree queries.

0 commit comments

Comments
 (0)