From 62d0c308a835b72693ded2b64bdfccee3dae1b21 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Fri, 17 Jan 2020 10:58:50 +0530 Subject: [PATCH 1/8] Add source ref --- x/wasm/client/cli/tx.go | 40 ++++++++++++++++++++++++-- x/wasm/handler.go | 2 +- x/wasm/internal/keeper/genesis.go | 2 +- x/wasm/internal/keeper/keeper.go | 5 ++-- x/wasm/internal/keeper/keeper_test.go | 8 +++--- x/wasm/internal/keeper/querier_test.go | 2 +- x/wasm/internal/types/msg.go | 4 +++ x/wasm/internal/types/types.go | 6 +++- 8 files changed, 56 insertions(+), 13 deletions(-) diff --git a/x/wasm/client/cli/tx.go b/x/wasm/client/cli/tx.go index a51890d..ed5a01a 100644 --- a/x/wasm/client/cli/tx.go +++ b/x/wasm/client/cli/tx.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io/ioutil" + "net/url" "strconv" "github.com/spf13/cobra" @@ -21,13 +22,20 @@ import ( ) const ( - flagTo = "to" - flagAmount = "amount" + flagTo = "to" + flagAmount = "amount" + flagSource = "source" + flagBuilder = "builder" ) // limit max bytes read to prevent gzip bombs const maxSize = 400 * 1024 +// whitelist +var validBuildTags = map[string]bool{ + "cosmwasm-opt:0.6.0": true, "cosmwasm-opt:0.5.2": true, +} + // GetTxCmd returns the transaction commands for this module func GetTxCmd(cdc *codec.Codec) *cobra.Command { txCmd := &cobra.Command{ @@ -48,7 +56,7 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command { // StoreCodeCmd will upload code to be reused. func StoreCodeCmd(cdc *codec.Codec) *cobra.Command { cmd := &cobra.Command{ - Use: "store [from_key_or_address] [wasm file]", + Use: "store [from_key_or_address] [wasm file] --source [source] --builder [builder]", Short: "Upload a wasm binary", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { @@ -62,6 +70,26 @@ func StoreCodeCmd(cdc *codec.Codec) *cobra.Command { return err } + source := viper.GetString(flagSource) + + // ensure source to be a valid uri + if source != "" { + _, err := url.Parse(source) + + if err != nil { + return fmt.Errorf("invalid url supplied for source %s", source) + } + } + + builder := viper.GetString(flagBuilder) + + // ensure builder to be a valid build tag + if builder != "" { + if !validBuildTags[builder] { + return fmt.Errorf("invalid tag supplied for builder %s", source) + } + } + // limit the input size if len(wasm) > maxSize { return fmt.Errorf("input size exceeds the max size allowed (allowed:%d, actual: %d)", @@ -83,10 +111,16 @@ func StoreCodeCmd(cdc *codec.Codec) *cobra.Command { msg := types.MsgStoreCode{ Sender: cliCtx.GetFromAddress(), WASMByteCode: wasm, + Source: source, + Builder: builder, } return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) }, } + + cmd.Flags().String(flagSource, "", "A valid URI reference to the contract's source code, optional") + cmd.Flags().String(flagBuilder, "", "A valid docker tag for the build system, optional") + return cmd } diff --git a/x/wasm/handler.go b/x/wasm/handler.go index 7078bfc..0ed3e3e 100644 --- a/x/wasm/handler.go +++ b/x/wasm/handler.go @@ -40,7 +40,7 @@ func NewHandler(k Keeper) sdk.Handler { } func handleStoreCode(ctx sdk.Context, k Keeper, msg *MsgStoreCode) sdk.Result { - codeID, err := k.Create(ctx, msg.Sender, msg.WASMByteCode) + codeID, err := k.Create(ctx, msg.Sender, msg.WASMByteCode, msg.Source, msg.Builder) if err != nil { return err.Result() } diff --git a/x/wasm/internal/keeper/genesis.go b/x/wasm/internal/keeper/genesis.go index e67ee85..ad72c7d 100644 --- a/x/wasm/internal/keeper/genesis.go +++ b/x/wasm/internal/keeper/genesis.go @@ -14,7 +14,7 @@ import ( // CONTRACT: all types of accounts must have been already initialized/created func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) { for _, code := range data.Codes { - newId, err := keeper.Create(ctx, code.CodeInfo.Creator, code.CodesBytes) + newId, err := keeper.Create(ctx, code.CodeInfo.Creator, code.CodesBytes, code.CodeInfo.Source, code.CodeInfo.Builder) if err != nil { panic(err) } diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index 7f274f6..d2d231f 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -62,7 +62,8 @@ func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, accountKeeper auth.Accou } // Create uploads and compiles a WASM contract, returning a short identifier for the contract -func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte) (codeID uint64, sdkErr sdk.Error) { +func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte, source string, + builder string) (codeID uint64, sdkErr sdk.Error) { wasmCode, err := uncompress(wasmCode) if err != nil { return 0, types.ErrCreateFailed(err) @@ -74,7 +75,7 @@ func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte) store := ctx.KVStore(k.storeKey) codeID = k.autoIncrementID(ctx, types.KeyLastCodeID) - contractInfo := types.NewCodeInfo(codeHash, creator) + contractInfo := types.NewCodeInfo(codeHash, creator, source, builder) // 0x01 | codeID (uint64) -> ContractInfo store.Set(types.GetCodeKey(codeID), k.cdc.MustMarshalBinaryBare(contractInfo)) diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index 55d1006..f52de20 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -37,7 +37,7 @@ func TestCreate(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode) + contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") require.NoError(t, err) require.Equal(t, uint64(1), contractID) // and verify content @@ -58,7 +58,7 @@ func TestCreateWithGzippedPayload(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm.gzip") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode) + contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") require.NoError(t, err) require.Equal(t, uint64(1), contractID) // and verify content @@ -81,7 +81,7 @@ func TestInstantiate(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode) + contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") require.NoError(t, err) initMsg := InitMsg{ @@ -138,7 +138,7 @@ func TestExecute(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode) + contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") require.NoError(t, err) _, _, bob := keyPubAddr() diff --git a/x/wasm/internal/keeper/querier_test.go b/x/wasm/internal/keeper/querier_test.go index eceb861..928e702 100644 --- a/x/wasm/internal/keeper/querier_test.go +++ b/x/wasm/internal/keeper/querier_test.go @@ -32,7 +32,7 @@ func TestQueryContractState(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode) + contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") require.NoError(t, err) _, _, bob := keyPubAddr() diff --git a/x/wasm/internal/types/msg.go b/x/wasm/internal/types/msg.go index 939b8e7..8f1e9c9 100644 --- a/x/wasm/internal/types/msg.go +++ b/x/wasm/internal/types/msg.go @@ -12,6 +12,10 @@ type MsgStoreCode struct { Sender sdk.AccAddress `json:"sender" yaml:"sender"` // WASMByteCode can be raw or gzip compressed WASMByteCode []byte `json:"wasm_byte_code" yaml:"wasm_byte_code"` + // Source is a valid URI reference to the contract's source code, optional + Source string `json:"source" yaml:"source"` + // Builder is a docker tag, optional + Builder string `json:"builder" yaml:"builder"` } func (msg MsgStoreCode) Route() string { diff --git a/x/wasm/internal/types/types.go b/x/wasm/internal/types/types.go index 7e5ffe7..55df13e 100644 --- a/x/wasm/internal/types/types.go +++ b/x/wasm/internal/types/types.go @@ -16,13 +16,17 @@ type Model struct { type CodeInfo struct { CodeHash []byte `json:"code_hash"` Creator sdk.AccAddress `json:"creator"` + Source string `json:"source"` + Builder string `json:"builder"` } // NewCodeInfo fills a new Contract struct -func NewCodeInfo(codeHash []byte, creator sdk.AccAddress) CodeInfo { +func NewCodeInfo(codeHash []byte, creator sdk.AccAddress, source string, builder string) CodeInfo { return CodeInfo{ CodeHash: codeHash, Creator: creator, + Source: source, + Builder: builder, } } From b2172a81e5594b7e107311218e4471e933e25749 Mon Sep 17 00:00:00 2001 From: Sahith Reddy Narahari Date: Fri, 17 Jan 2020 19:16:09 +0530 Subject: [PATCH 2/8] Added source and builder to validate basic --- x/wasm/client/cli/tx.go | 30 ++++-------------------------- x/wasm/client/utils/utils_test.go | 10 +++++----- x/wasm/handler.go | 1 + x/wasm/internal/keeper/keeper.go | 3 +-- x/wasm/internal/types/msg.go | 18 ++++++++++++++++++ 5 files changed, 29 insertions(+), 33 deletions(-) diff --git a/x/wasm/client/cli/tx.go b/x/wasm/client/cli/tx.go index ed5a01a..d46b313 100644 --- a/x/wasm/client/cli/tx.go +++ b/x/wasm/client/cli/tx.go @@ -4,7 +4,6 @@ import ( "bufio" "fmt" "io/ioutil" - "net/url" "strconv" "github.com/spf13/cobra" @@ -28,9 +27,6 @@ const ( flagBuilder = "builder" ) -// limit max bytes read to prevent gzip bombs -const maxSize = 400 * 1024 - // whitelist var validBuildTags = map[string]bool{ "cosmwasm-opt:0.6.0": true, "cosmwasm-opt:0.5.2": true, @@ -72,30 +68,8 @@ func StoreCodeCmd(cdc *codec.Codec) *cobra.Command { source := viper.GetString(flagSource) - // ensure source to be a valid uri - if source != "" { - _, err := url.Parse(source) - - if err != nil { - return fmt.Errorf("invalid url supplied for source %s", source) - } - } - builder := viper.GetString(flagBuilder) - // ensure builder to be a valid build tag - if builder != "" { - if !validBuildTags[builder] { - return fmt.Errorf("invalid tag supplied for builder %s", source) - } - } - - // limit the input size - if len(wasm) > maxSize { - return fmt.Errorf("input size exceeds the max size allowed (allowed:%d, actual: %d)", - maxSize, len(wasm)) - } - // gzip the wasm file if wasmUtils.IsWasm(wasm) { wasm, err = wasmUtils.GzipIt(wasm) @@ -114,6 +88,10 @@ func StoreCodeCmd(cdc *codec.Codec) *cobra.Command { Source: source, Builder: builder, } + err = msg.ValidateBasic() + if err != nil { + return fmt.Errorf("invalid message") + } return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) }, } diff --git a/x/wasm/client/utils/utils_test.go b/x/wasm/client/utils/utils_test.go index f389043..0594a9a 100644 --- a/x/wasm/client/utils/utils_test.go +++ b/x/wasm/client/utils/utils_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func GetTestData() ([]byte, []byte, []byte, error){ +func GetTestData() ([]byte, []byte, []byte, error) { wasmCode, err := ioutil.ReadFile("../../internal/keeper/testdata/contract.wasm") if err != nil { @@ -23,7 +23,7 @@ func GetTestData() ([]byte, []byte, []byte, error){ return wasmCode, someRandomStr, gzipData, nil } -func TestIsWasm (t *testing.T) { +func TestIsWasm(t *testing.T) { wasmCode, someRandomStr, gzipData, err := GetTestData() require.NoError(t, err) @@ -35,7 +35,7 @@ func TestIsWasm (t *testing.T) { require.True(t, IsWasm(wasmCode)) } -func TestIsGzip (t *testing.T) { +func TestIsGzip(t *testing.T) { wasmCode, someRandomStr, gzipData, err := GetTestData() require.NoError(t, err) @@ -44,7 +44,7 @@ func TestIsGzip (t *testing.T) { require.True(t, IsGzip(gzipData)) } -func TestGzipIt (t *testing.T) { +func TestGzipIt(t *testing.T) { wasmCode, someRandomStr, _, err := GetTestData() originalGzipData := []byte{31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 87, 40, 207, 47, 202, 73, 1, 4, 0, 0, 255, 255, 133, 17, 74, 13, 11, 0, 0, 0} @@ -61,4 +61,4 @@ func TestGzipIt (t *testing.T) { require.True(t, IsGzip(strToGzip)) require.NoError(t, err) require.Equal(t, originalGzipData, strToGzip) -} \ No newline at end of file +} diff --git a/x/wasm/handler.go b/x/wasm/handler.go index 0ed3e3e..6e07698 100644 --- a/x/wasm/handler.go +++ b/x/wasm/handler.go @@ -40,6 +40,7 @@ func NewHandler(k Keeper) sdk.Handler { } func handleStoreCode(ctx sdk.Context, k Keeper, msg *MsgStoreCode) sdk.Result { + err := msg.ValidateBasic() codeID, err := k.Create(ctx, msg.Sender, msg.WASMByteCode, msg.Source, msg.Builder) if err != nil { return err.Result() diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index d2d231f..82d2ea3 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -62,8 +62,7 @@ func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, accountKeeper auth.Accou } // Create uploads and compiles a WASM contract, returning a short identifier for the contract -func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte, source string, - builder string) (codeID uint64, sdkErr sdk.Error) { +func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte, source string, builder string) (codeID uint64, sdkErr sdk.Error) { wasmCode, err := uncompress(wasmCode) if err != nil { return 0, types.ErrCreateFailed(err) diff --git a/x/wasm/internal/types/msg.go b/x/wasm/internal/types/msg.go index 8f1e9c9..e1283ca 100644 --- a/x/wasm/internal/types/msg.go +++ b/x/wasm/internal/types/msg.go @@ -1,6 +1,9 @@ package types import ( + "net/url" + "regexp" + sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -33,6 +36,21 @@ func (msg MsgStoreCode) ValidateBasic() sdk.Error { if len(msg.WASMByteCode) > MaxWasmSize { return sdk.ErrInternal("wasm code too large") } + if msg.Source != "" { + _, err := url.Parse(msg.Source) + if err != nil { + return sdk.ErrInternal("invalid source") + } + } + if msg.Builder != "" { + ok, err := regexp.MatchString("cosmwasm-op:", msg.Builder) + if err != nil { + if !ok { + return sdk.ErrInternal("invalid tag supplied for builder") + } + } + } + return nil } From adb07de1dbb70e15a276fcee8646d0053f50e5a8 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Fri, 17 Jan 2020 21:32:19 +0530 Subject: [PATCH 3/8] Add genesis tests for source, builder --- go.mod | 1 + go.sum | 2 ++ x/wasm/client/cli/tx.go | 4 +++- x/wasm/genesis_test.go | 38 ++++++++++++++++++++++++++++++++++-- x/wasm/handler.go | 5 +++++ x/wasm/internal/types/msg.go | 25 ++++++++++++++---------- 6 files changed, 62 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 7c875cf..b087abf 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/cosmwasm/wasmd go 1.13 require ( + github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect github.com/confio/go-cosmwasm v0.6.0 github.com/cosmos/cosmos-sdk v0.34.4-0.20191114141721-d4c831e63ad3 diff --git a/go.sum b/go.sum index 6b31044..0ad08ac 100644 --- a/go.sum +++ b/go.sum @@ -12,6 +12,8 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0= +github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= diff --git a/x/wasm/client/cli/tx.go b/x/wasm/client/cli/tx.go index d46b313..05bb3a2 100644 --- a/x/wasm/client/cli/tx.go +++ b/x/wasm/client/cli/tx.go @@ -89,9 +89,11 @@ func StoreCodeCmd(cdc *codec.Codec) *cobra.Command { Builder: builder, } err = msg.ValidateBasic() + if err != nil { - return fmt.Errorf("invalid message") + return err } + return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg}) }, } diff --git a/x/wasm/genesis_test.go b/x/wasm/genesis_test.go index 9d58ae9..29d809b 100644 --- a/x/wasm/genesis_test.go +++ b/x/wasm/genesis_test.go @@ -24,11 +24,45 @@ func TestInitGenesis(t *testing.T) { h := data.module.NewHandler() q := data.module.NewQuerierHandler() + t.Log("fail with invalid source url") msg := MsgStoreCode{ Sender: creator, WASMByteCode: testContract, + Source: "someinvalidurl", + Builder: "", } + + err := msg.ValidateBasic() + require.Error(t, err) + res := h(data.ctx, msg) + require.False(t, res.IsOK()) + + t.Log("fail with invalid build tag") + msg = MsgStoreCode{ + Sender: creator, + WASMByteCode: testContract, + Source: "", + Builder: "somerandombuildtag-0.6.2", + } + + err = msg.ValidateBasic() + require.Error(t, err) + + res = h(data.ctx, msg) + require.False(t, res.IsOK()) + + t.Log("no error with valid source and build tag") + msg = MsgStoreCode{ + Sender: creator, + WASMByteCode: testContract, + Source: "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", + Builder: "cosmwasm-opt:0.5.2", + } + err = msg.ValidateBasic() + require.NoError(t, err) + + res = h(data.ctx, msg) require.True(t, res.IsOK()) require.Equal(t, res.Data, []byte("1")) @@ -37,8 +71,8 @@ func TestInitGenesis(t *testing.T) { Verifier: fred, Beneficiary: bob, } - initMsgBz, err := json.Marshal(initMsg) - require.NoError(t, err) + initMsgBz, _err := json.Marshal(initMsg) + require.NoError(t, _err) initCmd := MsgInstantiateContract{ Sender: creator, diff --git a/x/wasm/handler.go b/x/wasm/handler.go index 0808a35..97ac675 100644 --- a/x/wasm/handler.go +++ b/x/wasm/handler.go @@ -40,6 +40,11 @@ func NewHandler(k Keeper) sdk.Handler { } func handleStoreCode(ctx sdk.Context, k Keeper, msg *MsgStoreCode) sdk.Result { + _err := msg.ValidateBasic() + if _err != nil { + return sdk.ResultFromError(_err) + } + codeID, err := k.Create(ctx, msg.Sender, msg.WASMByteCode, msg.Source, msg.Builder) if err != nil { return sdk.ResultFromError(err) diff --git a/x/wasm/internal/types/msg.go b/x/wasm/internal/types/msg.go index e1283ca..d9bbfeb 100644 --- a/x/wasm/internal/types/msg.go +++ b/x/wasm/internal/types/msg.go @@ -1,14 +1,17 @@ package types import ( - "net/url" + "fmt" "regexp" + "github.com/asaskevich/govalidator" + sdk "github.com/cosmos/cosmos-sdk/types" ) const ( - MaxWasmSize = 500 * 1024 + MaxWasmSize = 500 * 1024 + BuildTagRegex = "cosmwasm-opt:" ) type MsgStoreCode struct { @@ -33,21 +36,23 @@ func (msg MsgStoreCode) ValidateBasic() sdk.Error { if len(msg.WASMByteCode) == 0 { return sdk.ErrInternal("empty wasm code") } + if len(msg.WASMByteCode) > MaxWasmSize { return sdk.ErrInternal("wasm code too large") } + if msg.Source != "" { - _, err := url.Parse(msg.Source) - if err != nil { - return sdk.ErrInternal("invalid source") + fmt.Println("source: ", msg.Source) + + if !govalidator.IsURL(msg.Source) { + return sdk.ErrInternal("source should be a valid url") } } + if msg.Builder != "" { - ok, err := regexp.MatchString("cosmwasm-op:", msg.Builder) - if err != nil { - if !ok { - return sdk.ErrInternal("invalid tag supplied for builder") - } + ok, err := regexp.MatchString(BuildTagRegex, msg.Builder) + if err != nil || !ok { + return sdk.ErrInternal("invalid tag supplied for builder") } } From cd082fe3536ce488929f73c300424842a54f6399 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Fri, 17 Jan 2020 21:41:14 +0530 Subject: [PATCH 4/8] Update tests --- x/wasm/client/cli/tx.go | 5 ----- x/wasm/internal/keeper/keeper_test.go | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/x/wasm/client/cli/tx.go b/x/wasm/client/cli/tx.go index 05bb3a2..b8df368 100644 --- a/x/wasm/client/cli/tx.go +++ b/x/wasm/client/cli/tx.go @@ -27,11 +27,6 @@ const ( flagBuilder = "builder" ) -// whitelist -var validBuildTags = map[string]bool{ - "cosmwasm-opt:0.6.0": true, "cosmwasm-opt:0.5.2": true, -} - // GetTxCmd returns the transaction commands for this module func GetTxCmd(cdc *codec.Codec) *cobra.Command { txCmd := &cobra.Command{ diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index 847a4be..a436add 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -38,7 +38,7 @@ func TestCreate(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") + contractID, err := keeper.Create(ctx, creator, wasmCode, "./testdata/contract.wasm", "cosmwasm-opt:0.5.2") require.NoError(t, err) require.Equal(t, uint64(1), contractID) // and verify content From 92e26efa790e5acf9b6457454aafb63f56045927 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Sat, 18 Jan 2020 12:32:34 +0530 Subject: [PATCH 5/8] Fix validate source url and Update genesis tests --- x/wasm/genesis_test.go | 44 +++++++++++++++++++++++++++++------- x/wasm/handler.go | 6 ++--- x/wasm/internal/types/msg.go | 22 ++++++++++++------ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/x/wasm/genesis_test.go b/x/wasm/genesis_test.go index 29d809b..bd2af27 100644 --- a/x/wasm/genesis_test.go +++ b/x/wasm/genesis_test.go @@ -32,12 +32,40 @@ func TestInitGenesis(t *testing.T) { Builder: "", } - err := msg.ValidateBasic() - require.Error(t, err) + sdkerr := msg.ValidateBasic() + require.Error(t, sdkerr) res := h(data.ctx, msg) require.False(t, res.IsOK()) + t.Log("fail with relative source url") + msg = MsgStoreCode{ + Sender: creator, + WASMByteCode: testContract, + Source: "./testdata/escrow.wasm", + Builder: "", + } + + sdkerr = msg.ValidateBasic() + require.Error(t, sdkerr) + + res = h(data.ctx, msg) + require.False(t, res.IsOK()) + + t.Log("fail with unreachable source url") + msg = MsgStoreCode{ + Sender: creator, + WASMByteCode: testContract, + Source: "https://github.com/cosmwasm/wasmddddddrandom", + Builder: "", + } + + sdkerr = msg.ValidateBasic() + require.Error(t, sdkerr) + + res = h(data.ctx, msg) + require.False(t, res.IsOK()) + t.Log("fail with invalid build tag") msg = MsgStoreCode{ Sender: creator, @@ -46,8 +74,8 @@ func TestInitGenesis(t *testing.T) { Builder: "somerandombuildtag-0.6.2", } - err = msg.ValidateBasic() - require.Error(t, err) + sdkerr = msg.ValidateBasic() + require.Error(t, sdkerr) res = h(data.ctx, msg) require.False(t, res.IsOK()) @@ -59,8 +87,8 @@ func TestInitGenesis(t *testing.T) { Source: "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", Builder: "cosmwasm-opt:0.5.2", } - err = msg.ValidateBasic() - require.NoError(t, err) + sdkerr = msg.ValidateBasic() + require.NoError(t, sdkerr) res = h(data.ctx, msg) require.True(t, res.IsOK()) @@ -71,8 +99,8 @@ func TestInitGenesis(t *testing.T) { Verifier: fred, Beneficiary: bob, } - initMsgBz, _err := json.Marshal(initMsg) - require.NoError(t, _err) + initMsgBz, err := json.Marshal(initMsg) + require.NoError(t, err) initCmd := MsgInstantiateContract{ Sender: creator, diff --git a/x/wasm/handler.go b/x/wasm/handler.go index 97ac675..989317a 100644 --- a/x/wasm/handler.go +++ b/x/wasm/handler.go @@ -40,9 +40,9 @@ func NewHandler(k Keeper) sdk.Handler { } func handleStoreCode(ctx sdk.Context, k Keeper, msg *MsgStoreCode) sdk.Result { - _err := msg.ValidateBasic() - if _err != nil { - return sdk.ResultFromError(_err) + sdkerr := msg.ValidateBasic() + if sdkerr != nil { + return sdk.ResultFromError(sdkerr) } codeID, err := k.Create(ctx, msg.Sender, msg.WASMByteCode, msg.Source, msg.Builder) diff --git a/x/wasm/internal/types/msg.go b/x/wasm/internal/types/msg.go index d9bbfeb..e1662a9 100644 --- a/x/wasm/internal/types/msg.go +++ b/x/wasm/internal/types/msg.go @@ -1,17 +1,16 @@ package types import ( - "fmt" + "net/http" + "net/url" "regexp" - "github.com/asaskevich/govalidator" - sdk "github.com/cosmos/cosmos-sdk/types" ) const ( MaxWasmSize = 500 * 1024 - BuildTagRegex = "cosmwasm-opt:" + BuildTagRegex = "^cosmwasm-opt:" ) type MsgStoreCode struct { @@ -42,11 +41,20 @@ func (msg MsgStoreCode) ValidateBasic() sdk.Error { } if msg.Source != "" { - fmt.Println("source: ", msg.Source) - - if !govalidator.IsURL(msg.Source) { + u, err := url.Parse(msg.Source) + if err != nil { return sdk.ErrInternal("source should be a valid url") } + + if !u.IsAbs() { + return sdk.ErrInternal("source should be an absolute url") + } + + // check if the source is reachable + resp, err := http.Get(msg.Source) + if err != nil || resp.StatusCode != 200 { + return sdk.ErrInternal("source url is not reachable") + } } if msg.Builder != "" { From 1d268be8eb6c3f9208e6923faec92d20dec039ba Mon Sep 17 00:00:00 2001 From: anilCSE Date: Sun, 19 Jan 2020 12:02:38 +0530 Subject: [PATCH 6/8] Remove unused govalidator dependency --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index b087abf..7c875cf 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/cosmwasm/wasmd go 1.13 require ( - github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect github.com/confio/go-cosmwasm v0.6.0 github.com/cosmos/cosmos-sdk v0.34.4-0.20191114141721-d4c831e63ad3 diff --git a/go.sum b/go.sum index 0ad08ac..6b31044 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,6 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0= -github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8= github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= From e895b262f92e9c77e8c9f338368a99f78222de75 Mon Sep 17 00:00:00 2001 From: anilCSE Date: Sun, 19 Jan 2020 12:07:31 +0530 Subject: [PATCH 7/8] Update tests --- x/wasm/internal/keeper/keeper_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index a436add..59fe1b5 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -38,7 +38,7 @@ func TestCreate(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode, "./testdata/contract.wasm", "cosmwasm-opt:0.5.2") + contractID, err := keeper.Create(ctx, creator, wasmCode, "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "cosmwasm-opt:0.5.2") require.NoError(t, err) require.Equal(t, uint64(1), contractID) // and verify content @@ -59,7 +59,7 @@ func TestCreateWithGzippedPayload(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm.gzip") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") + contractID, err := keeper.Create(ctx, creator, wasmCode, "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "") require.NoError(t, err) require.Equal(t, uint64(1), contractID) // and verify content @@ -82,7 +82,7 @@ func TestInstantiate(t *testing.T) { wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm") require.NoError(t, err) - contractID, err := keeper.Create(ctx, creator, wasmCode, "", "") + contractID, err := keeper.Create(ctx, creator, wasmCode, "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "") require.NoError(t, err) _, _, bob := keyPubAddr() From 2007e70977b63dc04dd78db4bf12078e4bce501e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 20 Jan 2020 10:15:19 +0100 Subject: [PATCH 8/8] Update gas price - saving source adds storage gas --- x/wasm/internal/keeper/keeper_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index 59fe1b5..323d79b 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -103,7 +103,7 @@ func TestInstantiate(t *testing.T) { require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String()) gasAfter := ctx.GasMeter().GasConsumed() - require.Equal(t, uint64(36698), gasAfter-gasBefore) + require.Equal(t, uint64(36923), gasAfter-gasBefore) } func TestInstantiateWithNonExistingCodeID(t *testing.T) {