Skip to content

Commit

Permalink
fix(assets,delegation): case insensitive query (#80)
Browse files Browse the repository at this point in the history
* fix(delegation): convert query to lowercase

* fix(assets): convert query to lowercase

* refactor(assets,delegation): check case with flag

* fix(assets,delegation): stateless validate cli

* fix(assets): deprecate StakerExoCoreAddr

* fix(assets): remove StakerExoCoreAddr
  • Loading branch information
MaxMustermann2 authored Jun 6, 2024
1 parent 34cf62f commit 3a3d018
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 270 deletions.
5 changes: 0 additions & 5 deletions proto/exocore/assets/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,5 @@ service Query {
option (cosmos.query.v1.module_query_safe) = true;
option (google.api.http).get = "/exocore/assets/v1/QueStakerSpecifiedAssetAmount";
}
// StakerExCoreAddr queries the staker exocore address.
rpc QueStakerExoCoreAddr(QueryStakerExCoreAddr) returns (QueryStakerExCoreAddrResponse) {
option (cosmos.query.v1.module_query_safe) = true;
option (google.api.http).get = "/exocore/assets/v1/QueStakerExoCoreAddr/{staker}";
}
}

59 changes: 22 additions & 37 deletions x/assets/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"fmt"
"strconv"
"strings"

errorsmod "cosmossdk.io/errors"
"github.com/ExocoreNetwork/exocore/x/assets/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +35,6 @@ func GetQueryCmd() *cobra.Command {
QueStakerSpecifiedAssetAmount(),
QueOperatorAssetInfos(),
QueOperatorSpecifiedAssetAmount(),
// QueStakerExoCoreAddr(),
)
return cmd
}
Expand Down Expand Up @@ -148,7 +149,7 @@ func QueStakingAssetInfo() *cobra.Command {
_, assetID := types.GetStakeIDAndAssetIDFromStr(clientChainLzID, "", args[0])
queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryStakingAssetInfo{
AssetID: assetID,
AssetID: assetID, // already lowercase
}
res, err := queryClient.QueStakingAssetInfo(context.Background(), req)
if err != nil {
Expand Down Expand Up @@ -202,9 +203,14 @@ func QueStakerAssetInfos() *cobra.Command {
return err
}

stakerID := args[0]
if _, _, err := types.ValidateID(stakerID, false, false); err != nil {
return errorsmod.Wrap(types.ErrInvalidCliCmdArg, err.Error())
}

queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryStakerAssetInfo{
StakerID: args[0],
StakerID: strings.ToLower(stakerID),
}
res, err := queryClient.QueStakerAssetInfos(context.Background(), req)
if err != nil {
Expand Down Expand Up @@ -238,8 +244,8 @@ func QueStakerSpecifiedAssetAmount() *cobra.Command {
}
stakerID, assetID := types.GetStakeIDAndAssetIDFromStr(clientChainLzID, args[1], args[2])
req := &types.QuerySpecifiedAssetAmountReq{
StakerID: stakerID,
AssetID: assetID,
StakerID: stakerID, // already lowercase
AssetID: assetID, // already lowercase
}
res, err := queryClient.QueStakerSpecifiedAssetAmount(context.Background(), req)
if err != nil {
Expand Down Expand Up @@ -267,8 +273,12 @@ func QueOperatorAssetInfos() *cobra.Command {
}

queryClient := types.NewQueryClient(clientCtx)
accAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return errorsmod.Wrap(types.ErrInvalidCliCmdArg, err.Error())
}
req := &types.QueryOperatorAssetInfos{
OperatorAddr: args[0],
OperatorAddr: accAddr.String(), // already lowercase
}
res, err := queryClient.QueOperatorAssetInfos(context.Background(), req)
if err != nil {
Expand Down Expand Up @@ -300,41 +310,16 @@ func QueOperatorSpecifiedAssetAmount() *cobra.Command {
return errorsmod.Wrap(types.ErrInvalidCliCmdArg, err.Error())
}
_, assetID := types.GetStakeIDAndAssetIDFromStr(clientChainLzID, "", args[2])
queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryOperatorSpecifiedAssetAmountReq{
OperatorAddr: args[0],
AssetID: assetID,
}
res, err := queryClient.QueOperatorSpecifiedAssetAmount(context.Background(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// QueStakerExoCoreAddr queries staker ExoCore address
func QueStakerExoCoreAddr() *cobra.Command {
cmd := &cobra.Command{
Use: "QueStakerExoCoreAddr stakerID",
Short: "Get staker ExoCore address",
Long: "Get staker ExoCore address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
accAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
return errorsmod.Wrap(types.ErrInvalidCliCmdArg, err.Error())
}

queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryStakerExCoreAddr{
Staker: args[0],
req := &types.QueryOperatorSpecifiedAssetAmountReq{
OperatorAddr: accAddr.String(), // already lowercase
AssetID: assetID, // already lowercase
}
res, err := queryClient.QueStakerExoCoreAddr(context.Background(), req)
res, err := queryClient.QueOperatorSpecifiedAssetAmount(context.Background(), req)
if err != nil {
return err
}
Expand Down
10 changes: 0 additions & 10 deletions x/assets/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,3 @@ func (k Keeper) QueOperatorSpecifiedAssetAmount(ctx context.Context, req *assets
}
return k.GetOperatorSpecifiedAssetInfo(c, addr, req.AssetID)
}

// QueStakerExoCoreAddr outdated,will be deprecated
func (k Keeper) QueStakerExoCoreAddr(ctx context.Context, req *assetstype.QueryStakerExCoreAddr) (*assetstype.QueryStakerExCoreAddrResponse, error) {
c := sdk.UnwrapSDKContext(ctx)
exoCoreAddr, err := k.GetStakerExoCoreAddr(c, req.Staker)
if err != nil {
return nil, err
}
return &assetstype.QueryStakerExCoreAddrResponse{ExoCoreAddr: exoCoreAddr}, nil
}
2 changes: 1 addition & 1 deletion x/assets/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (gs GenesisState) Validate() error {
// validate the stakerID
var stakerClientChainID uint64
var err error
if _, stakerClientChainID, err = ValidateID(stakerID, true); err != nil {
if _, stakerClientChainID, err = ValidateID(stakerID, true, true); err != nil {
return errorsmod.Wrapf(
ErrInvalidGenesisData,
"invalid stakerID: %s",
Expand Down
5 changes: 2 additions & 3 deletions x/assets/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ func ParseID(key string) (string, uint64, error) {
return keys[0], id, nil
}

func ValidateID(key string, validateEth bool) (string, uint64, error) {
// check lowercase
if key != strings.ToLower(key) {
func ValidateID(key string, checkLowercase bool, validateEth bool) (string, uint64, error) {
if checkLowercase && key != strings.ToLower(key) {
return "", 0, errorsmod.Wrapf(ErrParseAssetsStateKey, "ID not lowercase: %s", key)
}
// parse it
Expand Down
Loading

0 comments on commit 3a3d018

Please sign in to comment.