Skip to content

Commit

Permalink
Merge branch 'wasmvm-130' into zjg/upgrade-wasmvm130
Browse files Browse the repository at this point in the history
  • Loading branch information
zjg555543 committed Jul 28, 2023
2 parents bf1fcc1 + 233688f commit 9c907f8
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ require (
)

replace (
github.com/CosmWasm/wasmvm => github.com/okx/wasmvm v1.3.3-0.20230728015654-a16c7d2ea289
github.com/CosmWasm/wasmvm => github.com/okx/wasmvm v1.3.3-0.20230728083011-6960449ac9d5
github.com/buger/jsonparser => github.com/buger/jsonparser v1.0.0 // imported by nacos-go-sdk, upgraded to v1.0.0 in case of a known vulnerable bug
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/gorocksdb => github.com/okex/grocksdb v1.6.45-okc2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,8 @@ github.com/okex/go-ethereum v1.10.8-okc2/go.mod h1:pJNuIUYfX5+JKzSD/BTdNsvJSZ1TJ
github.com/okex/grocksdb v1.6.45-okc2 h1:JuUg2NcAFHZn78+xANcEKn9bcuF0tX8Jx3iMFfPnAEQ=
github.com/okex/grocksdb v1.6.45-okc2/go.mod h1:+/BHUY+mT0tbaVXwO2wTtD9eytazyw1W5n2O7AGyXZA=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/okx/wasmvm v1.3.3-0.20230728015654-a16c7d2ea289 h1:XcLpTU6Jsr2B3BqX69aDjZn9cXxNNXig6ofqMI2klf4=
github.com/okx/wasmvm v1.3.3-0.20230728015654-a16c7d2ea289/go.mod h1:vW/E3h8j9xBQs9bCoijDuawKo9kCtxOaS8N8J7KFtkc=
github.com/okx/wasmvm v1.3.3-0.20230728083011-6960449ac9d5 h1:o3w8M+Amz2IkgqI2/FqFUMkPDzoE7y3s3UV08H6/wWY=
github.com/okx/wasmvm v1.3.3-0.20230728083011-6960449ac9d5/go.mod h1:vW/E3h8j9xBQs9bCoijDuawKo9kCtxOaS8N8J7KFtkc=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down
6 changes: 4 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.W
env := types.NewEnv(ctx, contractAddress)
adapters := sdk.CoinsToCoinAdapters(deposit)
info := types.NewInfo(creator, adapters)
gasInfo := types.GetGasInfo(k.gasRegister.GetGasMultiplier())

// create prefixed data store
// 0x03 | BuildContractAddress (sdk.WasmAddress)
Expand All @@ -511,7 +512,7 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.W

// instantiate wasm contract
gas := k.runtimeGasForContract(ctx)
res, gasUsed, err := k.wasmVM.Instantiate(codeInfo.CodeHash, env, info, initMsg, prefixStoreAdapter, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, err := k.wasmVM.Instantiate(codeInfo.CodeHash, env, info, initMsg, prefixStoreAdapter, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization, gasInfo)
k.consumeRuntimeGas(ctx, gasUsed)
if !ctx.IsCheckTx() && k.innertxKeeper != nil {
k.innertxKeeper.UpdateWasmInnerTx(ctx.TxBytes(), ctx.BlockHeight(), innertx.CosmosDepth, creator, contractAddress, innertx.CosmosCallType, types.InstantiateInnertxName, sdk.Coins{}, err, k.gasRegister.FromWasmVMGas(gasUsed), string(initMsg))
Expand Down Expand Up @@ -579,6 +580,7 @@ func (k Keeper) execute(ctx sdk.Context, contractAddress sdk.WasmAddress, caller
env := types.NewEnv(ctx, contractAddress)
adapters := sdk.CoinsToCoinAdapters(coins)
info := types.NewInfo(caller, adapters)
gasInfo := types.GetGasInfo(k.gasRegister.GetGasMultiplier())

// prepare querier
querier := k.newQueryHandler(ctx, contractAddress)
Expand All @@ -596,7 +598,7 @@ func (k Keeper) execute(ctx sdk.Context, contractAddress sdk.WasmAddress, caller
}
}

res, gasUsed, execErr := k.wasmVM.Execute(codeInfo.CodeHash, env, info, msg, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.Execute(codeInfo.CodeHash, env, info, msg, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization, gasInfo)
k.consumeRuntimeGas(ctx, gasUsed)
if !ctx.IsCheckTx() && k.innertxKeeper != nil {
k.innertxKeeper.UpdateWasmInnerTx(ctx.TxBytes(), ctx.BlockHeight(), innertx.CosmosDepth, caller, contractAddress, innertx.CosmosCallType, types.ExecuteInnertxName, coins, err, k.gasRegister.FromWasmVMGas(gasUsed), string(msg))
Expand Down
4 changes: 2 additions & 2 deletions x/wasm/keeper/wasmtesting/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func (m *MockWasmer) UpdateMilestone(_ string, _ uint64) error {
return nil
}

func (m *MockWasmer) Instantiate(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
func (m *MockWasmer) Instantiate(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, initMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction, gasInfo wasmvmtypes.GasInfo) (*wasmvmtypes.Response, uint64, error) {
if m.InstantiateFn == nil {
panic("not supposed to be called!")
}
return m.InstantiateFn(codeID, env, info, initMsg, store, goapi, querier, gasMeter, gasLimit, deserCost)
}

func (m *MockWasmer) Execute(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, executeMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction) (*wasmvmtypes.Response, uint64, error) {
func (m *MockWasmer) Execute(codeID wasmvm.Checksum, env wasmvmtypes.Env, info wasmvmtypes.MessageInfo, executeMsg []byte, store wasmvm.KVStore, goapi wasmvm.GoAPI, querier wasmvm.Querier, gasMeter wasmvm.GasMeter, gasLimit uint64, deserCost wasmvmtypes.UFraction, gasInfo wasmvmtypes.GasInfo) (*wasmvmtypes.Response, uint64, error) {
if m.ExecuteFn == nil {
panic("not supposed to be called!")
}
Expand Down
20 changes: 16 additions & 4 deletions x/wasm/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
stypes "github.com/okex/exchain/libs/cosmos-sdk/store/types"
"reflect"
"strings"

Expand Down Expand Up @@ -131,10 +132,11 @@ func (c *ContractInfo) SetExtension(ext ContractInfoExtension) error {

// ReadExtension copies the extension value to the pointer passed as argument so that there is no need to cast
// For example with a custom extension of type `MyContractDetails` it will look as following:
// var d MyContractDetails
// if err := info.ReadExtension(&d); err != nil {
// return nil, sdkerrors.Wrap(err, "extension")
// }
//
// var d MyContractDetails
// if err := info.ReadExtension(&d); err != nil {
// return nil, sdkerrors.Wrap(err, "extension")
// }
func (c *ContractInfo) ReadExtension(e ContractInfoExtension) error {
rv := reflect.ValueOf(e)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
Expand Down Expand Up @@ -255,6 +257,16 @@ func (a *AbsoluteTxPosition) Bytes() []byte {
return r
}

func GetGasInfo(gasMultiplier uint64) wasmvmtypes.GasInfo {
gc := stypes.KVGasConfig()
return wasmvmtypes.GasInfo{
WriteCostFlat: gc.WriteCostFlat,
WriteCostPerByte: gc.ReadCostPerByte,
DeleteCost: gc.DeleteCost,
GasMultiplier: gasMultiplier,
}
}

// NewEnv initializes the environment for a contract instance
func NewEnv(ctx sdk.Context, contractAddr sdk.WasmAddress) wasmvmtypes.Env {
// safety checks before casting below
Expand Down
2 changes: 2 additions & 0 deletions x/wasm/types/wasmer_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type WasmerEngine interface {
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
gasInfo wasmvmtypes.GasInfo,
) (*wasmvmtypes.Response, uint64, error)

// Execute calls a given contract. Since the only difference between contracts with the same CodeID is the
Expand All @@ -66,6 +67,7 @@ type WasmerEngine interface {
gasMeter wasmvm.GasMeter,
gasLimit uint64,
deserCost wasmvmtypes.UFraction,
gasInfo wasmvmtypes.GasInfo,
) (*wasmvmtypes.Response, uint64, error)

// Query allows a client to execute a contract-specific query. If the result is not empty, it should be
Expand Down

0 comments on commit 9c907f8

Please sign in to comment.