Skip to content

Commit

Permalink
Merge pull request #612 from lightninglabs/universe-roots-group-optional
Browse files Browse the repository at this point in the history
universe: make additional details optional to speed up query
  • Loading branch information
Roasbeef authored Oct 24, 2023
2 parents 34f3283 + d2683f7 commit 8878d09
Show file tree
Hide file tree
Showing 12 changed files with 554 additions and 488 deletions.
12 changes: 10 additions & 2 deletions cmd/tapcli/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

const (
proofTypeName = "proof_type"
proofTypeName = "proof_type"
skipAmountsByIdName = "skip_amounts_by_id"
)

func getUniverseClient(ctx *cli.Context) (unirpc.UniverseClient, func()) {
Expand Down Expand Up @@ -70,6 +71,11 @@ var universeRootsCommand = cli.Command{
"either 'issuance' or 'transfer'",
Value: universe.ProofTypeIssuance.String(),
},
cli.BoolFlag{
Name: skipAmountsByIdName,
Usage: "skip showing the amounts by ID for grouped " +
"assets to optimize response size and speed",
},
},
Action: universeRoots,
}
Expand Down Expand Up @@ -154,7 +160,9 @@ func universeRoots(ctx *cli.Context) error {
// for all the known universe roots.
if universeID == nil {
universeRoots, err := client.AssetRoots(
ctxc, &unirpc.AssetRootRequest{},
ctxc, &unirpc.AssetRootRequest{
WithAmountsById: !ctx.Bool(skipAmountsByIdName),
},
)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2774,10 +2774,12 @@ func marshalUniverseRoot(node universe.BaseRoot) (*unirpc.UniverseRoot, error) {
// AssetRoots queries for the known Universe roots associated with each known
// asset. These roots represent the supply/audit state for each known asset.
func (r *rpcServer) AssetRoots(ctx context.Context,
_ *unirpc.AssetRootRequest) (*unirpc.AssetRootResponse, error) {
req *unirpc.AssetRootRequest) (*unirpc.AssetRootResponse, error) {

// First, we'll retrieve the full set of known asset Universe roots.
assetRoots, err := r.cfg.BaseUniverse.RootNodes(ctx)
assetRoots, err := r.cfg.BaseUniverse.RootNodes(
ctx, req.WithAmountsById,
)
if err != nil {
return nil, err
}
Expand Down
11 changes: 8 additions & 3 deletions tapdb/multiverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (b *MultiverseStore) RootNode(ctx context.Context,

// RootNodes returns the complete set of known base universe root nodes for the
// set of base universes tracked in the multiverse.
func (b *MultiverseStore) RootNodes(
ctx context.Context) ([]universe.BaseRoot, error) {
func (b *MultiverseStore) RootNodes(ctx context.Context,
withAmountsById bool) ([]universe.BaseRoot, error) {

var (
uniRoots []universe.BaseRoot
Expand Down Expand Up @@ -172,7 +172,12 @@ func (b *MultiverseStore) RootNodes(
if err != nil {
return err
}
}

// We skip the grouped assets if that wasn't explicitly
// requested by the user, saves us some calls for
// grouped assets.
if dbRoot.GroupKey != nil && withAmountsById {
groupLeaves, err := db.QueryUniverseLeaves(
ctx, UniverseLeafQuery{
Namespace: id.String(),
Expand All @@ -190,7 +195,7 @@ func (b *MultiverseStore) RootNodes(
copy(id[:], leaf.AssetID)
groupedAssets[id] = uint64(leaf.SumAmt)
}
} else {
} else if withAmountsById {
// For non-grouped assets, there's exactly one
// member, the asset itself.
groupedAssets = map[asset.ID]uint64{
Expand Down
4 changes: 2 additions & 2 deletions tapdb/universe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ func TestUniverseTreeIsolation(t *testing.T) {
)
multiverse := NewMultiverseStore(multiverseDB)

rootNodes, err := multiverse.RootNodes(ctx)
rootNodes, err := multiverse.RootNodes(ctx, true)
require.NoError(t, err)

// We should be able to find both of the roots we've inserted above.
Expand Down Expand Up @@ -514,7 +514,7 @@ func TestUniverseTreeIsolation(t *testing.T) {
require.ErrorIs(t, err, universe.ErrNoUniverseRoot)

// The deleted universe should not be present in the multiverse.
rootNodes, err = multiverse.RootNodes(ctx)
rootNodes, err = multiverse.RootNodes(ctx, true)
require.NoError(t, err)
require.Len(t, rootNodes, 1)
require.True(t, mssmt.IsEqualNode(rootNodes[0].Node, groupRoot))
Expand Down
952 changes: 483 additions & 469 deletions taprpc/universerpc/universe.pb.go

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions taprpc/universerpc/universe.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions taprpc/universerpc/universe.proto
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ service Universe {
}

message AssetRootRequest {
// If true, then the response will include the amounts for each asset ID
// of grouped assets.
bool with_amounts_by_id = 1;

// TODO(roasbeef): filter by asset ID, etc?
}

Expand Down
9 changes: 9 additions & 0 deletions taprpc/universerpc/universe.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,15 @@
}
}
},
"parameters": [
{
"name": "with_amounts_by_id",
"description": "If true, then the response will include the amounts for each asset ID\nof grouped assets.",
"in": "query",
"required": false,
"type": "boolean"
}
],
"tags": [
"Universe"
]
Expand Down
9 changes: 6 additions & 3 deletions universe/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ func (a *MintingArchive) RootNode(ctx context.Context,
}

// RootNodes returns the set of root nodes for all known base universes assets.
func (a *MintingArchive) RootNodes(ctx context.Context) ([]BaseRoot, error) {
log.Debugf("Fetching all known Universe roots")
func (a *MintingArchive) RootNodes(ctx context.Context,
withAmountsById bool) ([]BaseRoot, error) {

return a.cfg.Multiverse.RootNodes(ctx)
log.Debugf("Fetching all known Universe roots (with_amounts_by_id=%v)",
withAmountsById)

return a.cfg.Multiverse.RootNodes(ctx, withAmountsById)
}

// RegisterIssuance attempts to register a new issuance proof for a new minting
Expand Down
7 changes: 4 additions & 3 deletions universe/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/wire"

"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/mssmt"
"github.com/lightninglabs/taproot-assets/proof"
Expand Down Expand Up @@ -283,7 +282,8 @@ type MultiverseRoot struct {
type MultiverseArchive interface {
// RootNodes returns the complete set of known root nodes for the set
// of assets tracked in the base Universe.
RootNodes(ctx context.Context) ([]BaseRoot, error)
RootNodes(ctx context.Context, withAmountsById bool) ([]BaseRoot,
error)

// UpsertProofLeaf upserts a proof leaf within the multiverse tree and
// the universe tree that corresponds to the given key.
Expand Down Expand Up @@ -501,7 +501,8 @@ type DiffEngine interface {
RootNode(ctx context.Context, id Identifier) (BaseRoot, error)

// RootNodes returns the set of root nodes for all known universes.
RootNodes(ctx context.Context) ([]BaseRoot, error)
RootNodes(ctx context.Context, withAmountsById bool) ([]BaseRoot,
error)

// UniverseLeafKeys returns all the keys inserted in the universe.
UniverseLeafKeys(ctx context.Context, id Identifier) ([]LeafKey, error)
Expand Down
2 changes: 1 addition & 1 deletion universe/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *SimpleSyncer) executeSync(ctx context.Context, diffEngine DiffEngine,
// Otherwise, we'll just fetch all the roots from the remote universe.
default:
log.Infof("Fetching all roots for remote Universe server...")
targetRoots, err = diffEngine.RootNodes(ctx)
targetRoots, err = diffEngine.RootNodes(ctx, false)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions universe_rpc_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ func unmarshalUniverseRoots(

// RootNodes returns the complete set of known root nodes for the set
// of assets tracked in the base Universe.
func (r *RpcUniverseDiff) RootNodes(
ctx context.Context) ([]universe.BaseRoot, error) {
func (r *RpcUniverseDiff) RootNodes(ctx context.Context,
withAmountsById bool) ([]universe.BaseRoot, error) {

universeRoots, err := r.conn.AssetRoots(
ctx, &unirpc.AssetRootRequest{},
ctx, &unirpc.AssetRootRequest{
WithAmountsById: withAmountsById,
},
)
if err != nil {
return nil, err
Expand Down

0 comments on commit 8878d09

Please sign in to comment.