Skip to content

Commit

Permalink
Merge PR #3319: Distribution Queriers & CLI query commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes authored Jan 18, 2019
1 parent 36d1736 commit f65ae49
Show file tree
Hide file tree
Showing 9 changed files with 640 additions and 4 deletions.
2 changes: 1 addition & 1 deletion PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ FEATURES
* \#2996 Update the `AccountKeeper` to contain params used in the context of
the ante handler.
* [\#3179](https://github.com/cosmos/cosmos-sdk/pull/3179) New CodeNoSignatures error code.

* \#3319 [x/distribution] Queriers for all distribution state worth querying; distribution query commands

* Tendermint

Expand Down
1 change: 1 addition & 0 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest b
AddRoute(gov.RouterKey, gov.NewHandler(app.govKeeper))

app.QueryRouter().
AddRoute(distr.QuerierRoute, distr.NewQuerier(app.distrKeeper)).
AddRoute(gov.QuerierRoute, gov.NewQuerier(app.govKeeper)).
AddRoute(slashing.QuerierRoute, slashing.NewQuerier(app.slashingKeeper, app.cdc)).
AddRoute(staking.QuerierRoute, staking.NewQuerier(app.stakingKeeper, app.cdc))
Expand Down
42 changes: 42 additions & 0 deletions docs/gaia/gaiacli.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,48 @@ gaiacli query gov param tallying
gaiacli query gov param deposit
```

### Fee Distribution

#### Query distribution parameters

To check the current distribution parameters, run:

```bash
gaiacli query distr params
```

#### Query outstanding rewards

To check the current outstanding (un-withdrawn) rewards, run:

```bash
gaiacli query distr outstanding-rewards
```

#### Query validator commission

To check the current outstanding commission for a validator, run:

```bash
gaiacli query distr commission <validator_address>
```

#### Query validator slashes

To check historical slashes for a validator, run:

```bash
gaiacli query distr slashes <validator_address> <start_height> <end_height>
```

#### Query delegator rewards

To check current rewards for a delegation (were they to be withdrawn), run:

```bash
gaiacli query distr rewards <delegator_address> <validator_address>
```

### Multisig transactions

Multisig transactions require signatures of multiple private keys. Thus, generating and signing
Expand Down
13 changes: 11 additions & 2 deletions x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type (
StakingKeeper = types.StakingKeeper
BankKeeper = types.BankKeeper
FeeCollectionKeeper = types.FeeCollectionKeeper

// querier param types
QueryValidatorCommissionParams = keeper.QueryValidatorCommissionParams
QueryValidatorSlashesParams = keeper.QueryValidatorSlashesParams
QueryDelegationRewardsParams = keeper.QueryDelegationRewardsParams
)

const (
Expand All @@ -44,8 +49,12 @@ var (
NewMsgWithdrawDelegatorReward = types.NewMsgWithdrawDelegatorReward
NewMsgWithdrawValidatorCommission = types.NewMsgWithdrawValidatorCommission

NewKeeper = keeper.NewKeeper
DefaultParamspace = keeper.DefaultParamspace
NewKeeper = keeper.NewKeeper
NewQuerier = keeper.NewQuerier
NewQueryValidatorCommissionParams = keeper.NewQueryValidatorCommissionParams
NewQueryValidatorSlashesParams = keeper.NewQueryValidatorSlashesParams
NewQueryDelegationRewardsParams = keeper.NewQueryDelegationRewardsParams
DefaultParamspace = keeper.DefaultParamspace

RegisterCodec = types.RegisterCodec
DefaultGenesisState = types.DefaultGenesisState
Expand Down
201 changes: 201 additions & 0 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package cli

import (
"fmt"
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
)

// GetCmdQueryParams implements the query params command.
func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Args: cobra.ExactArgs(0),
Short: "Query distribution params",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, err := queryParams(cliCtx, cdc, queryRoute)
if err != nil {
return err
}

fmt.Println(string(res))
return nil

},
}
return cmd
}

func queryParams(cliCtx context.CLIContext, cdc *codec.Codec, queryRoute string) ([]byte, error) {
retCommunityTax, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/params/community_tax", queryRoute), []byte{})
if err != nil {
return nil, err
}

retBaseProposerReward, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/params/base_proposer_reward", queryRoute), []byte{})
if err != nil {
return nil, err
}

retBonusProposerReward, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/params/bonus_proposer_reward", queryRoute), []byte{})
if err != nil {
return nil, err
}

retWithdrawAddrEnabled, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/params/withdraw_addr_enabled", queryRoute), []byte{})
if err != nil {
return nil, err
}

return codec.MarshalJSONIndent(cdc, NewPrettyParams(retCommunityTax, retBaseProposerReward, retBonusProposerReward, retWithdrawAddrEnabled))
}

// GetCmdQueryOutstandingRewards implements the query outstanding rewards command.
func GetCmdQueryOutstandingRewards(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "outstanding-rewards",
Args: cobra.ExactArgs(0),
Short: "Query distribution outstanding (un-withdrawn) rewards",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

res, err := queryOutstandingRewards(cliCtx, cdc, queryRoute)
if err != nil {
return err
}

fmt.Println(string(res))
return nil
},
}
return cmd
}

func queryOutstandingRewards(cliCtx context.CLIContext, cdc *codec.Codec, queryRoute string) ([]byte, error) {
return cliCtx.QueryWithData(fmt.Sprintf("custom/%s/outstanding_rewards", queryRoute), []byte{})
}

// GetCmdQueryValidatorCommission implements the query validator commission command.
func GetCmdQueryValidatorCommission(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "commission [validator]",
Args: cobra.ExactArgs(1),
Short: "Query distribution validator commission",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

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

res, err := queryValidatorCommission(cliCtx, cdc, queryRoute, distr.NewQueryValidatorCommissionParams(validatorAddr))
if err != nil {
return err
}

fmt.Println(string(res))
return nil
},
}
return cmd
}

func queryValidatorCommission(cliCtx context.CLIContext, cdc *codec.Codec, queryRoute string, params distr.QueryValidatorCommissionParams) ([]byte, error) {
bz, err := cdc.MarshalJSON(params)
if err != nil {
return nil, err
}
return cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_commission", queryRoute), bz)
}

// GetCmdQueryValidatorSlashes implements the query validator slashes command.
func GetCmdQueryValidatorSlashes(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "slashes [validator] [start-height] [end-height]",
Args: cobra.ExactArgs(3),
Short: "Query distribution validator slashes",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

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

startHeight, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("start-height %s not a valid uint, please input a valid start-height", args[1])
}

endHeight, err := strconv.ParseUint(args[2], 10, 64)
if err != nil {
return fmt.Errorf("end-height %s not a valid uint, please input a valid end-height", args[2])
}

res, err := queryValidatorSlashes(cliCtx, cdc, queryRoute, distr.NewQueryValidatorSlashesParams(validatorAddr, startHeight, endHeight))
if err != nil {
return err
}

fmt.Println(string(res))
return nil
},
}
return cmd
}

func queryValidatorSlashes(cliCtx context.CLIContext, cdc *codec.Codec, queryRoute string, params distr.QueryValidatorSlashesParams) ([]byte, error) {
bz, err := cdc.MarshalJSON(params)
if err != nil {
return nil, err
}
return cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", queryRoute), bz)
}

// GetCmdQueryDelegatorRewards implements the query delegator rewards command.
func GetCmdQueryDelegatorRewards(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "rewards [delegator] [validator]",
Args: cobra.ExactArgs(2),
Short: "Query distribution delegator rewards",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

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

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

res, err := queryDelegationRewards(cliCtx, cdc, queryRoute, distr.NewQueryDelegationRewardsParams(delegatorAddr, validatorAddr))
if err != nil {
return err
}

fmt.Println(string(res))
return nil
},
}
return cmd
}

func queryDelegationRewards(cliCtx context.CLIContext, cdc *codec.Codec, queryRoute string, params distr.QueryDelegationRewardsParams) ([]byte, error) {
bz, err := cdc.MarshalJSON(params)
if err != nil {
return nil, err
}
return cliCtx.QueryWithData(fmt.Sprintf("custom/%s/delegation_rewards", queryRoute), bz)
}
23 changes: 23 additions & 0 deletions x/distribution/client/cli/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"encoding/json"
)

// Convenience struct for CLI output
type PrettyParams struct {
CommunityTax json.RawMessage `json:"community_tax"`
BaseProposerReward json.RawMessage `json:"base_proposer_reward"`
BonusProposerReward json.RawMessage `json:"bonus_proposer_reward"`
WithdrawAddrEnabled json.RawMessage `json:"withdraw_addr_enabled"`
}

// Construct a new PrettyParams
func NewPrettyParams(communityTax json.RawMessage, baseProposerReward json.RawMessage, bonusProposerReward json.RawMessage, withdrawAddrEnabled json.RawMessage) PrettyParams {
return PrettyParams{
CommunityTax: communityTax,
BaseProposerReward: baseProposerReward,
BonusProposerReward: bonusProposerReward,
WithdrawAddrEnabled: withdrawAddrEnabled,
}
}
15 changes: 14 additions & 1 deletion x/distribution/client/module_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@ func NewModuleClient(storeKey string, cdc *amino.Codec) ModuleClient {

// GetQueryCmd returns the cli query commands for this module
func (mc ModuleClient) GetQueryCmd() *cobra.Command {
return &cobra.Command{Hidden: true}
distQueryCmd := &cobra.Command{
Use: "distr",
Short: "Querying commands for the distribution module",
}

distQueryCmd.AddCommand(client.GetCommands(
distCmds.GetCmdQueryParams(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryOutstandingRewards(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryValidatorCommission(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryValidatorSlashes(mc.storeKey, mc.cdc),
distCmds.GetCmdQueryDelegatorRewards(mc.storeKey, mc.cdc),
)...)

return distQueryCmd
}

// GetTxCmd returns the transaction commands for this module
Expand Down
Loading

0 comments on commit f65ae49

Please sign in to comment.