From 67f1e12eecee98d90410b60ca04f57d5f535671f Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Thu, 2 May 2019 14:21:35 -0400 Subject: [PATCH] Merge PR #4253: Change User Supplied Param Change Value to json.RawMessage --- client/lcd/swagger-ui/swagger.yaml | 3 +- cmd/gaia/cli_test/cli_test.go | 2 +- cmd/gaia/lcd_test/helpers_test.go | 6 +-- docs/cosmos-hub/gaiacli.md | 2 +- x/params/client/cli/tx.go | 4 +- x/params/client/rest/rest.go | 2 +- x/params/client/utils/utils.go | 65 ++++++++++++++++++++++------- x/params/client/utils/utils_test.go | 35 ++++++++++++++++ 8 files changed, 93 insertions(+), 26 deletions(-) create mode 100644 x/params/client/utils/utils_test.go diff --git a/client/lcd/swagger-ui/swagger.yaml b/client/lcd/swagger-ui/swagger.yaml index f975e6e141..8db0cb9530 100644 --- a/client/lcd/swagger-ui/swagger.yaml +++ b/client/lcd/swagger-ui/swagger.yaml @@ -2408,5 +2408,4 @@ definitions: type: string example: "" value: - type: string - example: "105" + type: object diff --git a/cmd/gaia/cli_test/cli_test.go b/cmd/gaia/cli_test/cli_test.go index e825baaf7f..6709d4ddf6 100644 --- a/cmd/gaia/cli_test/cli_test.go +++ b/cmd/gaia/cli_test/cli_test.go @@ -623,7 +623,7 @@ func TestGaiaCLISubmitParamChangeProposal(t *testing.T) { { "subspace": "staking", "key": "MaxValidators", - "value": "105" + "value": 105 } ], "deposit": [ diff --git a/cmd/gaia/lcd_test/helpers_test.go b/cmd/gaia/lcd_test/helpers_test.go index 132ec1b5d7..8b544b9056 100644 --- a/cmd/gaia/lcd_test/helpers_test.go +++ b/cmd/gaia/lcd_test/helpers_test.go @@ -23,7 +23,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/lcd" "github.com/cosmos/cosmos-sdk/client/utils" "github.com/cosmos/cosmos-sdk/crypto/keys" - "github.com/cosmos/cosmos-sdk/x/params" "github.com/cosmos/cosmos-sdk/client/rpc" "github.com/cosmos/cosmos-sdk/client/tx" @@ -1179,8 +1178,8 @@ func doSubmitParamChangeProposal( Description: "test", Proposer: proposerAddr, Deposit: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, amount)}, - Changes: []params.ParamChange{ - params.NewParamChange("staking", "MaxValidators", "", "105"), + Changes: paramscutils.ParamChangesJSON{ + paramscutils.NewParamChangeJSON("staking", "MaxValidators", "", []byte(`105`)), }, } @@ -1188,7 +1187,6 @@ func doSubmitParamChangeProposal( require.NoError(t, err) resp, body := Request(t, port, "POST", "/gov/proposals/param_change", req) - fmt.Println(resp) require.Equal(t, http.StatusOK, resp.StatusCode, body) resp, body = signAndBroadcastGenTx(t, port, name, pwd, body, acc, client.DefaultGasAdjustment, false) diff --git a/docs/cosmos-hub/gaiacli.md b/docs/cosmos-hub/gaiacli.md index 1e918bd0cb..9ef76ff5c6 100644 --- a/docs/cosmos-hub/gaiacli.md +++ b/docs/cosmos-hub/gaiacli.md @@ -599,7 +599,7 @@ Where `proposal.json` contains the following: { "subspace": "staking", "key": "MaxValidators", - "value": "105" + "value": 105 } ], "deposit": [ diff --git a/x/params/client/cli/tx.go b/x/params/client/cli/tx.go index 2ff77dcf06..1997512f1c 100644 --- a/x/params/client/cli/tx.go +++ b/x/params/client/cli/tx.go @@ -44,7 +44,7 @@ where proposal.json contains: { "subspace": "staking", "key": "MaxValidators", - "value": "105" + "value": 105 } ], "deposit": [ @@ -67,7 +67,7 @@ where proposal.json contains: } from := cliCtx.GetFromAddress() - content := params.NewParameterChangeProposal(proposal.Title, proposal.Description, proposal.Changes) + content := params.NewParameterChangeProposal(proposal.Title, proposal.Description, proposal.Changes.ToParamChanges()) msg := gov.NewMsgSubmitProposal(content, proposal.Deposit, from) if err := msg.ValidateBasic(); err != nil { diff --git a/x/params/client/rest/rest.go b/x/params/client/rest/rest.go index 453f0fdcc4..4ec01c1f0f 100644 --- a/x/params/client/rest/rest.go +++ b/x/params/client/rest/rest.go @@ -35,7 +35,7 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han return } - content := params.NewParameterChangeProposal(req.Title, req.Description, req.Changes) + content := params.NewParameterChangeProposal(req.Title, req.Description, req.Changes.ToParamChanges()) msg := gov.NewMsgSubmitProposal(content, req.Deposit, req.Proposer) if err := msg.ValidateBasic(); err != nil { diff --git a/x/params/client/utils/utils.go b/x/params/client/utils/utils.go index cbc07e727d..e390b3d326 100644 --- a/x/params/client/utils/utils.go +++ b/x/params/client/utils/utils.go @@ -1,6 +1,7 @@ package utils import ( + "encoding/json" "io/ioutil" "github.com/cosmos/cosmos-sdk/codec" @@ -9,24 +10,58 @@ import ( "github.com/cosmos/cosmos-sdk/x/params" ) -// ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used -// to parse parameter change proposals from a JSON file. -type ParamChangeProposalJSON struct { - Title string `json:"title"` - Description string `json:"description"` - Changes []params.ParamChange `json:"changes"` - Deposit sdk.Coins `json:"deposit"` +type ( + // ParamChangesJSON defines a slice of ParamChangeJSON objects which can be + // converted to a slice of ParamChange objects. + ParamChangesJSON []ParamChangeJSON + + // ParamChangeJSON defines a parameter change used in JSON input. This + // allows values to be specified in raw JSON instead of being string encoded. + ParamChangeJSON struct { + Subspace string `json:"subspace"` + Key string `json:"key"` + Subkey string `json:"subkey,omitempty"` + Value json.RawMessage `json:"value"` + } + + // ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used + // to parse parameter change proposals from a JSON file. + ParamChangeProposalJSON struct { + Title string `json:"title"` + Description string `json:"description"` + Changes ParamChangesJSON `json:"changes"` + Deposit sdk.Coins `json:"deposit"` + } + + // ParamChangeProposalReq defines a parameter change proposal request body. + ParamChangeProposalReq struct { + BaseReq rest.BaseReq `json:"base_req"` + + Title string `json:"title"` + Description string `json:"description"` + Changes ParamChangesJSON `json:"changes"` + Proposer sdk.AccAddress `json:"proposer"` + Deposit sdk.Coins `json:"deposit"` + } +) + +func NewParamChangeJSON(subspace, key, subkey string, value json.RawMessage) ParamChangeJSON { + return ParamChangeJSON{subspace, key, subkey, value} } -// ParamChangeProposalReq defines a parameter change proposal request body. -type ParamChangeProposalReq struct { - BaseReq rest.BaseReq `json:"base_req"` +// ToParamChange converts a ParamChangeJSON object to ParamChange. +func (pcj ParamChangeJSON) ToParamChange() params.ParamChange { + return params.NewParamChange(pcj.Subspace, pcj.Key, pcj.Subkey, string(pcj.Value)) +} - Title string `json:"title"` - Description string `json:"description"` - Changes []params.ParamChange `json:"changes"` - Proposer sdk.AccAddress `json:"proposer"` - Deposit sdk.Coins `json:"deposit"` +// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of +// ParamChange. +func (pcj ParamChangesJSON) ToParamChanges() []params.ParamChange { + res := make([]params.ParamChange, len(pcj)) + for i, pc := range pcj { + res[i] = pc.ToParamChange() + } + return res } // ParseParamChangeProposalJSON reads and parses a ParamChangeProposalJSON from diff --git a/x/params/client/utils/utils_test.go b/x/params/client/utils/utils_test.go new file mode 100644 index 0000000000..1953d11ffe --- /dev/null +++ b/x/params/client/utils/utils_test.go @@ -0,0 +1,35 @@ +package utils + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewParamChangeJSON(t *testing.T) { + pcj := NewParamChangeJSON("subspace", "key", "subkey", json.RawMessage(`{}`)) + require.Equal(t, "subspace", pcj.Subspace) + require.Equal(t, "key", pcj.Key) + require.Equal(t, "subkey", pcj.Subkey) + require.Equal(t, json.RawMessage(`{}`), pcj.Value) +} + +func TestToParamChanges(t *testing.T) { + pcj1 := NewParamChangeJSON("subspace", "key1", "", json.RawMessage(`{}`)) + pcj2 := NewParamChangeJSON("subspace", "key2", "", json.RawMessage(`{}`)) + pcjs := ParamChangesJSON{pcj1, pcj2} + + paramChanges := pcjs.ToParamChanges() + require.Len(t, paramChanges, 2) + + require.Equal(t, paramChanges[0].Subspace, pcj1.Subspace) + require.Equal(t, paramChanges[0].Key, pcj1.Key) + require.Equal(t, paramChanges[0].Subkey, pcj1.Subkey) + require.Equal(t, paramChanges[0].Value, string(pcj1.Value)) + + require.Equal(t, paramChanges[1].Subspace, pcj2.Subspace) + require.Equal(t, paramChanges[1].Key, pcj2.Key) + require.Equal(t, paramChanges[1].Subkey, pcj2.Subkey) + require.Equal(t, paramChanges[1].Value, string(pcj2.Value)) +}