Skip to content

Commit

Permalink
Log all levels if round > 1 (ethereum#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasege authored and celo-ci-bot-user committed Jan 15, 2020
1 parent f86d673 commit 0a92f55
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
23 changes: 16 additions & 7 deletions consensus/istanbul/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ func New(config *istanbul.Config, db ethdb.Database) consensus.Istanbul {
}
backend.core = istanbulCore.New(backend, backend.config)

backend.logger = istanbul.NewIstLogger(
func() *big.Int {
if backend.core != nil && backend.core.CurrentView() != nil {
return backend.core.CurrentView().Round
}
return common.Big0
},
)

vph := &validatorPeerHandler{sb: backend}
table, err := enodes.OpenValidatorEnodeDB(config.ValidatorEnodeDBPath, vph)
if err != nil {
Expand Down Expand Up @@ -463,7 +472,7 @@ func (sb *Backend) Verify(proposal istanbul.Proposal) (time.Duration, error) {
// Get the state from this block's parent.
state, err := sb.stateAt(block.Header().ParentHash)
if err != nil {
log.Error("verify - Error in getting the block's parent's state", "parentHash", block.Header().ParentHash.Hex(), "err", err)
sb.logger.Error("verify - Error in getting the block's parent's state", "parentHash", block.Header().ParentHash.Hex(), "err", err)
return 0, err
}

Expand All @@ -473,20 +482,20 @@ func (sb *Backend) Verify(proposal istanbul.Proposal) (time.Duration, error) {
// Apply this block's transactions to update the state
receipts, _, usedGas, err := sb.processBlock(block, state)
if err != nil {
log.Error("verify - Error in processing the block", "err", err)
sb.logger.Error("verify - Error in processing the block", "err", err)
return 0, err
}

// Validate the block
if err := sb.validateState(block, state, receipts, usedGas); err != nil {
log.Error("verify - Error in validating the block", "err", err)
sb.logger.Error("verify - Error in validating the block", "err", err)
return 0, err
}

// verify the validator set diff if this is the last block of the epoch
if istanbul.IsLastBlockOfEpoch(block.Header().Number.Uint64(), sb.config.Epoch) {
if err := sb.verifyValSetDiff(proposal, block, state); err != nil {
log.Error("verify - Error in verifying the val set diff", "err", err)
sb.logger.Error("verify - Error in verifying the val set diff", "err", err)
return 0, err
}
}
Expand Down Expand Up @@ -515,7 +524,7 @@ func (sb *Backend) verifyValSetDiff(proposal istanbul.Proposal, block *types.Blo
newValSet, err := sb.getNewValidatorSet(block.Header(), state)
if err != nil {
if len(istExtra.AddedValidators) != 0 || istExtra.RemovedValidators.BitLen() != 0 {
log.Error("verifyValSetDiff - Invalid val set diff. Non empty diff when it should be empty.", "addedValidators", common.ConvertToStringSlice(istExtra.AddedValidators), "removedValidators", istExtra.RemovedValidators.Text(16))
sb.logger.Error("verifyValSetDiff - Invalid val set diff. Non empty diff when it should be empty.", "addedValidators", common.ConvertToStringSlice(istExtra.AddedValidators), "removedValidators", istExtra.RemovedValidators.Text(16))
return errInvalidValidatorSetDiff
}
} else {
Expand All @@ -539,7 +548,7 @@ func (sb *Backend) verifyValSetDiff(proposal istanbul.Proposal, block *types.Blo
}

if !istanbul.CompareValidatorSlices(addedValidatorsAddresses, istExtra.AddedValidators) || removedValidators.Cmp(istExtra.RemovedValidators) != 0 || !istanbul.CompareValidatorPublicKeySlices(addedValidatorsPublicKeys, istExtra.AddedValidatorsPublicKeys) {
log.Error("verifyValSetDiff - Invalid val set diff. Comparison failed. ", "got addedValidators", common.ConvertToStringSlice(istExtra.AddedValidators), "got removedValidators", istExtra.RemovedValidators.Text(16), "got addedValidatorsPublicKeys", istanbul.ConvertPublicKeysToStringSlice(istExtra.AddedValidatorsPublicKeys), "expected addedValidators", common.ConvertToStringSlice(addedValidatorsAddresses), "expected removedValidators", removedValidators.Text(16), "expected addedValidatorsPublicKeys", istanbul.ConvertPublicKeysToStringSlice(addedValidatorsPublicKeys))
sb.logger.Error("verifyValSetDiff - Invalid val set diff. Comparison failed. ", "got addedValidators", common.ConvertToStringSlice(istExtra.AddedValidators), "got removedValidators", istExtra.RemovedValidators.Text(16), "got addedValidatorsPublicKeys", istanbul.ConvertPublicKeysToStringSlice(istExtra.AddedValidatorsPublicKeys), "expected addedValidators", common.ConvertToStringSlice(addedValidatorsAddresses), "expected removedValidators", removedValidators.Text(16), "expected addedValidatorsPublicKeys", istanbul.ConvertPublicKeysToStringSlice(addedValidatorsPublicKeys))
return errInvalidValidatorSetDiff
}
}
Expand Down Expand Up @@ -571,7 +580,7 @@ func (sb *Backend) SignBlockHeader(data []byte) ([]byte, error) {
func (sb *Backend) CheckSignature(data []byte, address common.Address, sig []byte) error {
signer, err := istanbul.GetSignatureAddress(data, sig)
if err != nil {
log.Error("Failed to get signer address", "err", err)
sb.logger.Error("Failed to get signer address", "err", err)
return err
}
// Compare derived addresses
Expand Down
8 changes: 8 additions & 0 deletions consensus/istanbul/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func New(backend istanbul.Backend, config *istanbul.Config) Engine {
}, c.checkMessage)
c.backlog = msgBacklog
c.validateFn = c.checkValidatorSignature
c.logger = istanbul.NewIstLogger(
func() *big.Int {
if c != nil && c.current != nil {
return c.current.Round()
}
return common.Big0
},
)
return c
}

Expand Down
80 changes: 80 additions & 0 deletions consensus/istanbul/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2017 The celo Authors
// This file is part of the celo library.
//
// The celo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The celo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the celo library. If not, see <http://www.gnu.org/licenses/>.

package istanbul

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"math/big"
)

type istLogger struct {
logger log.Logger
round func() *big.Int
}

// NewIstLogger creates an Istanbul Logger with custom logic for exposing logs
func NewIstLogger(fn func() *big.Int, ctx ...interface{}) log.Logger {
return &istLogger{logger: log.New(ctx...), round: fn}
}

func (l *istLogger) New(ctx ...interface{}) log.Logger {
childLogger := l.logger.New(ctx...)
return &istLogger{logger: childLogger, round: l.round}
}

func (l *istLogger) Trace(msg string, ctx ...interface{}) {
// If the current round > 1, then upgrade this message to Info
if l.round != nil && l.round() != nil && l.round().Cmp(common.Big1) > 0 {
l.Info(msg, ctx...)
} else {
l.logger.Trace(msg, ctx...)
}
}

func (l *istLogger) Debug(msg string, ctx ...interface{}) {
// If the current round > 1, then upgrade this message to Info
if l.round != nil && l.round() != nil && l.round().Cmp(common.Big1) > 0 {
l.Info(msg, ctx...)
} else {
l.logger.Debug(msg, ctx...)
}
}

func (l *istLogger) Info(msg string, ctx ...interface{}) {
l.logger.Info(msg, ctx...)
}

func (l *istLogger) Warn(msg string, ctx ...interface{}) {
l.logger.Warn(msg, ctx...)
}

func (l *istLogger) Error(msg string, ctx ...interface{}) {
l.logger.Error(msg, ctx...)
}

func (l *istLogger) Crit(msg string, ctx ...interface{}) {
l.logger.Crit(msg, ctx...)
}

func (l *istLogger) GetHandler() log.Handler {
return l.logger.GetHandler()
}

func (l *istLogger) SetHandler(h log.Handler) {
l.logger.SetHandler(h)
}

0 comments on commit 0a92f55

Please sign in to comment.