Skip to content

Commit

Permalink
v0.3.6 fix (ethereum#787)
Browse files Browse the repository at this point in the history
* Fix get validator set in header verifier

* chg : commit tx logs from info to debug (ethereum#673)

* chg : commit tx logs from info to debug

* fix : minor changes

* chg : miner : commitTransactions-stats moved from info to debug

* lint : fix linters

* refactor logging

* miner : chg : UnauthorizedSignerError to debug

* lint : fix lint

* fix : log.Logger interface compatibility

---------

Co-authored-by: Evgeny Danienko <6655321@bk.ru>

* Remove unnecessary sorting of valset from header in verification

* dev: chg: version bump

---------

Co-authored-by: SHIVAM SHARMA <shivam691999@gmail.com>
Co-authored-by: Evgeny Danienko <6655321@bk.ru>
Co-authored-by: marcello33 <marcelloardizzone@hotmail.it>
  • Loading branch information
4 people authored Mar 23, 2023
1 parent 92cd006 commit 72a2220
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 51 deletions.
20 changes: 5 additions & 15 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,18 +464,10 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t

// Verify the validator list match the local contract
if IsSprintStart(number+1, c.config.CalculateSprint(number)) {
parentBlockNumber := number - 1

var newValidators []*valset.Validator

var err error

for newValidators, err = c.spanner.GetCurrentValidators(context.Background(), chain.GetHeaderByNumber(parentBlockNumber).Hash(), number+1); err != nil && parentBlockNumber > 0; {
parentBlockNumber--
}
newValidators, err := c.spanner.GetCurrentValidatorsByBlockNrOrHash(context.Background(), rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber), number+1)

if err != nil {
return errUnknownValidators
return err
}

sort.Sort(valset.ValidatorsByAddress(newValidators))
Expand All @@ -486,8 +478,6 @@ func (c *Bor) verifyCascadingFields(chain consensus.ChainHeaderReader, header *t
return err
}

sort.Sort(valset.ValidatorsByAddress(headerVals))

if len(newValidators) != len(headerVals) {
return errInvalidSpanValidators
}
Expand Down Expand Up @@ -563,7 +553,7 @@ func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash co
hash := checkpoint.Hash()

// get validators and current span
validators, err := c.spanner.GetCurrentValidators(context.Background(), hash, number+1)
validators, err := c.spanner.GetCurrentValidatorsByHash(context.Background(), hash, number+1)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -733,7 +723,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e

// get validator set if number
if IsSprintStart(number+1, c.config.CalculateSprint(number)) {
newValidators, err := c.spanner.GetCurrentValidators(context.Background(), header.ParentHash, number+1)
newValidators, err := c.spanner.GetCurrentValidatorsByHash(context.Background(), header.ParentHash, number+1)
if err != nil {
return errUnknownValidators
}
Expand Down Expand Up @@ -1263,7 +1253,7 @@ func (c *Bor) SetHeimdallClient(h IHeimdallClient) {
}

func (c *Bor) GetCurrentValidators(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*valset.Validator, error) {
return c.spanner.GetCurrentValidators(ctx, headerHash, blockNumber)
return c.spanner.GetCurrentValidatorsByHash(ctx, headerHash, blockNumber)
}

//
Expand Down
13 changes: 8 additions & 5 deletions consensus/bor/heimdall/span/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *ChainSpanner) GetCurrentSpan(ctx context.Context, headerHash common.Has
}

// GetCurrentValidators get current validators
func (c *ChainSpanner) GetCurrentValidators(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*valset.Validator, error) {
func (c *ChainSpanner) GetCurrentValidatorsByBlockNrOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, blockNumber uint64) ([]*valset.Validator, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -107,14 +107,11 @@ func (c *ChainSpanner) GetCurrentValidators(ctx context.Context, headerHash comm
toAddress := c.validatorContractAddress
gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2))

// block
blockNr := rpc.BlockNumberOrHashWithHash(headerHash, false)

result, err := c.ethAPI.Call(ctx, ethapi.TransactionArgs{
Gas: &gas,
To: &toAddress,
Data: &msgData,
}, blockNr, nil)
}, blockNrOrHash, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -144,6 +141,12 @@ func (c *ChainSpanner) GetCurrentValidators(ctx context.Context, headerHash comm
return valz, nil
}

func (c *ChainSpanner) GetCurrentValidatorsByHash(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*valset.Validator, error) {
blockNr := rpc.BlockNumberOrHashWithHash(headerHash, false)

return c.GetCurrentValidatorsByBlockNrOrHash(ctx, blockNr, blockNumber)
}

const method = "commitSpan"

func (c *ChainSpanner) CommitSpan(ctx context.Context, heimdallSpan HeimdallSpan, state *state.StateDB, header *types.Header, chainContext core.ChainContext) error {
Expand Down
4 changes: 3 additions & 1 deletion consensus/bor/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)

//go:generate mockgen -destination=./span_mock.go -package=bor . Spanner
type Spanner interface {
GetCurrentSpan(ctx context.Context, headerHash common.Hash) (*span.Span, error)
GetCurrentValidators(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*valset.Validator, error)
GetCurrentValidatorsByHash(ctx context.Context, headerHash common.Hash, blockNumber uint64) ([]*valset.Validator, error)
GetCurrentValidatorsByBlockNrOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, blockNumber uint64) ([]*valset.Validator, error)
CommitSpan(ctx context.Context, heimdallSpan span.HeimdallSpan, state *state.StateDB, header *types.Header, chainContext core.ChainContext) error
}
46 changes: 31 additions & 15 deletions consensus/bor/span_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/tests/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) {
ethAPIMock.EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

spanner := bor.NewMockSpanner(ctrl)
spanner.EXPECT().GetCurrentValidators(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*valset.Validator{
spanner.EXPECT().GetCurrentValidatorsByHash(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*valset.Validator{
{
ID: 0,
Address: miner.TestBankAddress,
Expand Down
32 changes: 32 additions & 0 deletions internal/testlog/testlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,35 @@ func (l *logger) flush() {
}
l.h.buf = nil
}

func (l *logger) OnTrace(fn func(l log.Logging)) {
if l.GetHandler().Level() >= log.LvlTrace {
fn(l.Trace)
}
}

func (l *logger) OnDebug(fn func(l log.Logging)) {
if l.GetHandler().Level() >= log.LvlDebug {
fn(l.Debug)
}
}
func (l *logger) OnInfo(fn func(l log.Logging)) {
if l.GetHandler().Level() >= log.LvlInfo {
fn(l.Info)
}
}
func (l *logger) OnWarn(fn func(l log.Logging)) {
if l.GetHandler().Level() >= log.LvlWarn {
fn(l.Warn)
}
}
func (l *logger) OnError(fn func(l log.Logging)) {
if l.GetHandler().Level() >= log.LvlError {
fn(l.Error)
}
}
func (l *logger) OnCrit(fn func(l log.Logging)) {
if l.GetHandler().Level() >= log.LvlCrit {
fn(l.Crit)
}
}
41 changes: 41 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type RecordKeyNames struct {
Ctx string
}

type Logging func(msg string, ctx ...interface{})

// A Logger writes key/value pairs to a Handler
type Logger interface {
// New returns a new Logger that has this logger's context plus the given context
Expand All @@ -124,6 +126,13 @@ type Logger interface {
Warn(msg string, ctx ...interface{})
Error(msg string, ctx ...interface{})
Crit(msg string, ctx ...interface{})

OnTrace(func(l Logging))
OnDebug(func(l Logging))
OnInfo(func(l Logging))
OnWarn(func(l Logging))
OnError(func(l Logging))
OnCrit(func(l Logging))
}

type logger struct {
Expand Down Expand Up @@ -198,6 +207,38 @@ func (l *logger) SetHandler(h Handler) {
l.h.Swap(h)
}

func (l *logger) OnTrace(fn func(l Logging)) {
if l.GetHandler().Level() >= LvlTrace {
fn(l.Trace)
}
}

func (l *logger) OnDebug(fn func(l Logging)) {
if l.GetHandler().Level() >= LvlDebug {
fn(l.Debug)
}
}
func (l *logger) OnInfo(fn func(l Logging)) {
if l.GetHandler().Level() >= LvlInfo {
fn(l.Info)
}
}
func (l *logger) OnWarn(fn func(l Logging)) {
if l.GetHandler().Level() >= LvlWarn {
fn(l.Warn)
}
}
func (l *logger) OnError(fn func(l Logging)) {
if l.GetHandler().Level() >= LvlError {
fn(l.Error)
}
}
func (l *logger) OnCrit(fn func(l Logging)) {
if l.GetHandler().Level() >= LvlCrit {
fn(l.Crit)
}
}

func normalize(ctx []interface{}) []interface{} {
// if the caller passed a Ctx object, then expand it
if len(ctx) == 1 {
Expand Down
32 changes: 32 additions & 0 deletions log/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ func Crit(msg string, ctx ...interface{}) {
os.Exit(1)
}

func OnTrace(fn func(l Logging)) {
if root.GetHandler().Level() >= LvlTrace {
fn(root.Trace)
}
}

func OnDebug(fn func(l Logging)) {
if root.GetHandler().Level() >= LvlDebug {
fn(root.Debug)
}
}
func OnInfo(fn func(l Logging)) {
if root.GetHandler().Level() >= LvlInfo {
fn(root.Info)
}
}
func OnWarn(fn func(l Logging)) {
if root.GetHandler().Level() >= LvlWarn {
fn(root.Warn)
}
}
func OnError(fn func(l Logging)) {
if root.GetHandler().Level() >= LvlError {
fn(root.Error)
}
}
func OnCrit(fn func(l Logging)) {
if root.GetHandler().Level() >= LvlCrit {
fn(root.Crit)
}
}

// Output is a convenient alias for write, allowing for the modification of
// the calldepth (number of stack frames to skip).
// calldepth influences the reported line number of the log message.
Expand Down
2 changes: 1 addition & 1 deletion miner/fake_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewBorDefaultMiner(t *testing.T) *DefaultBorMiner {
ethAPI.EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()

spanner := bor.NewMockSpanner(ctrl)
spanner.EXPECT().GetCurrentValidators(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*valset.Validator{
spanner.EXPECT().GetCurrentValidatorsByHash(gomock.Any(), gomock.Any(), gomock.Any()).Return([]*valset.Validator{
{
ID: 0,
Address: common.Address{0x1},
Expand Down
Loading

0 comments on commit 72a2220

Please sign in to comment.