Skip to content

Commit

Permalink
make CheckRuntimeVersion private
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Aug 10, 2022
1 parent f3a8cbf commit 29e178e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
23 changes: 22 additions & 1 deletion dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ChainSafe/gossamer/dot/rpc/modules/mocks"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
glog "github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -332,6 +333,26 @@ func setupWSConn(t *testing.T) (*WSConn, *websocket.Conn, func()) {
return wskt, ws, cancel
}

// 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) {
config := runtime.InstanceConfig{
LogLvl: glog.DoNotChange,
}
instance, err := wasmer.NewInstance(code, config)
if err != nil {
return version, fmt.Errorf("creating runtime instance: %w", err)
}
defer instance.Stop()

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

return version, nil
}

func TestRuntimeChannelListener_Listen(t *testing.T) {
notifyChan := make(chan runtime.Version)
mockConnection := &mockWSConnAPI{}
Expand All @@ -355,7 +376,7 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
require.NoError(t, err)
code, err := os.ReadFile(polkadotRuntimeFilepath)
require.NoError(t, err)
version, err := wasmer.CheckRuntimeVersion(code)
version, err := checkRuntimeVersion(code)
require.NoError(t, err)

expectedUpdatedVersion := modules.StateRuntimeVersionResponse{
Expand Down
23 changes: 22 additions & 1 deletion dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -591,7 +592,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
codeSubBlockHash := bs.baseState.LoadCodeSubstitutedBlockHash()

if !codeSubBlockHash.Equal(common.Hash{}) {
newVersion, err := wasmer.CheckRuntimeVersion(code)
newVersion, err := checkRuntimeVersion(code)
if err != nil {
return err
}
Expand Down Expand Up @@ -663,3 +664,23 @@ func (bs *BlockState) StoreRuntime(hash common.Hash, rt runtime.Instance) {
func (bs *BlockState) GetNonFinalisedBlocks() []common.Hash {
return bs.bt.GetAllBlocks()
}

// 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) {
config := runtime.InstanceConfig{
LogLvl: log.DoNotChange,
}
instance, err := wasmer.NewInstance(code, config)
if err != nil {
return version, fmt.Errorf("creating runtime instance: %w", err)
}
defer instance.Stop()

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

return version, nil
}
2 changes: 1 addition & 1 deletion lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64
instanceContext := wasm.IntoInstanceContext(context)
code := asMemorySlice(instanceContext, dataSpan)

version, err := CheckRuntimeVersion(code)
version, err := checkRuntimeVersion(code)
if err != nil {
logger.Errorf("failed to get runtime version: %s", err)
out, _ := toWasmMemoryOptional(instanceContext, nil)
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ func (in *Instance) UpdateRuntimeCode(code []byte) (err error) {
return nil
}

// CheckRuntimeVersion finds the runtime version by initiating a temporary
// 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) {
func checkRuntimeVersion(code []byte) (version runtime.Version, err error) {
config := runtime.InstanceConfig{
LogLvl: log.DoNotChange,
}
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/wasmer/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_CheckRuntimeVersion(t *testing.T) {
require.NoError(t, err)
code, err := os.ReadFile(polkadotRuntimeFilepath)
require.NoError(t, err)
version, err := CheckRuntimeVersion(code)
version, err := checkRuntimeVersion(code)
require.NoError(t, err)

expected := runtime.NewVersionData(
Expand Down Expand Up @@ -72,7 +72,7 @@ func Benchmark_CheckRuntimeVersion(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
code, _ := os.ReadFile(polkadotRuntimeFilepath)
_, _ = CheckRuntimeVersion(code)
_, _ = checkRuntimeVersion(code)
}
}

Expand Down

0 comments on commit 29e178e

Please sign in to comment.