-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add gas table (#201) * add gas table * change depend wasmvm * upgrade wasmvm (#199) * update * update * update * wasm create contract (#200) * wasm create contract * fix merge error * Update go.mod * wasmvm cross call (#203) * add wasm cross call * merge * fix merge error (#204) * update gomod (#205) * support the initance for delcall (#207) * add venus7 for wasm upgrade (#210) * add venus7 for wasm upgrade * update go.mod * update go.mod * upgrade go mod wasmvm version * fix ut --------- Co-authored-by: chengzhinei <[email protected]> Co-authored-by: JianGuo <[email protected]> Co-authored-by: ylsGit <[email protected]>
- Loading branch information
1 parent
cecd23d
commit bcdc8ed
Showing
17 changed files
with
388 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/json" | ||
wasmvm "github.com/CosmWasm/wasmvm" | ||
sdk "github.com/okx/okbchain/libs/cosmos-sdk/types" | ||
"github.com/okx/okbchain/x/wasm/types" | ||
) | ||
|
||
var ( | ||
wasmCache wasmvm.Cache | ||
) | ||
|
||
func SetWasmCache(cache wasmvm.Cache) { | ||
wasmCache = cache | ||
} | ||
|
||
func GetWasmCacheInfo() wasmvm.Cache { | ||
return wasmCache | ||
} | ||
|
||
func getCallerInfoFunc(ctx sdk.Context, keeper Keeper) func(contractAddress, storeAddress string) ([]byte, uint64, wasmvm.KVStore, wasmvm.Querier, wasmvm.GasMeter, error) { | ||
return func(contractAddress, storeAddress string) ([]byte, uint64, wasmvm.KVStore, wasmvm.Querier, wasmvm.GasMeter, error) { | ||
gasBefore := ctx.GasMeter().GasConsumed() | ||
codeHash, store, querier, gasMeter, err := getCallerInfo(ctx, keeper, contractAddress, storeAddress) | ||
gasAfter := ctx.GasMeter().GasConsumed() | ||
return codeHash, keeper.gasRegister.ToWasmVMGas(gasAfter - gasBefore), store, querier, gasMeter, err | ||
} | ||
} | ||
|
||
func getCallerInfo(ctx sdk.Context, keeper Keeper, 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 := keeper.contractInstance(ctx, cAddr) | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
// 2. contractAddress == storeAddress and direct return | ||
if contractAddress == storeAddress { | ||
queryHandler := keeper.newQueryHandler(ctx, cAddr) | ||
return codeInfo.CodeHash, prefixStore, queryHandler, keeper.gasMeter(ctx), nil | ||
} | ||
// 3. get store from storeaddress | ||
sAddr, err := sdk.WasmAddressFromBech32(storeAddress) | ||
if err != nil { | ||
return nil, nil, nil, nil, err | ||
} | ||
prefixStore = types.NewStoreAdapter(keeper.getStorageStore(ctx, sAddr)) | ||
queryHandler := keeper.newQueryHandler(ctx, sAddr) | ||
return codeInfo.CodeHash, prefixStore, queryHandler, keeper.gasMeter(ctx), nil | ||
} | ||
|
||
func transferCoinsFunc(ctx sdk.Context, keeper Keeper) func(contractAddress, caller string, coinsData []byte) (uint64, error) { | ||
return func(contractAddress, caller string, coinsData []byte) (uint64, error) { | ||
var coins sdk.Coins | ||
err := json.Unmarshal(coinsData, &coins) | ||
if err != nil { | ||
return 0, err | ||
} | ||
gasBefore := ctx.GasMeter().GasConsumed() | ||
err = transferCoins(ctx, keeper, contractAddress, caller, coins) | ||
gasAfter := ctx.GasMeter().GasConsumed() | ||
return keeper.gasRegister.ToWasmVMGas(gasAfter - gasBefore), err | ||
} | ||
} | ||
|
||
func transferCoins(ctx sdk.Context, keeper Keeper, contractAddress, caller string, coins sdk.Coins) error { | ||
if !coins.IsZero() { | ||
contractAddr, err := sdk.WasmAddressFromBech32(contractAddress) | ||
if err != nil { | ||
return err | ||
} | ||
callerAddr, err := sdk.WasmAddressFromBech32(caller) | ||
if err != nil { | ||
return err | ||
} | ||
if err := keeper.bank.TransferCoins(ctx, callerAddr, contractAddr, coins); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package keeper | ||
|
||
import ( | ||
"encoding/json" | ||
sdk "github.com/okx/okbchain/libs/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestGetWasmCallInfo(t *testing.T) { | ||
ctx, keepers := CreateTestInput(t, false, SupportedFeatures) | ||
keeper := keepers.ContractKeeper | ||
|
||
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000)) | ||
creator := keepers.Faucet.NewFundedAccount(ctx, deposit...) | ||
|
||
codeID, err := keeper.Create(ctx, creator, hackatomWasm, nil) | ||
require.NoError(t, err) | ||
|
||
_, _, bob := keyPubAddr() | ||
_, _, fred := keyPubAddr() | ||
|
||
initMsg := HackatomExampleInitMsg{ | ||
Verifier: fred, | ||
Beneficiary: bob, | ||
} | ||
initMsgBz, err := json.Marshal(initMsg) | ||
require.NoError(t, err) | ||
|
||
em := sdk.NewEventManager() | ||
// create with no balance is also legal | ||
ctx.SetEventManager(em) | ||
gotContractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "demo contract 1", nil) | ||
require.NoError(t, err) | ||
|
||
wasmkeeper := *keepers.WasmKeeper | ||
// 1. contractAddress is equal to storeAddress | ||
_, _, _, _, err = getCallerInfo(ctx, wasmkeeper, gotContractAddr.String(), gotContractAddr.String()) | ||
require.NoError(t, err) | ||
|
||
// 2. contractAddress is not exist | ||
_, _, _, _, err = getCallerInfo(ctx, wasmkeeper, "0xE70e7466a2f18FAd8C97c45Ba8fEc57d90F3435E", "0xE70e7466a2f18FAd8C97c45Ba8fEc57d90F3435E") | ||
require.NotNil(t, err) | ||
|
||
// 3. storeAddress is not exist | ||
_, _, _, _, err = getCallerInfo(ctx, wasmkeeper, gotContractAddr.String(), "0xE70e7466a2f18FAd8C97c45Ba8fEc57d90F3435E") | ||
require.NoError(t, err) | ||
|
||
// 4. contractAddress is not equal to storeAddress | ||
gotContractAddr2, _, err := keepers.ContractKeeper.Instantiate(ctx, codeID, creator, nil, initMsgBz, "demo contract 1", nil) | ||
_, kvs, q, _, err := getCallerInfo(ctx, wasmkeeper, gotContractAddr.String(), gotContractAddr2.String()) | ||
require.NoError(t, err) | ||
require.NotNil(t, kvs) | ||
require.NotNil(t, q) | ||
} |
Oops, something went wrong.