Skip to content

Commit

Permalink
Production code changes
Browse files Browse the repository at this point in the history
- `internal/runtime/metrics/roothash`
- `internal/runtime/metrics/proof`
- `internal/state/metrics`
  • Loading branch information
qdm12 committed Aug 3, 2022
1 parent 9979cd8 commit cf909d8
Show file tree
Hide file tree
Showing 36 changed files with 549 additions and 105 deletions.
7 changes: 5 additions & 2 deletions dot/build_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ func BuildFromDB(path string) (*BuildSpec, error) {
Telemetry: telemetry.NewNoopMailer(),
}

stateSrvc := state.NewService(config)
stateSrvc, err := state.NewService(config)
if err != nil {
return nil, fmt.Errorf("creating state service: %w", err)
}

err := stateSrvc.SetupBase()
err = stateSrvc.SetupBase()
if err != nil {
return nil, fmt.Errorf("cannot setup state database: %w", err)
}
Expand Down
17 changes: 16 additions & 1 deletion dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -41,7 +42,8 @@ type BlockState interface {
HighestCommonAncestor(a, b common.Hash) (common.Hash, error)
SubChain(start, end common.Hash) ([]common.Hash, error)
GetBlockBody(hash common.Hash) (*types.Body, error)
HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash) error
HandleRuntimeChanges(newState *rtstorage.TrieState, in runtime.Instance, bHash common.Hash,
rootHashMetrics state.RootHashMetrics, proofMetrics state.ProofMetrics) error
GetRuntime(*common.Hash) (runtime.Instance, error)
StoreRuntime(common.Hash, runtime.Instance)
}
Expand Down Expand Up @@ -87,3 +89,16 @@ type CodeSubstitutedState interface {
LoadCodeSubstitutedBlockHash() common.Hash
StoreCodeSubstitutedBlockHash(hash common.Hash) error
}

type TrieMetrics interface {
NodesAdded(n uint32)
NodesDeleted(n uint32)
}

type RootHashMetrics interface {
TrieMetrics
}

type ProofMetrics interface {
TrieMetrics
}
20 changes: 13 additions & 7 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Service struct {

// Keystore
keys *keystore.GlobalKeystore

rootHashMetrics RootHashMetrics
proofMetrics ProofMetrics
}

