Skip to content

Commit

Permalink
x/{gov,params,upgrade,distribution} CLI: In-Process test & use grpc q…
Browse files Browse the repository at this point in the history
…uery service (cosmos#6664)

* refactor CLI to use grpc query service

* In process CLI for gov

* ReadQueryCommandFlags

* gov tx

* Fix compiler errors

* Formatting

* x/distribution: use gRPC query

* Consistent

* Fix x/distrib test

* Update x/gov

* Add ReadQueryCommandFlags

* Fix lint

* Revert x/params

* x/params use grpc query

* Fix tests

* Use page request

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and chengwenxi committed Jul 22, 2020
1 parent 018cc8a commit 35728c9
Show file tree
Hide file tree
Showing 22 changed files with 368 additions and 325 deletions.
2 changes: 2 additions & 0 deletions x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.accountKeeper)
}

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}

// InitGenesis performs genesis initialization for the auth module. It returns
Expand Down
2 changes: 2 additions & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type AppModule struct {
accountKeeper types.AccountKeeper
}

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
}
Expand Down
2 changes: 2 additions & 0 deletions x/capability/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (AppModule) QuerierRoute() string { return "" }
// NewQuerierHandler returns the capability module's Querier.
func (am AppModule) NewQuerierHandler() sdk.Querier { return nil }

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}

// RegisterInvariants registers the capability module's invariants.
Expand Down
2 changes: 2 additions & 0 deletions x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (AppModule) QuerierRoute() string { return "" }
// NewQuerierHandler returns no sdk.Querier.
func (AppModule) NewQuerierHandler() sdk.Querier { return nil }

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}

// InitGenesis performs genesis initialization for the crisis module. It returns
Expand Down
129 changes: 52 additions & 77 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"
"strconv"
"strings"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/distribution/client/common"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

Expand Down Expand Up @@ -49,19 +49,14 @@ func GetCmdQueryParams() *cobra.Command {
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := clientCtx.QueryWithData(route, nil)
res, err := queryClient.Params(context.Background(), &types.QueryParamsRequest{})
if err != nil {
return err
}

var params types.Params
if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &params); err != nil {
return fmt.Errorf("failed to unmarshal params: %w", err)
}

return clientCtx.PrintOutput(params)
return clientCtx.PrintOutput(res.GetParams())
},
}

Expand Down Expand Up @@ -91,32 +86,22 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

params := types.NewQueryValidatorOutstandingRewardsParams(valAddr)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

resp, _, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryValidatorOutstandingRewards),
bz,
res, err := queryClient.ValidatorOutstandingRewards(
context.Background(),
&types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: validatorAddr},
)
if err != nil {
return err
}

var outstandingRewards types.ValidatorOutstandingRewards
if err := clientCtx.JSONMarshaler.UnmarshalJSON(resp, &outstandingRewards); err != nil {
return err
}

return clientCtx.PrintOutput(outstandingRewards)
return clientCtx.PrintOutput(res.GetRewards())
},
}

Expand Down Expand Up @@ -145,23 +130,22 @@ $ %s query distribution commission cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9l
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}

res, err := common.QueryValidatorCommission(clientCtx, validatorAddr)
res, err := queryClient.ValidatorCommission(
context.Background(),
&types.QueryValidatorCommissionRequest{ValidatorAddress: validatorAddr},
)
if err != nil {
return err
}

var valCom types.ValidatorAccumulatedCommission
if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &valCom); err != nil {
return err
}

return clientCtx.PrintOutput(valCom)
return clientCtx.PrintOutput(res.GetCommission())
},
}

Expand Down Expand Up @@ -190,6 +174,7 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
Expand All @@ -206,27 +191,27 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
return fmt.Errorf("end-height %s not a valid uint, please input a valid end-height", args[2])
}

params := types.NewQueryValidatorSlashesParams(validatorAddr, startHeight, endHeight)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
return err
}
pageReq := client.ReadPageRequest(cmd.Flags())

