From 8503c1822bd4fcd5294a24f00a08f40084e08a1f Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Wed, 2 Aug 2023 16:55:01 +0800 Subject: [PATCH] refactor(client): use cmtservice for `comet-validator-set` command (#17187) Co-authored-by: Julien Robert --- client/grpc/cmtservice/block.go | 9 +++ client/grpc/cmtservice/service.go | 29 +++++---- client/grpc/cmtservice/validator.go | 17 ++++++ client/rpc/validators.go | 95 +---------------------------- tests/e2e/gov/query.go | 2 +- x/gov/client/cli/query.go | 7 ++- 6 files changed, 53 insertions(+), 106 deletions(-) create mode 100644 client/grpc/cmtservice/validator.go diff --git a/client/grpc/cmtservice/block.go b/client/grpc/cmtservice/block.go index 9b2ca6bb0c9c..1575626fa629 100644 --- a/client/grpc/cmtservice/block.go +++ b/client/grpc/cmtservice/block.go @@ -9,6 +9,15 @@ import ( "github.com/cosmos/cosmos-sdk/client" ) +func getBlockHeight(ctx context.Context, clientCtx client.Context) (int64, error) { + status, err := getNodeStatus(ctx, clientCtx) + if err != nil { + return 0, err + } + height := status.SyncInfo.LatestBlockHeight + return height, nil +} + func getBlock(ctx context.Context, clientCtx client.Context, height *int64) (*coretypes.ResultBlock, error) { // get the node node, err := clientCtx.GetNode() diff --git a/client/grpc/cmtservice/service.go b/client/grpc/cmtservice/service.go index 8740c0d8f117..39e17ce1a9dc 100644 --- a/client/grpc/cmtservice/service.go +++ b/client/grpc/cmtservice/service.go @@ -11,9 +11,10 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/rpc" codectypes "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" qtypes "github.com/cosmos/cosmos-sdk/types/query" "github.com/cosmos/cosmos-sdk/version" ) @@ -80,12 +81,12 @@ func (s queryServer) GetLatestBlock(ctx context.Context, _ *GetLatestBlockReques // GetBlockByHeight implements ServiceServer.GetBlockByHeight func (s queryServer) GetBlockByHeight(ctx context.Context, req *GetBlockByHeightRequest) (*GetBlockByHeightResponse, error) { - chainHeight, err := rpc.GetChainHeight(s.clientCtx) + blockHeight, err := getBlockHeight(ctx, s.clientCtx) if err != nil { return nil, err } - if req.Height > chainHeight { + if req.Height > blockHeight { return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length") } @@ -108,7 +109,7 @@ func (s queryServer) GetLatestValidatorSet(ctx context.Context, req *GetLatestVa return nil, err } - return validatorsOutput(ctx, s.clientCtx, nil, page, limit) + return ValidatorsOutput(ctx, s.clientCtx, nil, page, limit) } func (m *GetLatestValidatorSetResponse) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { @@ -130,16 +131,16 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida return nil, err } - chainHeight, err := rpc.GetChainHeight(s.clientCtx) + blockHeight, err := getBlockHeight(ctx, s.clientCtx) if err != nil { return nil, status.Error(codes.Internal, "failed to parse chain height") } - if req.Height > chainHeight { + if req.Height > blockHeight { return nil, status.Error(codes.InvalidArgument, "requested block height is bigger then the chain length") } - r, err := validatorsOutput(ctx, s.clientCtx, &req.Height, page, limit) + r, err := ValidatorsOutput(ctx, s.clientCtx, &req.Height, page, limit) if err != nil { return nil, err } @@ -151,8 +152,8 @@ func (s queryServer) GetValidatorSetByHeight(ctx context.Context, req *GetValida }, nil } -func validatorsOutput(ctx context.Context, cctx client.Context, height *int64, page, limit int) (*GetLatestValidatorSetResponse, error) { - vs, err := rpc.GetValidators(ctx, cctx, height, &page, &limit) +func ValidatorsOutput(ctx context.Context, clientCtx client.Context, height *int64, page, limit int) (*GetLatestValidatorSetResponse, error) { + vs, err := getValidators(ctx, clientCtx, height, page, limit) if err != nil { return nil, err } @@ -161,18 +162,22 @@ func validatorsOutput(ctx context.Context, cctx client.Context, height *int64, p BlockHeight: vs.BlockHeight, Validators: make([]*Validator, len(vs.Validators)), Pagination: &qtypes.PageResponse{ - Total: vs.Total, + Total: uint64(vs.Total), }, } for i, v := range vs.Validators { - anyPub, err := codectypes.NewAnyWithValue(v.PubKey) + pk, err := cryptocodec.FromCmtPubKeyInterface(v.PubKey) + if err != nil { + return nil, err + } + anyPub, err := codectypes.NewAnyWithValue(pk) if err != nil { return nil, err } resp.Validators[i] = &Validator{ - Address: v.Address.String(), + Address: sdk.ConsAddress(v.Address).String(), ProposerPriority: v.ProposerPriority, PubKey: anyPub, VotingPower: v.VotingPower, diff --git a/client/grpc/cmtservice/validator.go b/client/grpc/cmtservice/validator.go new file mode 100644 index 000000000000..5b81d2bc4cac --- /dev/null +++ b/client/grpc/cmtservice/validator.go @@ -0,0 +1,17 @@ +package cmtservice + +import ( + "context" + + coretypes "github.com/cometbft/cometbft/rpc/core/types" + + "github.com/cosmos/cosmos-sdk/client" +) + +func getValidators(ctx context.Context, clientCtx client.Context, height *int64, page, limit int) (*coretypes.ResultValidators, error) { + node, err := clientCtx.GetNode() + if err != nil { + return nil, err + } + return node.Validators(ctx, height, &page, &limit) +} diff --git a/client/rpc/validators.go b/client/rpc/validators.go index 55c2262a8881..7facee4259d4 100644 --- a/client/rpc/validators.go +++ b/client/rpc/validators.go @@ -1,24 +1,16 @@ package rpc import ( - "context" - "fmt" "strconv" - "strings" - cmttypes "github.com/cometbft/cometbft/types" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" "github.com/cosmos/cosmos-sdk/types/query" ) -// TODO these next two functions feel kinda hacky based on their placement - // ValidatorCommand returns the validator set for a given height func ValidatorCommand() *cobra.Command { cmd := &cobra.Command{ @@ -49,12 +41,12 @@ func ValidatorCommand() *cobra.Command { page, _ := cmd.Flags().GetInt(flags.FlagPage) limit, _ := cmd.Flags().GetInt(flags.FlagLimit) - result, err := GetValidators(cmd.Context(), clientCtx, height, &page, &limit) + response, err := cmtservice.ValidatorsOutput(cmd.Context(), clientCtx, height, page, limit) if err != nil { return err } - return clientCtx.PrintObjectLegacy(result) + return clientCtx.PrintProto(response) }, } @@ -65,84 +57,3 @@ func ValidatorCommand() *cobra.Command { return cmd } - -// Validator output -type ValidatorOutput struct { - Address sdk.ConsAddress `json:"address"` - PubKey cryptotypes.PubKey `json:"pub_key"` - ProposerPriority int64 `json:"proposer_priority"` - VotingPower int64 `json:"voting_power"` -} - -// Validators at a certain height output in bech32 format -type ResultValidatorsOutput struct { - BlockHeight int64 `json:"block_height"` - Validators []ValidatorOutput `json:"validators"` - Total uint64 `json:"total"` -} - -func (rvo ResultValidatorsOutput) String() string { - var b strings.Builder - - fmt.Fprintf(&b, "block height: %d\n", rvo.BlockHeight) - fmt.Fprintf(&b, "total count: %d\n", rvo.Total) - - for _, val := range rvo.Validators { - fmt.Fprintf(&b, ` - Address: %s - Pubkey: %s - ProposerPriority: %d - VotingPower: %d - `, - val.Address, val.PubKey, val.ProposerPriority, val.VotingPower, - ) - } - - return b.String() -} - -func validatorOutput(validator *cmttypes.Validator) (ValidatorOutput, error) { - pk, err := cryptocodec.FromCmtPubKeyInterface(validator.PubKey) - if err != nil { - return ValidatorOutput{}, err - } - - return ValidatorOutput{ - Address: sdk.ConsAddress(validator.Address), - PubKey: pk, - ProposerPriority: validator.ProposerPriority, - VotingPower: validator.VotingPower, - }, nil -} - -// GetValidators from client -func GetValidators(ctx context.Context, clientCtx client.Context, height *int64, page, limit *int) (ResultValidatorsOutput, error) { - // get the node - node, err := clientCtx.GetNode() - if err != nil { - return ResultValidatorsOutput{}, err - } - - validatorsRes, err := node.Validators(ctx, height, page, limit) - if err != nil { - return ResultValidatorsOutput{}, err - } - - total := validatorsRes.Total - if validatorsRes.Total < 0 { - total = 0 - } - out := ResultValidatorsOutput{ - BlockHeight: validatorsRes.BlockHeight, - Validators: make([]ValidatorOutput, len(validatorsRes.Validators)), - Total: uint64(total), - } - for i := 0; i < len(validatorsRes.Validators); i++ { - out.Validators[i], err = validatorOutput(validatorsRes.Validators[i]) - if err != nil { - return out, err - } - } - - return out, nil -} diff --git a/tests/e2e/gov/query.go b/tests/e2e/gov/query.go index 40e3d3aa27a9..a600649d67b2 100644 --- a/tests/e2e/gov/query.go +++ b/tests/e2e/gov/query.go @@ -33,7 +33,7 @@ func (s *E2ETestSuite) TestCmdProposer() { fmt.Sprintf("--%s=json", flags.FlagOutput), }, false, - fmt.Sprintf("{\"proposal_id\":\"%s\",\"proposer\":\"%s\"}", "1", val.Address.String()), + fmt.Sprintf("{\"proposal_id\":%d,\"proposer\":\"%s\"}", 1, val.Address.String()), }, } diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index 9ef1dbf729a9..a6c1538c0ac0 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -1,6 +1,7 @@ package cli import ( + "encoding/json" "fmt" "strconv" @@ -59,7 +60,11 @@ func GetCmdQueryProposer() *cobra.Command { return err } - return clientCtx.PrintObjectLegacy(prop) + output, err := json.Marshal(prop) + if err != nil { + return err + } + return clientCtx.PrintRaw(output) }, }