Skip to content

Commit

Permalink
Remove runtime instance from sync service.
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD committed Jun 28, 2021
1 parent 1dbb99c commit e9d5092
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 110 deletions.
1 change: 1 addition & 0 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type BlockState interface {
HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error
GetRuntime(*common.Hash) (runtime.Instance, bool)
StoreRuntime(common.Hash, runtime.Instance)
GetAllBlocks() []common.Hash
}

// StorageState interface for storage state methods
Expand Down
2 changes: 2 additions & 0 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
require.NoError(t, err)

cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), rt)
} else {
cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), cfg.Runtime)
}

if cfg.Network == nil {
Expand Down
34 changes: 30 additions & 4 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/lib/utils"
log "github.com/ChainSafe/log15"
Expand Down Expand Up @@ -259,7 +260,7 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,
}

// create runtime
rt, err := createRuntime(cfg, stateSrvc, ks, networkSrvc)
err = loadRuntime(cfg, stateSrvc, ks, networkSrvc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -287,13 +288,13 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,
}
nodeSrvcs = append(nodeSrvcs, bp)

fg, err := createGRANDPAService(cfg, rt, stateSrvc, dh, ks.Gran, networkSrvc)
fg, err := createGRANDPAService(cfg, stateSrvc, dh, ks.Gran, networkSrvc)
if err != nil {
return nil, err
}
nodeSrvcs = append(nodeSrvcs, fg)

syncer, err := newSyncService(cfg, stateSrvc, fg, ver, rt, coreSrvc)
syncer, err := newSyncService(cfg, stateSrvc, fg, ver, coreSrvc)
if err != nil {
return nil, err
}
Expand All @@ -311,7 +312,7 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,

