Skip to content

Commit

Permalink
CheckRuntimeVersion without function receiver
Browse files Browse the repository at this point in the history
- No dependency on existing instance
- Closes temporary instance on exit
- Remove `CheckRuntimeVersion` from Instance interface
  • Loading branch information
qdm12 committed Jul 22, 2022
1 parent 2ee6693 commit 091170a
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 69 deletions.
3 changes: 1 addition & 2 deletions dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 0 additions & 15 deletions dot/sync/mock_instance_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions lib/blocktree/mock_instance_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 0 additions & 23 deletions lib/runtime/mocks/instance.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
5 changes: 2 additions & 3 deletions lib/runtime/wasmer/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 091170a

Please sign in to comment.