Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(client): use cmtservice for comet-validator-set command #17187

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/grpc/cmtservice/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 17 additions & 12 deletions client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}

Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions client/grpc/cmtservice/validator.go
Original file line number Diff line number Diff line change
@@ -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)
}
95 changes: 3 additions & 92 deletions client/rpc/validators.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down Expand Up @@ -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)
},
}

Expand All @@ -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
}
2 changes: 1 addition & 1 deletion tests/e2e/gov/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
}

Expand Down
7 changes: 6 additions & 1 deletion x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"encoding/json"
"fmt"
"strconv"

Expand Down Expand Up @@ -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)
},
}

Expand Down