From e1476c1f9d6cc01c7b816e06faec822e2bfc0b98 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Fri, 10 Jul 2020 15:59:26 -0400 Subject: [PATCH] x/distribution: CLI & Module Tweaks (#6684) * init commit * cleanup methods * fix --- client/flags/flags.go | 127 ++++++++++++---------- docs/building-modules/structure.md | 4 +- x/distribution/client/cli/cli_test.go | 20 ++-- x/distribution/client/cli/query.go | 34 ++++-- x/distribution/client/cli/tx.go | 21 +++- x/distribution/client/testutil/helpers.go | 3 +- x/distribution/{ => keeper}/genesis.go | 59 +++++----- x/distribution/module.go | 4 +- x/gov/client/cli/tx.go | 28 +++-- 9 files changed, 175 insertions(+), 125 deletions(-) rename x/distribution/{ => keeper}/genesis.go (64%) diff --git a/client/flags/flags.go b/client/flags/flags.go index 85ac252d8f..4caf411dc2 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -71,70 +71,87 @@ var ( GasFlagVar = GasSetting{Gas: DefaultGasLimit} ) -// GetCommands adds common flags to query commands +// AddQueryFlagsToCmd adds common flags to a module query command. +func AddQueryFlagsToCmd(cmd *cobra.Command) { + cmd.Flags().Bool(FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)") + cmd.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") + cmd.Flags().String(FlagNode, "tcp://localhost:26657", ": to Tendermint RPC interface for this chain") + cmd.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)") + cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") + cmd.Flags().StringP(tmcli.OutputFlag, "o", "text", "Output format (text|json)") + + cmd.MarkFlagRequired(FlagChainID) + + cmd.SetErr(cmd.ErrOrStderr()) + cmd.SetOut(cmd.OutOrStdout()) + + // TODO: REMOVE VIPER CALLS! + viper.BindPFlag(FlagTrustNode, cmd.Flags().Lookup(FlagTrustNode)) + viper.BindPFlag(FlagUseLedger, cmd.Flags().Lookup(FlagUseLedger)) + viper.BindPFlag(FlagNode, cmd.Flags().Lookup(FlagNode)) + viper.BindPFlag(FlagKeyringBackend, cmd.Flags().Lookup(FlagKeyringBackend)) +} + +// AddTxFlagsToCmd adds common flags to a module tx command. +func AddTxFlagsToCmd(cmd *cobra.Command) { + cmd.Flags().String(FlagFrom, "", "Name or address of private key with which to sign") + cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)") + cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)") + cmd.Flags().String(FlagMemo, "", "Memo to send along with transaction") + cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom") + cmd.Flags().String(FlagGasPrices, "", "Gas prices to determine the transaction fee (e.g. 10uatom)") + cmd.Flags().String(FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") + cmd.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") + cmd.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") + cmd.Flags().StringP(FlagBroadcastMode, "b", BroadcastSync, "Transaction broadcasting mode (sync|async|block)") + cmd.Flags().Bool(FlagTrustNode, true, "Trust connected full node (don't verify proofs for responses)") + cmd.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it") + cmd.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible)") + cmd.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality") + cmd.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation") + cmd.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") + cmd.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json), this is an advanced feature") + + // --gas can accept integers and "simulate" + // + // TODO: Remove usage of var in favor of string as this is technical creating + // a singleton usage pattern and can cause issues in parallel tests. + // + // REF: https://github.com/cosmos/cosmos-sdk/issues/6545 + cmd.Flags().Var(&GasFlagVar, "gas", fmt.Sprintf( + "gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)", + GasFlagAuto, DefaultGasLimit, + )) + + cmd.MarkFlagRequired(FlagChainID) + + cmd.SetErr(cmd.ErrOrStderr()) + cmd.SetOut(cmd.OutOrStdout()) + + // TODO: REMOVE VIPER CALLS! + viper.BindPFlag(FlagTrustNode, cmd.Flags().Lookup(FlagTrustNode)) + viper.BindPFlag(FlagUseLedger, cmd.Flags().Lookup(FlagUseLedger)) + viper.BindPFlag(FlagNode, cmd.Flags().Lookup(FlagNode)) + viper.BindPFlag(FlagKeyringBackend, cmd.Flags().Lookup(FlagKeyringBackend)) +} + +// GetCommands adds common flags to query commands. +// +// TODO: REMOVE. func GetCommands(cmds ...*cobra.Command) []*cobra.Command { for _, c := range cmds { - c.Flags().Bool(FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)") - c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") - c.Flags().String(FlagNode, "tcp://localhost:26657", ": to Tendermint RPC interface for this chain") - c.Flags().Int64(FlagHeight, 0, "Use a specific height to query state at (this can error if the node is pruning state)") - c.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") - c.Flags().StringP(tmcli.OutputFlag, "o", "text", "Output format (text|json)") - - // TODO: REMOVE VIPER CALLS! - viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode)) - viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger)) - viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode)) - viper.BindPFlag(FlagKeyringBackend, c.Flags().Lookup(FlagKeyringBackend)) - - c.MarkFlagRequired(FlagChainID) - - c.SetErr(c.ErrOrStderr()) - c.SetOut(c.OutOrStdout()) + AddQueryFlagsToCmd(c) } + return cmds } // PostCommands adds common flags for commands to post tx +// +// TODO: REMOVE. func PostCommands(cmds ...*cobra.Command) []*cobra.Command { for _, c := range cmds { - c.Flags().String(FlagFrom, "", "Name or address of private key with which to sign") - c.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)") - c.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)") - c.Flags().String(FlagMemo, "", "Memo to send along with transaction") - c.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom") - c.Flags().String(FlagGasPrices, "", "Gas prices to determine the transaction fee (e.g. 10uatom)") - c.Flags().String(FlagNode, "tcp://localhost:26657", ": to tendermint rpc interface for this chain") - c.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device") - c.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ") - c.Flags().StringP(FlagBroadcastMode, "b", BroadcastSync, "Transaction broadcasting mode (sync|async|block)") - c.Flags().Bool(FlagTrustNode, true, "Trust connected full node (don't verify proofs for responses)") - c.Flags().Bool(FlagDryRun, false, "ignore the --gas flag and perform a simulation of a transaction, but don't broadcast it") - c.Flags().Bool(FlagGenerateOnly, false, "Build an unsigned transaction and write it to STDOUT (when enabled, the local Keybase is not accessible)") - c.Flags().Bool(FlagOffline, false, "Offline mode (does not allow any online functionality") - c.Flags().BoolP(FlagSkipConfirmation, "y", false, "Skip tx broadcasting prompt confirmation") - c.Flags().String(FlagKeyringBackend, DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") - c.Flags().String(FlagSignMode, "", "Choose sign mode (direct|amino-json), this is an advanced feature") - - // --gas can accept integers and "simulate" - // - // TODO: Remove usage of var in favor of string as this is technical creating - // a singleton usage pattern and can cause issues in parallel tests. - c.Flags().Var(&GasFlagVar, "gas", fmt.Sprintf( - "gas limit to set per-transaction; set to %q to calculate required gas automatically (default %d)", - GasFlagAuto, DefaultGasLimit, - )) - - // TODO: REMOVE VIPER CALLS! - viper.BindPFlag(FlagTrustNode, c.Flags().Lookup(FlagTrustNode)) - viper.BindPFlag(FlagUseLedger, c.Flags().Lookup(FlagUseLedger)) - viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode)) - viper.BindPFlag(FlagKeyringBackend, c.Flags().Lookup(FlagKeyringBackend)) - - c.MarkFlagRequired(FlagChainID) - - c.SetErr(c.ErrOrStderr()) - c.SetOut(c.OutOrStdout()) + AddTxFlagsToCmd(c) } return cmds } diff --git a/docs/building-modules/structure.md b/docs/building-modules/structure.md index b07a51eb84..15e9c4bced 100644 --- a/docs/building-modules/structure.md +++ b/docs/building-modules/structure.md @@ -23,6 +23,7 @@ x/{module} │   └── exported.go ├── keeper │   ├── invariants.go +│   ├── genesis.go │   ├── keeper.go │   ├── ... │   └── querier.go @@ -46,7 +47,6 @@ x/{module} │   ├── params.go │   └── proposals.go ├── abci.go -├── genesis.go ├── handler.go ├── ... └── module.go @@ -64,8 +64,6 @@ to the contract's implementing module and this is where `exported/` comes into p Types defined here allow for `expected_keepers.go` in other modules to define contracts that use single canonical types. This pattern allows for code to remain DRY and also alleviates import cycle chaos. -- `genesis.go`: The module's genesis related business logic (e.g. `InitGenesis`). -Note, genesis types are defined in `internal/types`. - `handler.go`: The module's message handlers. - `keeper/`: The module's keeper implementation along with any auxiliary implementations such as the querier and invariants. diff --git a/x/distribution/client/cli/cli_test.go b/x/distribution/client/cli/cli_test.go index 0a561f5c29..932aa17303 100644 --- a/x/distribution/client/cli/cli_test.go +++ b/x/distribution/client/cli/cli_test.go @@ -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) diff --git a/x/distribution/client/cli/query.go b/x/distribution/client/cli/query.go index 8eeb8ae0a9..d0364aa398 100644 --- a/x/distribution/client/cli/query.go +++ b/x/distribution/client/cli/query.go @@ -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 } diff --git a/x/distribution/client/cli/tx.go b/x/distribution/client/cli/tx.go index 7a58d0261e..576f1b84b4 100644 --- a/x/distribution/client/cli/tx.go +++ b/x/distribution/client/cli/tx.go @@ -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 } diff --git a/x/distribution/client/testutil/helpers.go b/x/distribution/client/testutil/helpers.go index 941a805f7d..19612c23fe 100644 --- a/x/distribution/client/testutil/helpers.go +++ b/x/distribution/client/testutil/helpers.go @@ -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) diff --git a/x/distribution/genesis.go b/x/distribution/keeper/genesis.go similarity index 64% rename from x/distribution/genesis.go rename to x/distribution/keeper/genesis.go index 1d9e9ba64e..8b8e19ff0a 100644 --- a/x/distribution/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -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, diff --git a/x/distribution/module.go b/x/distribution/module.go index ce887651b7..fd3dde6abe 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -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) } diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index fe5e7733f2..a214fbaed1 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -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 +}