Skip to content

Commit

Permalink
Force state version to 0 for host api test runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Aug 16, 2022
1 parent b396bd7 commit 2b18bf8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
22 changes: 14 additions & 8 deletions lib/runtime/wasmer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/trie"
)

// 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
Storage runtime.Storage
Keystore *keystore.GlobalKeystore
LogLvl log.Level
Role common.Roles
NodeStorage runtime.NodeStorage
Network runtime.BasicNetwork
Transaction runtime.TransactionState
CodeHash common.Hash
testVersions *testVersions
}

type testVersions struct {
stateVersion trie.Version
}
24 changes: 19 additions & 5 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,33 @@ func NewInstance(code []byte, cfg Config) (instance *Instance, err error) {
codeHash: cfg.CodeHash,
}

// Find runtime instance state version and cache it in its
// instance context.
version, err := instance.Version()
stateVersion, err := getStateVersion(instance, cfg.testVersions)
if err != nil {
instance.close()
return nil, fmt.Errorf("getting instance version: %w", err)
return nil, fmt.Errorf("getting state version: %w", err)
}
instance.ctx.StateVersion = trie.Version(version.StateVersion)

instance.ctx.StateVersion = stateVersion
wasmInstance.SetContextData(instance.ctx)

return instance, nil
}

func getStateVersion(instance *Instance, testVersions *testVersions) (
stateVersion trie.Version, err error) {
if testVersions != nil {
return testVersions.stateVersion, nil
}

// Find runtime state version from running instance
version, err := instance.Version()
if err != nil {
return stateVersion, fmt.Errorf("getting instance version: %w", err)
}

return trie.Version(version.StateVersion), nil
}

// decompressWasm decompresses a Wasm blob that may or may not be compressed with zstd
// ref: https://github.com/paritytech/substrate/blob/master/primitives/maybe-compressed-blob/src/lib.rs
func decompressWasm(code []byte) ([]byte, error) {
Expand Down
30 changes: 21 additions & 9 deletions lib/runtime/wasmer/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewTestInstance(t *testing.T, targetRuntime string) *Instance {
func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie) *Instance {
t.Helper()

cfg := setupConfig(t, tt, DefaultTestLogLvl, common.NoNetworkRole)
cfg := setupConfig(t, tt, DefaultTestLogLvl, common.NoNetworkRole, targetRuntime)
runtimeFilepath, err := runtime.GetRuntime(context.Background(), targetRuntime)
require.NoError(t, err)

Expand All @@ -40,7 +40,8 @@ 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) Config {
func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level,
role common.Roles, targetRuntime string) Config {
t.Helper()

s := storage.NewTrieState(tt)
Expand All @@ -51,14 +52,25 @@ 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
}

var versions *testVersions
if targetRuntime == runtime.HOST_API_TEST_RUNTIME {
// Force state version to 0 since the host api test runtime
// does not implement the Core_version call so we cannot get the
// state version from it.
versions = &testVersions{
stateVersion: 0,
}
}

return Config{
Storage: s,
Keystore: keystore.NewGlobalKeystore(),
LogLvl: lvl,
NodeStorage: ns,
Network: new(runtime.TestRuntimeNetwork),
Transaction: newTransactionStateMock(),
Role: role,
Storage: s,
Keystore: keystore.NewGlobalKeystore(),
LogLvl: lvl,
NodeStorage: ns,
Network: new(runtime.TestRuntimeNetwork),
Transaction: newTransactionStateMock(),
Role: role,
testVersions: versions,
}
}

Expand Down

0 comments on commit 2b18bf8

Please sign in to comment.