From 741c41478085be46c1c1803c91bf94251ceaef0b Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 22 Jun 2020 13:30:32 +0200 Subject: [PATCH 1/4] Update go-cosmwasm deps --- go.mod | 4 ++-- go.sum | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index aac24008605..ae9cc8872ec 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/CosmWasm/wasmd go 1.13 require ( - // Note: update ENV GO_COSMWASM in Dockerfile when updating this - github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615221537-0fc920db0349 + // Note: update ENV GO_COSMWASM in Dockerfile when updating this + github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200622095049-ee163b5aafe0 github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect github.com/cosmos/cosmos-sdk v0.38.3 github.com/golang/mock v1.4.3 // indirect diff --git a/go.sum b/go.sum index 675bb931e3f..97d140a7ac5 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615215318-6f82d95b1cb6 h1:o/CwMvx7 github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615215318-6f82d95b1cb6/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615221537-0fc920db0349 h1:SsPn/AHKtsZqKlGfXXArT/OUur9wJY79YNjFCIv1JZw= github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615221537-0fc920db0349/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= +github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200622095049-ee163b5aafe0 h1:cMbSZNFHkOsz+uJ+OsqiYPa5RTLQ/7eQsOIRJJM7eh4= +github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200622095049-ee163b5aafe0/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= From ac95b583fc52ab945df35ee9e96e3463c0146a44 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 22 Jun 2020 13:44:06 +0200 Subject: [PATCH 2/4] Update to handle multiple response types --- x/wasm/internal/keeper/keeper.go | 35 +++++++++++++++------------ x/wasm/internal/keeper/test_common.go | 2 +- x/wasm/internal/types/types.go | 34 ++++++++++++++------------ 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index c1188e8d538..6b624847169 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -141,8 +141,8 @@ func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A } // emit all events from this contract itself - value := types.CosmosResult(*res, contractAddress) - ctx.EventManager().EmitEvents(value.Events) + events := types.ParseEvents(res.Log, contractAddress) + ctx.EventManager().EmitEvents(events) err = k.dispatchMessages(ctx, contractAddress, res.Messages) if err != nil { @@ -158,17 +158,17 @@ func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A } // Execute executes the contract instance -func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, msg []byte, coins sdk.Coins) (sdk.Result, error) { +func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller sdk.AccAddress, msg []byte, coins sdk.Coins) (*sdk.Result, error) { codeInfo, prefixStore, err := k.contractInstance(ctx, contractAddress) if err != nil { - return sdk.Result{}, err + return nil, err } // add more funds if !coins.IsZero() { sdkerr := k.bankKeeper.SendCoins(ctx, caller, contractAddress, coins) if sdkerr != nil { - return sdk.Result{}, sdkerr + return nil, sdkerr } } @@ -184,20 +184,19 @@ func (k Keeper) Execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller res, gasUsed, execErr := k.wasmer.Execute(codeInfo.CodeHash, params, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas) consumeGas(ctx, gasUsed) if execErr != nil { - return sdk.Result{}, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error()) + return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error()) } // emit all events from this contract itself - value := types.CosmosResult(*res, contractAddress) - ctx.EventManager().EmitEvents(value.Events) - value.Events = nil + events := types.ParseEvents(res.Log, contractAddress) + ctx.EventManager().EmitEvents(events) err = k.dispatchMessages(ctx, contractAddress, res.Messages) if err != nil { - return sdk.Result{}, err + return nil, err } - return value, nil + return types.ResultFromData(res.Data), nil } // Migrate allows to upgrade a contract to a new code with data migration. @@ -235,10 +234,14 @@ func (k Keeper) Migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller return nil, sdkerrors.Wrap(types.ErrMigrationFailed, err.Error()) } - // emit all events from this contract migration itself - value := types.CosmosResult(*res, contractAddress) - ctx.EventManager().EmitEvents(value.Events) - value.Events = nil + // emit all events from this contract itself + events := types.ParseEvents(res.Log, contractAddress) + ctx.EventManager().EmitEvents(events) + + err = k.dispatchMessages(ctx, contractAddress, res.Messages) + if err != nil { + return nil, err + } contractInfo.UpdateCodeID(ctx, newCodeID) k.setContractInfo(ctx, contractAddress, contractInfo) @@ -247,7 +250,7 @@ func (k Keeper) Migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller return nil, sdkerrors.Wrap(err, "dispatch") } - return &value, nil + return types.ResultFromData(res.Data), nil } // UpdateContractAdmin sets the admin value on the ContractInfo. New admin can be nil to disable further migrations/ updates. diff --git a/x/wasm/internal/keeper/test_common.go b/x/wasm/internal/keeper/test_common.go index 1343130ebf1..ae60b7a44b7 100644 --- a/x/wasm/internal/keeper/test_common.go +++ b/x/wasm/internal/keeper/test_common.go @@ -216,5 +216,5 @@ func handleExecute(ctx sdk.Context, k Keeper, msg *wasmTypes.MsgExecuteContract) } res.Events = ctx.EventManager().Events() - return &res, nil + return res, nil } diff --git a/x/wasm/internal/types/types.go b/x/wasm/internal/types/types.go index 028d867d09a..6652e83e442 100644 --- a/x/wasm/internal/types/types.go +++ b/x/wasm/internal/types/types.go @@ -144,24 +144,26 @@ func NewWasmCoins(cosmosCoins sdk.Coins) (wasmCoins []wasmTypes.Coin) { const CustomEventType = "wasm" const AttributeKeyContractAddr = "contract_address" -// CosmosResult converts from a Wasm Result type -func CosmosResult(wasmResult wasmTypes.Result, contractAddr sdk.AccAddress) sdk.Result { - var events []sdk.Event - if len(wasmResult.Log) > 0 { - // we always tag with the contract address issuing this event - attrs := []sdk.Attribute{sdk.NewAttribute(AttributeKeyContractAddr, contractAddr.String())} - for _, l := range wasmResult.Log { - // and reserve the contract_address key for our use (not contract) - if l.Key != AttributeKeyContractAddr { - attr := sdk.NewAttribute(l.Key, l.Value) - attrs = append(attrs, attr) - } +// ParseEvents converts wasm LogAttributes into an sdk.Events (with 0 or 1 elements) +func ParseEvents(logs []wasmTypes.LogAttribute, contractAddr sdk.AccAddress) sdk.Events { + if len(logs) == 0 { + return nil + } + // we always tag with the contract address issuing this event + attrs := []sdk.Attribute{sdk.NewAttribute(AttributeKeyContractAddr, contractAddr.String())} + for _, l := range logs { + // and reserve the contract_address key for our use (not contract) + if l.Key != AttributeKeyContractAddr { + attr := sdk.NewAttribute(l.Key, l.Value) + attrs = append(attrs, attr) } - events = []sdk.Event{sdk.NewEvent(CustomEventType, attrs...)} } - return sdk.Result{ - Data: []byte(wasmResult.Data), - Events: events, + return sdk.Events{sdk.NewEvent(CustomEventType, attrs...)} +} + +func ResultFromData(data string) *sdk.Result { + return &sdk.Result{ + Data: []byte(data), } } From cefd743146301bc081d0fc5a716710bdcde59da6 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 22 Jun 2020 14:00:50 +0200 Subject: [PATCH 3/4] Update test code --- x/wasm/alias.go | 3 ++- x/wasm/handler.go | 2 +- x/wasm/internal/keeper/keeper.go | 5 ----- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/x/wasm/alias.go b/x/wasm/alias.go index c29b2585e8d..acb3a2b1e34 100644 --- a/x/wasm/alias.go +++ b/x/wasm/alias.go @@ -46,7 +46,8 @@ var ( NewContractInfo = types.NewContractInfo NewEnv = types.NewEnv NewWasmCoins = types.NewWasmCoins - CosmosResult = types.CosmosResult + ParseEvents = types.ParseEvents + ResultFromData = types.ResultFromData DefaultWasmConfig = types.DefaultWasmConfig InitGenesis = keeper.InitGenesis ExportGenesis = keeper.ExportGenesis diff --git a/x/wasm/handler.go b/x/wasm/handler.go index 980950e11c9..15e302b418f 100644 --- a/x/wasm/handler.go +++ b/x/wasm/handler.go @@ -125,7 +125,7 @@ func handleExecute(ctx sdk.Context, k Keeper, msg *MsgExecuteContract) (*sdk.Res ) res.Events = append(events, ourEvent) - return &res, nil + return res, nil } func handleMigration(ctx sdk.Context, k Keeper, msg *MsgMigrateContract) (*sdk.Result, error) { diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index 6b624847169..7fc35033ae5 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -238,11 +238,6 @@ func (k Keeper) Migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller events := types.ParseEvents(res.Log, contractAddress) ctx.EventManager().EmitEvents(events) - err = k.dispatchMessages(ctx, contractAddress, res.Messages) - if err != nil { - return nil, err - } - contractInfo.UpdateCodeID(ctx, newCodeID) k.setContractInfo(ctx, contractAddress, contractInfo) From b537571e376f634e56b08dbc419570a919308b1f Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 22 Jun 2020 15:19:57 +0200 Subject: [PATCH 4/4] Update to go-cosmwasm v0.9.0-alpha2 --- Dockerfile | 2 +- go.mod | 2 +- go.sum | 8 ++------ 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index b6dc1444ff3..219c81ebfe9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ RUN go mod download # TODO: how to use this instead of hardcoding GO_COSMWASM RUN basename $(ls -d /go/pkg/mod/github.com/\!cosm\!wasm/go-cosmwasm@v*) -ENV GO_COSMWASM="v0.8.2-0.20200615221537-0fc920db0349" +ENV GO_COSMWASM="v0.9.0-alpha2" # build go-cosmwasm *.a and install it WORKDIR /go/pkg/mod/github.com/\!cosm\!wasm/go-cosmwasm@${GO_COSMWASM} diff --git a/go.mod b/go.mod index ae9cc8872ec..2e4dfdc0098 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( // Note: update ENV GO_COSMWASM in Dockerfile when updating this - github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200622095049-ee163b5aafe0 + github.com/CosmWasm/go-cosmwasm v0.9.0-alpha2 github.com/btcsuite/btcd v0.0.0-20190807005414-4063feeff79a // indirect github.com/cosmos/cosmos-sdk v0.38.3 github.com/golang/mock v1.4.3 // indirect diff --git a/go.sum b/go.sum index 97d140a7ac5..ec5c0c1f86d 100644 --- a/go.sum +++ b/go.sum @@ -9,12 +9,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f h1:4O1om+UVU+Hfcihr1timk8YNXHxzZWgCo7ofnrZRApw= github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4= -github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615215318-6f82d95b1cb6 h1:o/CwMvx7YP+Mnu4MvEypThjpmimGcd0ahJYUqKLI4q8= -github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615215318-6f82d95b1cb6/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= -github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615221537-0fc920db0349 h1:SsPn/AHKtsZqKlGfXXArT/OUur9wJY79YNjFCIv1JZw= -github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200615221537-0fc920db0349/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= -github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200622095049-ee163b5aafe0 h1:cMbSZNFHkOsz+uJ+OsqiYPa5RTLQ/7eQsOIRJJM7eh4= -github.com/CosmWasm/go-cosmwasm v0.8.2-0.20200622095049-ee163b5aafe0/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= +github.com/CosmWasm/go-cosmwasm v0.9.0-alpha2 h1:dOY+aro+Hgnpwrt5T/JqaxVWqdIx+onRy7swqe1PLCQ= +github.com/CosmWasm/go-cosmwasm v0.9.0-alpha2/go.mod h1:gAFCwllx97ejI+m9SqJQrmd2SBW7HA0fOjvWWJjM2uc= github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=