Skip to content

Commit

Permalink
chore(lib/runtime): remove imports function field (#2675)
Browse files Browse the repository at this point in the history
- Remove wasmer `Config` struct
- Use `runtime.InstanceConfig` as wasmer config
- Always use `ImportsNodeRuntime` (as it was)
- Remove `imports` field from `Instance`
- Change tests runtime log levels to critical
- Add comment about closing imports
- Unexport `ImportsNodeRuntime`
  • Loading branch information
qdm12 authored Jul 20, 2022
1 parent 1ec0d47 commit 70181cd
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 134 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 {
rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig

var err error
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
Expand Down
14 changes: 6 additions & 8 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
// QueryKeyValueChanges represents the key-value data inside a block storage
type QueryKeyValueChanges map[string]string

type wasmerInstanceFunc func(code []byte, cfg *wasmer.Config) (instance *wasmer.Instance, err error)
type wasmerInstanceFunc func(code []byte, cfg runtime.InstanceConfig) (instance *wasmer.Instance, err error)

// Service is an overhead layer that allows communication between the runtime,
// BABE session, and network service. It deals with the validation of transactions
Expand Down Expand Up @@ -265,15 +265,13 @@ func (s *Service) handleCodeSubstitution(

// 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 := &wasmer.Config{
Imports: wasmer.ImportsNodeRuntime,
cfg := runtime.InstanceConfig{
Storage: state,
Keystore: rt.Keystore(),
NodeStorage: rt.NodeStorage(),
Network: rt.NetworkService(),
}

cfg.Storage = state
cfg.Keystore = rt.Keystore()
cfg.NodeStorage = rt.NodeStorage()
cfg.Network = rt.NetworkService()

if rt.Validator() {
cfg.Role = 4
}
Expand Down
11 changes: 4 additions & 7 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.Acco
nodeStorage := runtime.NodeStorage{
BaseDB: runtime.NewInMemoryDB(t),
}
cfg := &wasmer.Config{
InstanceConfig: runtime.InstanceConfig{
Storage: genState,
LogLvl: log.Error,
NodeStorage: nodeStorage,
},
Imports: nil,
cfg := runtime.InstanceConfig{
Storage: genState,
LogLvl: log.Error,
NodeStorage: nodeStorage,
}

rt, err := wasmer.NewRuntimeFromGenesis(cfg)
Expand Down
2 changes: 1 addition & 1 deletion dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func Test_Service_StorageRoot(t *testing.T) {

func Test_Service_handleCodeSubstitution(t *testing.T) {
t.Parallel()
newTestInstance := func(code []byte, cfg *wasmer.Config) (*wasmer.Instance, error) {
newTestInstance := func(code []byte, cfg runtime.InstanceConfig) (*wasmer.Instance, error) {
return &wasmer.Instance{}, nil
}

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)

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
Expand Down
30 changes: 16 additions & 14 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ type useRuntimeInstace func(*testing.T, *storage.TrieState) runtime.Instance
func useInstanceFromGenesis(t *testing.T, rtStorage *storage.TrieState) (instance runtime.Instance) {
t.Helper()

cfg := &wasmer.Config{}
cfg.Storage = rtStorage
cfg.LogLvl = log.Warn
cfg.NodeStorage = runtime.NodeStorage{
BaseDB: runtime.NewInMemoryDB(t),
cfg := runtime.InstanceConfig{
Storage: rtStorage,
LogLvl: log.Warn,
NodeStorage: runtime.NodeStorage{
BaseDB: runtime.NewInMemoryDB(t),
},
}

runtimeInstance, err := wasmer.NewRuntimeFromGenesis(cfg)
Expand All @@ -61,15 +62,16 @@ func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (in

rtStorage.Set(common.CodeKey, bytes)

cfg := &wasmer.Config{}
cfg.Role = 0
cfg.LogLvl = log.Warn
cfg.Storage = rtStorage
cfg.Keystore = keystore.NewGlobalKeystore()
cfg.NodeStorage = runtime.NodeStorage{
LocalStorage: runtime.NewInMemoryDB(t),
PersistentStorage: runtime.NewInMemoryDB(t),
BaseDB: runtime.NewInMemoryDB(t),
cfg := runtime.InstanceConfig{
Role: 0,
LogLvl: log.Critical,
Storage: rtStorage,
Keystore: keystore.NewGlobalKeystore(),
NodeStorage: runtime.NodeStorage{
LocalStorage: runtime.NewInMemoryDB(t),
PersistentStorage: runtime.NewInMemoryDB(t),
BaseDB: runtime.NewInMemoryDB(t),
},
}

runtimeInstance, err := wasmer.NewInstanceFromTrie(rtStorage.Trie(), cfg)
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)

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
Expand Down
17 changes: 8 additions & 9 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,15 @@ func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service,
var rt runtime.Instance
switch cfg.Core.WasmInterpreter {
case wasmer.Name:
rtCfg := &wasmer.Config{
Imports: wasmer.ImportsNodeRuntime,
rtCfg := runtime.InstanceConfig{
Storage: ts,
Keystore: ks,
LogLvl: cfg.Log.RuntimeLvl,
NodeStorage: ns,
Network: net,
Role: cfg.Core.Roles,
CodeHash: codeHash,
}
rtCfg.Storage = ts
rtCfg.Keystore = ks
rtCfg.LogLvl = cfg.Log.RuntimeLvl
rtCfg.NodeStorage = ns
rtCfg.Network = net
rtCfg.Role = cfg.Core.Roles
rtCfg.CodeHash = codeHash

// create runtime executor
rt, err = wasmer.NewInstance(code, rtCfg)
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 @@ -74,7 +74,7 @@ func newStateServiceWithoutMock(t *testing.T) *state.Service {

stateSrvc.Epoch = epochState

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dot/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func newStateService(t *testing.T, ctrl *gomock.Controller) *state.Service {

stateSrvc.Epoch = epochState

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
Expand Down
14 changes: 6 additions & 8 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,16 +609,14 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
bHash, codeHash, previousVersion.SpecVersion(), currCodeHash, newVersion.SpecVersion())
}

rtCfg := &wasmer.Config{
Imports: wasmer.ImportsNodeRuntime,
rtCfg := runtime.InstanceConfig{
Storage: newState,
Keystore: rt.Keystore(),
NodeStorage: rt.NodeStorage(),
Network: rt.NetworkService(),
CodeHash: currCodeHash,
}

rtCfg.Storage = newState
rtCfg.Keystore = rt.Keystore()
rtCfg.NodeStorage = rt.NodeStorage()
rtCfg.Network = rt.NetworkService()
rtCfg.CodeHash = currCodeHash

if rt.Validator() {
rtCfg.Role = 4
}
Expand Down
7 changes: 4 additions & 3 deletions dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ func (s *Service) CreateGenesisRuntime(t *trie.Trie, gen *genesis.Genesis) (runt
}

// create genesis runtime
rtCfg := &wasmer.Config{}
rtCfg.Storage = genTrie
rtCfg.LogLvl = s.logLvl
rtCfg := runtime.InstanceConfig{
LogLvl: s.logLvl,
Storage: genTrie,
}

r, err := wasmer.NewRuntimeFromGenesis(rtCfg)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions dot/sync/syncer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func newTestSyncer(t *testing.T) *Service {
genState, err := rtstorage.NewTrieState(genTrie)
require.NoError(t, err)

rtCfg := &wasmer.Config{}
rtCfg.Storage = genState
rtCfg.LogLvl = 3
rtCfg.NodeStorage = runtime.NodeStorage{}
rtCfg := runtime.InstanceConfig{
Storage: genState,
LogLvl: log.Critical,
}

if stateSrvc != nil {
rtCfg.NodeStorage.BaseDB = stateSrvc.Base
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
}

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)

Expand Down Expand Up @@ -178,7 +178,7 @@ func newTestServiceSetupParameters(t *testing.T) (*Service, *state.EpochState, *
_ = dbSrv.Stop()
})

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
rt, err := wasmer.NewRuntimeFromGenesis(rtCfg)
Expand Down
3 changes: 2 additions & 1 deletion lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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 @@ -56,7 +57,7 @@ func newTestState(t *testing.T) *state.Service {
block, err := state.NewBlockStateFromGenesis(db, tries, testGenesisHeader, telemetryMock)
require.NoError(t, err)

rtCfg := &wasmer.Config{}
var rtCfg runtime.InstanceConfig

rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 70181cd

Please sign in to comment.