Skip to content

Commit

Permalink
bor: remove duplicate validator_set file and debug logger (erigontech…
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr authored and racytech committed Jan 12, 2024
1 parent 5c66868 commit af78099
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 753 deletions.
4 changes: 2 additions & 2 deletions cmd/devnet/services/polygon/heimdall.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (h *Heimdall) addValidator(validatorAddress libcommon.Address, votingPower
VotingPower: votingPower,
ProposerPriority: proposerPriority,
},
}, h.logger)
})
} else {
h.validatorSet.UpdateWithChangeSet([]*valset.Validator{
{
Expand All @@ -366,7 +366,7 @@ func (h *Heimdall) addValidator(validatorAddress libcommon.Address, votingPower
VotingPower: votingPower,
ProposerPriority: proposerPriority,
},
}, h.logger)
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions eth/stagedsync/stagedsynctest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func InitHarness(ctx context.Context, t *testing.T, cfg HarnessCfg) Harness {
}

if cfg.ChainConfig.Bor != nil {
h.setHeimdallNextMockSpan(logger)
h.setHeimdallNextMockSpan()
h.mockBorSpanner()
h.mockHeimdallClient()
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func (h *Harness) mockChainHeaderReader(ctrl *gomock.Controller) consensus.Chain
return mockChainHR
}

func (h *Harness) setHeimdallNextMockSpan(logger log.Logger) {
func (h *Harness) setHeimdallNextMockSpan() {
validators := []*valset.Validator{
{
ID: 1,
Expand All @@ -511,7 +511,7 @@ func (h *Harness) setHeimdallNextMockSpan(logger log.Logger) {
},
}

validatorSet := valset.NewValidatorSet(validators, logger)
validatorSet := valset.NewValidatorSet(validators)
selectedProducers := make([]valset.Validator, len(validators))
for i := range validators {
selectedProducers[i] = *validators[i]
Expand Down
2 changes: 1 addition & 1 deletion polygon/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ func getUpdatedValidatorSet(oldValidatorSet *valset.ValidatorSet, newVals []*val
}
}

if err := v.UpdateWithChangeSet(changes, logger); err != nil {
if err := v.UpdateWithChangeSet(changes); err != nil {
logger.Error("[bor] Error while updating change set", "error", err)
}

Expand Down
4 changes: 2 additions & 2 deletions polygon/bor/bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func newValidator(t *testing.T, heimdall *test_heimdall, blocks map[uint64]*type
VotingPower: 1000,
ProposerPriority: 1,
},
}, logger)
})
} else {
heimdall.validatorSet.UpdateWithChangeSet([]*valset.Validator{
{
Expand All @@ -315,7 +315,7 @@ func newValidator(t *testing.T, heimdall *test_heimdall, blocks map[uint64]*type
VotingPower: 1000,
ProposerPriority: 1,
},
}, logger)
})
}

bor.Authorize(validatorAddress, func(_ libcommon.Address, mimeType string, message []byte) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions polygon/bor/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewSnapshot(
sigcache: sigcache,
Number: number,
Hash: hash,
ValidatorSet: valset.NewValidatorSet(validators, logger),
ValidatorSet: valset.NewValidatorSet(validators),
Recents: make(map[uint64]common.Address),
}
return snap
Expand Down Expand Up @@ -187,7 +187,7 @@ func (s *Snapshot) Apply(parent *types.Header, headers []*types.Header, logger l
// get validators from headers and use that for new validator set
newVals, _ := valset.ParseValidators(validatorBytes)
v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals, logger)
v.IncrementProposerPriority(1, logger)
v.IncrementProposerPriority(1)
snap.ValidatorSet = v
}

Expand Down
16 changes: 8 additions & 8 deletions polygon/bor/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"sort"
"testing"

"github.com/maticnetwork/crand"
"github.com/stretchr/testify/require"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/polygon/bor"
"github.com/ledgerwatch/erigon/polygon/bor/valset"
"github.com/ledgerwatch/log/v3"
"github.com/maticnetwork/crand"
"github.com/stretchr/testify/require"
)

