Skip to content

Commit

Permalink
chore(lib/runtime): GetRuntimeVersion independent from existing run…
Browse files Browse the repository at this point in the history
…time 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`
  • Loading branch information
qdm12 authored Aug 15, 2022
1 parent 0fff418 commit 2444d0b
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 104 deletions.
15 changes: 0 additions & 15 deletions dot/core/mocks_runtime_test.go

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

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.GetRuntimeVersion(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.GetRuntimeVersion(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.

17 changes: 2 additions & 15 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,13 @@ import (
"time"
"unsafe"

"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
rtype "github.com/ChainSafe/gossamer/lib/common/types"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/crypto/secp256k1"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/trie/proof"
Expand Down Expand Up @@ -944,20 +942,9 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64
logger.Trace("executing...")

instanceContext := wasm.IntoInstanceContext(context)
data := asMemorySlice(instanceContext, dataSpan)

cfg := runtime.InstanceConfig{
LogLvl: log.DoNotChange,
Storage: rtstorage.NewTrieState(nil),
}

instance, err := NewInstance(data, cfg)
if err != nil {
logger.Errorf("failed to create instance: %s", err)
return 0
}
code := asMemorySlice(instanceContext, dataSpan)

version, err := instance.Version()
version, err := GetRuntimeVersion(code)
if err != nil {
logger.Errorf("failed to get runtime version: %s", err)
out, _ := toWasmMemoryOptional(instanceContext, nil)
Expand Down
27 changes: 13 additions & 14 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,24 @@ 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()

wasmInstance, allocator, err := setupVM(code)
// GetRuntimeVersion finds the runtime version by initiating a temporary
// runtime instance using the WASM code provided, and querying it.
func GetRuntimeVersion(code []byte) (version runtime.Version, err error) {
config := runtime.InstanceConfig{
LogLvl: log.DoNotChange,
}
instance, err := NewInstance(code, config)
if err != nil {
return nil, fmt.Errorf("setting up VM: %w", err)
return version, fmt.Errorf("creating runtime instance: %w", err)
}
defer instance.Stop()

in.ctx.Allocator = allocator // TODO we should no change the allocator of the parent instance
wasmInstance.SetContextData(in.ctx)

instance := Instance{
vm: wasmInstance,
ctx: in.ctx,
version, err = instance.Version()
if err != nil {
return nil, fmt.Errorf("running runtime: %w", err)
}

return instance.Version()
return version, nil
}

var (
Expand Down
17 changes: 14 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_GetRuntimeVersion(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 := GetRuntimeVersion(code)
require.NoError(t, err)

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

func Benchmark_GetRuntimeVersion(b *testing.B) {
polkadotRuntimeFilepath, err := runtime.GetRuntime(
context.Background(), runtime.POLKADOT_RUNTIME)
require.NoError(b, err)

b.ResetTimer()
for i := 0; i < b.N; i++ {
code, _ := os.ReadFile(polkadotRuntimeFilepath)
_, _ = GetRuntimeVersion(code)
}
}

func TestDecompressWasm(t *testing.T) {
encoder, err := zstd.NewWriter(nil)
require.NoError(t, err)
Expand Down

0 comments on commit 2444d0b

Please sign in to comment.