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

add callback for cross call #3187

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,5 @@ replace (
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
github.com/tendermint/go-amino => github.com/okex/go-amino v0.15.1-okc4
github.com/CosmWasm/wasmvm => /Volumes/E/project/github.com/okx/wasmvm
)
73 changes: 73 additions & 0 deletions x/wasm/keeper/cross_contarct_call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package keeper

import "C"

import (
"errors"
wasmvm "github.com/CosmWasm/wasmvm"
"github.com/CosmWasm/wasmvm/api"

Check failure on line 8 in x/wasm/keeper/cross_contarct_call.go

View workflow job for this annotation

GitHub Actions / x ut

github.com/CosmWasm/[email protected]: replacement directory /Volumes/E/project/github.com/okx/wasmvm does not exist

Check failure on line 8 in x/wasm/keeper/cross_contarct_call.go

View workflow job for this annotation

GitHub Actions / x ut

github.com/CosmWasm/[email protected]: replacement directory /Volumes/E/project/github.com/okx/wasmvm does not exist

Check failure on line 8 in x/wasm/keeper/cross_contarct_call.go

View workflow job for this annotation

GitHub Actions / app ut

github.com/CosmWasm/[email protected]: replacement directory /Volumes/E/project/github.com/okx/wasmvm does not exist

Check failure on line 8 in x/wasm/keeper/cross_contarct_call.go

View workflow job for this annotation

GitHub Actions / app ut

github.com/CosmWasm/[email protected]: replacement directory /Volumes/E/project/github.com/okx/wasmvm does not exist

Check failure on line 8 in x/wasm/keeper/cross_contarct_call.go

View workflow job for this annotation

GitHub Actions / libs tm ut

github.com/CosmWasm/[email protected]: replacement directory /Volumes/E/project/github.com/okx/wasmvm does not exist

Check failure on line 8 in x/wasm/keeper/cross_contarct_call.go

View workflow job for this annotation

GitHub Actions / libs tm ut

github.com/CosmWasm/[email protected]: replacement directory /Volumes/E/project/github.com/okx/wasmvm does not exist
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"unsafe"
)

var (
wasmKeeper Keeper

// wasmvm cache param
filePath string
supportedFeatures string
contractMemoryLimit uint32 = ContractMemoryLimit
contractDebugMode bool
memoryCacheSize uint32

wasmCache api.Cache
)

func SetWasmKeeper(k *Keeper) {
wasmKeeper = *k
}

func SetWasmCache(cache api.Cache) {
wasmCache = cache
}

func GetWasmCacheInfo() (wasmvm.GoAPI, api.Cache) {
return cosmwasmAPI, wasmCache
}

func GetWasmCallInfo(q unsafe.Pointer, contractAddress, storeAddress string) ([]byte, wasmvm.KVStore, wasmvm.Querier, wasmvm.GasMeter, error) {
goQuerier := *(*wasmvm.Querier)(q)
qq, ok := goQuerier.(QueryHandler)
if !ok {
return nil, nil, nil, nil, errors.New("can not switch the pointer to the QueryHandler")
}
return getCallerInfo(qq.Ctx, contractAddress, storeAddress)
}

func getCallerInfo(ctx sdk.Context, contractAddress, storeAddress string) ([]byte, wasmvm.KVStore, wasmvm.Querier, wasmvm.GasMeter, error) {
cAddr, err := sdk.WasmAddressFromBech32(contractAddress)
if err != nil {
return nil, nil, nil, nil, err
}
// 1. get wasm code from contractAddress
_, codeInfo, prefixStore, err := wasmKeeper.contractInstance(ctx, cAddr)
if err != nil {
return nil, nil, nil, nil, err
}
// 2. contractAddress == storeAddress and direct return
if contractAddress == storeAddress {
queryHandler := wasmKeeper.newQueryHandler(ctx, cAddr)
return codeInfo.CodeHash, prefixStore, queryHandler, wasmKeeper.gasMeter(ctx), nil
}
// 3. get store from storeaddress
sAddr, err := sdk.WasmAddressFromBech32(storeAddress)
if err != nil {
return nil, nil, nil, nil, err
}
_, _, prefixStore, err = wasmKeeper.contractInstance(ctx, sAddr)
if err != nil {
return nil, nil, nil, nil, err
}
queryHandler := wasmKeeper.newQueryHandler(ctx, sAddr)
return codeInfo.CodeHash, prefixStore, queryHandler, wasmKeeper.gasMeter(ctx), nil
}
8 changes: 8 additions & 0 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package keeper

import "C"
import (
"bytes"
"context"
Expand All @@ -12,6 +13,7 @@ import (
"strings"

wasmvm "github.com/CosmWasm/wasmvm"
wasmapi "github.com/CosmWasm/wasmvm/api"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/gogo/protobuf/proto"

Expand Down Expand Up @@ -211,6 +213,12 @@ func newKeeper(cdc *codec.CodecProxy,
}
// not updateable, yet
keeper.wasmVMResponseHandler = NewDefaultWasmVMContractResponseHandler(NewMessageDispatcher(keeper.messenger, keeper))

// register
wasmapi.RegisterGetWasmCallInfo(GetWasmCallInfo)
wasmapi.RegisterGetWasmCacheInfo(GetWasmCacheInfo)
SetWasmKeeper(keeper)
SetWasmCache(wasmer.GetCache())
return *keeper
}

Expand Down
Loading