Skip to content

Commit

Permalink
feat(lib/runtime): Update default runtime.Instance to wazero (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 authored Jul 21, 2023
1 parent 7f6830d commit 308b10a
Show file tree
Hide file tree
Showing 31 changed files with 393 additions and 174 deletions.
5 changes: 3 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/os"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero "github.com/ChainSafe/gossamer/lib/runtime/wazero"
)

const (
Expand All @@ -37,7 +38,7 @@ const (
// DefaultRole is the default node role
DefaultRole = common.AuthorityRole
// DefaultWasmInterpreter is the default wasm interpreter
DefaultWasmInterpreter = wasmer.Name
DefaultWasmInterpreter = wazero.Name

// DefaultNetworkPort is the default network port
DefaultNetworkPort = 7001
Expand Down Expand Up @@ -278,7 +279,7 @@ func (c *CoreConfig) ValidateBasic() error {
if c.WasmInterpreter == "" {
return fmt.Errorf("wasm-interpreter cannot be empty")
}
if c.WasmInterpreter != wasmer.Name {
if c.WasmInterpreter != wasmer.Name && c.WasmInterpreter != wazero.Name {
return fmt.Errorf("wasm-interpreter is invalid")
}

Expand Down
14 changes: 7 additions & 7 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/pkg/scale"
Expand Down Expand Up @@ -94,7 +94,7 @@ func createTestService(t *testing.T, genesisFilePath string,
cfgStorageState := stateSrvc.Storage
cfgCodeSubstitutedState := stateSrvc.Base

var rtCfg wasmer.Config
var rtCfg wazero_runtime.Config
rtCfg.Storage = rtstorage.NewTrieState(&genesisTrie)

rtCfg.CodeHash, err = cfgStorageState.LoadCodeHash(nil)
Expand All @@ -105,12 +105,12 @@ func createTestService(t *testing.T, genesisFilePath string,

rtCfg.NodeStorage = nodeStorage

cfgRuntime, err := wasmer.NewRuntimeFromGenesis(rtCfg)
cfgRuntime, err := wazero_runtime.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)

cfgRuntime.GetContext().Storage.Put(aliceBalanceKey, encodedAccountInfo)
cfgRuntime.Context.Storage.Put(aliceBalanceKey, encodedAccountInfo)
// this key is System.UpgradedToDualRefCount -> set to true since all accounts have been upgraded to v0.9 format
cfgRuntime.GetContext().Storage.Put(common.UpgradedToDualRefKey, []byte{1})
cfgRuntime.Context.Storage.Put(common.UpgradedToDualRefKey, []byte{1})

cfgBlockState.StoreRuntime(cfgBlockState.BestBlockHash(), cfgRuntime)

Expand Down Expand Up @@ -212,7 +212,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
}

if cfg.Runtime == nil {
var rtCfg wasmer.Config
var rtCfg wazero_runtime.Config

rtCfg.Storage = rtstorage.NewTrieState(&genesisTrie)

Expand All @@ -231,7 +231,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {

rtCfg.NodeStorage = nodeStorage

cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(rtCfg)
cfg.Runtime, err = wazero_runtime.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)
}
cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), cfg.Runtime)
Expand Down
6 changes: 3 additions & 3 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"

cscale "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
Expand Down Expand Up @@ -272,7 +272,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,

// this needs to create a new runtime instance, otherwise it will update
// the blocks that reference the current runtime version to use the code substition
cfg := wasmer.Config{
cfg := wazero_runtime.Config{
Storage: state,
Keystore: rt.Keystore(),
NodeStorage: rt.NodeStorage(),
Expand All @@ -283,7 +283,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,
cfg.Role = 4
}

next, err := wasmer.NewInstance(code, cfg)
next, err := wazero_runtime.NewInstance(code, cfg)
if err != nil {
return fmt.Errorf("creating new runtime instance: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
t.Skip() // need to update this test to use a valid transaction

cfg := &Config{
Runtime: wasmer.NewTestInstance(t, runtime.WESTEND_RUNTIME_v0929),
Runtime: wazero_runtime.NewTestInstance(t, runtime.WESTEND_RUNTIME_v0929),
}

s := NewTestService(t, cfg)
Expand Down
11 changes: 5 additions & 6 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/hex"
"errors"
"io"
"testing"

"github.com/ChainSafe/gossamer/dot/network"
Expand All @@ -20,7 +21,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
Expand Down Expand Up @@ -251,9 +252,7 @@ func Test_Service_handleCodeSubstitution(t *testing.T) {
}
},
blockHash: common.Hash{0x01},
errWrapped: wasmer.ErrWASMDecompress,
errMessage: "creating new runtime instance: setting up VM: " +
"wasm decompression failed: unexpected EOF",
errWrapped: io.ErrUnexpectedEOF,
},
"store_code_substitution_block_hash_error": {
serviceBuilder: func(ctrl *gomock.Controller) *Service {
Expand Down Expand Up @@ -302,7 +301,7 @@ func Test_Service_handleCodeSubstitution(t *testing.T) {
Return(nil)

blockState.EXPECT().StoreRuntime(common.Hash{0x01},
gomock.AssignableToTypeOf(&wasmer.Instance{}))
gomock.AssignableToTypeOf(&wazero_runtime.Instance{}))

return &Service{
blockState: blockState,
Expand All @@ -326,7 +325,7 @@ func Test_Service_handleCodeSubstitution(t *testing.T) {

err := service.handleCodeSubstitution(testCase.blockHash, testCase.trieState)
assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
if testCase.errMessage != "" {
assert.EqualError(t, err, testCase.errMessage)
}
})
Expand Down
4 changes: 2 additions & 2 deletions dot/node_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
gomock "github.com/golang/mock/gomock"
Expand All @@ -56,7 +56,7 @@ func TestNewNode(t *testing.T) {
initConfig.ChainSpec = genFile
initConfig.Account.Key = "alice"
initConfig.Core.Role = common.FullNodeRole
initConfig.Core.WasmInterpreter = wasmer.Name
initConfig.Core.WasmInterpreter = wazero_runtime.Name

initConfig.Log.Digest = "critical"

Expand Down
6 changes: 3 additions & 3 deletions dot/rpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/libp2p/go-libp2p/core/peer"

"github.com/ChainSafe/gossamer/dot/core"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/btcsuite/btcutil/base58"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -340,7 +340,7 @@ func newCoreServiceTest(t *testing.T) *core.Service {
err = cfg.Keystore.Acco.Insert(kp)
require.NoError(t, err)

var rtCfg wasmer.Config
var rtCfg wazero_runtime.Config

rtCfg.Storage = rtstorage.NewTrieState(&genesisTrie)

Expand All @@ -352,7 +352,7 @@ func newCoreServiceTest(t *testing.T) *core.Service {
}
rtCfg.NodeStorage = nodeStorage

cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(rtCfg)
cfg.Runtime, err = wazero_runtime.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)

cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), cfg.Runtime)
Expand Down
10 changes: 5 additions & 5 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
Expand All @@ -49,15 +49,15 @@ type useRuntimeInstance func(*testing.T, *storage.TrieState) runtime.Instance
func useInstanceFromGenesis(t *testing.T, rtStorage *storage.TrieState) (instance runtime.Instance) {
t.Helper()

cfg := wasmer.Config{
cfg := wazero_runtime.Config{
Storage: rtStorage,
LogLvl: log.Warn,
NodeStorage: runtime.NodeStorage{
BaseDB: runtime.NewInMemoryDB(t),
},
}

runtimeInstance, err := wasmer.NewRuntimeFromGenesis(cfg)
runtimeInstance, err := wazero_runtime.NewRuntimeFromGenesis(cfg)
require.NoError(t, err)

return runtimeInstance
Expand All @@ -71,7 +71,7 @@ func useInstanceFromRuntimeV0929(t *testing.T, rtStorage *storage.TrieState) (in

rtStorage.Put(common.CodeKey, bytes)

cfg := wasmer.Config{
cfg := wazero_runtime.Config{
Role: 0,
LogLvl: log.Critical,
Storage: rtStorage,
Expand All @@ -83,7 +83,7 @@ func useInstanceFromRuntimeV0929(t *testing.T, rtStorage *storage.TrieState) (in
},
}

runtimeInstance, err := wasmer.NewInstanceFromTrie(rtStorage.Trie(), cfg)
runtimeInstance, err := wazero_runtime.NewInstanceFromTrie(rtStorage.Trie(), cfg)
require.NoError(t, err)

return runtimeInstance
Expand Down
6 changes: 3 additions & 3 deletions dot/rpc/modules/chain_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/pkg/scale"
Expand Down Expand Up @@ -360,7 +360,7 @@ func newTestStateService(t *testing.T) *state.Service {
err = stateSrvc.Start()
require.NoError(t, err)

var rtCfg wasmer.Config
var rtCfg wazero_runtime.Config

rtCfg.Storage = rtstorage.NewTrieState(&genesisTrie)

Expand All @@ -371,7 +371,7 @@ func newTestStateService(t *testing.T) *state.Service {
require.NoError(t, err)
}

rt, err := wasmer.NewRuntimeFromGenesis(rtCfg)
rt, err := wazero_runtime.NewRuntimeFromGenesis(rtCfg)
require.NoError(t, err)

loadTestBlocks(t, genesisHeader.Hash(), stateSrvc.Block, rt)
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/dev_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -57,7 +57,7 @@ func newBABEService(t *testing.T) *babe.Service {

bs, es := newState(t)
tt := trie.NewEmptyTrie()
rt := wasmer.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0929, tt)
rt := wazero_runtime.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0929, tt)
bs.StoreRuntime(bs.GenesisHash(), rt)
tt.Put(
common.MustHexToBytes("0x886726f904d8372fdabb7707870c2fad"),
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"net/http"
"testing"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"

"github.com/ChainSafe/gossamer/dot/rpc/modules/mocks"
testdata "github.com/ChainSafe/gossamer/dot/rpc/modules/test_data"
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestStateModuleGetKeysPaged(t *testing.T) {
func TestCall(t *testing.T) {
ctrl := gomock.NewController(t)
testHash := common.NewHash([]byte{0x01, 0x02})
rt := wasmer.NewTestInstance(t, runtime.WESTEND_RUNTIME_v0929)
rt := wazero_runtime.NewTestInstance(t, runtime.WESTEND_RUNTIME_v0929)

mockNetworkAPI := mocks.NewMockNetworkAPI(ctrl)
mockStorageAPI := mocks.NewMockStorageAPI(ctrl)
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/system_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
Expand Down Expand Up @@ -354,7 +354,7 @@ func setupSystemModule(t *testing.T) *SystemModule {
func newCoreService(t *testing.T, srvc *state.Service) *core.Service {
// setup service
tt := trie.NewEmptyTrie()
rt := wasmer.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0929, tt)
rt := wazero_runtime.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0929, tt)
ks := keystore.NewGlobalKeystore()
t.Cleanup(func() {
rt.Stop()
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/subscription/listeners_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/grandpa"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/pkg/scale"
gomock "github.com/golang/mock/gomock"
Expand Down Expand Up @@ -319,7 +319,7 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
require.NoError(t, err)
code, err := os.ReadFile(polkadotRuntimeFilepath)
require.NoError(t, err)
version, err := wasmer.GetRuntimeVersion(code)
version, err := wazero_runtime.GetRuntimeVersion(code)
require.NoError(t, err)

expectedUpdatedVersion := modules.StateRuntimeVersionResponse{
Expand Down
17 changes: 17 additions & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/utils"
)

Expand Down Expand Up @@ -164,6 +165,22 @@ func createRuntime(config *cfg.Config, ns runtime.NodeStorage, st *state.Service
if err != nil {
return nil, fmt.Errorf("failed to create runtime executor: %s", err)
}
case wazero_runtime.Name:
rtCfg := wazero_runtime.Config{
Storage: ts,
Keystore: ks,
LogLvl: wasmerLogLevel,
NodeStorage: ns,
Network: net,
Role: config.Core.Role,
CodeHash: codeHash,
}

// create runtime executor
rt, err = wazero_runtime.NewInstance(code, rtCfg)
if err != nil {
return nil, fmt.Errorf("failed to create runtime executor: %s", err)
}
default:
return nil, fmt.Errorf("%w: %s", ErrWasmInterpreterName, config.Core.WasmInterpreter)
}
Expand Down
Loading

0 comments on commit 308b10a

Please sign in to comment.