Skip to content

Commit 67f1e12

Browse files
authored
Merge PR #4253: Change User Supplied Param Change Value to json.RawMessage
1 parent 60a6a8d commit 67f1e12

File tree

8 files changed

+93
-26
lines changed

8 files changed

+93
-26
lines changed

client/lcd/swagger-ui/swagger.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2408,5 +2408,4 @@ definitions:
24082408
type: string
24092409
example: ""
24102410
value:
2411-
type: string
2412-
example: "105"
2411+
type: object

cmd/gaia/cli_test/cli_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ func TestGaiaCLISubmitParamChangeProposal(t *testing.T) {
623623
{
624624
"subspace": "staking",
625625
"key": "MaxValidators",
626-
"value": "105"
626+
"value": 105
627627
}
628628
],
629629
"deposit": [

cmd/gaia/lcd_test/helpers_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/cosmos/cosmos-sdk/client/lcd"
2424
"github.com/cosmos/cosmos-sdk/client/utils"
2525
"github.com/cosmos/cosmos-sdk/crypto/keys"
26-
"github.com/cosmos/cosmos-sdk/x/params"
2726

2827
"github.com/cosmos/cosmos-sdk/client/rpc"
2928
"github.com/cosmos/cosmos-sdk/client/tx"
@@ -1179,16 +1178,15 @@ func doSubmitParamChangeProposal(
11791178
Description: "test",
11801179
Proposer: proposerAddr,
11811180
Deposit: sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, amount)},
1182-
Changes: []params.ParamChange{
1183-
params.NewParamChange("staking", "MaxValidators", "", "105"),
1181+
Changes: paramscutils.ParamChangesJSON{
1182+
paramscutils.NewParamChangeJSON("staking", "MaxValidators", "", []byte(`105`)),
11841183
},
11851184
}
11861185

11871186
req, err := cdc.MarshalJSON(pr)
11881187
require.NoError(t, err)
11891188

11901189
resp, body := Request(t, port, "POST", "/gov/proposals/param_change", req)
1191-
fmt.Println(resp)
11921190
require.Equal(t, http.StatusOK, resp.StatusCode, body)
11931191

11941192
resp, body = signAndBroadcastGenTx(t, port, name, pwd, body, acc, client.DefaultGasAdjustment, false)

docs/cosmos-hub/gaiacli.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ Where `proposal.json` contains the following:
599599
{
600600
"subspace": "staking",
601601
"key": "MaxValidators",
602-
"value": "105"
602+
"value": 105
603603
}
604604
],
605605
"deposit": [

x/params/client/cli/tx.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where proposal.json contains:
4444
{
4545
"subspace": "staking",
4646
"key": "MaxValidators",
47-
"value": "105"
47+
"value": 105
4848
}
4949
],
5050
"deposit": [
@@ -67,7 +67,7 @@ where proposal.json contains:
6767
}
6868

6969
from := cliCtx.GetFromAddress()
70-
content := params.NewParameterChangeProposal(proposal.Title, proposal.Description, proposal.Changes)
70+
content := params.NewParameterChangeProposal(proposal.Title, proposal.Description, proposal.Changes.ToParamChanges())
7171

7272
msg := gov.NewMsgSubmitProposal(content, proposal.Deposit, from)
7373
if err := msg.ValidateBasic(); err != nil {

x/params/client/rest/rest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func postProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Han
3535
return
3636
}
3737

38-
content := params.NewParameterChangeProposal(req.Title, req.Description, req.Changes)
38+
content := params.NewParameterChangeProposal(req.Title, req.Description, req.Changes.ToParamChanges())
3939

