good morning!!!!

Skip to content
Snippets Groups Projects
Commit 54609b61 authored by atvanguard's avatar atvanguard
Browse files

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

parent 18266b0d
No related branches found
No related tags found
No related merge requests found
......@@ -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,
)
}
......@@ -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
}
......
......@@ -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())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment