Skip to content

Commit 407b965

Browse files
authored
Merge pull request cosmos#520 from CosmWasm/contract_msg_cast
Remove json type cast for contract msgs
2 parents 4631658 + 98431c6 commit 407b965

File tree

6 files changed

+189
-112
lines changed

6 files changed

+189
-112
lines changed

proto/cosmwasm/wasm/v1beta1/proposal.proto

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ message InstantiateContractProposal {
4545
// Label is optional metadata to be stored with a constract instance.
4646
string label = 6;
4747
// InitMsg json encoded message to be passed to the contract on instantiation
48-
bytes init_msg = 7 [ (gogoproto.casttype) = "encoding/json.RawMessage" ];
48+
bytes init_msg = 7;
4949
// Funds coins that are transferred to the contract on instantiation
5050
repeated cosmos.base.v1beta1.Coin funds = 8 [
5151
(gogoproto.nullable) = false,
@@ -66,7 +66,7 @@ message MigrateContractProposal {
6666
// CodeID references the new WASM code
6767
uint64 code_id = 5 [ (gogoproto.customname) = "CodeID" ];
6868
// MigrateMsg json encoded message to be passed to the contract on migration
69-
bytes migrate_msg = 6 [ (gogoproto.casttype) = "encoding/json.RawMessage" ];
69+
bytes migrate_msg = 6;
7070
}
7171

7272
// UpdateAdminProposal gov proposal content type to set an admin for a contract.

proto/cosmwasm/wasm/v1beta1/tx.proto

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ message MsgInstantiateContract {
5858
// Label is optional metadata to be stored with a contract instance.
5959
string label = 4;
6060
// InitMsg json encoded message to be passed to the contract on instantiation
61-
bytes init_msg = 5 [ (gogoproto.casttype) = "encoding/json.RawMessage" ];
61+
bytes init_msg = 5;
6262
// Funds coins that are transferred to the contract on instantiation
6363
repeated cosmos.base.v1beta1.Coin funds = 6 [
6464
(gogoproto.nullable) = false,
@@ -80,7 +80,7 @@ message MsgExecuteContract {
8080
// Contract is the address of the smart contract
8181
string contract = 2;
8282
// Msg json encoded message to be passed to the contract
83-
bytes msg = 3 [ (gogoproto.casttype) = "encoding/json.RawMessage" ];
83+
bytes msg = 3;
8484
// Funds coins that are transferred to the contract on execution
8585
repeated cosmos.base.v1beta1.Coin funds = 5 [
8686
(gogoproto.nullable) = false,
@@ -103,7 +103,7 @@ message MsgMigrateContract {
103103
// CodeID references the new WASM code
104104
uint64 code_id = 3 [ (gogoproto.customname) = "CodeID" ];
105105
// MigrateMsg json encoded message to be passed to the contract on migration
106-
bytes migrate_msg = 4 [ (gogoproto.casttype) = "encoding/json.RawMessage" ];
106+
bytes migrate_msg = 4;
107107
}
108108

109109
// MsgMigrateContractResponse returns contract migration result data.

x/wasm/types/proposal.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"encoding/base64"
5+
"encoding/json"
56
"fmt"
67
"strings"
78

@@ -184,8 +185,11 @@ func (p InstantiateContractProposal) ValidateBasic() error {
184185
return err
185186
}
186187
}
187-
return nil
188+
if !json.Valid(p.InitMsg) {
189+
return sdkerrors.Wrap(ErrInvalid, "init msg json")
190+
}
188191

192+
return nil
189193
}
190194

191195
// String implements the Stringer interface.
@@ -251,6 +255,9 @@ func (p MigrateContractProposal) ValidateBasic() error {
251255
if _, err := sdk.AccAddressFromBech32(p.RunAs); err != nil {
252256
return sdkerrors.Wrap(err, "run as")
253257
}
258+
if !json.Valid(p.MigrateMsg) {
259+
return sdkerrors.Wrap(ErrInvalid, "migrate msg json")
260+
}
254261
return nil
255262
}
256263

x/wasm/types/proposal.pb.go

+48-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/wasm/types/proposal_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"strings"
67
"testing"
78

@@ -199,6 +200,13 @@ func TestValidateInstantiateContractProposal(t *testing.T) {
199200
src: InstantiateContractProposalFixture(func(p *InstantiateContractProposal) {
200201
p.InitMsg = nil
201202
}),
203+
expErr: true,
204+
},
205+
"with invalid init msg": {
206+
src: InstantiateContractProposalFixture(func(p *InstantiateContractProposal) {
207+
p.InitMsg = []byte("not a json string")
208+
}),
209+
expErr: true,
202210
},
203211
"without init funds": {
204212
src: InstantiateContractProposalFixture(func(p *InstantiateContractProposal) {
@@ -282,6 +290,13 @@ func TestValidateMigrateContractProposal(t *testing.T) {
282290
src: MigrateContractProposalFixture(func(p *MigrateContractProposal) {
283291
p.MigrateMsg = nil
284292
}),
293+
expErr: true,
294+
},
295+
"migrate msg with invalid json": {
296+
src: MigrateContractProposalFixture(func(p *MigrateContractProposal) {
297+
p.MigrateMsg = []byte("not a json message")
298+
}),
299+
expErr: true,
285300
},
286301
"base data missing": {
287302
src: MigrateContractProposalFixture(func(p *MigrateContractProposal) {
@@ -695,3 +710,63 @@ func TestConvertToProposals(t *testing.T) {
695710
})
696711
}
697712
}
713+
714+
func TestUnmarshalContentFromJson(t *testing.T) {
715+
specs := map[string]struct {
716+
src string
717+
got govtypes.Content
718+
exp govtypes.Content
719+
}{
720+
"instantiate ": {
721+
src: `
722+
{
723+
"title": "foo",
724+
"description": "bar",
725+
"admin": "myAdminAddress",
726+
"code_id": 1,
727+
"funds": [{"denom": "ALX", "amount": "2"},{"denom": "BLX","amount": "3"}],
728+
"init_msg": "e30=",
729+
"label": "testing",
730+
"run_as": "myRunAsAddress"
731+
}`,
732+
got: &InstantiateContractProposal{},
733+
exp: &InstantiateContractProposal{
734+
Title: "foo",
735+
Description: "bar",
736+
RunAs: "myRunAsAddress",
737+
Admin: "myAdminAddress",
738+
CodeID: 1,
739+
Label: "testing",
740+
InitMsg: []byte("{}"),
741+
Funds: sdk.NewCoins(sdk.NewCoin("ALX", sdk.NewInt(2)), sdk.NewCoin("BLX", sdk.NewInt(3))),
742+
},
743+
},
744+
"migrate ": {
745+
src: `
746+
{
747+
"title": "foo",
748+
"description": "bar",
749+
"code_id": 1,
750+
"contract": "myContractAddr",
751+
"migrate_msg": "e30=",
752+
"run_as": "myRunAsAddress"
753+
}`,
754+
got: &MigrateContractProposal{},
755+
exp: &MigrateContractProposal{
756+
Title: "foo",
757+
Description: "bar",
758+
RunAs: "myRunAsAddress",
759+
Contract: "myContractAddr",
760+
CodeID: 1,
761+
MigrateMsg: []byte("{}"),
762+
},
763+
},
764+
}
765+
for name, spec := range specs {
766+
t.Run(name, func(t *testing.T) {
767+
require.NoError(t, json.Unmarshal([]byte(spec.src), spec.got))
768+
assert.Equal(t, spec.exp, spec.got)
769+
})
770+
}
771+
772+
}

0 commit comments

Comments
 (0)