Skip to content

Commit

Permalink
chore(dot/digest): add digest to loglevel config (#2080)
Browse files Browse the repository at this point in the history
* 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 <elizabethjbinks@gmail.com>
  • Loading branch information
1garo and noot committed Jan 31, 2022
1 parent 479ef1b commit 6c122b2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions chain/dev/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ runtime = ""
babe = ""
grandpa = ""
sync = ""
digest = ""

[init]
genesis = "./chain/dev/genesis-spec.json"
Expand Down
1 change: 1 addition & 0 deletions chain/gssmr/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ runtime = ""
babe = ""
grandpa = ""
sync = ""
digest = ""

[init]
genesis = "./chain/gssmr/genesis-spec.json"
Expand Down
1 change: 1 addition & 0 deletions chain/kusama/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ runtime = ""
babe = ""
grandpa = ""
sync = ""
digest = ""

[init]
genesis = "./chain/kusama/genesis.json"
Expand Down
1 change: 1 addition & 0 deletions chain/polkadot/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ runtime = ""
babe = ""
grandpa = ""
sync = ""
digest = ""

[init]
genesis = "./chain/polkadot/genesis.json"
Expand Down
37 changes: 20 additions & 17 deletions dot/digest/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +40,8 @@ type Handler struct {
grandpaForcedChange *grandpaChange
grandpaPause *pause
grandpaResume *resume

logger log.LeveledLogger
}

type grandpaChange struct {
Expand All @@ -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())
Expand All @@ -73,6 +75,7 @@ func NewHandler(lvl log.Level, blockState BlockState, epochState EpochState,
grandpaState: grandpaState,
imported: imported,
finalised: finalised,
logger: logger,
}, nil
}

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand All @@ -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())
Expand Down
1 change: 0 additions & 1 deletion dot/digest/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func newTestHandler(t *testing.T) *Handler {

config := state.Config{
Path: testDatadirPath,
LogLevel: log.Info,
Telemetry: telemetryMock,
}
stateSrvc := state.NewService(config)
Expand Down
26 changes: 23 additions & 3 deletions dot/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

0 comments on commit 6c122b2

Please sign in to comment.