Add Label to MsgInstantiateContract and fix validation

This commit is contained in:
Ethan Frey 2020-02-25 16:59:13 +01:00
parent 0db1a3d040
commit b80fbdaa9e
2 changed files with 116 additions and 0 deletions

View File

@ -12,6 +12,9 @@ import (
const (
MaxWasmSize = 500 * 1024
// MaxLabelSize is the longest label that can be used when Instantiating a contract
MaxLabelSize = 128
// BuildTagRegexp is a docker image regexp. We remove support for non-standard registries for simplicity.
// https://docs.docker.com/engine/reference/commandline/tag/#extended-description
//
@ -89,6 +92,7 @@ func (msg MsgStoreCode) GetSigners() []sdk.AccAddress {
type MsgInstantiateContract struct {
Sender sdk.AccAddress `json:"sender" yaml:"sender"`
Code uint64 `json:"code_id" yaml:"code_id"`
Label string `json:"string" yaml:"string"`
InitMsg json.RawMessage `json:"init_msg" yaml:"init_msg"`
InitFunds sdk.Coins `json:"init_funds" yaml:"init_funds"`
}
@ -102,6 +106,20 @@ func (msg MsgInstantiateContract) Type() string {
}
func (msg MsgInstantiateContract) ValidateBasic() error {
if err := sdk.VerifyAddressFormat(msg.Sender); err != nil {
return err
}
if msg.Code == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "code_id is required")
}
if msg.Label == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "label is required")
}
if len(msg.Label) > MaxLabelSize {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "label cannot be longer than 128 characters")
}
if msg.InitFunds.IsAnyNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "negative InitFunds")
}
@ -132,6 +150,13 @@ func (msg MsgExecuteContract) Type() string {
}
func (msg MsgExecuteContract) ValidateBasic() error {
if err := sdk.VerifyAddressFormat(msg.Sender); err != nil {
return err
}
if err := sdk.VerifyAddressFormat(msg.Contract); err != nil {
return err
}
if msg.SentFunds.IsAnyNegative() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "negative SentFunds")
}

View File

@ -3,6 +3,7 @@ package types
import (
"fmt"
"regexp"
"strings"
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -116,3 +117,93 @@ func TestStoreCodeValidation(t *testing.T) {
}
}
func TestInstantiateContractValidation(t *testing.T) {
badAddress, err := sdk.AccAddressFromHex("012345")
require.NoError(t, err)
// proper address size
goodAddress := sdk.AccAddress(make([]byte, 20))
cases := map[string]struct {
msg MsgInstantiateContract
valid bool
}{
"empty": {
msg: MsgInstantiateContract{},
valid: false,
},
"correct minimal": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Code: 1,
Label: "foo",
InitMsg: []byte("{}"),
},
valid: true,
},
"missing code": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Label: "foo",
InitMsg: []byte("{}"),
},
valid: false,
},
"missing label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
InitMsg: []byte("{}"),
},
valid: false,
},
"label too long": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Label: strings.Repeat("food", 33),
},
valid: false,
},
"bad sender minimal": {
msg: MsgInstantiateContract{
Sender: badAddress,
Code: 1,
Label: "foo",
InitMsg: []byte("{}"),
},
valid: false,
},
"correct maximal": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Code: 1,
Label: "foo",
InitMsg: []byte(`{"some": "data"}`),
InitFunds: sdk.Coins{sdk.Coin{Denom: "foobar", Amount: sdk.NewInt(200)}},
},
valid: true,
},
"negative funds": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Code: 1,
Label: "foo",
InitMsg: []byte(`{"some": "data"}`),
// we cannot use sdk.NewCoin() constructors as they panic on creating invalid data (before we can test)
InitFunds: sdk.Coins{sdk.Coin{Denom: "foobar", Amount: sdk.NewInt(-200)}},
},
valid: false,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
err := tc.msg.ValidateBasic()
if tc.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}