Skip to content

Commit

Permalink
add gas table (#3231)
Browse files Browse the repository at this point in the history
* add gas table

* change depend wasmvm
  • Loading branch information
chengzhinei authored Jul 28, 2023
1 parent f438319 commit 233688f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 53 deletions.
10 changes: 2 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.8.1
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954
github.com/tendermint/btcd v0.1.1
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15
github.com/tendermint/go-amino v0.15.1
github.com/tendermint/tm-db v0.6.7
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef
github.com/valyala/fastjson v1.6.3
github.com/willf/bitset v1.1.11
Expand All @@ -96,7 +95,6 @@ require (
)

require (
github.com/DataDog/zstd v1.4.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
Expand All @@ -105,17 +103,12 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cosmos/ledger-go v0.9.2 // indirect
github.com/danieljoos/wincred v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/go-kit/log v0.2.0 // indirect
Expand Down Expand Up @@ -183,6 +176,7 @@ require (
)

replace (
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
40 changes: 6 additions & 34 deletions go.sum

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,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 @@ -503,7 +504,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 @@ -571,6 +572,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 @@ -588,7 +590,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 @@ -94,14 +94,14 @@ func (m *MockWasmer) AnalyzeCode(codeID wasmvm.Checksum) (*wasmvmtypes.AnalysisR
return m.AnalyzeCodeFn(codeID)
}

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
6 changes: 3 additions & 3 deletions x/wasm/types/store_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package types

import (
"fmt"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"

"github.com/okex/exchain/libs/cosmos-sdk/types"
dbm "github.com/tendermint/tm-db"
)

type StoreAdapter struct {
Expand All @@ -30,7 +30,7 @@ func (sa StoreAdapter) Delete(key []byte) {
// Start must be less than end, or the Iterator is invalid.
// Iterator must be closed by caller.
// To iterate over entire domain, use store.Iterator(nil, nil)
func (sa StoreAdapter) Iterator(start, end []byte) dbm.Iterator {
func (sa StoreAdapter) Iterator(start, end []byte) wasmvmtypes.Iterator {
iter := sa.parent.Iterator(start, end)
adapter := newIteratorAdapter(iter)
return adapter
Expand All @@ -39,7 +39,7 @@ func (sa StoreAdapter) Iterator(start, end []byte) dbm.Iterator {
// Iterator over a domain of keys in descending order. End is exclusive.
// Start must be less than end, or the Iterator is invalid.
// Iterator must be closed by caller.
func (sa StoreAdapter) ReverseIterator(start, end []byte) dbm.Iterator {
func (sa StoreAdapter) ReverseIterator(start, end []byte) wasmvmtypes.Iterator {
iter := sa.parent.ReverseIterator(start, end)
adapter := newIteratorAdapter(iter)
return adapter
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 @@ -43,6 +43,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 @@ -62,6 +63,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 233688f

Please sign in to comment.