Skip to content

create an interface that allows us to choose weather to use wasmer or wazero #626

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions internal/api/newvm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

import (
"fmt"

"github.com/CosmWasm/wasmvm/v2/internal/wasmer"

Check failure on line 6 in internal/api/newvm.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/CosmWasm/wasmvm/v2/internal/wasmer (-: build constraints exclude all Go files in internal/wasmer) (typecheck)
"github.com/CosmWasm/wasmvm/v2/types"
)

func NewVMWithRuntime(runtimeType string, dataDir string, capabilities map[string]struct{}, memoryLimit uint32, logger types.InfoLogger) (WasmVMRuntime, error) {
switch runtimeType {
case "wasmer":
// Create the VM instance (implementation details omitted for brevity)
vm, err := wasmer.NewVM(dataDir, capabilities, memoryLimit, logger)
if err != nil {
return nil, err
}
return wasmer.NewWasmerRuntime(vm), nil
default:
return nil, fmt.Errorf("unsupported runtime type: %s", runtimeType)
}
}
77 changes: 77 additions & 0 deletions internal/api/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package api

import (
"github.com/CosmWasm/wasmvm/v2/types"
)

// WasmVMRuntime defines the interface for Wasm VM runtimes (e.g., Wasmer, Wazero).
type WasmVMRuntime interface {
// NewVM initializes a new VM instance with the given configuration.
NewVM(dataDir string, supportedCapabilities map[string]struct{}, memoryLimit uint32, logger types.InfoLogger) error

// Close releases all resources held by the VM.
Close() error

// StoreCode compiles and stores Wasm code, returning its checksum and gas used.
StoreCode(code []byte, gasLimit uint64) (types.Checksum, uint64, error)

// SimulateStoreCode estimates gas for storing code without persisting it.
SimulateStoreCode(code []byte, gasLimit uint64) (types.Checksum, uint64, error)

// GetCode retrieves the Wasm code for a given checksum.
GetCode(checksum types.Checksum) ([]byte, error)

// RemoveCode removes code and its cached compiled module.
RemoveCode(checksum types.Checksum) error

// Pin ensures a module remains in memory (not evicted by cache).
Pin(checksum types.Checksum) error

// Unpin allows a module to be evicted from cache.
Unpin(checksum types.Checksum) error

// AnalyzeCode performs static analysis on the Wasm code.
AnalyzeCode(checksum types.Checksum) (*types.AnalysisReport, error)

// Instantiate calls the contract's instantiate entry point.
Instantiate(checksum types.Checksum, env types.Env, info types.MessageInfo, msg []byte, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64, deserCost types.UFraction) (*types.ContractResult, uint64, error)

// Execute calls the contract's execute entry point.
Execute(checksum types.Checksum, env types.Env, info types.MessageInfo, msg []byte, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64, deserCost types.UFraction) (*types.ContractResult, uint64, error)

// Query calls the contract's query entry point.
Query(checksum types.Checksum, env types.Env, queryMsg []byte, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.ContractResult, uint64, error)

// Migrate calls the contract's migrate entry point.
Migrate(checksum types.Checksum, env types.Env, migrateMsg []byte, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.ContractResult, uint64, error)

// Sudo calls the contract's sudo entry point.
Sudo(checksum types.Checksum, env types.Env, sudoMsg []byte, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.ContractResult, uint64, error)

// Reply calls the contract's reply entry point.
Reply(checksum types.Checksum, env types.Env, reply types.Reply, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.ContractResult, uint64, error)

// IBCChannelOpen calls the contract's IBC channel open entry point.
IBCChannelOpen(checksum types.Checksum, env types.Env, msg types.IBCChannelOpenMsg, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.IBCChannelOpenResult, uint64, error)

// IBCChannelConnect calls the contract's IBC channel connect entry point.
IBCChannelConnect(checksum types.Checksum, env types.Env, msg types.IBCChannelConnectMsg, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.IBCBasicResult, uint64, error)

// IBCChannelClose calls the contract's IBC channel close entry point.
IBCChannelClose(checksum types.Checksum, env types.Env, msg types.IBCChannelCloseMsg, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.IBCBasicResult, uint64, error)

// IBCPacketReceive calls the contract's IBC packet receive entry point.
IBCPacketReceive(checksum types.Checksum, env types.Env, msg types.IBCPacketReceiveMsg, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.IBCReceiveResult, uint64, error)

// IBCPacketAck calls the contract's IBC packet acknowledge entry point.
IBCPacketAck(checksum types.Checksum, env types.Env, msg types.IBCPacketAckMsg, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.IBCBasicResult, uint64, error)

// IBCPacketTimeout calls the contract's IBC packet timeout entry point.
IBCPacketTimeout(checksum types.Checksum, env types.Env, msg types.IBCPacketTimeoutMsg, store types.KVStore, api types.GoAPI, querier types.Querier, gasMeter types.GasMeter, gasLimit uint64) (*types.IBCBasicResult, uint64, error)

// GetMetrics returns cache usage metrics.
GetMetrics() (*types.Metrics, error)

// GetPinnedMetrics returns detailed metrics for pinned contracts.
GetPinnedMetrics() (*types.PinnedMetrics, error)
}
Loading
Loading