// check if rpc service is enabled
if enabled := cfg.RPC.Enabled; enabled {
rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, bp, rt, sysSrvc)
rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, bp, sysSrvc)
nodeSrvcs = append(nodeSrvcs, rpcSrvc)
} else {
logger.Debug("rpc service disabled by default", "rpc", enabled)
Expand Down Expand Up @@ -446,3 +447,28 @@ func (n *Node) Stop() {
n.Services.StopAll()
n.wg.Done()
}

func loadRuntime(cfg *Config, stateSrvc *state.Service, ks *keystore.GlobalKeystore, net *network.Service) error {
blocks := stateSrvc.Block.GetAllBlocks()
runtimeCode := make(map[string]runtime.Instance)
for _, hash := range blocks {
code, err := stateSrvc.Storage.GetStorageByBlockHash(hash, []byte(":code"))
if err != nil {
return err
}

if rt, ok := runtimeCode[string(code)]; ok {
stateSrvc.Block.StoreRuntime(hash, rt)
continue
}

rt, err := createRuntime(cfg, stateSrvc, ks, net, code)
if err != nil {
return err
}

runtimeCode[string(code)] = rt
}

return nil
}
32 changes: 9 additions & 23 deletions dot/rpc/modules/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (

"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/trie"

database "github.com/ChainSafe/chaindb"
Expand Down Expand Up @@ -319,25 +319,16 @@ func newTestStateService(t *testing.T) *state.Service {
stateSrvc.UseMemDB()

err = stateSrvc.Initialise(gen, genesisHeader, genTrie)
if err != nil {
t.Fatal(err)
}

hash := genesisHeader.Hash()
rt, ok := stateSrvc.Block.GetRuntime(&hash)
require.True(t, ok)
require.NoError(t, err)

err = stateSrvc.Start()
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

stateSrvc.Block.StoreRuntime(hash, rt)
rt, err := stateSrvc.CreateGenesisRuntime(genTrie, gen)
require.NoError(t, err)

err = loadTestBlocks(genesisHeader.Hash(), stateSrvc.Block)
if err != nil {
t.Fatal(err)
}
err = loadTestBlocks(genesisHeader.Hash(), stateSrvc.Block, rt)
require.NoError(t, err)

t.Cleanup(func() {
stateSrvc.Stop()
Expand All @@ -363,7 +354,7 @@ func newTestGenesisWithTrieAndHeader() (*genesis.Genesis, *trie.Trie, *types.Hea
return gen, genTrie, genesisHeader
}

func loadTestBlocks(gh common.Hash, bs *state.BlockState) error {
func loadTestBlocks(gh common.Hash, bs *state.BlockState, rt runtime.Instance) error {
// Create header
header0 := &types.Header{
Number: big.NewInt(0),
Expand All @@ -386,11 +377,6 @@ func loadTestBlocks(gh common.Hash, bs *state.BlockState) error {
return err
}

rt, ok := bs.GetRuntime(&gh)
if !ok {
return blocktree.ErrFailedToGetRuntime
}

bs.StoreRuntime(block0.Header.Hash(), rt)

// Create header & blockData for block 1
Expand All @@ -417,7 +403,7 @@ func loadTestBlocks(gh common.Hash, bs *state.BlockState) error {
return err
}

bs.StoreRuntime(block0.Header.Hash(), rt)
bs.StoreRuntime(block1.Header.Hash(), rt)

return nil
}
3 changes: 1 addition & 2 deletions dot/rpc/modules/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ func newBABEService(t *testing.T) *babe.Service {
bs, es := newState(t)
tt := trie.NewEmptyTrie()
rt := wasmer.NewTestInstanceWithTrie(t, runtime.NODE_RUNTIME, tt, log.LvlInfo)

bs.StoreRuntime(bs.GenesisHash(), rt)
tt.Put(common.MustHexToBytes("0x886726f904d8372fdabb7707870c2fad"), common.MustHexToBytes("0x24d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01000000000000008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48010000000000000090b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe220100000000000000306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc200100000000000000e659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e01000000000000001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c01000000000000004603307f855321776922daeea21ee31720388d097cdaac66f05a6f8462b317570100000000000000be1d9d59de1283380100550a7b024501cb62d6cc40e3db35fcc5cf341814986e01000000000000001206960f920a23f7f4c43cc9081ec2ed0721f31a9bef2c10fd7602e16e08a32c0100000000000000"))

cfg := &babe.ServiceConfig{
BlockState: bs,
EpochState: es,
Keypair: kr.Alice().(*sr25519.Keypair),
Runtime: rt,
IsDev: true,
BlockImportHandler: new(babemocks.BlockImportHandler),
}
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 @@ -443,8 +443,8 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) {

err = chain.Block.AddBlock(b)
require.NoError(t, err)
genHash := chain.Block.GenesisHash()
rt, ok := chain.Block.GetRuntime(&genHash)

rt, ok := chain.Block.GetRuntime(&b.Header.ParentHash)
require.True(t, ok)

chain.Block.StoreRuntime(b.Header.Hash(), rt)
Expand Down
3 changes: 3 additions & 0 deletions dot/rpc/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package rpc

import (
"flag"
"fmt"
"log"
"net/url"
"testing"
Expand Down Expand Up @@ -91,6 +92,8 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {

_, message, err := c.ReadMessage()
require.Nil(t, err)
fmt.Println("message", string(message))
fmt.Println("item.expected", string(item.expected))
require.Equal(t, item.expected, message)
}
}
23 changes: 11 additions & 12 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/dot/system"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe"
"github.com/ChainSafe/gossamer/lib/blocktree"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
Expand Down Expand Up @@ -90,18 +91,12 @@ func createStateService(cfg *Config) (*state.Service, error) {
return stateSrvc, nil
}

func createRuntime(cfg *Config, st *state.Service, ks *keystore.GlobalKeystore, net *network.Service) (runtime.Instance, error) {
func createRuntime(cfg *Config, st *state.Service, ks *keystore.GlobalKeystore, net *network.Service, code []byte) (runtime.Instance, error) {
logger.Info(
"creating runtime...",
"interpreter", cfg.Core.WasmInterpreter,
)

// load runtime code from trie
code, err := st.Storage.GetStorage(nil, []byte(":code"))
if err != nil {
return nil, fmt.Errorf("failed to retrieve :code from trie: %s", err)
}

// check if code substitute is in use, if so replace code
codeSubHash := st.Base.LoadCodeSubstitutedBlockHash()

Expand Down Expand Up @@ -324,7 +319,7 @@ func createNetworkService(cfg *Config, stateSrvc *state.Service) (*network.Servi
// RPC Service

// createRPCService creates the RPC service from the provided core configuration
func createRPCService(cfg *Config, stateSrvc *state.Service, coreSrvc *core.Service, networkSrvc *network.Service, bp modules.BlockProducerAPI, rt runtime.Instance, sysSrvc *system.Service) *rpc.HTTPServer {
func createRPCService(cfg *Config, stateSrvc *state.Service, coreSrvc *core.Service, networkSrvc *network.Service, bp modules.BlockProducerAPI, sysSrvc *system.Service) *rpc.HTTPServer {
logger.Info(
"creating rpc service...",
"host", cfg.RPC.Host,
Expand All @@ -344,7 +339,7 @@ func createRPCService(cfg *Config, stateSrvc *state.Service, coreSrvc *core.Serv
NetworkAPI: networkSrvc,
CoreAPI: coreSrvc,
BlockProducerAPI: bp,
RuntimeAPI: rt,
RuntimeAPI: stateSrvc.Block,
TransactionQueueAPI: stateSrvc.Transaction,
RPCAPI: rpcService,
SystemAPI: sysSrvc,
Expand Down Expand Up @@ -372,7 +367,12 @@ func createSystemService(cfg *types.SystemInfo, stateSrvc *state.Service) (*syst
}

// createGRANDPAService creates a new GRANDPA service
func createGRANDPAService(cfg *Config, rt runtime.Instance, st *state.Service, dh *digest.Handler, ks keystore.Keystore, net *network.Service) (*grandpa.Service, error) {
func createGRANDPAService(cfg *Config, st *state.Service, dh *digest.Handler, ks keystore.Keystore, net *network.Service) (*grandpa.Service, error) {
rt, ok := st.Block.GetRuntime(nil)
if !ok {
return nil, blocktree.ErrFailedToGetRuntime
}

ad, err := rt.GrandpaAuthorities()
if err != nil {
return nil, err
Expand Down Expand Up @@ -415,15 +415,14 @@ func createBlockVerifier(st *state.Service) (*babe.VerificationManager, error) {
return ver, nil
}

func newSyncService(cfg *Config, st *state.Service, fg sync.FinalityGadget, verifier *babe.VerificationManager, rt runtime.Instance, cs *core.Service) (*sync.Service, error) {
func newSyncService(cfg *Config, st *state.Service, fg sync.FinalityGadget, verifier *babe.VerificationManager, cs *core.Service) (*sync.Service, error) {
syncCfg := &sync.Config{
LogLvl: cfg.Log.SyncLvl,
BlockState: st.Block,
StorageState: st.Storage,
TransactionState: st.Transaction,
FinalityGadget: fg,
Verifier: verifier,
Runtime: rt,
BlockImportHandler: cs,
}

Expand Down
19 changes: 10 additions & 9 deletions dot/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestCreateSyncService(t *testing.T) {
coreSrvc, err := createCoreService(cfg, ks, stateSrvc, &network.Service{}, dh)
require.NoError(t, err)

_, err = newSyncService(cfg, stateSrvc, nil, ver, nil, coreSrvc)
_, err = newSyncService(cfg, stateSrvc, nil, ver, coreSrvc)
require.NoError(t, err)
}

Expand Down Expand Up @@ -197,7 +197,7 @@ func TestCreateRPCService(t *testing.T) {
ed25519Keyring, _ := keystore.NewEd25519Keyring()
ks.Gran.Insert(ed25519Keyring.Alice())

rt, err := createRuntime(cfg, stateSrvc, ks, networkSrvc)
err = loadRuntime(cfg, stateSrvc, ks, networkSrvc)
require.NoError(t, err)

dh, err := createDigestHandler(stateSrvc)
Expand All @@ -209,7 +209,7 @@ func TestCreateRPCService(t *testing.T) {
sysSrvc, err := createSystemService(&cfg.System, stateSrvc)
require.NoError(t, err)

rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, nil, rt, sysSrvc)
rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, nil, sysSrvc)
require.NotNil(t, rpcSrvc)
}

Expand Down Expand Up @@ -237,7 +237,7 @@ func TestCreateBABEService(t *testing.T) {
require.NoError(t, err)
ks.Babe.Insert(kr.Alice())

rt, err := createRuntime(cfg, stateSrvc, ks, &network.Service{})
err = loadRuntime(cfg, stateSrvc, ks, &network.Service{})
require.NoError(t, err)

dh, err := createDigestHandler(stateSrvc)
Expand All @@ -246,7 +246,7 @@ func TestCreateBABEService(t *testing.T) {
coreSrvc, err := createCoreService(cfg, ks, stateSrvc, &network.Service{}, dh)
require.NoError(t, err)

bs, err := createBABEService(cfg, rt, stateSrvc, ks.Babe, coreSrvc)
bs, err := createBABEService(cfg, stateSrvc, ks.Babe, coreSrvc)
require.NoError(t, err)
require.NotNil(t, bs)
}
Expand Down Expand Up @@ -275,13 +275,13 @@ func TestCreateGrandpaService(t *testing.T) {
require.NoError(t, err)
ks.Gran.Insert(kr.Alice())

rt, err := createRuntime(cfg, stateSrvc, ks, &network.Service{})
err = loadRuntime(cfg, stateSrvc, ks, &network.Service{})
require.NoError(t, err)

dh, err := createDigestHandler(stateSrvc)
require.NoError(t, err)

gs, err := createGRANDPAService(cfg, rt, stateSrvc, dh, ks.Gran, &network.Service{})
gs, err := createGRANDPAService(cfg, stateSrvc, dh, ks.Gran, &network.Service{})
require.NoError(t, err)
require.NotNil(t, gs)
}
Expand Down Expand Up @@ -327,7 +327,8 @@ func TestNewWebSocketServer(t *testing.T) {
ks := keystore.NewGlobalKeystore()
ed25519Keyring, _ := keystore.NewEd25519Keyring()
ks.Gran.Insert(ed25519Keyring.Alice())
rt, err := createRuntime(cfg, stateSrvc, ks, networkSrvc)

err = loadRuntime(cfg, stateSrvc, ks, networkSrvc)
require.NoError(t, err)

dh, err := createDigestHandler(stateSrvc)
Expand All @@ -339,7 +340,7 @@ func TestNewWebSocketServer(t *testing.T) {
sysSrvc, err := createSystemService(&cfg.System, stateSrvc)
require.NoError(t, err)

rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, nil, rt, sysSrvc)
rpcSrvc := createRPCService(cfg, stateSrvc, coreSrvc, networkSrvc, nil, sysSrvc)
err = rpcSrvc.Start()
require.Nil(t, err)

Expand Down
Loading

0 comments on commit e9d5092

Please sign in to comment.