From ef08e99c5af915f2703cea2e6f7e5672bade0452 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Thu, 9 Apr 2020 10:10:42 -0400 Subject: [PATCH] Update x/params tx CLI commands --- x/params/client/cli/tx.go | 83 ++++++++++++++++++++++++++++++++++ x/params/client/utils/utils.go | 2 +- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/x/params/client/cli/tx.go b/x/params/client/cli/tx.go index 969d9052ea56..020854f2a125 100644 --- a/x/params/client/cli/tx.go +++ b/x/params/client/cli/tx.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/version" @@ -18,8 +19,90 @@ import ( paramproposal "github.com/cosmos/cosmos-sdk/x/params/types/proposal" ) +// NewSubmitParamChangeProposalTxCmd returns a CLI command handler for creating +// a parameter change proposal governance transaction. +func NewSubmitParamChangeProposalTxCmd(m codec.Marshaler, txg tx.Generator, ar tx.AccountRetriever) *cobra.Command { + cmd := &cobra.Command{ + Use: "param-change [proposal-file]", + Args: cobra.ExactArgs(1), + Short: "Submit a parameter change proposal", + Long: strings.TrimSpace( + fmt.Sprintf(`Submit a parameter proposal along with an initial deposit. +The proposal details must be supplied via a JSON file. For values that contains +objects, only non-empty fields will be updated. + +IMPORTANT: Currently parameter changes are evaluated but not validated, so it is +very important that any "value" change is valid (ie. correct type and within bounds) +for its respective parameter, eg. "MaxValidators" should be an integer and not a decimal. + +Proper vetting of a parameter change proposal should prevent this from happening +(no deposits should occur during the governance process), but it should be noted +regardless. + +Example: +$ %s tx gov submit-proposal param-change --from= + +Where proposal.json contains: + +{ + "title": "Staking Param Change", + "description": "Update max validators", + "changes": [ + { + "subspace": "staking", + "key": "MaxValidators", + "value": 105 + } + ], + "deposit": "1000stake" +} +`, + version.ClientName, + ), + ), + RunE: func(cmd *cobra.Command, args []string) error { + inBuf := bufio.NewReader(cmd.InOrStdin()) + cliCtx := context.NewCLIContextWithInput(inBuf).WithMarshaler(m) + txf := tx.NewFactoryFromCLI(inBuf).WithTxGenerator(txg).WithAccountRetriever(ar) + + proposal, err := paramscutils.ParseParamChangeProposalJSON(m, args[0]) + if err != nil { + return err + } + + from := cliCtx.GetFromAddress() + content := paramproposal.NewParameterChangeProposal( + proposal.Title, proposal.Description, proposal.Changes.ToParamChanges(), + ) + + deposit, err := sdk.ParseCoins(proposal.Deposit) + if err != nil { + return err + } + + msg := govtypes.NewMsgSubmitProposal(content, deposit, from) + if err := msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTx(cliCtx, txf, msg) + }, + } + + return cmd +} + +// --------------------------------------------------------------------------- +// Deprecated +// +// TODO: Remove once client-side Protobuf migration has been completed. +// --------------------------------------------------------------------------- + // GetCmdSubmitProposal implements a command handler for submitting a parameter // change proposal transaction. +// +// TODO: Remove once client-side Protobuf migration has been completed. +// ref: https://github.com/cosmos/cosmos-sdk/issues/5864 func GetCmdSubmitProposal(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "param-change [proposal-file]", diff --git a/x/params/client/utils/utils.go b/x/params/client/utils/utils.go index 2cac0b21a839..cb3199bef4f6 100644 --- a/x/params/client/utils/utils.go +++ b/x/params/client/utils/utils.go @@ -65,7 +65,7 @@ func (pcj ParamChangesJSON) ToParamChanges() []proposal.ParamChange { // ParseParamChangeProposalJSON reads and parses a ParamChangeProposalJSON from // file. -func ParseParamChangeProposalJSON(cdc *codec.Codec, proposalFile string) (ParamChangeProposalJSON, error) { +func ParseParamChangeProposalJSON(cdc codec.JSONMarshaler, proposalFile string) (ParamChangeProposalJSON, error) { proposal := ParamChangeProposalJSON{} contents, err := ioutil.ReadFile(proposalFile)