Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cosmwasm #147

Merged
merged 4 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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.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
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +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.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=
Expand Down
3 changes: 2 additions & 1 deletion x/wasm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
30 changes: 14 additions & 16 deletions x/wasm/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}

Expand All @@ -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.
Expand Down Expand Up @@ -235,10 +234,9 @@ 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)

contractInfo.UpdateCodeID(ctx, newCodeID)
k.setContractInfo(ctx, contractAddress, contractInfo)
Expand All @@ -247,7 +245,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.
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/internal/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
34 changes: 18 additions & 16 deletions x/wasm/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down