// Config holds the configuration for the core Service.
Expand All @@ -76,7 +79,8 @@ type Config struct {

// NewService returns a new core service that connects the runtime, BABE
// session, and network service.
func NewService(cfg *Config) (*Service, error) {
func NewService(cfg *Config, rootHashMetrics RootHashMetrics,
proofMetrics ProofMetrics) (*Service, error) {
if cfg.Keystore == nil {
return nil, ErrNilKeystore
}
Expand All @@ -102,7 +106,7 @@ func NewService(cfg *Config) (*Service, error) {
blockAddCh := make(chan *types.Block, 256)

ctx, cancel := context.WithCancel(context.Background())
srv := &Service{
return &Service{
ctx: ctx,
cancel: cancel,
keys: cfg.Keystore,
Expand All @@ -114,9 +118,9 @@ func NewService(cfg *Config) (*Service, error) {
blockAddCh: blockAddCh,
codeSubstitute: cfg.CodeSubstitutes,
codeSubstitutedState: cfg.CodeSubstitutedState,
}

return srv, nil
rootHashMetrics: rootHashMetrics,
proofMetrics: proofMetrics,
}, nil
}

// Start starts the core service
Expand Down Expand Up @@ -216,7 +220,9 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
}

// check for runtime changes
if err := s.blockState.HandleRuntimeChanges(state, rt, block.Header.Hash()); err != nil {
err = s.blockState.HandleRuntimeChanges(state, rt,
block.Header.Hash(), s.rootHashMetrics, s.proofMetrics)
if err != nil {
logger.Criticalf("failed to update runtime code: %s", err)
return err
}
Expand Down Expand Up @@ -272,7 +278,7 @@ func (s *Service) handleCodeSubstitution(hash common.Hash,
cfg.Role = 4
}

next, err := wasmer.NewInstance(code, cfg)
next, err := wasmer.NewInstance(code, cfg, s.rootHashMetrics, s.proofMetrics)
if err != nil {
return fmt.Errorf("creating new runtime instance: %w", err)
}
Expand Down
13 changes: 9 additions & 4 deletions dot/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
statemetrics "github.com/ChainSafe/gossamer/internal/state/metrics"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"

"github.com/ChainSafe/gossamer/internal/log"
)

// ImportState imports the state in the given files to the database with the given path.
Expand All @@ -37,7 +37,11 @@ func ImportState(basepath, stateFP, headerFP string, firstSlot uint64) error {
Path: basepath,
LogLevel: log.Info,
}
srv := state.NewService(config)
srv, err := state.NewService(config)
if err != nil {
return fmt.Errorf("creating state service: %w", err)
}

return srv.Import(header, tr, firstSlot)
}

Expand All @@ -62,7 +66,8 @@ func newTrieFromPairs(filename string) (*trie.Trie, error) {
entries[pairArr[0].(string)] = pairArr[1].(string)
}

tr := trie.NewEmptyTrie()
trieMetrics := statemetrics.NewNoop() // we don't care about metrics
tr := trie.NewEmptyTrie(trieMetrics)
err = tr.LoadFromMap(entries)
if err != nil {
return nil, err
Expand Down
14 changes: 14 additions & 0 deletions dot/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dot

type RootHashMetrics interface {
TrieMetrics
}

type ProofMetrics interface {
TrieMetrics
}

type TrieMetrics interface {
NodesAdded(n uint32)
NodesDeleted(n uint32)
}
40 changes: 32 additions & 8 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/internal/metrics"
proofmetrics "github.com/ChainSafe/gossamer/internal/runtime/metrics/proof"
roothashmetrics "github.com/ChainSafe/gossamer/internal/runtime/metrics/roothash"
statemetrics "github.com/ChainSafe/gossamer/internal/state/metrics"
"github.com/ChainSafe/gossamer/lib/babe"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/genesis"
Expand Down Expand Up @@ -57,10 +60,11 @@ type nodeBuilderIface interface {
error)
createRuntimeStorage(st *state.Service) (*runtime.NodeStorage, error)
loadRuntime(cfg *Config, ns *runtime.NodeStorage, stateSrvc *state.Service, ks *keystore.GlobalKeystore,
net *network.Service) error
net *network.Service, rootHashMetrics RootHashMetrics, proofMetrics ProofMetrics) error
createBlockVerifier(st *state.Service) (*babe.VerificationManager, error)
createDigestHandler(lvl log.Level, st *state.Service) (*digest.Handler, error)
createCoreService(cfg *Config, ks *keystore.GlobalKeystore, st *state.Service, net *network.Service,
createCoreService(cfg *Config, rootHashMetrics RootHashMetrics, proofMetrics ProofMetrics,
ks *keystore.GlobalKeystore, st *state.Service, net *network.Service,
dh *digest.Handler) (*core.Service, error)
createGRANDPAService(cfg *Config, st *state.Service, ks keystore.Keystore,
net *network.Service, telemetryMailer telemetry.Client) (*grandpa.Service, error)
Expand Down Expand Up @@ -144,7 +148,12 @@ func (*nodeBuilder) initNode(cfg *Config) error {
}

// create trie from genesis
t, err := genesis.NewTrieFromGenesis(gen)
stateMetrics, err := statemetrics.NewPrometheus()
if err != nil {
return fmt.Errorf("cannot setup state Prometheus metrics: %w", err)
}

t, err := genesis.NewTrieFromGenesis(gen, stateMetrics)
if err != nil {
return fmt.Errorf("failed to create trie from genesis: %w", err)
}
Expand Down Expand Up @@ -172,7 +181,10 @@ func (*nodeBuilder) initNode(cfg *Config) error {
}

// create new state service
stateSrvc := state.NewService(config)
stateSrvc, err := state.NewService(config)
if err != nil {
return fmt.Errorf("creating state service: %w", err)
}

// initialise state service with genesis data, block, and trie
err = stateSrvc.Initialise(gen, header, t)
Expand Down Expand Up @@ -319,7 +331,18 @@ func newNode(cfg *Config,
return nil, err
}

err = builder.loadRuntime(cfg, ns, stateSrvc, ks, networkSrvc)
rootHashMetrics, err := roothashmetrics.NewPrometheus()
if err != nil {
return nil, fmt.Errorf("creating root hash Prometheus metrics: %s", err)
}

proofMetrics, err := proofmetrics.NewPrometheus()
if err != nil {
return nil, fmt.Errorf("creating proof Prometheus metrics: %s", err)
}

err = builder.loadRuntime(cfg, ns, stateSrvc, ks, networkSrvc,
rootHashMetrics, proofMetrics)
if err != nil {
return nil, err
}
Expand All @@ -335,7 +358,7 @@ func newNode(cfg *Config,
}
nodeSrvcs = append(nodeSrvcs, dh)

coreSrvc, err := builder.createCoreService(cfg, ks, stateSrvc, networkSrvc, dh)
coreSrvc, err := builder.createCoreService(cfg, rootHashMetrics, proofMetrics, ks, stateSrvc, networkSrvc, dh)
if err != nil {
return nil, fmt.Errorf("failed to create core service: %s", err)
}
Expand Down Expand Up @@ -493,7 +516,8 @@ func (n *Node) Stop() {

func (n *nodeBuilder) loadRuntime(cfg *Config, ns *runtime.NodeStorage,
stateSrvc *state.Service, ks *keystore.GlobalKeystore,
net *network.Service) error {
net *network.Service, rootHashMetrics RootHashMetrics,
proofMetrics ProofMetrics) (err error) {
blocks := stateSrvc.Block.GetNonFinalisedBlocks()
runtimeCode := make(map[string]runtime.Instance)
for i := range blocks {
Expand All @@ -513,7 +537,7 @@ func (n *nodeBuilder) loadRuntime(cfg *Config, ns *runtime.NodeStorage,
continue
}

rt, err := createRuntime(cfg, *ns, stateSrvc, ks, net, code)
rt, err := createRuntime(cfg, *ns, stateSrvc, ks, net, code, rootHashMetrics, proofMetrics)
if err != nil {
return err
}
Expand Down
20 changes: 13 additions & 7 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ func (nodeBuilder) createStateService(cfg *Config) (*state.Service, error) {
Metrics: metrics.NewIntervalConfig(cfg.Global.PublishMetrics),
}

stateSrvc := state.NewService(config)
stateSrvc, err := state.NewService(config)
if err != nil {
return nil, fmt.Errorf("creating state service: %s", err)
}

err := stateSrvc.SetupBase()
err = stateSrvc.SetupBase()
if err != nil {
return nil, fmt.Errorf("cannot setup base: %w", err)
}
Expand Down Expand Up @@ -103,8 +106,9 @@ func (nodeBuilder) createRuntimeStorage(st *state.Service) (*runtime.NodeStorage
}

func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service,
ks *keystore.GlobalKeystore, net *network.Service, code []byte) (
runtime.Instance, error) {
ks *keystore.GlobalKeystore, net *network.Service, code []byte,
rootHashMetrics RootHashMetrics, proofMetrics ProofMetrics) (
instance runtime.Instance, err error) {
logger.Info("creating runtime with interpreter " + cfg.Core.WasmInterpreter + "...")

// check if code substitute is in use, if so replace code
Expand Down Expand Up @@ -145,7 +149,7 @@ func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service,
}

// create runtime executor
rt, err = wasmer.NewInstance(code, rtCfg)
rt, err = wasmer.NewInstance(code, rtCfg, rootHashMetrics, proofMetrics)
if err != nil {
return nil, fmt.Errorf("failed to create runtime executor: %s", err)
}
Expand Down Expand Up @@ -219,7 +223,9 @@ func (nodeBuilder) createBABEServiceWithBuilder(cfg *Config, st *state.Service,
// Core Service

// createCoreService creates the core service from the provided core configuration
func (nodeBuilder) createCoreService(cfg *Config, ks *keystore.GlobalKeystore,
func (nodeBuilder) createCoreService(cfg *Config,
rootHashMetrics RootHashMetrics, proofMetrics ProofMetrics,
ks *keystore.GlobalKeystore,
st *state.Service, net *network.Service, dh *digest.Handler) (
*core.Service, error) {
logger.Debug("creating core service" +
Expand Down Expand Up @@ -250,7 +256,7 @@ func (nodeBuilder) createCoreService(cfg *Config, ks *keystore.GlobalKeystore,
}

// create new core service
coreSrvc, err := core.NewService(coreConfig)
coreSrvc, err := core.NewService(coreConfig, rootHashMetrics, proofMetrics)
if err != nil {
logger.Errorf("failed to create core service: %s", err)
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ func (bs *BlockState) setArrivalTime(hash common.Hash, arrivalTime time.Time) er

// HandleRuntimeChanges handles the update in runtime.
func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
rt runtime.Instance, bHash common.Hash) error {
rt runtime.Instance, bHash common.Hash, rootHashMetrics RootHashMetrics,
proofMetrics ProofMetrics) error {
currCodeHash, err := newState.LoadCodeHash()
if err != nil {
return err
Expand Down Expand Up @@ -621,7 +622,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
rtCfg.Role = 4
}

instance, err := wasmer.NewInstance(code, rtCfg)
instance, err := wasmer.NewInstance(code, rtCfg, rootHashMetrics, proofMetrics)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions dot/state/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie
return fmt.Errorf("failed to write genesis values to database: %s", err)
}

tries, err := NewTries(t)
tries, err := NewTries(t, s.stateMetrics)
if err != nil {
return fmt.Errorf("cannot setup tries: %w", err)
}
Expand All @@ -75,7 +75,8 @@ func (s *Service) Initialise(gen *genesis.Genesis, header *types.Header, t *trie
}

// create storage state from genesis trie
storageState, err := NewStorageState(db, blockState, tries, pruner.Config{})
storageState, err := NewStorageState(db, blockState, tries, pruner.Config{},
s.stateMetrics, s.proofMetrics)
if err != nil {
return fmt.Errorf("failed to create storage state from trie: %s", err)
}
Expand Down Expand Up @@ -164,7 +165,7 @@ func (s *Service) CreateGenesisRuntime(t *trie.Trie, gen *genesis.Genesis) (runt
Storage: genTrie,
}

r, err := wasmer.NewRuntimeFromGenesis(rtCfg)
r, err := wasmer.NewRuntimeFromGenesis(rtCfg, s.rootHashMetrics, s.proofMetrics)
if err != nil {
return nil, fmt.Errorf("failed to create genesis runtime: %w", err)
}
Expand Down
18 changes: 18 additions & 0 deletions dot/state/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package state

type Metrics interface {
TrieMetrics
}

type RootHashMetrics interface {
TrieMetrics
}

type ProofMetrics interface {
TrieMetrics
}

type TrieMetrics interface {
NodesAdded(n uint32)
NodesDeleted(n uint32)
}
Loading

0 comments on commit cf909d8

Please sign in to comment.