Upgrade to wasmvm 1.2.1 (#1245)
* Use wasmvm store adapter * Bump wasmvm to v1.2.1
This commit is contained in:
@@ -289,14 +289,14 @@ func (k Keeper) instantiate(
|
||||
// create prefixed data store
|
||||
// 0x03 | BuildContractAddressClassic (sdk.AccAddress)
|
||||
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
|
||||
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
|
||||
vmStore := types.NewStoreAdapter(prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey))
|
||||
|
||||
// prepare querier
|
||||
querier := k.newQueryHandler(ctx, contractAddress)
|
||||
|
||||
// instantiate wasm contract
|
||||
gas := k.runtimeGasForContract(ctx)
|
||||
res, gasUsed, err := k.wasmVM.Instantiate(codeInfo.CodeHash, env, info, initMsg, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
|
||||
res, gasUsed, err := k.wasmVM.Instantiate(codeInfo.CodeHash, env, info, initMsg, vmStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
|
||||
k.consumeRuntimeGas(ctx, gasUsed)
|
||||
if err != nil {
|
||||
return nil, nil, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
|
||||
@@ -428,9 +428,9 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
|
||||
querier := k.newQueryHandler(ctx, contractAddress)
|
||||
|
||||
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
|
||||
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
|
||||
vmStore := types.NewStoreAdapter(prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey))
|
||||
gas := k.runtimeGasForContract(ctx)
|
||||
res, gasUsed, err := k.wasmVM.Migrate(newCodeInfo.CodeHash, env, msg, &prefixStore, cosmwasmAPI, &querier, k.gasMeter(ctx), gas, costJSONDeserialization)
|
||||
res, gasUsed, err := k.wasmVM.Migrate(newCodeInfo.CodeHash, env, msg, vmStore, cosmwasmAPI, &querier, k.gasMeter(ctx), gas, costJSONDeserialization)
|
||||
k.consumeRuntimeGas(ctx, gasUsed)
|
||||
if err != nil {
|
||||
return nil, sdkerrors.Wrap(types.ErrMigrationFailed, err.Error())
|
||||
@@ -705,25 +705,26 @@ func (k Keeper) QueryRaw(ctx sdk.Context, contractAddress sdk.AccAddress, key []
|
||||
return prefixStore.Get(key)
|
||||
}
|
||||
|
||||
func (k Keeper) contractInstance(ctx sdk.Context, contractAddress sdk.AccAddress) (types.ContractInfo, types.CodeInfo, prefix.Store, error) {
|
||||
// internal helper function
|
||||
func (k Keeper) contractInstance(ctx sdk.Context, contractAddress sdk.AccAddress) (types.ContractInfo, types.CodeInfo, wasmvm.KVStore, error) {
|
||||
store := ctx.KVStore(k.storeKey)
|
||||
|
||||
contractBz := store.Get(types.GetContractAddressKey(contractAddress))
|
||||
if contractBz == nil {
|
||||
return types.ContractInfo{}, types.CodeInfo{}, prefix.Store{}, sdkerrors.Wrap(types.ErrNotFound, "contract")
|
||||
return types.ContractInfo{}, types.CodeInfo{}, nil, sdkerrors.Wrap(types.ErrNotFound, "contract")
|
||||
}
|
||||
var contractInfo types.ContractInfo
|
||||
k.cdc.MustUnmarshal(contractBz, &contractInfo)
|
||||
|
||||
codeInfoBz := store.Get(types.GetCodeKey(contractInfo.CodeID))
|
||||
if codeInfoBz == nil {
|
||||
return contractInfo, types.CodeInfo{}, prefix.Store{}, sdkerrors.Wrap(types.ErrNotFound, "code info")
|
||||
return contractInfo, types.CodeInfo{}, nil, sdkerrors.Wrap(types.ErrNotFound, "code info")
|
||||
}
|
||||
var codeInfo types.CodeInfo
|
||||
k.cdc.MustUnmarshal(codeInfoBz, &codeInfo)
|
||||
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
|
||||
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
|
||||
return contractInfo, codeInfo, prefixStore, nil
|
||||
return contractInfo, codeInfo, types.NewStoreAdapter(prefixStore), nil
|
||||
}
|
||||
|
||||
func (k Keeper) GetContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress) *types.ContractInfo {
|
||||
|
||||
@@ -3,6 +3,7 @@ package types
|
||||
import (
|
||||
wasmvm "github.com/CosmWasm/wasmvm"
|
||||
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// DefaultMaxQueryStackSize maximum size of the stack of contract instances doing queries
|
||||
@@ -239,3 +240,38 @@ type WasmerEngine interface {
|
||||
// GetMetrics some internal metrics for monitoring purposes.
|
||||
GetMetrics() (*wasmvmtypes.Metrics, error)
|
||||
}
|
||||
|
||||
var _ wasmvm.KVStore = &StoreAdapter{}
|
||||
|
||||
// StoreAdapter adapter to bridge SDK store impl to wasmvm
|
||||
type StoreAdapter struct {
|
||||
parent sdk.KVStore
|
||||
}
|
||||
|
||||
// NewStoreAdapter constructor
|
||||
func NewStoreAdapter(s sdk.KVStore) *StoreAdapter {
|
||||
if s == nil {
|
||||
panic("store must not be nil")
|
||||
}
|
||||
return &StoreAdapter{parent: s}
|
||||
}
|
||||
|
||||
func (s StoreAdapter) Get(key []byte) []byte {
|
||||
return s.parent.Get(key)
|
||||
}
|
||||
|
||||
func (s StoreAdapter) Set(key, value []byte) {
|
||||
s.parent.Set(key, value)
|
||||
}
|
||||
|
||||
func (s StoreAdapter) Delete(key []byte) {
|
||||
s.parent.Delete(key)
|
||||
}
|
||||
|
||||
func (s StoreAdapter) Iterator(start, end []byte) wasmvmtypes.Iterator {
|
||||
return s.parent.Iterator(start, end)
|
||||
}
|
||||
|
||||
func (s StoreAdapter) ReverseIterator(start, end []byte) wasmvmtypes.Iterator {
|
||||
return s.parent.ReverseIterator(start, end)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user