const (
Expand All @@ -21,7 +21,7 @@ func TestGetSignerSuccessionNumber_ProposerIsSigner(t *testing.T) {
t.Parallel()

validators := buildRandomValidatorSet(numVals)
validatorSet := valset.NewValidatorSet(validators, log.New())
validatorSet := valset.NewValidatorSet(validators)
snap := bor.Snapshot{
ValidatorSet: validatorSet,
}
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestGetSignerSuccessionNumber_SignerIndexIsLarger(t *testing.T) {
// give highest ProposerPriority to a particular val, so that they become the proposer
validators[proposerIndex].VotingPower = 200
snap := bor.Snapshot{
ValidatorSet: valset.NewValidatorSet(validators, log.New()),
ValidatorSet: valset.NewValidatorSet(validators),
}

// choose a signer at an index greater than proposer index
Expand All @@ -71,7 +71,7 @@ func TestGetSignerSuccessionNumber_SignerIndexIsSmaller(t *testing.T) {
// give highest ProposerPriority to a particular val, so that they become the proposer
validators[proposerIndex].VotingPower = 200
snap := bor.Snapshot{
ValidatorSet: valset.NewValidatorSet(validators, log.New()),
ValidatorSet: valset.NewValidatorSet(validators),
}

// choose a signer at an index greater than proposer index
Expand All @@ -89,7 +89,7 @@ func TestGetSignerSuccessionNumber_ProposerNotFound(t *testing.T) {

validators := buildRandomValidatorSet(numVals)
snap := bor.Snapshot{
ValidatorSet: valset.NewValidatorSet(validators, log.New()),
ValidatorSet: valset.NewValidatorSet(validators),
}

dummyProposerAddress := randomAddress()
Expand All @@ -111,7 +111,7 @@ func TestGetSignerSuccessionNumber_SignerNotFound(t *testing.T) {

validators := buildRandomValidatorSet(numVals)
snap := bor.Snapshot{
ValidatorSet: valset.NewValidatorSet(validators, log.New()),
ValidatorSet: valset.NewValidatorSet(validators),
}

dummySignerAddress := randomAddress()
Expand Down
40 changes: 18 additions & 22 deletions polygon/bor/valset/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"sort"
"strings"

"github.com/ledgerwatch/log/v3"

libcommon "github.com/ledgerwatch/erigon-lib/common"
)

Expand Down Expand Up @@ -56,16 +54,16 @@ type ValidatorSet struct {
// the new ValidatorSet will have an empty list of Validators.
// The addresses of validators in `valz` must be unique otherwise the
// function panics.
func NewValidatorSet(valz []*Validator, logger log.Logger) *ValidatorSet {
func NewValidatorSet(valz []*Validator) *ValidatorSet {
vals := &ValidatorSet{}

err := vals.updateWithChangeSet(valz, false, logger)
err := vals.updateWithChangeSet(valz, false)
if err != nil {
panic(fmt.Sprintf("cannot create validator set: %s", err))
}

if len(valz) > 0 {
vals.IncrementProposerPriority(1, logger)
vals.IncrementProposerPriority(1)
}

return vals
Expand All @@ -77,17 +75,17 @@ func (vals *ValidatorSet) IsNilOrEmpty() bool {
}

// Increment ProposerPriority and update the proposer on a copy, and return it.
func (vals *ValidatorSet) CopyIncrementProposerPriority(times int, logger log.Logger) *ValidatorSet {
func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet {
validatorCopy := vals.Copy()
validatorCopy.IncrementProposerPriority(times, logger)
validatorCopy.IncrementProposerPriority(times)

return validatorCopy
}

// IncrementProposerPriority increments ProposerPriority of each validator and updates the
// proposer. Panics if validator set is empty.
// `times` must be positive.
func (vals *ValidatorSet) IncrementProposerPriority(times int, logger log.Logger) {
func (vals *ValidatorSet) IncrementProposerPriority(times int) {
if vals.IsNilOrEmpty() {
panic("empty validator set")
}
Expand All @@ -99,14 +97,14 @@ func (vals *ValidatorSet) IncrementProposerPriority(times int, logger log.Logger
// Cap the difference between priorities to be proportional to 2*totalPower by
// re-normalizing priorities, i.e., rescale all priorities by multiplying with:
// 2*totalVotingPower/(maxPriority - minPriority)
diffMax := PriorityWindowSizeFactor * vals.TotalVotingPower(logger)
diffMax := PriorityWindowSizeFactor * vals.TotalVotingPower()
vals.RescalePriorities(diffMax)
vals.shiftByAvgProposerPriority()

var proposer *Validator
// Call IncrementProposerPriority(1) times times.
for i := 0; i < times; i++ {
proposer = vals.incrementProposerPriority(logger)
proposer = vals.incrementProposerPriority()
}

vals.Proposer = proposer
Expand Down Expand Up @@ -136,7 +134,7 @@ func (vals *ValidatorSet) RescalePriorities(diffMax int64) {
}
}

func (vals *ValidatorSet) incrementProposerPriority(logger log.Logger) *Validator {
func (vals *ValidatorSet) incrementProposerPriority() *Validator {
for _, val := range vals.Validators {
// Check for overflow for sum.
newPrio := safeAddClip(val.ProposerPriority, val.VotingPower)
Expand All @@ -145,7 +143,7 @@ func (vals *ValidatorSet) incrementProposerPriority(logger log.Logger) *Validato
// Decrement the validator with most ProposerPriority.
mostest := vals.getValWithMostPriority()
// Mind the underflow.
mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, vals.TotalVotingPower(logger))
mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, vals.TotalVotingPower())

return mostest
}
Expand Down Expand Up @@ -305,10 +303,8 @@ func (vals *ValidatorSet) UpdateTotalVotingPower() error {

// TotalVotingPower returns the sum of the voting powers of all validators.
// It recomputes the total voting power if required.
func (vals *ValidatorSet) TotalVotingPower(logger log.Logger) int64 {
func (vals *ValidatorSet) TotalVotingPower() int64 {
if vals.totalVotingPower == 0 {
logger.Info("invoking updateTotalVotingPower before returning it")

if err := vals.UpdateTotalVotingPower(); err != nil {
// Can/should we do better?
panic(err)
Expand Down Expand Up @@ -429,8 +425,8 @@ func processChanges(origChanges []*Validator) (updates, removals []*Validator, e
// 'updates' should be a list of proper validator changes, i.e. they have been verified
// by processChanges for duplicates and invalid values.
// No changes are made to the validator set 'vals'.
func verifyUpdates(updates []*Validator, vals *ValidatorSet, logger log.Logger) (updatedTotalVotingPower int64, numNewValidators int, err error) {
updatedTotalVotingPower = vals.TotalVotingPower(logger)
func verifyUpdates(updates []*Validator, vals *ValidatorSet) (updatedTotalVotingPower int64, numNewValidators int, err error) {
updatedTotalVotingPower = vals.TotalVotingPower()

for _, valUpdate := range updates {
address := valUpdate.Address
Expand Down Expand Up @@ -578,7 +574,7 @@ func (vals *ValidatorSet) applyRemovals(deletes []*Validator) {
// If 'allowDeletes' is false then delete operations (identified by validators with voting power 0)
// are not allowed and will trigger an error if present in 'changes'.
// The 'allowDeletes' flag is set to false by NewValidatorSet() and to true by UpdateWithChangeSet().
func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes bool, logger log.Logger) error {
func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes bool) error {
if len(changes) < 1 {
return nil
}
Expand All @@ -599,7 +595,7 @@ func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes
}

// Verify that applying the 'updates' against 'vals' will not result in error.
updatedTotalVotingPower, numNewValidators, err := verifyUpdates(updates, vals, logger)
updatedTotalVotingPower, numNewValidators, err := verifyUpdates(updates, vals)
if err != nil {
return err
}
Expand All @@ -620,7 +616,7 @@ func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes
}

// Scale and center.
vals.RescalePriorities(PriorityWindowSizeFactor * vals.TotalVotingPower(logger))
vals.RescalePriorities(PriorityWindowSizeFactor * vals.TotalVotingPower())
vals.shiftByAvgProposerPriority()

return nil
Expand Down Expand Up @@ -654,8 +650,8 @@ func (vals *ValidatorSet) UpdateValidatorMap() {
//
// If an error is detected during verification steps, it is returned and the validator set
// is not changed.
func (vals *ValidatorSet) UpdateWithChangeSet(changes []*Validator, logger log.Logger) error {
return vals.updateWithChangeSet(changes, true, logger)
func (vals *ValidatorSet) UpdateWithChangeSet(changes []*Validator) error {
return vals.updateWithChangeSet(changes, true)
}

// Difficulty returns the difficulty for a particular signer at the current snapshot number
Expand Down
10 changes: 3 additions & 7 deletions polygon/sync/difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ type difficultyCalculatorImpl struct {
span *heimdallspan.HeimdallSpan
validatorSetFactory func() validatorSetInterface
signaturesCache *lru.ARCCache[libcommon.Hash, libcommon.Address]

log log.Logger
}

// valset.ValidatorSet abstraction for unit tests
type validatorSetInterface interface {
IncrementProposerPriority(times int, logger log.Logger)
IncrementProposerPriority(times int)
Difficulty(signer libcommon.Address) (uint64, error)
}

Expand All @@ -50,8 +48,6 @@ func NewDifficultyCalculator(
span: span,
validatorSetFactory: validatorSetFactory,
signaturesCache: signaturesCache,

log: log,
}

if validatorSetFactory == nil {
Expand All @@ -62,7 +58,7 @@ func NewDifficultyCalculator(
}

func (impl *difficultyCalculatorImpl) makeValidatorSet() validatorSetInterface {
return valset.NewValidatorSet(impl.span.ValidatorSet.Validators, impl.log)
return valset.NewValidatorSet(impl.span.ValidatorSet.Validators)
}

func (impl *difficultyCalculatorImpl) SetSpan(span *heimdallspan.HeimdallSpan) {
Expand All @@ -82,7 +78,7 @@ func (impl *difficultyCalculatorImpl) signerDifficulty(signer libcommon.Address,

sprintNum := impl.borConfig.CalculateSprintNumber(headerNum)
if sprintNum > 0 {
validatorSet.IncrementProposerPriority(int(sprintNum), impl.log)
validatorSet.IncrementProposerPriority(int(sprintNum))
}

return validatorSet.Difficulty(signer)
Expand Down
2 changes: 1 addition & 1 deletion polygon/sync/difficulty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type testValidatorSetInterface struct {
sprintNum int
}

func (v *testValidatorSetInterface) IncrementProposerPriority(times int, _ log.Logger) {
func (v *testValidatorSetInterface) IncrementProposerPriority(times int) {
v.sprintNum = times
}

Expand Down
2 changes: 2 additions & 0 deletions turbo/jsonrpc/bor_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func validatorContains(a []*valset.Validator, x *valset.Validator) (*valset.Vali
return nil, false
}

type ValidatorSet = valset.ValidatorSet

// getUpdatedValidatorSet applies changes to a validator set and returns a new validator set
func getUpdatedValidatorSet(oldValidatorSet *ValidatorSet, newVals []*valset.Validator) *ValidatorSet {
v := oldValidatorSet
Expand Down
2 changes: 1 addition & 1 deletion turbo/jsonrpc/bor_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func (s *Snapshot) apply(headers []*types.Header) (*Snapshot, error) {
}

// check if signer is in validator set
if !snap.ValidatorSet.HasAddress(signer.Bytes()) {
if !snap.ValidatorSet.HasAddress(signer) {
return nil, &bor.UnauthorizedSignerError{Number: number, Signer: signer.Bytes()}
}

Expand Down
Loading

0 comments on commit af78099

Please sign in to comment.