refactor: Bubble up error in updateTotalVotingPower instead of panic (2.29)

This commit is contained in:
atvanguard 2020-04-22 13:48:59 +05:30
parent 18266b0d7a
commit 54609b6168
3 changed files with 30 additions and 9 deletions

View file

@ -25,3 +25,18 @@ type SignerNotFoundError struct {
func (e *SignerNotFoundError) Error() string { func (e *SignerNotFoundError) Error() string {
return fmt.Sprintf("Signer: %s not found", e.Address.Hex()) 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,
)
}

View file

@ -87,7 +87,9 @@ func loadSnapshot(config *params.BorConfig, sigcache *lru.ARCCache, db ethdb.Dat
snap.ethAPI = ethAPI snap.ethAPI = ethAPI
// update total voting power // update total voting power
snap.ValidatorSet.updateTotalVotingPower() if err := snap.ValidatorSet.updateTotalVotingPower(); err != nil {
return nil, err
}
return snap, nil return snap, nil
} }

View file

@ -11,6 +11,7 @@ import (
"strings" "strings"
"github.com/maticnetwork/bor/common" "github.com/maticnetwork/bor/common"
"github.com/maticnetwork/bor/log"
) )
// MaxTotalVotingPower - the maximum allowed total voting power. // 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. // Force recalculation of the set's total voting power.
func (vals *ValidatorSet) updateTotalVotingPower() { func (vals *ValidatorSet) updateTotalVotingPower() error {
sum := int64(0) sum := int64(0)
for _, val := range vals.Validators { for _, val := range vals.Validators {
// mind overflow // mind overflow
sum = safeAddClip(sum, val.VotingPower) sum = safeAddClip(sum, val.VotingPower)
if sum > MaxTotalVotingPower { if sum > MaxTotalVotingPower {
panic(fmt.Sprintf( return &TotalVotingPowerExceededError{sum, vals.Validators}
"Total voting power should be guarded to not exceed %v; got: %v",
MaxTotalVotingPower,
sum))
} }
} }
vals.totalVotingPower = sum vals.totalVotingPower = sum
return nil
} }
// TotalVotingPower returns the sum of the voting powers of all validators. // TotalVotingPower returns the sum of the voting powers of all validators.
// It recomputes the total voting power if required. // It recomputes the total voting power if required.
func (vals *ValidatorSet) TotalVotingPower() int64 { func (vals *ValidatorSet) TotalVotingPower() int64 {
if vals.totalVotingPower == 0 { 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 return vals.totalVotingPower
} }
@ -562,7 +564,9 @@ func (vals *ValidatorSet) updateWithChangeSet(changes []*Validator, allowDeletes
vals.applyUpdates(updates) vals.applyUpdates(updates)
vals.applyRemovals(deletes) vals.applyRemovals(deletes)
vals.updateTotalVotingPower() if err := vals.updateTotalVotingPower(); err != nil {
return err
}
// Scale and center. // Scale and center.
vals.RescalePriorities(PriorityWindowSizeFactor * vals.TotalVotingPower()) vals.RescalePriorities(PriorityWindowSizeFactor * vals.TotalVotingPower())