From 6c122b26d60be999a7363ee5905ef0e6a5f62064 Mon Sep 17 00:00:00 2001 From: Alexandre Vardai <44412643+1garo@users.noreply.github.com> Date: Mon, 31 Jan 2022 15:58:00 -0300 Subject: [PATCH] chore(dot/digest): add digest to `loglevel` config (#2080) * add digest in config files * add digest lvl inside struct * add log in handler and use it instead of global * pass log in handler creation and add tests for it * rename to be more clear * remove useless comment * update loglevel on state cfg and on digest logger * fix: add Telemetry on state.Config * fix unit test Co-authored-by: noot --- chain/dev/config.toml | 1 + chain/gssmr/config.toml | 1 + chain/kusama/config.toml | 1 + chain/polkadot/config.toml | 1 + dot/digest/digest.go | 37 ++++++++++++++++++++----------------- dot/digest/digest_test.go | 1 - dot/services_test.go | 26 +++++++++++++++++++++++--- 7 files changed, 47 insertions(+), 21 deletions(-) diff --git a/chain/dev/config.toml b/chain/dev/config.toml index 9e30f02c91..f7bdce58a4 100644 --- a/chain/dev/config.toml +++ b/chain/dev/config.toml @@ -12,6 +12,7 @@ runtime = "" babe = "" grandpa = "" sync = "" +digest = "" [init] genesis = "./chain/dev/genesis-spec.json" diff --git a/chain/gssmr/config.toml b/chain/gssmr/config.toml index 80301de7d2..e26c8cb37c 100644 --- a/chain/gssmr/config.toml +++ b/chain/gssmr/config.toml @@ -12,6 +12,7 @@ runtime = "" babe = "" grandpa = "" sync = "" +digest = "" [init] genesis = "./chain/gssmr/genesis-spec.json" diff --git a/chain/kusama/config.toml b/chain/kusama/config.toml index 37aabc8165..6e7dfb88a1 100644 --- a/chain/kusama/config.toml +++ b/chain/kusama/config.toml @@ -12,6 +12,7 @@ runtime = "" babe = "" grandpa = "" sync = "" +digest = "" [init] genesis = "./chain/kusama/genesis.json" diff --git a/chain/polkadot/config.toml b/chain/polkadot/config.toml index 86dc72ad6e..24271f884e 100644 --- a/chain/polkadot/config.toml +++ b/chain/polkadot/config.toml @@ -12,6 +12,7 @@ runtime = "" babe = "" grandpa = "" sync = "" +digest = "" [init] genesis = "./chain/polkadot/genesis.json" diff --git a/dot/digest/digest.go b/dot/digest/digest.go index 078223f4b3..ef273585bc 100644 --- a/dot/digest/digest.go +++ b/dot/digest/digest.go @@ -18,8 +18,7 @@ import ( var maxUint64 = uint64(math.MaxUint64) var ( - _ services.Service = &Handler{} - logger = log.NewFromGlobal(log.AddContext("pkg", "digest")) // TODO: add to config options (#1851) + _ services.Service = &Handler{} ) // Handler is used to handle consensus messages and relevant authority updates to BABE and GRANDPA @@ -41,6 +40,8 @@ type Handler struct { grandpaForcedChange *grandpaChange grandpaPause *pause grandpaResume *resume + + logger log.LeveledLogger } type grandpaChange struct { @@ -62,6 +63,7 @@ func NewHandler(lvl log.Level, blockState BlockState, epochState EpochState, imported := blockState.GetImportedBlockNotifierChannel() finalised := blockState.GetFinalisedNotifierChannel() + logger := log.NewFromGlobal(log.AddContext("pkg", "digest")) logger.Patch(log.SetLevel(lvl)) ctx, cancel := context.WithCancel(context.Background()) @@ -73,6 +75,7 @@ func NewHandler(lvl log.Level, blockState BlockState, epochState EpochState, grandpaState: grandpaState, imported: imported, finalised: finalised, + logger: logger, }, nil } @@ -122,7 +125,7 @@ func (h *Handler) HandleDigests(header *types.Header) { if ok { err := h.handleConsensusDigest(&val, header) if err != nil { - logger.Errorf("cannot handle digests for block number %s, index %d, digest %s: %s", + h.logger.Errorf("cannot handle digests for block number %s, index %d, digest %s: %s", header.Number, i, d.Value(), err) } } @@ -178,12 +181,12 @@ func (h *Handler) handleGrandpaConsensusDigest(digest scale.VaryingDataType, hea func (h *Handler) handleBabeConsensusDigest(digest scale.VaryingDataType, header *types.Header) error { switch val := digest.Value().(type) { case types.NextEpochData: - logger.Debugf("handling BABENextEpochData data: %v", digest) + h.logger.Debugf("handling BABENextEpochData data: %v", digest) return h.handleNextEpochData(val, header) case types.BABEOnDisabled: return h.handleBABEOnDisabled(val, header) case types.NextConfigData: - logger.Debugf("handling BABENextConfigData data: %v", digest) + h.logger.Debugf("handling BABENextConfigData data: %v", digest) return h.handleNextConfigData(val, header) } @@ -200,7 +203,7 @@ func (h *Handler) handleBlockImport(ctx context.Context) { err := h.handleGrandpaChangesOnImport(block.Header.Number) if err != nil { - logger.Errorf("failed to handle grandpa changes on block import: %s", err) + h.logger.Errorf("failed to handle grandpa changes on block import: %s", err) } case <-ctx.Done(): return @@ -218,7 +221,7 @@ func (h *Handler) handleBlockFinalisation(ctx context.Context) { err := h.handleGrandpaChangesOnFinalization(info.Header.Number) if err != nil { - logger.Errorf("failed to handle grandpa changes on block finalisation: %s", err) + h.logger.Errorf("failed to handle grandpa changes on block finalisation: %s", err) } case <-ctx.Done(): return @@ -245,7 +248,7 @@ func (h *Handler) handleGrandpaChangesOnImport(num *big.Int) error { return err } - logger.Debugf("incremented grandpa set id %d", curr) + h.logger.Debugf("incremented grandpa set id %d", curr) } return nil @@ -270,7 +273,7 @@ func (h *Handler) handleGrandpaChangesOnFinalization(num *big.Int) error { return err } - logger.Debugf("incremented grandpa set id %d", curr) + h.logger.Debugf("incremented grandpa set id %d", curr) } // if blocks get finalised before forced change takes place, disregard it @@ -288,7 +291,7 @@ func (h *Handler) handleScheduledChange(sc types.GrandpaScheduledChange, header return nil } - logger.Debugf("handling GrandpaScheduledChange data: %v", sc) + h.logger.Debugf("handling GrandpaScheduledChange data: %v", sc) c, err := newGrandpaChange(sc.Auths, sc.Delay, curr.Number) if err != nil { @@ -301,7 +304,7 @@ func (h *Handler) handleScheduledChange(sc types.GrandpaScheduledChange, header if err != nil { return err } - logger.Debugf("setting GrandpaScheduledChange at block %s", + h.logger.Debugf("setting GrandpaScheduledChange at block %s", big.NewInt(0).Add(header.Number, big.NewInt(int64(sc.Delay)))) return h.grandpaState.SetNextChange( types.NewGrandpaVotersFromAuthorities(auths), @@ -318,7 +321,7 @@ func (h *Handler) handleForcedChange(fc types.GrandpaForcedChange, header *types return errors.New("already have forced change scheduled") } - logger.Debugf("handling GrandpaForcedChange with data %v", fc) + h.logger.Debugf("handling GrandpaForcedChange with data %v", fc) c, err := newGrandpaChange(fc.Auths, fc.Delay, header.Number) if err != nil { @@ -332,7 +335,7 @@ func (h *Handler) handleForcedChange(fc types.GrandpaForcedChange, header *types return err } - logger.Debugf("setting GrandpaForcedChange at block %s", + h.logger.Debugf("setting GrandpaForcedChange at block %s", big.NewInt(0).Add(header.Number, big.NewInt(int64(fc.Delay)))) return h.grandpaState.SetNextChange( types.NewGrandpaVotersFromAuthorities(auths), @@ -383,8 +386,8 @@ func newGrandpaChange(raw []types.GrandpaAuthoritiesRaw, delay uint32, currBlock }, nil } -func (h *Handler) handleBABEOnDisabled(d types.BABEOnDisabled, _ *types.Header) error { - logger.Debug("handling BABEOnDisabled") +func (h *Handler) handleBABEOnDisabled(_ types.BABEOnDisabled, _ *types.Header) error { + h.logger.Debug("handling BABEOnDisabled") return nil } @@ -400,7 +403,7 @@ func (h *Handler) handleNextEpochData(act types.NextEpochData, header *types.Hea return err } - logger.Debugf("setting data for block number %s and epoch %d with data: %v", + h.logger.Debugf("setting data for block number %s and epoch %d with data: %v", header.Number, currEpoch+1, data) return h.epochState.SetEpochData(currEpoch+1, data) } @@ -411,7 +414,7 @@ func (h *Handler) handleNextConfigData(config types.NextConfigData, header *type return err } - logger.Debugf("setting BABE config data for block number %s and epoch %d with data: %v", + h.logger.Debugf("setting BABE config data for block number %s and epoch %d with data: %v", header.Number, currEpoch+1, config.ToConfigData()) // set EpochState config data for upcoming epoch return h.epochState.SetConfigData(currEpoch+1, config.ToConfigData()) diff --git a/dot/digest/digest_test.go b/dot/digest/digest_test.go index a20574a07a..f701cb54c6 100644 --- a/dot/digest/digest_test.go +++ b/dot/digest/digest_test.go @@ -33,7 +33,6 @@ func newTestHandler(t *testing.T) *Handler { config := state.Config{ Path: testDatadirPath, - LogLevel: log.Info, Telemetry: telemetryMock, } stateSrvc := state.NewService(config) diff --git a/dot/services_test.go b/dot/services_test.go index c5f0d220d8..b83636ca0e 100644 --- a/dot/services_test.go +++ b/dot/services_test.go @@ -398,10 +398,30 @@ func TestNewWebSocketServer(t *testing.T) { func Test_createPprofService(t *testing.T) { t.Parallel() - settings := pprof.Settings{} - service := createPprofService(settings) - require.NotNil(t, service) } + +func Test_createDigestHandler(t *testing.T) { + cfg := NewTestConfig(t) + require.NotNil(t, cfg) + + genFile := NewTestGenesisRawFile(t, cfg) + + cfg.Core.Roles = types.AuthorityRole + cfg.Init.Genesis = genFile + + err := InitNode(cfg) + require.NoError(t, err) + + stateSrvc, err := createStateService(cfg) + require.NoError(t, err) + + err = startStateService(cfg, stateSrvc) + require.NoError(t, err) + + _, err = createDigestHandler(cfg.Log.DigestLvl, stateSrvc) + require.NoError(t, err) + +}