diff --git a/consensus/bor/errors.go b/consensus/bor/errors.go index 29b879ebe3b4c0cf16b91d2600371f80633cb9f2..a3b0dc656d08352a5a8ad3f5f1dddfca3aee52b3 100644 --- a/consensus/bor/errors.go +++ b/consensus/bor/errors.go @@ -25,3 +25,18 @@ type SignerNotFoundError struct { func (e *SignerNotFoundError) Error() string { return fmt.Sprintf("Signer: %s not found", e.Address.Hex()) } + +// TotalVotingPowerExceededError is returned when the maximum allowed total voting power is exceeded +type TotalVotingPowerExceededError struct { + Sum int64 + Validators []*Validator +} + +func (e *TotalVotingPowerExceededError) Error() string { + return fmt.Sprintf( + "Total voting power should be guarded to not exceed %v; got: %v; for validator set: %v", + MaxTotalVotingPower, + e.Sum, + e.Validators, + ) +} diff --git a/consensus/bor/snapshot.go b/consensus/bor/snapshot.go index 97ba2fa0a82ce6d5607b1117e0fa4ef3039c44d1..7707719462af995356549c8847b4ce2fd21f7eb1 100644 --- a/consensus/bor/snapshot.go +++ b/consensus/bor/snapshot.go @@ -87,7 +87,9 @@ func loadSnapshot(config *params.BorConfig, sigcache *lru.ARCCache, db ethdb.Dat snap.ethAPI = ethAPI // update total voting power - snap.ValidatorSet.updateTotalVotingPower() + if err := snap.ValidatorSet.updateTotalVotingPower(); err != nil { + return nil, err + } return snap, nil } diff --git a/consensus/bor/validator_set.go b/consensus/bor/validator_set.go index 89e0c16b136968909723e8713e4aef22bbccea5c..dbe987ce780dedc3fe1b96d0b5a86d0b757409f7 100644 --- a/consensus/bor/validator_set.go +++ b/consensus/bor/validator_set.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/maticnetwork/bor/common" + "github.com/maticnetwork/bor/log" ) // MaxTotalVotingPower - the maximum allowed total voting power. @@ -256,28 +257,29 @@ func (vals *ValidatorSet) Size() int { } // Force recalculation of the set's total voting power. -func (vals *ValidatorSet) updateTotalVotingPower() { +func (vals *ValidatorSet) updateTotalVotingPower() error { sum := int64(0) for _, val := range vals.Validators { // mind overflow sum = safeAddClip(sum, val.VotingPower) if sum > MaxTotalVotingPower { - panic(fmt.Sprintf( - "Total voting power should be guarded to not exceed %v; got: %v", - MaxTotalVotingPower, - sum)) + return &TotalVotingPowerExceededError{sum, vals.Validators} } } - vals.totalVotingPower = sum + return nil } // TotalVotingPower returns the sum of the voting powers of all validators. // It recomputes the total voting power if required. func (vals *ValidatorSet) TotalVotingPower() int64 { if vals.totalVotingPower == 0 { - vals.updateTotalVotingPower() + log.Info("invoking updateTotalVotingPower before returning it") + if err := vals.updateTotalVotingPower(); err != nil { + // Can/should we do better? + panic(err) + } } return vals.totalVotingPower } @@ -562,7 +564,9 @@ func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes vals.applyUpdates(updates) vals.applyRemovals(deletes) - vals.updateTotalVotingPower() + if err := vals.updateTotalVotingPower(); err != nil { + return err + } // Scale and center. vals.RescalePriorities(PriorityWindowSizeFactor * vals.TotalVotingPower())