Skip to content

Commit 2444d0b

Browse files
authored
chore(lib/runtime): GetRuntimeVersion independent from existing runtime instance (#2687)
- `GetRuntimeVersion` as function instead of instance method - No dependency on existing instance - Fix: do not modify parent instance allocator - Fix: close temporary instance on exit - Remove `CheckRuntimeVersion` from Instance interface - `ext_misc_runtime_version_version_1` uses `GetRuntimeVersion`
1 parent 0fff418 commit 2444d0b

File tree

10 files changed

+31
-104
lines changed

10 files changed

+31
-104
lines changed

dot/core/mocks_runtime_test.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/rpc/subscription/listeners_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,11 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
351351
expectedInitialResponse.Method = "state_runtimeVersion"
352352
expectedInitialResponse.Params.Result = expectedInitialVersion
353353

354-
instance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME)
355354
polkadotRuntimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME)
356355
require.NoError(t, err)
357356
code, err := os.ReadFile(polkadotRuntimeFilepath)
358357
require.NoError(t, err)
359-
version, err := instance.CheckRuntimeVersion(code)
358+
version, err := wasmer.GetRuntimeVersion(code)
360359
require.NoError(t, err)
361360

362361
expectedUpdatedVersion := modules.StateRuntimeVersionResponse{

dot/state/block.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
591591
codeSubBlockHash := bs.baseState.LoadCodeSubstitutedBlockHash()
592592

593593
if !codeSubBlockHash.Equal(common.Hash{}) {
594-
newVersion, err := rt.CheckRuntimeVersion(code)
594+
newVersion, err := wasmer.GetRuntimeVersion(code)
595595
if err != nil {
596596
return err
597597
}

dot/sync/mock_instance_test.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/blocktree/mock_instance_test.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/runtime/interface.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
// Instance is the interface a v0.8 runtime instance must implement
1717
type Instance interface {
1818
UpdateRuntimeCode([]byte) error
19-
CheckRuntimeVersion([]byte) (Version, error)
2019
Stop()
2120
NodeStorage() NodeStorage
2221
NetworkService() BasicNetwork

lib/runtime/mocks/instance.go

Lines changed: 0 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/runtime/wasmer/imports.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,13 @@ import (
110110
"time"
111111
"unsafe"
112112

113-
"github.com/ChainSafe/gossamer/internal/log"
114113
"github.com/ChainSafe/gossamer/lib/common"
115114
rtype "github.com/ChainSafe/gossamer/lib/common/types"
116115
"github.com/ChainSafe/gossamer/lib/crypto"
117116
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
118117
"github.com/ChainSafe/gossamer/lib/crypto/secp256k1"
119118
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
120119
"github.com/ChainSafe/gossamer/lib/runtime"
121-
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
122120
"github.com/ChainSafe/gossamer/lib/transaction"
123121
"github.com/ChainSafe/gossamer/lib/trie"
124122
"github.com/ChainSafe/gossamer/lib/trie/proof"
@@ -944,20 +942,9 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64
944942
logger.Trace("executing...")
945943

946944
instanceContext := wasm.IntoInstanceContext(context)
947-
data := asMemorySlice(instanceContext, dataSpan)
948-
949-
cfg := runtime.InstanceConfig{
950-
LogLvl: log.DoNotChange,
951-
Storage: rtstorage.NewTrieState(nil),
952-
}
953-
954-
instance, err := NewInstance(data, cfg)
955-
if err != nil {
956-
logger.Errorf("failed to create instance: %s", err)
957-
return 0
958-
}
945+
code := asMemorySlice(instanceContext, dataSpan)
959946

960-
version, err := instance.Version()
947+
version, err := GetRuntimeVersion(code)
961948
if err != nil {
962949
logger.Errorf("failed to get runtime version: %s", err)
963950
out, _ := toWasmMemoryOptional(instanceContext, nil)

lib/runtime/wasmer/instance.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,25 +156,24 @@ func (in *Instance) UpdateRuntimeCode(code []byte) (err error) {
156156
return nil
157157
}
158158

159-
// CheckRuntimeVersion calculates runtime Version for runtime blob passed in
160-
func (in *Instance) CheckRuntimeVersion(code []byte) (runtime.Version, error) {
161-
in.mutex.Lock()
162-
defer in.mutex.Unlock()
163-
164-
wasmInstance, allocator, err := setupVM(code)
159+
// GetRuntimeVersion finds the runtime version by initiating a temporary
160+
// runtime instance using the WASM code provided, and querying it.
161+
func GetRuntimeVersion(code []byte) (version runtime.Version, err error) {
162+
config := runtime.InstanceConfig{
163+
LogLvl: log.DoNotChange,
164+
}
165+
instance, err := NewInstance(code, config)
165166
if err != nil {
166-
return nil, fmt.Errorf("setting up VM: %w", err)
167+
return version, fmt.Errorf("creating runtime instance: %w", err)
167168
}
169+
defer instance.Stop()
168170

169-
in.ctx.Allocator = allocator // TODO we should no change the allocator of the parent instance
170-
wasmInstance.SetContextData(in.ctx)
171-
172-
instance := Instance{
173-
vm: wasmInstance,
174-
ctx: in.ctx,
171+
version, err = instance.Version()
172+
if err != nil {
173+
return nil, fmt.Errorf("running runtime: %w", err)
175174
}
176175

177-
return instance.Version()
176+
return version, nil
178177
}
179178

180179
var (

lib/runtime/wasmer/instance_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,13 @@ func TestPointerSize(t *testing.T) {
3636
require.Equal(t, in, res)
3737
}
3838

39-
func TestInstance_CheckRuntimeVersion(t *testing.T) {
40-
instance := NewTestInstance(t, runtime.NODE_RUNTIME)
39+
func Test_GetRuntimeVersion(t *testing.T) {
4140
polkadotRuntimeFilepath, err := runtime.GetRuntime(
4241
context.Background(), runtime.POLKADOT_RUNTIME)
4342
require.NoError(t, err)
4443
code, err := os.ReadFile(polkadotRuntimeFilepath)
4544
require.NoError(t, err)
46-
version, err := instance.CheckRuntimeVersion(code)
45+
version, err := GetRuntimeVersion(code)
4746
require.NoError(t, err)
4847

4948
expected := runtime.NewVersionData(
@@ -65,6 +64,18 @@ func TestInstance_CheckRuntimeVersion(t *testing.T) {
6564
require.Equal(t, expected.TransactionVersion(), version.TransactionVersion())
6665
}
6766

67+
func Benchmark_GetRuntimeVersion(b *testing.B) {
68+
polkadotRuntimeFilepath, err := runtime.GetRuntime(
69+
context.Background(), runtime.POLKADOT_RUNTIME)
70+
require.NoError(b, err)
71+
72+
b.ResetTimer()
73+
for i := 0; i < b.N; i++ {
74+
code, _ := os.ReadFile(polkadotRuntimeFilepath)
75+
_, _ = GetRuntimeVersion(code)
76+
}
77+
}
78+
6879
func TestDecompressWasm(t *testing.T) {
6980
encoder, err := zstd.NewWriter(nil)
7081
require.NoError(t, err)

0 commit comments

Comments
 (0)