Skip to content

Commit

Permalink
Expose VMConfig constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
chipshort committed Oct 12, 2024
1 parent 7250c10 commit ec3a0a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion internal/api/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
Expand Down Expand Up @@ -49,7 +50,7 @@ func InitCache(config types.VMConfig) (Cache, error) {
return Cache{}, fmt.Errorf("Could not create base directory")
}

lockfile, err := os.OpenFile(config.Cache.BaseDir+"/exclusive.lock", os.O_WRONLY|os.O_CREATE, 0o666)
lockfile, err := os.OpenFile(filepath.Join(config.Cache.BaseDir, "exclusive.lock"), os.O_WRONLY|os.O_CREATE, 0o666)
if err != nil {
return Cache{}, fmt.Errorf("Could not open exclusive.lock")
}
Expand Down
12 changes: 11 additions & 1 deletion lib_libwasmvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type VM struct {
// `cacheSize` sets the size in MiB of an in-memory cache for e.g. module caching. Set to 0 to disable.
// `deserCost` sets the gas cost of deserializing one byte of data.
func NewVM(dataDir string, supportedCapabilities []string, memoryLimit uint32, printDebug bool, cacheSize uint32) (*VM, error) {
// TODO: expose config variant of NewVM
cache, err := api.InitCache(types.VMConfig{
Cache: types.CacheOptions{
BaseDir: dataDir,
Expand All @@ -45,6 +44,17 @@ func NewVM(dataDir string, supportedCapabilities []string, memoryLimit uint32, p
return &VM{cache: cache, printDebug: printDebug}, nil
}

// NewVMWithConfig creates a new VM with a custom configuration.
// This allows for more fine-grained control over the VM's behavior compared to NewVM and
// can be extended more easily in the future.
func NewVMWithConfig(config types.VMConfig, printDebug bool) (*VM, error) {
cache, err := api.InitCache(config)
if err != nil {
return nil, err
}
return &VM{cache: cache, printDebug: printDebug}, nil
}

// Cleanup should be called when no longer using this instances.
// It frees resources in libwasmvm (the Rust part) and releases a lock in the base directory.
func (vm *VM) Cleanup() {
Expand Down
3 changes: 3 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"encoding/json"
)

// VMConfig defines the configuration for the VM.
// For full documentation see the Rust side:
// https://github.com/CosmWasm/cosmwasm/blob/main/packages/vm/src/config.rs#L27
type VMConfig struct {
WasmLimits WasmLimits `json:"wasm_limits"`
Cache CacheOptions `json:"cache"`
Expand Down

0 comments on commit ec3a0a7

Please sign in to comment.