Skip to content

Commit

Permalink
RNS query fix (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Nov 5, 2024
2 parents 803842c + b15ec71 commit 61000af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
42 changes: 34 additions & 8 deletions x/rns/keeper/grpc_query_list_owned_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -22,21 +24,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
}
18 changes: 17 additions & 1 deletion x/rns/keeper/register_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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"
Expand All @@ -26,7 +28,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)
Expand Down Expand Up @@ -67,4 +69,18 @@ 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))
}

0 comments on commit 61000af

Please sign in to comment.