Skip to content

Commit

Permalink
chore(runtime): move instance config struct to lib/runtime/wasmer (#…
Browse files Browse the repository at this point in the history
…2751)

Move `InstanceConfig` from `lib/runtime` to `lib/runtime/wasmer` as `Config`
  • Loading branch information
qdm12 authored Aug 16, 2022
1 parent 2444d0b commit 8b0f6f0
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 51 deletions.
2 changes: 1 addition & 1 deletion dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
}

if cfg.Runtime == nil {
var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config

rtCfg.Storage = rtstorage.NewTrieState(genTrie)

Expand Down
2 changes: 1 addition & 1 deletion dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,

// this needs to create a new runtime instance, otherwise it will update
// the blocks that reference the current runtime version to use the code substition
cfg := runtime.InstanceConfig{
cfg := wasmer.Config{
Storage: state,
Keystore: rt.Keystore(),
NodeStorage: rt.NodeStorage(),
Expand Down
2 changes: 1 addition & 1 deletion dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.Acco
nodeStorage := runtime.NodeStorage{
BaseDB: runtime.NewInMemoryDB(t),
}
cfg := runtime.InstanceConfig{
cfg := wasmer.Config{
Storage: genState,
LogLvl: log.Error,
NodeStorage: nodeStorage,
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func newCoreServiceTest(t *testing.T) *core.Service {
err = cfg.Keystore.Acco.Insert(kp)
require.NoError(t, err)

var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config

rtCfg.Storage = rtstorage.NewTrieState(genTrie)

Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type useRuntimeInstace func(*testing.T, *storage.TrieState) runtime.Instance
func useInstanceFromGenesis(t *testing.T, rtStorage *storage.TrieState) (instance runtime.Instance) {
t.Helper()

cfg := runtime.InstanceConfig{
cfg := wasmer.Config{
Storage: rtStorage,
LogLvl: log.Warn,
NodeStorage: runtime.NodeStorage{
Expand All @@ -62,7 +62,7 @@ func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (in

rtStorage.Set(common.CodeKey, bytes)

cfg := runtime.InstanceConfig{
cfg := wasmer.Config{
Role: 0,
LogLvl: log.Critical,
Storage: rtStorage,
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/chain_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func newTestStateService(t *testing.T) *state.Service {
err = stateSrvc.Start()
require.NoError(t, err)

var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config

rtCfg.Storage = rtstorage.NewTrieState(genTrie)

Expand Down
2 changes: 1 addition & 1 deletion dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service,
var rt runtime.Instance
switch cfg.Core.WasmInterpreter {
case wasmer.Name:
rtCfg := runtime.InstanceConfig{
rtCfg := wasmer.Config{
Storage: ts,
Keystore: ks,
LogLvl: cfg.Log.RuntimeLvl,
Expand Down
2 changes: 1 addition & 1 deletion dot/services_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func newStateServiceWithoutMock(t *testing.T) *state.Service {

stateSrvc.Epoch = epochState

var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config

rtCfg.Storage = rtstorage.NewTrieState(genTrie)

Expand Down
2 changes: 1 addition & 1 deletion dot/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func newStateService(t *testing.T, ctrl *gomock.Controller) *state.Service {

stateSrvc.Epoch = epochState

var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config

rtCfg.Storage = rtstorage.NewTrieState(genTrie)

Expand Down
2 changes: 1 addition & 1 deletion dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
bHash, codeHash, previousVersion.SpecVersion(), currCodeHash, newVersion.SpecVersion())
}

rtCfg := runtime.InstanceConfig{
rtCfg := wasmer.Config{
Storage: newState,
Keystore: rt.Keystore(),
NodeStorage: rt.NodeStorage(),
Expand Down
2 changes: 1 addition & 1 deletion dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *Service) CreateGenesisRuntime(t *trie.Trie, gen *genesis.Genesis) (runt
genTrie := rtstorage.NewTrieState(t)

// create genesis runtime
rtCfg := runtime.InstanceConfig{
rtCfg := wasmer.Config{
LogLvl: s.logLvl,
Storage: genTrie,
}
Expand Down
3 changes: 1 addition & 2 deletions dot/sync/syncer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"
Expand Down Expand Up @@ -64,7 +63,7 @@ func newTestSyncer(t *testing.T) *Service {
// initialise runtime
genState := rtstorage.NewTrieState(genTrie)

rtCfg := runtime.InstanceConfig{
rtCfg := wasmer.Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down
4 changes: 2 additions & 2 deletions lib/babe/babe_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service {
cfg.EpochState = dbSrv.Epoch
}

var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config
rtCfg.Storage = rtstorage.NewTrieState(genTrie)

storageState := cfg.StorageState.(core.StorageState)
Expand Down Expand Up @@ -177,7 +177,7 @@ func newTestServiceSetupParameters(t *testing.T) (*Service, *state.EpochState, *
_ = dbSrv.Stop()
})

rtCfg := runtime.InstanceConfig{
rtCfg := wasmer.Config{
Storage: rtstorage.NewTrieState(genTrie),
}

Expand Down
3 changes: 1 addition & 2 deletions lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"
Expand Down Expand Up @@ -57,7 +56,7 @@ func newTestState(t *testing.T) *state.Service {
block, err := state.NewBlockStateFromGenesis(db, tries, testGenesisHeader, telemetryMock)
require.NoError(t, err)

var rtCfg runtime.InstanceConfig
var rtCfg wasmer.Config

rtCfg.Storage = rtstorage.NewTrieState(genTrie)

Expand Down
14 changes: 0 additions & 14 deletions lib/runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package runtime

import (
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime/offchain"
Expand Down Expand Up @@ -47,18 +45,6 @@ func (n *NodeStorage) GetPersistent(k []byte) ([]byte, error) {
return n.PersistentStorage.Get(k)
}

// InstanceConfig represents a runtime instance configuration
type InstanceConfig struct {
Storage Storage
Keystore *keystore.GlobalKeystore
LogLvl log.Level
Role common.Roles
NodeStorage NodeStorage
Network BasicNetwork
Transaction TransactionState
CodeHash common.Hash
}

// Context is the context for the wasm interpreter's imported functions
type Context struct {
Storage Storage
Expand Down
24 changes: 24 additions & 0 deletions lib/runtime/wasmer/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package wasmer

import (
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
)

// Config is the configuration used to create a
// Wasmer runtime instance.
type Config struct {
Storage runtime.Storage
Keystore *keystore.GlobalKeystore
LogLvl log.Level
Role common.Roles
NodeStorage runtime.NodeStorage
Network runtime.BasicNetwork
Transaction runtime.TransactionState
CodeHash common.Hash
}
24 changes: 12 additions & 12 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestInstance_Version_KusamaRuntime(t *testing.T) {
// set state to genesis state
genState := storage.NewTrieState(genTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -296,7 +296,7 @@ func TestNodeRuntime_ValidateTransaction(t *testing.T) {
// set state to genesis state
genState := storage.NewTrieState(genTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -526,7 +526,7 @@ func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) {
// set state to genesis state
genState := storage.NewTrieState(genTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -556,7 +556,7 @@ func TestInstance_ApplyExtrinsic_GossamerRuntime(t *testing.T) {
// set state to genesis state
genState := storage.NewTrieState(genTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -616,7 +616,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) {
// set state to genesis state
genState := storage.NewTrieState(genTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -668,7 +668,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) {
// set state to genesis state
genState := storage.NewTrieState(genTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: genState,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -714,7 +714,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock3784(t *testing.T) {
// set state to genesis state
state3783 := storage.NewTrieState(gossTrie3783)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: state3783,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -760,7 +760,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock901442(t *testing.T) {
// set state to genesis state
state901441 := storage.NewTrieState(ksmTrie901441)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: state901441,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -806,7 +806,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1377831(t *testing.T) {
// set state to genesis state
state := storage.NewTrieState(ksmTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: state,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -852,7 +852,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1482003(t *testing.T) {
// set state to genesis state
state := storage.NewTrieState(ksmTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: state,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -900,7 +900,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock4939774(t *testing.T) {
// set state to genesis state
state := storage.NewTrieState(ksmTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: state,
LogLvl: log.Critical,
}
Expand Down Expand Up @@ -943,7 +943,7 @@ func TestInstance_ExecuteBlock_PolkadotBlock1089328(t *testing.T) {
// set state to genesis state
state := storage.NewTrieState(dotTrie)

cfg := runtime.InstanceConfig{
cfg := Config{
Storage: state,
LogLvl: log.Critical,
}
Expand Down
10 changes: 5 additions & 5 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Instance struct {
}

// NewRuntimeFromGenesis creates a runtime instance from the genesis data
func NewRuntimeFromGenesis(cfg runtime.InstanceConfig) (instance runtime.Instance, err error) {
func NewRuntimeFromGenesis(cfg Config) (instance runtime.Instance, err error) {
if cfg.Storage == nil {
return nil, errors.New("storage is nil")
}
Expand All @@ -61,7 +61,7 @@ func NewRuntimeFromGenesis(cfg runtime.InstanceConfig) (instance runtime.Instanc
}

// NewInstanceFromTrie returns a new runtime instance with the code provided in the given trie
func NewInstanceFromTrie(t *trie.Trie, cfg runtime.InstanceConfig) (*Instance, error) {
func NewInstanceFromTrie(t *trie.Trie, cfg Config) (*Instance, error) {
code := t.Get(common.CodeKey)
if len(code) == 0 {
return nil, fmt.Errorf("cannot find :code in trie")
Expand All @@ -71,7 +71,7 @@ func NewInstanceFromTrie(t *trie.Trie, cfg runtime.InstanceConfig) (*Instance, e
}

// NewInstanceFromFile instantiates a runtime from a .wasm file
func NewInstanceFromFile(fp string, cfg runtime.InstanceConfig) (*Instance, error) {
func NewInstanceFromFile(fp string, cfg Config) (*Instance, error) {
// Reads the WebAssembly module as bytes.
bytes, err := wasm.ReadBytes(fp)
if err != nil {
Expand All @@ -82,7 +82,7 @@ func NewInstanceFromFile(fp string, cfg runtime.InstanceConfig) (*Instance, erro
}

// NewInstance instantiates a runtime from raw wasm bytecode
func NewInstance(code []byte, cfg runtime.InstanceConfig) (instance *Instance, err error) {
func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
logger.Patch(log.SetLevel(cfg.LogLvl), log.SetCallerFunc(true))

wasmInstance, allocator, err := setupVM(code)
Expand Down Expand Up @@ -159,7 +159,7 @@ func (in *Instance) UpdateRuntimeCode(code []byte) (err error) {
// GetRuntimeVersion finds the runtime version by initiating a temporary
// runtime instance using the WASM code provided, and querying it.
func GetRuntimeVersion(code []byte) (version runtime.Version, err error) {
config := runtime.InstanceConfig{
config := Config{
LogLvl: log.DoNotChange,
}
instance, err := NewInstance(code, config)
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/wasmer/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie)
return r
}

func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role common.Roles) runtime.InstanceConfig {
func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role common.Roles) Config {
t.Helper()

s := storage.NewTrieState(tt)
Expand All @@ -52,7 +52,7 @@ func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role common.Roles)
BaseDB: runtime.NewInMemoryDB(t), // we're using a local storage here since this is a test runtime
}

return runtime.InstanceConfig{
return Config{
Storage: s,
Keystore: keystore.NewGlobalKeystore(),
LogLvl: lvl,
Expand Down

0 comments on commit 8b0f6f0

Please sign in to comment.