4040
msg := gov.NewMsgSubmitProposal(content, req.Deposit, req.Proposer)
4141
if err := msg.ValidateBasic(); err != nil {

x/params/client/utils/utils.go

+50-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56

67
"github.com/cosmos/cosmos-sdk/codec"
@@ -9,24 +10,58 @@ import (
910
"github.com/cosmos/cosmos-sdk/x/params"
1011
)
1112

12-
// ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used
13-
// to parse parameter change proposals from a JSON file.
14-
type ParamChangeProposalJSON struct {
15-
Title string `json:"title"`
16-
Description string `json:"description"`
17-
Changes []params.ParamChange `json:"changes"`
18-
Deposit sdk.Coins `json:"deposit"`
13+
type (
14+
// ParamChangesJSON defines a slice of ParamChangeJSON objects which can be
15+
// converted to a slice of ParamChange objects.
16+
ParamChangesJSON []ParamChangeJSON
17+
18+
// ParamChangeJSON defines a parameter change used in JSON input. This
19+
// allows values to be specified in raw JSON instead of being string encoded.
20+
ParamChangeJSON struct {
21+
Subspace string `json:"subspace"`
22+
Key string `json:"key"`
23+
Subkey string `json:"subkey,omitempty"`
24+
Value json.RawMessage `json:"value"`
25+
}
26+
27+
// ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used
28+
// to parse parameter change proposals from a JSON file.
29+
ParamChangeProposalJSON struct {
30+
Title string `json:"title"`
31+
Description string `json:"description"`
32+
Changes ParamChangesJSON `json:"changes"`
33+
Deposit sdk.Coins `json:"deposit"`
34+
}
35+
36+
// ParamChangeProposalReq defines a parameter change proposal request body.
37+
ParamChangeProposalReq struct {
38+
BaseReq rest.BaseReq `json:"base_req"`
39+
40+
Title string `json:"title"`
41+
Description string `json:"description"`
42+
Changes ParamChangesJSON `json:"changes"`
43+
Proposer sdk.AccAddress `json:"proposer"`
44+
Deposit sdk.Coins `json:"deposit"`
45+
}
46+
)
47+
48+
func NewParamChangeJSON(subspace, key, subkey string, value json.RawMessage) ParamChangeJSON {
49+
return ParamChangeJSON{subspace, key, subkey, value}
1950
}
2051

21-
// ParamChangeProposalReq defines a parameter change proposal request body.
22-
type ParamChangeProposalReq struct {
23-
BaseReq rest.BaseReq `json:"base_req"`
52+
// ToParamChange converts a ParamChangeJSON object to ParamChange.
53+
func (pcj ParamChangeJSON) ToParamChange() params.ParamChange {
54+
return params.NewParamChange(pcj.Subspace, pcj.Key, pcj.Subkey, string(pcj.Value))
55+
}
2456

25-
Title string `json:"title"`
26-
Description string `json:"description"`
27-
Changes []params.ParamChange `json:"changes"`
28-
Proposer sdk.AccAddress `json:"proposer"`
29-
Deposit sdk.Coins `json:"deposit"`
57+
// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of
58+
// ParamChange.
59+
func (pcj ParamChangesJSON) ToParamChanges() []params.ParamChange {
60+
res := make([]params.ParamChange, len(pcj))
61+
for i, pc := range pcj {
62+
res[i] = pc.ToParamChange()
63+
}
64+
return res
3065
}
3166

3267
// ParseParamChangeProposalJSON reads and parses a ParamChangeProposalJSON from

x/params/client/utils/utils_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package utils
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestNewParamChangeJSON(t *testing.T) {
11+
pcj := NewParamChangeJSON("subspace", "key", "subkey", json.RawMessage(`{}`))
12+
require.Equal(t, "subspace", pcj.Subspace)
13+
require.Equal(t, "key", pcj.Key)
14+
require.Equal(t, "subkey", pcj.Subkey)
15+
require.Equal(t, json.RawMessage(`{}`), pcj.Value)
16+
}
17+
18+
func TestToParamChanges(t *testing.T) {
19+
pcj1 := NewParamChangeJSON("subspace", "key1", "", json.RawMessage(`{}`))
20+
pcj2 := NewParamChangeJSON("subspace", "key2", "", json.RawMessage(`{}`))
21+
pcjs := ParamChangesJSON{pcj1, pcj2}
22+
23+
paramChanges := pcjs.ToParamChanges()
24+
require.Len(t, paramChanges, 2)
25+
26+
require.Equal(t, paramChanges[0].Subspace, pcj1.Subspace)
27+
require.Equal(t, paramChanges[0].Key, pcj1.Key)
28+
require.Equal(t, paramChanges[0].Subkey, pcj1.Subkey)
29+
require.Equal(t, paramChanges[0].Value, string(pcj1.Value))
30+
31+
require.Equal(t, paramChanges[1].Subspace, pcj2.Subspace)
32+
require.Equal(t, paramChanges[1].Key, pcj2.Key)
33+
require.Equal(t, paramChanges[1].Subkey, pcj2.Subkey)
34+
require.Equal(t, paramChanges[1].Value, string(pcj2.Value))
35+
}

0 commit comments

Comments
 (0)