From 3a9992c8029c61e1da33c789831127c092331c27 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 21 Jul 2022 14:14:59 +0000 Subject: [PATCH] `CheckRuntimeVersion` without function receiver - No dependency on existing instance - Closes temporary instance on exit - Remove `CheckRuntimeVersion` from Instance interface --- dot/rpc/subscription/listeners_test.go | 3 +-- dot/state/block.go | 2 +- dot/sync/mock_instance_test.go | 15 --------------- lib/blocktree/mock_instance_test.go | 15 --------------- lib/runtime/interface.go | 1 - lib/runtime/mocks/instance.go | 23 ----------------------- lib/runtime/wasmer/instance.go | 24 +++++++++++++++--------- lib/runtime/wasmer/instance_test.go | 5 ++--- 8 files changed, 19 insertions(+), 69 deletions(-) diff --git a/dot/rpc/subscription/listeners_test.go b/dot/rpc/subscription/listeners_test.go index 6576216e917..b1002a18114 100644 --- a/dot/rpc/subscription/listeners_test.go +++ b/dot/rpc/subscription/listeners_test.go @@ -351,12 +351,11 @@ func TestRuntimeChannelListener_Listen(t *testing.T) { expectedInitialResponse.Method = "state_runtimeVersion" expectedInitialResponse.Params.Result = expectedInitialVersion - instance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME) polkadotRuntimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME) require.NoError(t, err) code, err := os.ReadFile(polkadotRuntimeFilepath) require.NoError(t, err) - version, err := instance.CheckRuntimeVersion(code) + version, err := wasmer.CheckRuntimeVersion(code) require.NoError(t, err) expectedUpdatedVersion := modules.StateRuntimeVersionResponse{ diff --git a/dot/state/block.go b/dot/state/block.go index e0409ae9711..eed45a6129c 100644 --- a/dot/state/block.go +++ b/dot/state/block.go @@ -591,7 +591,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState, codeSubBlockHash := bs.baseState.LoadCodeSubstitutedBlockHash() if !codeSubBlockHash.Equal(common.Hash{}) { - newVersion, err := rt.CheckRuntimeVersion(code) + newVersion, err := wasmer.CheckRuntimeVersion(code) if err != nil { return err } diff --git a/dot/sync/mock_instance_test.go b/dot/sync/mock_instance_test.go index ce3af4cb602..a5264026355 100644 --- a/dot/sync/mock_instance_test.go +++ b/dot/sync/mock_instance_test.go @@ -80,21 +80,6 @@ func (mr *MockInstanceMockRecorder) CheckInherents() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckInherents", reflect.TypeOf((*MockInstance)(nil).CheckInherents)) } -// CheckRuntimeVersion mocks base method. -func (m *MockInstance) CheckRuntimeVersion(arg0 []byte) (runtime.Version, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CheckRuntimeVersion", arg0) - ret0, _ := ret[0].(runtime.Version) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CheckRuntimeVersion indicates an expected call of CheckRuntimeVersion. -func (mr *MockInstanceMockRecorder) CheckRuntimeVersion(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckRuntimeVersion", reflect.TypeOf((*MockInstance)(nil).CheckRuntimeVersion), arg0) -} - // DecodeSessionKeys mocks base method. func (m *MockInstance) DecodeSessionKeys(arg0 []byte) ([]byte, error) { m.ctrl.T.Helper() diff --git a/lib/blocktree/mock_instance_test.go b/lib/blocktree/mock_instance_test.go index a6f99b5b1a6..64c90b571ed 100644 --- a/lib/blocktree/mock_instance_test.go +++ b/lib/blocktree/mock_instance_test.go @@ -80,21 +80,6 @@ func (mr *MockInstanceMockRecorder) CheckInherents() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckInherents", reflect.TypeOf((*MockInstance)(nil).CheckInherents)) } -// CheckRuntimeVersion mocks base method. -func (m *MockInstance) CheckRuntimeVersion(arg0 []byte) (runtime.Version, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CheckRuntimeVersion", arg0) - ret0, _ := ret[0].(runtime.Version) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// CheckRuntimeVersion indicates an expected call of CheckRuntimeVersion. -func (mr *MockInstanceMockRecorder) CheckRuntimeVersion(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CheckRuntimeVersion", reflect.TypeOf((*MockInstance)(nil).CheckRuntimeVersion), arg0) -} - // DecodeSessionKeys mocks base method. func (m *MockInstance) DecodeSessionKeys(arg0 []byte) ([]byte, error) { m.ctrl.T.Helper() diff --git a/lib/runtime/interface.go b/lib/runtime/interface.go index e34fda6954e..30fc3a6e439 100644 --- a/lib/runtime/interface.go +++ b/lib/runtime/interface.go @@ -16,7 +16,6 @@ import ( // Instance is the interface a v0.8 runtime instance must implement type Instance interface { UpdateRuntimeCode([]byte) error - CheckRuntimeVersion([]byte) (Version, error) Stop() NodeStorage() NodeStorage NetworkService() BasicNetwork diff --git a/lib/runtime/mocks/instance.go b/lib/runtime/mocks/instance.go index b7e9ecc2d7a..fe94c837053 100644 --- a/lib/runtime/mocks/instance.go +++ b/lib/runtime/mocks/instance.go @@ -71,29 +71,6 @@ func (_m *Instance) CheckInherents() { _m.Called() } -// CheckRuntimeVersion provides a mock function with given fields: _a0 -func (_m *Instance) CheckRuntimeVersion(_a0 []byte) (runtime.Version, error) { - ret := _m.Called(_a0) - - var r0 runtime.Version - if rf, ok := ret.Get(0).(func([]byte) runtime.Version); ok { - r0 = rf(_a0) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(runtime.Version) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func([]byte) error); ok { - r1 = rf(_a0) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - // DecodeSessionKeys provides a mock function with given fields: enc func (_m *Instance) DecodeSessionKeys(enc []byte) ([]byte, error) { ret := _m.Called(enc) diff --git a/lib/runtime/wasmer/instance.go b/lib/runtime/wasmer/instance.go index 965d535a9e5..a57f71b1754 100644 --- a/lib/runtime/wasmer/instance.go +++ b/lib/runtime/wasmer/instance.go @@ -156,25 +156,31 @@ func (in *Instance) UpdateRuntimeCode(code []byte) (err error) { return nil } -// CheckRuntimeVersion calculates runtime Version for runtime blob passed in -func (in *Instance) CheckRuntimeVersion(code []byte) (runtime.Version, error) { - in.mutex.Lock() - defer in.mutex.Unlock() - +// CheckRuntimeVersion finds the runtime version by initiating a temporary +// runtime instance using the WASM code provided, and querying it. +func CheckRuntimeVersion(code []byte) (version runtime.Version, err error) { wasmInstance, allocator, err := setupVM(code) if err != nil { return nil, fmt.Errorf("setting up VM: %w", err) } - in.ctx.Allocator = allocator // TODO we should no change the allocator of the parent instance - wasmInstance.SetContextData(in.ctx) + ctx := &runtime.Context{ + Allocator: allocator, + } + wasmInstance.SetContextData(ctx) instance := Instance{ vm: wasmInstance, - ctx: in.ctx, + ctx: ctx, + } + defer instance.close() + + version, err = instance.Version() + if err != nil { + return nil, fmt.Errorf("running runtime: %w", err) } - return instance.Version() + return version, nil } var ( diff --git a/lib/runtime/wasmer/instance_test.go b/lib/runtime/wasmer/instance_test.go index 8f1a9b3c9bd..c51de540c5d 100644 --- a/lib/runtime/wasmer/instance_test.go +++ b/lib/runtime/wasmer/instance_test.go @@ -36,14 +36,13 @@ func TestPointerSize(t *testing.T) { require.Equal(t, in, res) } -func TestInstance_CheckRuntimeVersion(t *testing.T) { - instance := NewTestInstance(t, runtime.NODE_RUNTIME) +func Test_CheckRuntimeVersion(t *testing.T) { polkadotRuntimeFilepath, err := runtime.GetRuntime( context.Background(), runtime.POLKADOT_RUNTIME) require.NoError(t, err) code, err := os.ReadFile(polkadotRuntimeFilepath) require.NoError(t, err) - version, err := instance.CheckRuntimeVersion(code) + version, err := CheckRuntimeVersion(code) require.NoError(t, err) expected := runtime.NewVersionData(