Skip to content

Commit d8ee7a4

Browse files
committed
dbft: add ability to customize dBFT logs verbosity
Close #125. Use a separate flag since we need to be able to customize dBFT log level irrespectively of the node's log level. Example of setting: ``` --dbft.loglevel debug ``` `info` is used by default: ``` 2025-02-18T20:27:27.351+0300 INFO received PrepareRequest {"validator": 3, "tx": 0} 2025-02-18T20:27:27.351+0300 INFO sending PrepareResponse {"height": 7, "view": 0} 2025-02-18T20:27:27.351+0300 INFO received PrepareResponse {"validator": 2} 2025-02-18T20:27:27.351+0300 INFO sending Commit {"height": 7, "view": 0} 2025-02-18T20:27:27.352+0300 INFO received PrepareResponse {"validator": 1} 2025-02-18T20:27:27.352+0300 INFO received Commit {"validator": 2} 2025-02-18T20:27:27.352+0300 INFO received Commit {"validator": 1} 2025-02-18T20:27:27.352+0300 INFO approving block {"height": 7, "hash": "0xa1f58afe5a7f7cc35f6506162c88097b759d2b101e8f8ae61da71bfba9a644b3", "tx_count": 0, "merkle": "0xe30caa43e5c2608a9940137c8ba34686767539c8becb2c6bdef678b95e0f9aea", "prev": "0x12ac0408a1d49dcded5ef43414aeda5f71fff66bc57b5f1791ab5b22b6f579e2"} INFO [02-18|20:27:27.354] Chain reorg detected number=5 hash=e612ec..a71dd2 drop=1 dropfrom=9cb4a2..346b88 add=2 addfrom=61b7c1..6e0c78 INFO [02-18|20:27:27.356] Successfully wrote new block with state number=7 hash=61b7c1..6e0c78 ``` Signed-off-by: Anna Shaleva <[email protected]>
1 parent b27f46e commit d8ee7a4

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var (
131131
utils.AntiMEVKeyStoreFlag,
132132
utils.AntiMEVEnforceECDSABlockSignatureSchemeFlag,
133133
utils.AntiMEVPasswordFlag,
134+
utils.DBFTLogLevelFlag,
134135
utils.NATFlag,
135136
utils.NoDiscoverFlag,
136137
utils.DiscoveryV4Flag,

cmd/utils/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,11 @@ var (
587587
Usage: "Disables db compaction after import",
588588
Category: flags.LoggingCategory,
589589
}
590+
DBFTLogLevelFlag = &cli.StringFlag{
591+
Name: "dbft.loglevel",
592+
Usage: `Logging level to use for dBFT consensus engine events. The following values are supported: "debug", "info" (default), "warn", "error", "dpanic", "panic", "fatal"`,
593+
Category: flags.LoggingCategory,
594+
}
590595

591596
// MISC settings
592597
SyncTargetFlag = &cli.StringFlag{
@@ -1742,6 +1747,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
17421747
if ctx.IsSet(AntiMEVEnforceECDSABlockSignatureSchemeFlag.Name) {
17431748
cfg.AntiMEVEnforceECDSABlockSignatureScheme = ctx.Bool(AntiMEVEnforceECDSABlockSignatureSchemeFlag.Name)
17441749
}
1750+
if ctx.IsSet(DBFTLogLevelFlag.Name) {
1751+
cfg.DBFTLogLevel = ctx.String(DBFTLogLevelFlag.Name)
1752+
}
17451753
if !ctx.Bool(SnapshotFlag.Name) || cfg.SnapshotCache == 0 {
17461754
// If snap-sync is requested, this flag is also required
17471755
if cfg.SyncMode == downloader.SnapSync {

consensus/dbft/dbft.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import (
6363
"github.com/nspcc-dev/dbft"
6464
"github.com/nspcc-dev/dbft/timer"
6565
"go.uber.org/zap"
66+
"go.uber.org/zap/zapcore"
6667
"golang.org/x/crypto/sha3"
6768
"golang.org/x/exp/slices"
6869
)
@@ -234,6 +235,7 @@ type config struct {
234235
dkgEnablingHeight int64
235236
antiMEVEnablingHeight int64
236237
enforceECDSASignatures bool
238+
logLevel *zap.AtomicLevel
237239
}
238240

239241
// New creates a DBFT proof-of-authority consensus engine with the initial
@@ -284,10 +286,11 @@ func New(chainCfg *params.ChainConfig, _ ethdb.Database) (*DBFT, error) {
284286
}
285287

286288
var err error
287-
logger, err := zap.NewDevelopment()
289+
logger, logLevel, err := buildLogger()
288290
if err != nil {
289291
return nil, fmt.Errorf("failed to initialize dBFT logger: %w", err)
290292
}
293+
c.config.logLevel = logLevel
291294
dbftCfg := []func(*dbft.Config[common.Hash]){
292295
dbft.WithTimer[common.Hash](timer.New()),
293296
dbft.WithLogger[common.Hash](logger),
@@ -333,6 +336,23 @@ func New(chainCfg *params.ChainConfig, _ ethdb.Database) (*DBFT, error) {
333336
return c, nil
334337
}
335338

339+
// buildLogger builds zap logger for dbft library events logging with Info level
340+
// used as a default.
341+
func buildLogger() (*zap.Logger, *zap.AtomicLevel, error) {
342+
cc := zap.NewProductionConfig()
343+
cc.DisableCaller = true
344+
cc.DisableStacktrace = true
345+
cc.EncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
346+
cc.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
347+
cc.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
348+
cc.Encoding = "console"
349+
cc.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
350+
cc.Sampling = nil
351+
352+
log, err := cc.Build()
353+
return log, &cc.Level, err
354+
}
355+
336356
// getKeyPairCb is a dbft library setting callback.
337357
func (c *DBFT) getKeyPairCb(keys []dbft.PublicKey) (int, dbft.PrivateKey, dbft.PublicKey) {
338358
c.lock.RLock()
@@ -1356,6 +1376,20 @@ func (c *DBFT) WithEthAPIBackend(b ethapi.Backend) {
13561376
c.txAPI = ethapi.NewTransactionAPI(b, new(ethapi.AddrLocker))
13571377
}
13581378

1379+
// WithLogLevel updates dBFT logger log level to the provided one if specified.
1380+
func (c *DBFT) WithLogLevel(logLevel string) error {
1381+
if len(logLevel) == 0 {
1382+
return nil
1383+
}
1384+
1385+
level, err := zapcore.ParseLevel(logLevel)
1386+
if err != nil {
1387+
return fmt.Errorf("unexpected dBFT log level %s: %w", logLevel, err)
1388+
}
1389+
c.config.logLevel.SetLevel(level)
1390+
return nil
1391+
}
1392+
13591393
// EnforceECDSASignatures enforces ECDSA multisignature block signing scheme.
13601394
// This setting will be applied starting from NeoXAMEV fork.
13611395
func (c *DBFT) EnforceECDSASignatures() {

eth/backend.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
300300
bft.WithTxPool(eth.TxPool())
301301
bft.WithRequestTxs(eth.handler.BroadcastRequestTxs)
302302
bft.WithMux(eth.EventMux())
303+
err := bft.WithLogLevel(config.DBFTLogLevel)
304+
if err != nil {
305+
return nil, err
306+
}
303307
if config.AntiMEVEnforceECDSABlockSignatureScheme {
304308
bft.EnforceECDSASignatures()
305309
}

eth/ethconfig/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ type Config struct {
165165
// AntiMEVEnforceECDSABlockSignatureScheme enables ECDSA multisignatures for
166166
// block signing instead of threshold-based.
167167
AntiMEVEnforceECDSABlockSignatureScheme bool `toml:",omitempty"`
168+
169+
// DBFTLogLevel is a dBFT engine log level. The following values are allowed:
170+
// "debug", "info", "warn", "error", "dpanic", "panic", "fatal". If not specified,
171+
// "info" is used.
172+
DBFTLogLevel string `toml:",omitempty"`
168173
}
169174

170175
// CreateConsensusEngine creates a consensus engine for the given chain config.

0 commit comments

Comments
 (0)