Add the compare validator powers proposal as discussed with Zarko in the ADR

This commit is contained in:
Zaki Manian
2019-07-16 19:55:09 -07:00
parent 88b7c53dcf
commit 415d9579e1
2 changed files with 48 additions and 0 deletions

View File

@@ -130,3 +130,25 @@ func IsErrCommitExpired(err error) bool {
}
return false
}
type errValidatorChange struct {
change float64
}
func (e errValidatorChange) Error() string {
return fmt.Sprintf("%f is more than 1/3rd validator change", e.change)
}
func ErrValidatorChange(change float64) error {
return cmn.ErrorWrap(errValidatorChange{
change: change,
}, "")
}
func IsErrValidatorChange(err error) bool {
if err_, ok := err.(cmn.Error); ok {
_, ok := err_.Data().(errValidatorChange)
return ok
}
return false
}

View File

@@ -7,6 +7,7 @@ import (
"bytes"
"errors"
"fmt"
"math"
"sync"
"time"
@@ -369,10 +370,35 @@ func (vp *Provider) verifyAndSave(trustedFC, newFC lite.FullCommit) error {
return err
}
}
change := CompareVotingPowers(trustedFC, newFC)
if change > float64(1/3) {
return lerr.ErrValidatorChange(change)
}
return vp.trusted.SaveFullCommit(newFC)
}
func CompareVotingPowers(trustedFC, newFC lite.FullCommit) float64 {
var diffAccumulator float64
for _, val := range newFC.Validators.Validators {
newPowerRatio := float64(val.VotingPower) / float64(newFC.Validators.TotalVotingPower())
_, tval := trustedFC.NextValidators.GetByAddress(val.Address)
oldPowerRatio := float64(tval.VotingPower) / float64(trustedFC.NextValidators.TotalVotingPower())
diffAccumulator += math.Abs(newPowerRatio - oldPowerRatio)
}
return diffAccumulator
}
func (vp *Provider) fetchAndVerifyToHeightLinear(h int64) (lite.FullCommit, error) {
// Fetch latest full commit from source.