From 3a2b192c57e84a415bcc75f14febae1d9a3a5676 Mon Sep 17 00:00:00 2001 From: marston Date: Tue, 5 Nov 2024 09:58:49 -0500 Subject: [PATCH 1/2] query RNS fix --- x/rns/keeper/grpc_query_list_owned_names.go | 41 +++++++++++++++++---- x/rns/keeper/register_test.go | 18 ++++++++- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/x/rns/keeper/grpc_query_list_owned_names.go b/x/rns/keeper/grpc_query_list_owned_names.go index 5524332f..1a8d7d92 100644 --- a/x/rns/keeper/grpc_query_list_owned_names.go +++ b/x/rns/keeper/grpc_query_list_owned_names.go @@ -2,6 +2,7 @@ package keeper import ( "context" + storetypes "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,21 +23,45 @@ func (k Keeper) ListOwnedNames(goCtx context.Context, req *types.QueryListOwnedN store := ctx.KVStore(k.storeKey) namesStore := prefix.NewStore(store, types.KeyPrefix(types.NamesKeyPrefix)) - pageRes, err := query.Paginate(namesStore, req.Pagination, func(_ []byte, value []byte) error { + reverse := false + var limit uint64 = 100 + if req.Pagination != nil { // HERE IS THE FIX + reverse = req.Pagination.Reverse + limit = req.Pagination.Limit + } + + var iterator storetypes.Iterator + if reverse { + iterator = sdk.KVStoreReversePrefixIterator(namesStore, []byte{}) + } else { + iterator = sdk.KVStorePrefixIterator(namesStore, []byte{}) + } + + defer iterator.Close() + var i uint64 + + for ; iterator.Valid(); iterator.Next() { + + if i > limit { + break + } + var names types.Names - if err := k.cdc.Unmarshal(value, &names); err != nil { - return err + if err := k.cdc.Unmarshal(iterator.Value(), &names); err != nil { + continue } if names.Value == req.Address { namess = append(namess, names) + i++ } - return nil - }) - if err != nil { - return nil, status.Error(codes.Internal, err.Error()) } - return &types.QueryListOwnedNamesResponse{Names: namess, Pagination: pageRes}, nil + qpr := query.PageResponse{ + NextKey: nil, + Total: uint64(len(namess)), + } + + return &types.QueryListOwnedNamesResponse{Names: namess, Pagination: &qpr}, nil } diff --git a/x/rns/keeper/register_test.go b/x/rns/keeper/register_test.go index e0e6dc44..769087fd 100644 --- a/x/rns/keeper/register_test.go +++ b/x/rns/keeper/register_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/jackalLabs/canine-chain/v4/testutil" "github.com/jackalLabs/canine-chain/v4/x/rns/keeper" @@ -26,7 +27,7 @@ func (suite *KeeperTestSuite) TestMsgRegisterName() { cost, err := keeper.GetCostOfName(n, t) suite.Require().NoError(err) - coin := sdk.NewCoin("ujkl", sdk.NewInt(10000000000)) + coin := sdk.NewCoin("ujkl", sdk.NewInt(10000000000000000)) coins := sdk.NewCoins(coin) err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, address, coins) @@ -67,4 +68,19 @@ func (suite *KeeperTestSuite) TestMsgRegisterName() { _, err = suite.queryClient.Name(suite.ctx.Context(), &nameReq) suite.Require().NoError(err) + + for i := 0; i < 100; i++ { + err = suite.rnsKeeper.RegisterRNSName(suite.ctx, address.String(), fmt.Sprintf("mrpumpkinman%d.jkl", i), "{}", 1, false) // adding time to registration + suite.Require().NoError(err) + } + + r := types.QueryListOwnedNames{ + Address: address.String(), + } + + res, err := suite.queryClient.ListOwnedNames(suite.ctx.Context(), &r) + suite.Require().NoError(err) + + suite.Require().Equal(101, len(res.Names)) + } From b15ec714b03b71b4ac7c9951d6b2be20e1a3ecf8 Mon Sep 17 00:00:00 2001 From: marston Date: Tue, 5 Nov 2024 11:54:00 -0500 Subject: [PATCH 2/2] lint --- x/rns/keeper/grpc_query_list_owned_names.go | 1 + x/rns/keeper/register_test.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/x/rns/keeper/grpc_query_list_owned_names.go b/x/rns/keeper/grpc_query_list_owned_names.go index 1a8d7d92..f09c9f91 100644 --- a/x/rns/keeper/grpc_query_list_owned_names.go +++ b/x/rns/keeper/grpc_query_list_owned_names.go @@ -2,6 +2,7 @@ package keeper import ( "context" + storetypes "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/store/prefix" diff --git a/x/rns/keeper/register_test.go b/x/rns/keeper/register_test.go index 769087fd..1ef7806e 100644 --- a/x/rns/keeper/register_test.go +++ b/x/rns/keeper/register_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/jackalLabs/canine-chain/v4/testutil" "github.com/jackalLabs/canine-chain/v4/x/rns/keeper" @@ -82,5 +83,4 @@ func (suite *KeeperTestSuite) TestMsgRegisterName() { suite.Require().NoError(err) suite.Require().Equal(101, len(res.Names)) - }