-
Notifications
You must be signed in to change notification settings - Fork 182
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 (#3231) * add gas table * change depend wasmvm * update (#3230) * wasmvm130 create contract (#3229) * adapt to wasmvm v1.2.4 * go mod tidy * support create contract by contract * support create2 * support create contract by codeID * change contract address2 * update wasmvm version * change GoAPI as local var: fix create smb * add CallCreateDepth * fix merge error --------- Co-authored-by: ylsGit <[email protected]> Co-authored-by: ylsGit <[email protected]> * wasmvm cross call (#3232) * adapt to wasmvm v1.2.4 * go mod tidy * switch to wasmvm v1.2.4 * replace the dependce * add file * add ut * add transfer * modity the go.mod for recursion * add getCallerInfoFunc transferCoinsFunc in api * merge --------- Co-authored-by: ylsGit <[email protected]> Co-authored-by: ylsGit <[email protected]> * fix merge error (#3233) * update gomod (#3235) * support the initance for delcall (#3239) * add venus7 for wasm upgrade (#3243) * add venus7 for wasm upgrade * upgrade go mod * upgrade 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]> Co-authored-by: ylsGit <[email protected]> Co-authored-by: lyh169 <[email protected]> Co-authored-by: lyh169 <[email protected]>
- Loading branch information
1 parent
2bee5f9
commit 3301eae
Showing
17 changed files
with
400 additions
and
74 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/okex/exchain/libs/cosmos-sdk/types" | ||
"github.com/okex/exchain/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/okex/exchain/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.