Skip to content

Commit

Permalink
Merge pull request #634 from GeorgeTsagk/universerpc-pages
Browse files Browse the repository at this point in the history
Add pagination to universerpc calls
  • Loading branch information
Roasbeef authored Nov 2, 2023
2 parents 263a22a + ddf7eaa commit 3e6579c
Show file tree
Hide file tree
Showing 26 changed files with 1,625 additions and 864 deletions.
27 changes: 24 additions & 3 deletions cmd/tapcli/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,30 @@ func universeKeys(ctx *cli.Context) error {
return err
}

assetKeys, err := client.AssetLeafKeys(ctxc, universeID)
if err != nil {
return err
assetKeys := &unirpc.AssetLeafKeyResponse{}
offset := 0

for {
tempKeys, err := client.AssetLeafKeys(
ctxc, &unirpc.AssetLeafKeysRequest{
Id: universeID,
Offset: int32(offset),
Limit: universe.MaxPageSize,
},
)

if err != nil {
return err
}

if len(tempKeys.AssetKeys) == 0 {
break
}

assetKeys.AssetKeys = append(
assetKeys.AssetKeys, tempKeys.AssetKeys...,
)
offset += universe.MaxPageSize
}

printRespJSON(assetKeys)
Expand Down
55 changes: 51 additions & 4 deletions itest/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightninglabs/taproot-assets/universe"
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1157,10 +1158,10 @@ func AssertUniverseRootsEqual(a, b *unirpc.AssetRootResponse) bool {
func AssertUniverseStateEqual(t *testing.T, a, b unirpc.UniverseClient) bool {
ctxb := context.Background()

rootsA, err := a.AssetRoots(ctxb, &unirpc.AssetRootRequest{})
rootsA, err := assetRoots(ctxb, a, universe.MaxPageSize/100)
require.NoError(t, err)

rootsB, err := b.AssetRoots(ctxb, &unirpc.AssetRootRequest{})
rootsB, err := assetRoots(ctxb, b, universe.MaxPageSize/100)
require.NoError(t, err)

return AssertUniverseRootsEqual(rootsA, rootsB)
Expand Down Expand Up @@ -1196,10 +1197,20 @@ func AssertUniverseKeysEqual(t *testing.T, uniIDs []*unirpc.ID,
a, b unirpc.UniverseClient) {

for _, uniID := range uniIDs {
aUniKeys, err := a.AssetLeafKeys(context.Background(), uniID)
aUniKeys, err := a.AssetLeafKeys(
context.Background(),
&unirpc.AssetLeafKeysRequest{
Id: uniID,
},
)
require.NoError(t, err)

bUniKeys, err := b.AssetLeafKeys(context.Background(), uniID)
bUniKeys, err := b.AssetLeafKeys(
context.Background(),
&unirpc.AssetLeafKeysRequest{
Id: uniID,
},
)
require.NoError(t, err)

require.Equal(
Expand Down Expand Up @@ -1506,3 +1517,39 @@ func assertGroups(t *testing.T, client taprpc.TaprootAssetsClient,
equalityCheck(issuableAssets[0].Asset, groupedAssets[0])
equalityCheck(issuableAssets[1].Asset, groupedAssets[1])
}

// assetRoots is a helper method that fetches all roots from a given universe
// rpc by scanning for all pages.
func assetRoots(ctx context.Context,
uni unirpc.UniverseClient, pageSize int32) (*unirpc.AssetRootResponse, error) {

offset := int32(0)
roots := make(map[string]*unirpc.UniverseRoot)

for {
res, err := uni.AssetRoots(
ctx,
&unirpc.AssetRootRequest{
Offset: offset,
Limit: pageSize,
},
)
if err != nil {
return nil, err
}

if len(res.UniverseRoots) == 0 {
break
}

for k, v := range res.UniverseRoots {
roots[k] = v
}

offset += pageSize
}

return &unirpc.AssetRootResponse{
UniverseRoots: roots,
}, nil
}
6 changes: 5 additions & 1 deletion itest/collectible_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ func testCollectibleGroupSend(t *harnessTest) {
// The universe tree should also have a key for each asset, with all
// outpoints matching the chain anchor of the group anchor.
mintOutpoint := collectibleAnchor.ChainAnchor.AnchorOutpoint
uniKeys, err := t.tapd.AssetLeafKeys(ctxb, &collectUniID)
uniKeys, err := t.tapd.AssetLeafKeys(
ctxb, &unirpc.AssetLeafKeysRequest{
Id: &collectUniID,
},
)
require.NoError(t.t, err)
require.Len(t.t, uniKeys.AssetKeys, batchSize)

Expand Down
6 changes: 5 additions & 1 deletion itest/loadtest/mint_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ func mintTest(t *testing.T, ctx context.Context, cfg *Config) {
// The universe tree should also have a key for each asset, with all
// outpoints matching the chain anchor of the group anchor.
mintOutpoint := collectibleAnchor.ChainAnchor.AnchorOutpoint
uniKeys, err := alice.AssetLeafKeys(ctx, &collectUniID)
uniKeys, err := alice.AssetLeafKeys(
ctx, &unirpc.AssetLeafKeysRequest{
Id: &collectUniID,
},
)
require.NoError(t, err)
require.Len(t, uniKeys.AssetKeys, batchSize)

Expand Down
8 changes: 5 additions & 3 deletions itest/mint_batch_stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ func mintBatchStressTest(
// The universe tree should also have a key for each asset, with all
// outpoints matching the chain anchor of the group anchor.
mintOutpoint := collectibleAnchor.ChainAnchor.AnchorOutpoint
uniKeys, err := alice.AssetLeafKeys(ctx, &collectUniID)

leafKeys, err := fetchAllLeafKeys(t, alice, &collectUniID)
require.NoError(t, err)
require.Len(t, uniKeys.AssetKeys, batchSize)

correctOp := fn.All(uniKeys.AssetKeys, func(key *unirpc.AssetKey) bool {
require.Len(t, leafKeys, batchSize)

correctOp := fn.All(leafKeys, func(key *unirpc.AssetKey) bool {
return key.GetOpStr() == mintOutpoint
})
require.True(t, correctOp)
Expand Down
4 changes: 4 additions & 0 deletions itest/test_list_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ var testCases = []*testCase{
name: "federation sync config",
test: testFederationSyncConfig,
},
{
name: "universe pagination simple",
test: testUniversePaginationSimple,
},
}

var optionalTestCases = []*testCase{
Expand Down
Loading

0 comments on commit 3e6579c

Please sign in to comment.