Skip to content

Commit 8b0f6f0

Browse files
authored
chore(runtime): move instance config struct to lib/runtime/wasmer (#2751)
Move `InstanceConfig` from `lib/runtime` to `lib/runtime/wasmer` as `Config`
1 parent 2444d0b commit 8b0f6f0

19 files changed

+59
-51
lines changed

dot/core/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
9191
}
9292

9393
if cfg.Runtime == nil {
94-
var rtCfg runtime.InstanceConfig
94+
var rtCfg wasmer.Config
9595

9696
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
9797

dot/core/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,
261261

262262
// this needs to create a new runtime instance, otherwise it will update
263263
// the blocks that reference the current runtime version to use the code substition
264-
cfg := runtime.InstanceConfig{
264+
cfg := wasmer.Config{
265265
Storage: state,
266266
Keystore: rt.Keystore(),
267267
NodeStorage: rt.NodeStorage(),

dot/core/service_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.Acco
6363
nodeStorage := runtime.NodeStorage{
6464
BaseDB: runtime.NewInMemoryDB(t),
6565
}
66-
cfg := runtime.InstanceConfig{
66+
cfg := wasmer.Config{
6767
Storage: genState,
6868
LogLvl: log.Error,
6969
NodeStorage: nodeStorage,

dot/rpc/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func newCoreServiceTest(t *testing.T) *core.Service {
413413
err = cfg.Keystore.Acco.Insert(kp)
414414
require.NoError(t, err)
415415

416-
var rtCfg runtime.InstanceConfig
416+
var rtCfg wasmer.Config
417417

418418
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
419419

dot/rpc/modules/author_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type useRuntimeInstace func(*testing.T, *storage.TrieState) runtime.Instance
4040
func useInstanceFromGenesis(t *testing.T, rtStorage *storage.TrieState) (instance runtime.Instance) {
4141
t.Helper()
4242

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

6363
rtStorage.Set(common.CodeKey, bytes)
6464

65-
cfg := runtime.InstanceConfig{
65+
cfg := wasmer.Config{
6666
Role: 0,
6767
LogLvl: log.Critical,
6868
Storage: rtStorage,

dot/rpc/modules/chain_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func newTestStateService(t *testing.T) *state.Service {
361361
err = stateSrvc.Start()
362362
require.NoError(t, err)
363363

364-
var rtCfg runtime.InstanceConfig
364+
var rtCfg wasmer.Config
365365

366366
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
367367

dot/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service,
134134
var rt runtime.Instance
135135
switch cfg.Core.WasmInterpreter {
136136
case wasmer.Name:
137-
rtCfg := runtime.InstanceConfig{
137+
rtCfg := wasmer.Config{
138138
Storage: ts,
139139
Keystore: ks,
140140
LogLvl: cfg.Log.RuntimeLvl,

dot/services_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func newStateServiceWithoutMock(t *testing.T) *state.Service {
7575

7676
stateSrvc.Epoch = epochState
7777

78-
var rtCfg runtime.InstanceConfig
78+
var rtCfg wasmer.Config
7979

8080
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
8181

dot/services_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func newStateService(t *testing.T, ctrl *gomock.Controller) *state.Service {
269269

270270
stateSrvc.Epoch = epochState
271271

272-
var rtCfg runtime.InstanceConfig
272+
var rtCfg wasmer.Config
273273

274274
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
275275

dot/state/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
609609
bHash, codeHash, previousVersion.SpecVersion(), currCodeHash, newVersion.SpecVersion())
610610
}
611611

612-
rtCfg := runtime.InstanceConfig{
612+
rtCfg := wasmer.Config{
613613
Storage: newState,
614614
Keystore: rt.Keystore(),
615615
NodeStorage: rt.NodeStorage(),

dot/state/initialize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *Service) CreateGenesisRuntime(t *trie.Trie, gen *genesis.Genesis) (runt
159159
genTrie := rtstorage.NewTrieState(t)
160160

161161
// create genesis runtime
162-
rtCfg := runtime.InstanceConfig{
162+
rtCfg := wasmer.Config{
163163
LogLvl: s.logLvl,
164164
Storage: genTrie,
165165
}

dot/sync/syncer_integration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/ChainSafe/gossamer/internal/log"
1717
"github.com/ChainSafe/gossamer/lib/common"
1818
"github.com/ChainSafe/gossamer/lib/genesis"
19-
"github.com/ChainSafe/gossamer/lib/runtime"
2019
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
2120
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
2221
"github.com/ChainSafe/gossamer/lib/trie"
@@ -64,7 +63,7 @@ func newTestSyncer(t *testing.T) *Service {
6463
// initialise runtime
6564
genState := rtstorage.NewTrieState(genTrie)
6665

67-
rtCfg := runtime.InstanceConfig{
66+
rtCfg := wasmer.Config{
6867
Storage: genState,
6968
LogLvl: log.Critical,
7069
}

lib/babe/babe_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service {
124124
cfg.EpochState = dbSrv.Epoch
125125
}
126126

127-
var rtCfg runtime.InstanceConfig
127+
var rtCfg wasmer.Config
128128
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
129129

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

180-
rtCfg := runtime.InstanceConfig{
180+
rtCfg := wasmer.Config{
181181
Storage: rtstorage.NewTrieState(genTrie),
182182
}
183183

lib/grandpa/grandpa_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
1616
"github.com/ChainSafe/gossamer/lib/genesis"
1717
"github.com/ChainSafe/gossamer/lib/keystore"
18-
"github.com/ChainSafe/gossamer/lib/runtime"
1918
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
2019
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
2120
"github.com/ChainSafe/gossamer/lib/trie"
@@ -57,7 +56,7 @@ func newTestState(t *testing.T) *state.Service {
5756
block, err := state.NewBlockStateFromGenesis(db, tries, testGenesisHeader, telemetryMock)
5857
require.NoError(t, err)
5958

60-
var rtCfg runtime.InstanceConfig
59+
var rtCfg wasmer.Config
6160

6261
rtCfg.Storage = rtstorage.NewTrieState(genTrie)
6362

lib/runtime/types.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
package runtime
55

66
import (
7-
"github.com/ChainSafe/gossamer/internal/log"
8-
"github.com/ChainSafe/gossamer/lib/common"
97
"github.com/ChainSafe/gossamer/lib/crypto"
108
"github.com/ChainSafe/gossamer/lib/keystore"
119
"github.com/ChainSafe/gossamer/lib/runtime/offchain"
@@ -47,18 +45,6 @@ func (n *NodeStorage) GetPersistent(k []byte) ([]byte, error) {
4745
return n.PersistentStorage.Get(k)
4846
}
4947

50-
// InstanceConfig represents a runtime instance configuration
51-
type InstanceConfig struct {
52-
Storage Storage
53-
Keystore *keystore.GlobalKeystore
54-
LogLvl log.Level
55-
Role common.Roles
56-
NodeStorage NodeStorage
57-
Network BasicNetwork
58-
Transaction TransactionState
59-
CodeHash common.Hash
60-
}
61-
6248
// Context is the context for the wasm interpreter's imported functions
6349
type Context struct {
6450
Storage Storage

lib/runtime/wasmer/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2022 ChainSafe Systems (ON)
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
package wasmer
5+
6+
import (
7+
"github.com/ChainSafe/gossamer/internal/log"
8+
"github.com/ChainSafe/gossamer/lib/common"
9+
"github.com/ChainSafe/gossamer/lib/keystore"
10+
"github.com/ChainSafe/gossamer/lib/runtime"
11+
)
12+
13+
// Config is the configuration used to create a
14+
// Wasmer runtime instance.
15+
type Config struct {
16+
Storage runtime.Storage
17+
Keystore *keystore.GlobalKeystore
18+
LogLvl log.Level
19+
Role common.Roles
20+
NodeStorage runtime.NodeStorage
21+
Network runtime.BasicNetwork
22+
Transaction runtime.TransactionState
23+
CodeHash common.Hash
24+
}

lib/runtime/wasmer/exports_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestInstance_Version_KusamaRuntime(t *testing.T) {
174174
// set state to genesis state
175175
genState := storage.NewTrieState(genTrie)
176176

177-
cfg := runtime.InstanceConfig{
177+
cfg := Config{
178178
Storage: genState,
179179
LogLvl: log.Critical,
180180
}
@@ -296,7 +296,7 @@ func TestNodeRuntime_ValidateTransaction(t *testing.T) {
296296
// set state to genesis state
297297
genState := storage.NewTrieState(genTrie)
298298

299-
cfg := runtime.InstanceConfig{
299+
cfg := Config{
300300
Storage: genState,
301301
LogLvl: log.Critical,
302302
}
@@ -526,7 +526,7 @@ func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) {
526526
// set state to genesis state
527527
genState := storage.NewTrieState(genTrie)
528528

529-
cfg := runtime.InstanceConfig{
529+
cfg := Config{
530530
Storage: genState,
531531
LogLvl: log.Critical,
532532
}
@@ -556,7 +556,7 @@ func TestInstance_ApplyExtrinsic_GossamerRuntime(t *testing.T) {
556556
// set state to genesis state
557557
genState := storage.NewTrieState(genTrie)
558558

559-
cfg := runtime.InstanceConfig{
559+
cfg := Config{
560560
Storage: genState,
561561
LogLvl: log.Critical,
562562
}
@@ -616,7 +616,7 @@ func TestInstance_ExecuteBlock_PolkadotRuntime_PolkadotBlock1(t *testing.T) {
616616
// set state to genesis state
617617
genState := storage.NewTrieState(genTrie)
618618

619-
cfg := runtime.InstanceConfig{
619+
cfg := Config{
620620
Storage: genState,
621621
LogLvl: log.Critical,
622622
}
@@ -668,7 +668,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1(t *testing.T) {
668668
// set state to genesis state
669669
genState := storage.NewTrieState(genTrie)
670670

671-
cfg := runtime.InstanceConfig{
671+
cfg := Config{
672672
Storage: genState,
673673
LogLvl: log.Critical,
674674
}
@@ -714,7 +714,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock3784(t *testing.T) {
714714
// set state to genesis state
715715
state3783 := storage.NewTrieState(gossTrie3783)
716716

717-
cfg := runtime.InstanceConfig{
717+
cfg := Config{
718718
Storage: state3783,
719719
LogLvl: log.Critical,
720720
}
@@ -760,7 +760,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock901442(t *testing.T) {
760760
// set state to genesis state
761761
state901441 := storage.NewTrieState(ksmTrie901441)
762762

763-
cfg := runtime.InstanceConfig{
763+
cfg := Config{
764764
Storage: state901441,
765765
LogLvl: log.Critical,
766766
}
@@ -806,7 +806,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1377831(t *testing.T) {
806806
// set state to genesis state
807807
state := storage.NewTrieState(ksmTrie)
808808

809-
cfg := runtime.InstanceConfig{
809+
cfg := Config{
810810
Storage: state,
811811
LogLvl: log.Critical,
812812
}
@@ -852,7 +852,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock1482003(t *testing.T) {
852852
// set state to genesis state
853853
state := storage.NewTrieState(ksmTrie)
854854

855-
cfg := runtime.InstanceConfig{
855+
cfg := Config{
856856
Storage: state,
857857
LogLvl: log.Critical,
858858
}
@@ -900,7 +900,7 @@ func TestInstance_ExecuteBlock_KusamaRuntime_KusamaBlock4939774(t *testing.T) {
900900
// set state to genesis state
901901
state := storage.NewTrieState(ksmTrie)
902902

903-
cfg := runtime.InstanceConfig{
903+
cfg := Config{
904904
Storage: state,
905905
LogLvl: log.Critical,
906906
}
@@ -943,7 +943,7 @@ func TestInstance_ExecuteBlock_PolkadotBlock1089328(t *testing.T) {
943943
// set state to genesis state
944944
state := storage.NewTrieState(dotTrie)
945945

946-
cfg := runtime.InstanceConfig{
946+
cfg := Config{
947947
Storage: state,
948948
LogLvl: log.Critical,
949949
}

lib/runtime/wasmer/instance.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Instance struct {
4747
}
4848

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

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

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

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

8888
wasmInstance, allocator, err := setupVM(code)
@@ -159,7 +159,7 @@ func (in *Instance) UpdateRuntimeCode(code []byte) (err error) {
159159
// GetRuntimeVersion finds the runtime version by initiating a temporary
160160
// runtime instance using the WASM code provided, and querying it.
161161
func GetRuntimeVersion(code []byte) (version runtime.Version, err error) {
162-
config := runtime.InstanceConfig{
162+
config := Config{
163163
LogLvl: log.DoNotChange,
164164
}
165165
instance, err := NewInstance(code, config)

lib/runtime/wasmer/test_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie)
4141
return r
4242
}
4343

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

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

55-
return runtime.InstanceConfig{
55+
return Config{
5656
Storage: s,
5757
Keystore: keystore.NewGlobalKeystore(),
5858
LogLvl: lvl,

0 commit comments

Comments
 (0)