Skip to content

Commit

Permalink
fix(lib/runtime): avoid caching version in runtime instance (#2425)
Browse files Browse the repository at this point in the history
- No longer provide workaround for runtimes with old babe implementation
- Stop caching runtime version inside life and wasmer instance
- The runtime environment no longer expect the `Version()` Runtime API to be present in all runtimes
  • Loading branch information
FlorianFranzen authored Mar 25, 2022
1 parent d2ec68d commit 7ab31f0
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 40 deletions.
7 changes: 0 additions & 7 deletions lib/runtime/life/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package life

import (
"bytes"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -141,12 +140,6 @@ func (in *Instance) ExecuteBlock(block *types.Block) ([]byte, error) {

// remove seal digest only
for _, d := range block.Header.Digest.Types {
// hack since substrate node_runtime can't seem to handle BABE pre-runtime digests
// with type prefix (ie Primary, Secondary...)
if bytes.Equal(in.version.SpecName(), []byte("node")) {
break
}

switch d.Value().(type) {
case types.SealDigest:
continue
Expand Down
2 changes: 2 additions & 0 deletions lib/runtime/life/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func TestInstance_FinalizeBlock_NodeRuntime(t *testing.T) {
}

func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) {
t.Skip("Broken due to outdated runtime")

instance := newInstanceFromGenesis(t)
block := buildBlock(t, instance)

Expand Down
11 changes: 3 additions & 8 deletions lib/runtime/life/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ type Config struct {
Resolver exec.ImportResolver
}

// Instance represents a v0.8 runtime life instance
// Instance is a runtime life instance
type Instance struct {
vm *exec.VirtualMachine
mu sync.Mutex
version runtime.Version
vm *exec.VirtualMachine
mu sync.Mutex
}

// GetCodeHash returns code hash of the runtime
Expand Down Expand Up @@ -123,10 +122,6 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) {
}

ctx = runtimeCtx
inst.version, err = inst.Version()
if err != nil {
logger.Errorf("error checking instance version: %s", err)
}
return inst, nil
}

Expand Down
12 changes: 0 additions & 12 deletions lib/runtime/wasmer/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func (in *Instance) ValidateTransaction(e types.Extrinsic) (*transaction.Validit

// Version calls runtime function Core_Version
func (in *Instance) Version() (runtime.Version, error) {
// kusama seems to use the legacy version format
if in.version != nil {
return in.version, nil
}

res, err := in.exec(runtime.CoreVersion, []byte{})
if err != nil {
return nil, err
Expand Down Expand Up @@ -141,13 +136,6 @@ func (in *Instance) ExecuteBlock(block *types.Block) ([]byte, error) {
return nil, err
}

if in.version == nil {
in.version, err = in.Version()
if err != nil {
return nil, err
}
}

b.Header.Digest = types.NewDigest()

// remove seal digest only
Expand Down
8 changes: 3 additions & 5 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,9 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64
return 0
}

// instance version is set and cached in NewInstance
version := instance.version

if version == nil {
logger.Error("failed to get runtime version")
version, err := instance.Version()
if err != nil {
logger.Errorf("failed to get runtime version: %s", err)
out, _ := toWasmMemoryOptional(instanceContext, nil)
return C.int64_t(out)
}
Expand Down
8 changes: 0 additions & 8 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type Config struct {
type Instance struct {
vm wasm.Instance
ctx *runtime.Context
version runtime.Version
imports func() (*wasm.Imports, error)
isClosed bool
codeHash common.Hash
Expand Down Expand Up @@ -162,7 +161,6 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) {
codeHash: cfg.CodeHash,
}

inst.version, _ = inst.Version()
return inst, nil
}

Expand Down Expand Up @@ -201,12 +199,6 @@ func (in *Instance) UpdateRuntimeCode(code []byte) error {
return err
}

in.version = nil
in.version, err = in.Version()
if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit 7ab31f0

Please sign in to comment.