res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", types.QuerierRoute), bz)
res, err := queryClient.ValidatorSlashes(
context.Background(),
&types.QueryValidatorSlashesRequest{
ValidatorAddress: validatorAddr,
StartingHeight: startHeight,
EndingHeight: endHeight,
Req: pageReq,
},
)
if err != nil {
return err
}

var slashes []types.ValidatorSlashEvent
if err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &slashes); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}

return clientCtx.PrintOutput(slashes)
return clientCtx.PrintOutput(res.GetSlashes())
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "validator slashes")
return cmd
}

Expand All @@ -252,54 +237,48 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

// query for rewards from a particular delegation
if len(args) == 2 {
resp, _, err := common.QueryDelegationRewards(clientCtx, args[0], args[1])
validatorAddr, err := sdk.ValAddressFromBech32(args[1])
if err != nil {
return err
}

var result sdk.DecCoins
if err = clientCtx.JSONMarshaler.UnmarshalJSON(resp, &result); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
res, err := queryClient.DelegationRewards(
context.Background(),
&types.QueryDelegationRewardsRequest{DelegatorAddress: delegatorAddr, ValidatorAddress: validatorAddr},
)
if err != nil {
return err
}

return clientCtx.PrintOutput(result)
}

delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

params := types.NewQueryDelegatorParams(delegatorAddr)
bz, err := clientCtx.JSONMarshaler.MarshalJSON(params)
if err != nil {
return fmt.Errorf("failed to marshal params: %w", err)
return clientCtx.PrintOutput(res.GetRewards())
}

// query for delegator total rewards
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDelegatorTotalRewards)
res, _, err := clientCtx.QueryWithData(route, bz)
res, err := queryClient.DelegationTotalRewards(
context.Background(),
&types.QueryDelegationTotalRewardsRequest{DelegatorAddress: delegatorAddr},
)
if err != nil {
return err
}

var result types.QueryDelegatorTotalRewardsResponse
if err = clientCtx.JSONMarshaler.UnmarshalJSON(res, &result); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}

return clientCtx.PrintOutput(result)
return clientCtx.PrintOutput(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryCommunityPool returns the command for fetching community pool info
// GetCmdQueryCommunityPool returns the command for fetching community pool info.
func GetCmdQueryCommunityPool() *cobra.Command {
cmd := &cobra.Command{
Use: "community-pool",
Expand All @@ -320,18 +299,14 @@ $ %s query distribution community-pool
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", types.QuerierRoute), nil)
res, err := queryClient.CommunityPool(context.Background(), &types.QueryCommunityPoolRequest{})
if err != nil {
return err
}

var result sdk.DecCoins
if err := clientCtx.JSONMarshaler.UnmarshalJSON(res, &result); err != nil {
return err
}

return clientCtx.PrintOutput(result)
return clientCtx.PrintOutput(res.GetPool())
},
}

Expand Down
2 changes: 2 additions & 0 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.keeper)
}

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
}
Expand Down
2 changes: 2 additions & 0 deletions x/evidence/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func (am AppModule) NewQuerierHandler() sdk.Querier {
return keeper.NewQuerier(am.keeper)
}

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
func (am AppModule) RegisterQueryService(grpc.Server) {}

// RegisterInvariants registers the evidence module's invariants.
Expand Down
3 changes: 1 addition & 2 deletions x/gov/client/cli/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil"
)

Expand All @@ -23,7 +22,7 @@ func TestParseSubmitProposalFlags(t *testing.T) {
badJSON, cleanup2 := testutil.WriteToNewTempFile(t, "bad json")
t.Cleanup(cleanup2)

fs := NewCmdSubmitProposal(client.Context{}).Flags()
fs := NewCmdSubmitProposal().Flags()

// nonexistent json
fs.Set(FlagProposal, "fileDoesNotExist")
Expand Down
Loading

0 comments on commit 35728c9

Please sign in to comment.