x/distribution: CLI & Module Tweaks (#6684)

* init commit

* cleanup methods

* fix
This commit is contained in:
Alexander Bezobchuk
2020-07-10 19:59:26 +00:00
committed by GitHub
parent 400c3cb08e
commit e1476c1f9d
9 changed files with 175 additions and 125 deletions
+10 -10
View File
@@ -93,7 +93,7 @@ withdraw_addr_enabled: true`,
tc := tc
s.Run(tc.name, func() {
cmd := flags.GetCommands(cli.GetCmdQueryParams())[0]
cmd := cli.GetCmdQueryParams()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -158,7 +158,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorOutstandingRewards() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.GetCommands(cli.GetCmdQueryValidatorOutstandingRewards())[0]
cmd := cli.GetCmdQueryValidatorOutstandingRewards()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -228,7 +228,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorCommission() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.GetCommands(cli.GetCmdQueryValidatorCommission())[0]
cmd := cli.GetCmdQueryValidatorCommission()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -314,7 +314,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryValidatorSlashes() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.GetCommands(cli.GetCmdQueryValidatorSlashes())[0]
cmd := cli.GetCmdQueryValidatorSlashes()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -420,7 +420,7 @@ total:
tc := tc
s.Run(tc.name, func() {
cmd := flags.GetCommands(cli.GetCmdQueryDelegatorRewards())[0]
cmd := cli.GetCmdQueryDelegatorRewards()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -470,7 +470,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryCommunityPool() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.GetCommands(cli.GetCmdQueryCommunityPool())[0]
cmd := cli.GetCmdQueryCommunityPool()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -593,7 +593,7 @@ func (s *IntegrationTestSuite) TestNewWithdrawAllRewardsCmd() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.PostCommands(cli.NewWithdrawAllRewardsCmd())[0]
cmd := cli.NewWithdrawAllRewardsCmd()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -656,7 +656,7 @@ func (s *IntegrationTestSuite) TestNewSetWithdrawAddrCmd() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.PostCommands(cli.NewSetWithdrawAddrCmd())[0]
cmd := cli.NewSetWithdrawAddrCmd()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -719,7 +719,7 @@ func (s *IntegrationTestSuite) TestNewFundCommunityPoolCmd() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.PostCommands(cli.NewFundCommunityPoolCmd())[0]
cmd := cli.NewFundCommunityPoolCmd()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
@@ -812,7 +812,7 @@ func (s *IntegrationTestSuite) TestGetCmdSubmitProposal() {
tc := tc
s.Run(tc.name, func() {
cmd := flags.PostCommands(cli.GetCmdSubmitProposal())[0]
cmd := cli.GetCmdSubmitProposal()
_, out := testutil.ApplyMockIO(cmd)
clientCtx := val.ClientCtx.WithOutput(out)
+26 -8
View File
@@ -25,21 +25,21 @@ func GetQueryCmd() *cobra.Command {
RunE: client.ValidateCmd,
}
distQueryCmd.AddCommand(flags.GetCommands(
distQueryCmd.AddCommand(
GetCmdQueryParams(),
GetCmdQueryValidatorOutstandingRewards(),
GetCmdQueryValidatorCommission(),
GetCmdQueryValidatorSlashes(),
GetCmdQueryDelegatorRewards(),
GetCmdQueryCommunityPool(),
)...)
)
return distQueryCmd
}
// GetCmdQueryParams implements the query params command.
func GetCmdQueryParams() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
Short: "Query distribution params",
@@ -64,12 +64,15 @@ func GetCmdQueryParams() *cobra.Command {
return clientCtx.PrintOutput(params)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorOutstandingRewards implements the query validator
// outstanding rewards command.
func GetCmdQueryValidatorOutstandingRewards() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "validator-outstanding-rewards [validator]",
Args: cobra.ExactArgs(1),
Short: "Query distribution outstanding (un-withdrawn) rewards for a validator and all their delegations",
@@ -116,11 +119,14 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
return clientCtx.PrintOutput(outstandingRewards)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorCommission implements the query validator commission command.
func GetCmdQueryValidatorCommission() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "commission [validator]",
Args: cobra.ExactArgs(1),
Short: "Query distribution validator commission",
@@ -158,11 +164,14 @@ $ %s query distribution commission cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9l
return clientCtx.PrintOutput(valCom)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryValidatorSlashes implements the query validator slashes command.
func GetCmdQueryValidatorSlashes() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "slashes [validator] [start-height] [end-height]",
Args: cobra.ExactArgs(3),
Short: "Query distribution validator slashes",
@@ -216,11 +225,14 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
return clientCtx.PrintOutput(slashes)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryDelegatorRewards implements the query delegator rewards command.
func GetCmdQueryDelegatorRewards() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "rewards [delegator-addr] [validator-addr]",
Args: cobra.RangeArgs(1, 2),
Short: "Query all distribution delegator rewards or rewards from a particular validator",
@@ -282,11 +294,14 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
return clientCtx.PrintOutput(result)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdQueryCommunityPool returns the command for fetching community pool info
func GetCmdQueryCommunityPool() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "community-pool",
Args: cobra.NoArgs,
Short: "Query the amount of coins in the community pool",
@@ -319,4 +334,7 @@ $ %s query distribution community-pool
return clientCtx.PrintOutput(result)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
+16 -5
View File
@@ -37,12 +37,12 @@ func NewTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}
distTxCmd.AddCommand(flags.PostCommands(
distTxCmd.AddCommand(
NewWithdrawRewardsCmd(),
NewWithdrawAllRewardsCmd(),
NewSetWithdrawAddrCmd(),
NewFundCommunityPoolCmd(),
)...)
)
return distTxCmd
}
@@ -122,6 +122,7 @@ $ %s tx distribution withdraw-rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fx
}
cmd.Flags().Bool(FlagCommission, false, "Withdraw the validator's commission in addition to the rewards")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
@@ -165,11 +166,12 @@ $ %s tx distribution withdraw-all-rewards --from mykey
}
cmd.Flags().Int(FlagMaxMessagesPerTx, MaxMessagesPerTxDefault, "Limit the number of messages per tx (0 for unlimited)")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewSetWithdrawAddrCmd() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "set-withdraw-addr [withdraw-addr]",
Short: "change the default withdraw address for rewards associated with an address",
Long: strings.TrimSpace(
@@ -203,10 +205,13 @@ $ %s tx distribution set-withdraw-addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
func NewFundCommunityPoolCmd() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "fund-community-pool [amount]",
Args: cobra.ExactArgs(1),
Short: "Funds the community pool with the specified amount",
@@ -240,11 +245,14 @@ $ %s tx distribution fund-community-pool 100uatom --from mykey
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetCmdSubmitProposal implements the command to submit a community-pool-spend proposal
func GetCmdSubmitProposal() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "community-pool-spend [proposal-file]",
Args: cobra.ExactArgs(1),
Short: "Submit a community pool spend proposal",
@@ -305,4 +313,7 @@ Where proposal.json contains:
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
+1 -2
View File
@@ -6,7 +6,6 @@ import (
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
distrcli "github.com/cosmos/cosmos-sdk/x/distribution/client/cli"
)
@@ -20,7 +19,7 @@ func MsgWithdrawDelegatorRewardExec(clientCtx client.Context, valAddr fmt.String
args := []string{valAddr.String()}
args = append(args, extraArgs...)
cmd := flags.PostCommands(distrcli.NewWithdrawRewardsCmd())[0]
cmd := distrcli.NewWithdrawRewardsCmd()
cmd.SetErr(buf)
cmd.SetOut(buf)
cmd.SetArgs(args)
@@ -1,70 +1,71 @@
package distribution
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)
// InitGenesis sets distribution information for genesis
func InitGenesis(ctx sdk.Context, ak types.AccountKeeper, bk types.BankKeeper, keeper keeper.Keeper, data types.GenesisState) {
func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
var moduleHoldings sdk.DecCoins
keeper.SetFeePool(ctx, data.FeePool)
keeper.SetParams(ctx, data.Params)
k.SetFeePool(ctx, data.FeePool)
k.SetParams(ctx, data.Params)
for _, dwi := range data.DelegatorWithdrawInfos {
keeper.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress)
k.SetDelegatorWithdrawAddr(ctx, dwi.DelegatorAddress, dwi.WithdrawAddress)
}
keeper.SetPreviousProposerConsAddr(ctx, data.PreviousProposer)
k.SetPreviousProposerConsAddr(ctx, data.PreviousProposer)
for _, rew := range data.OutstandingRewards {
keeper.SetValidatorOutstandingRewards(ctx, rew.ValidatorAddress, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
k.SetValidatorOutstandingRewards(ctx, rew.ValidatorAddress, types.ValidatorOutstandingRewards{Rewards: rew.OutstandingRewards})
moduleHoldings = moduleHoldings.Add(rew.OutstandingRewards...)
}
for _, acc := range data.ValidatorAccumulatedCommissions {
keeper.SetValidatorAccumulatedCommission(ctx, acc.ValidatorAddress, acc.Accumulated)
k.SetValidatorAccumulatedCommission(ctx, acc.ValidatorAddress, acc.Accumulated)
}
for _, his := range data.ValidatorHistoricalRewards {
keeper.SetValidatorHistoricalRewards(ctx, his.ValidatorAddress, his.Period, his.Rewards)
k.SetValidatorHistoricalRewards(ctx, his.ValidatorAddress, his.Period, his.Rewards)
}
for _, cur := range data.ValidatorCurrentRewards {
keeper.SetValidatorCurrentRewards(ctx, cur.ValidatorAddress, cur.Rewards)
k.SetValidatorCurrentRewards(ctx, cur.ValidatorAddress, cur.Rewards)
}
for _, del := range data.DelegatorStartingInfos {
keeper.SetDelegatorStartingInfo(ctx, del.ValidatorAddress, del.DelegatorAddress, del.StartingInfo)
k.SetDelegatorStartingInfo(ctx, del.ValidatorAddress, del.DelegatorAddress, del.StartingInfo)
}
for _, evt := range data.ValidatorSlashEvents {
keeper.SetValidatorSlashEvent(ctx, evt.ValidatorAddress, evt.Height, evt.Period, evt.Event)
k.SetValidatorSlashEvent(ctx, evt.ValidatorAddress, evt.Height, evt.Period, evt.Event)
}
moduleHoldings = moduleHoldings.Add(data.FeePool.CommunityPool...)
moduleHoldingsInt, _ := moduleHoldings.TruncateDecimal()
// check if the module account exists
moduleAcc := keeper.GetDistributionAccount(ctx)
moduleAcc := k.GetDistributionAccount(ctx)
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
balances := bk.GetAllBalances(ctx, moduleAcc.GetAddress())
balances := k.bankKeeper.GetAllBalances(ctx, moduleAcc.GetAddress())
if balances.IsZero() {
if err := bk.SetBalances(ctx, moduleAcc.GetAddress(), moduleHoldingsInt); err != nil {
if err := k.bankKeeper.SetBalances(ctx, moduleAcc.GetAddress(), moduleHoldingsInt); err != nil {
panic(err)
}
ak.SetModuleAccount(ctx, moduleAcc)
k.authKeeper.SetModuleAccount(ctx, moduleAcc)
}
}
// ExportGenesis returns a GenesisState for a given context and keeper.
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
feePool := keeper.GetFeePool(ctx)
params := keeper.GetParams(ctx)
func (k Keeper) ExportGenesis(ctx sdk.Context) types.GenesisState {
feePool := k.GetFeePool(ctx)
params := k.GetParams(ctx)
dwi := make([]types.DelegatorWithdrawInfo, 0)
keeper.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
k.IterateDelegatorWithdrawAddrs(ctx, func(del sdk.AccAddress, addr sdk.AccAddress) (stop bool) {
dwi = append(dwi, types.DelegatorWithdrawInfo{
DelegatorAddress: del,
WithdrawAddress: addr,
@@ -72,9 +73,10 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
return false
})
pp := keeper.GetPreviousProposerConsAddr(ctx)
pp := k.GetPreviousProposerConsAddr(ctx)
outstanding := make([]types.ValidatorOutstandingRewardsRecord, 0)
keeper.IterateValidatorOutstandingRewards(ctx,
k.IterateValidatorOutstandingRewards(ctx,
func(addr sdk.ValAddress, rewards types.ValidatorOutstandingRewards) (stop bool) {
outstanding = append(outstanding, types.ValidatorOutstandingRewardsRecord{
ValidatorAddress: addr,
@@ -85,7 +87,7 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
)
acc := make([]types.ValidatorAccumulatedCommissionRecord, 0)
keeper.IterateValidatorAccumulatedCommissions(ctx,
k.IterateValidatorAccumulatedCommissions(ctx,
func(addr sdk.ValAddress, commission types.ValidatorAccumulatedCommission) (stop bool) {
acc = append(acc, types.ValidatorAccumulatedCommissionRecord{
ValidatorAddress: addr,
@@ -96,7 +98,7 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
)
his := make([]types.ValidatorHistoricalRewardsRecord, 0)
keeper.IterateValidatorHistoricalRewards(ctx,
k.IterateValidatorHistoricalRewards(ctx,
func(val sdk.ValAddress, period uint64, rewards types.ValidatorHistoricalRewards) (stop bool) {
his = append(his, types.ValidatorHistoricalRewardsRecord{
ValidatorAddress: val,
@@ -108,7 +110,7 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
)
cur := make([]types.ValidatorCurrentRewardsRecord, 0)
keeper.IterateValidatorCurrentRewards(ctx,
k.IterateValidatorCurrentRewards(ctx,
func(val sdk.ValAddress, rewards types.ValidatorCurrentRewards) (stop bool) {
cur = append(cur, types.ValidatorCurrentRewardsRecord{
ValidatorAddress: val,
@@ -117,8 +119,9 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
return false
},
)
dels := make([]types.DelegatorStartingInfoRecord, 0)
keeper.IterateDelegatorStartingInfos(ctx,
k.IterateDelegatorStartingInfos(ctx,
func(val sdk.ValAddress, del sdk.AccAddress, info types.DelegatorStartingInfo) (stop bool) {
dels = append(dels, types.DelegatorStartingInfoRecord{
ValidatorAddress: val,
@@ -130,7 +133,7 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) types.GenesisState {
)
slashes := make([]types.ValidatorSlashEventRecord, 0)
keeper.IterateValidatorSlashEvents(ctx,
k.IterateValidatorSlashEvents(ctx,
func(val sdk.ValAddress, height uint64, event types.ValidatorSlashEvent) (stop bool) {
slashes = append(slashes, types.ValidatorSlashEventRecord{
ValidatorAddress: val,
+2 -2
View File
@@ -141,14 +141,14 @@ func (am AppModule) RegisterQueryService(grpc.Server) {}
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState types.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
InitGenesis(ctx, am.accountKeeper, am.bankKeeper, am.keeper, genesisState)
am.keeper.InitGenesis(ctx, genesisState)
return []abci.ValidatorUpdate{}
}
// ExportGenesis returns the exported genesis state as raw bytes for the distribution
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
gs := ExportGenesis(ctx, am.keeper)
gs := am.keeper.ExportGenesis(ctx)
return cdc.MustMarshalJSON(gs)
}
+16 -12
View File
@@ -50,10 +50,7 @@ var ProposalFlags = []string{
// it contains a slice of "proposal" child commands. These commands are respective
// to proposal type handlers that are implemented in other modules but are mounted
// under the governance CLI (eg. parameter change proposals).
func NewTxCmd(
ctx client.Context,
pcmds []*cobra.Command,
) *cobra.Command {
func NewTxCmd(ctx client.Context, propCmds []*cobra.Command) *cobra.Command {
govTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Governance transactions subcommands",
@@ -63,15 +60,15 @@ func NewTxCmd(
}
cmdSubmitProp := NewCmdSubmitProposal(ctx)
for _, pcmd := range pcmds {
cmdSubmitProp.AddCommand(flags.PostCommands(pcmd)[0])
for _, propCmd := range propCmds {
cmdSubmitProp.AddCommand(propCmd)
}
govTxCmd.AddCommand(flags.PostCommands(
govTxCmd.AddCommand(
NewCmdDeposit(ctx),
NewCmdVote(ctx),
cmdSubmitProp,
)...)
)
return govTxCmd
}
@@ -137,13 +134,14 @@ $ %s tx gov submit-proposal --title="Test Proposal" --description="My awesome pr
cmd.Flags().String(flagProposalType, "", "proposalType of proposal, types: text/parameter_change/software_upgrade")
cmd.Flags().String(FlagDeposit, "", "deposit of proposal")
cmd.Flags().String(FlagProposal, "", "proposal file path (if this path is given, other proposal flags are ignored)")
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewCmdDeposit implements depositing tokens for an active proposal.
func NewCmdDeposit(clientCtx client.Context) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "deposit [proposal-id] [deposit]",
Args: cobra.ExactArgs(2),
Short: "Deposit tokens for an active proposal",
@@ -184,11 +182,15 @@ $ %s tx gov deposit 1 10stake --from mykey
return tx.GenerateOrBroadcastTx(clientCtx, msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// NewCmdVote implements creating a new vote command.
func NewCmdVote(clientCtx client.Context) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "vote [proposal-id] [option]",
Args: cobra.ExactArgs(2),
Short: "Vote for an active proposal, options: yes/no/no_with_veto/abstain",
@@ -231,6 +233,8 @@ $ %s tx gov vote 1 yes --from mykey
return tx.GenerateOrBroadcastTx(clientCtx, msg)
},
}
}
// DONTCOVER
flags.AddTxFlagsToCmd(cmd)
return cmd
}