Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Validator): provide handling for validatorPower & totalPower isZero #1

Open
wants to merge 1 commit into
base: v0.38.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,16 @@ func (cs *State) calculatePrevoteMessageDelayMetrics() {
}
}
if ps.HasAll() {
if len(pl) < 1 {
fmt.Printf("ERROR: prevotes.hasAll() but len(prevotes) < 1 in round %d\n", cs.Round)
fmt.Printf("prevotes: %v\n", ps)
fmt.Printf("votes: %v\n", cs.Votes)
fmt.Printf("proposal: %v\n", cs.Proposal)
fmt.Printf("lock: %v\n", cs.LockedBlock)
fmt.Printf("commit: %v\n", cs.ValidBlock)
fmt.Printf("pl %v", pl)
return
}
cs.metrics.FullPrevoteDelay.With("proposer_address", cs.Validators.GetProposer().Address.String()).Set(pl[len(pl)-1].Timestamp.Sub(cs.Proposal.Timestamp).Seconds())
}
}
Expand Down
8 changes: 4 additions & 4 deletions types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ func verifyCommitBatch(

// if we don't need to verify all signatures and already have sufficient
// voting power we can break from batching and verify all the signatures
if !countAllSignatures && talliedVotingPower > votingPowerNeeded {
if !countAllSignatures && talliedVotingPower > votingPowerNeeded || (talliedVotingPower == 0 && votingPowerNeeded == 0) {
break
}
}

// ensure that we have batched together enough signatures to exceed the
// voting power needed else there is no need to even verify
if got, needed := talliedVotingPower, votingPowerNeeded; got <= needed {
if got, needed := talliedVotingPower, votingPowerNeeded; got <= needed && needed > 0 {
return ErrNotEnoughVotingPowerSigned{Got: got, Needed: needed}
}

Expand Down Expand Up @@ -319,12 +319,12 @@ func verifyCommitSingle(
}

// check if we have enough signatures and can thus exit early
if !countAllSignatures && talliedVotingPower > votingPowerNeeded {
if !countAllSignatures && talliedVotingPower > votingPowerNeeded || (talliedVotingPower == 0 && votingPowerNeeded == 0) {
return nil
}
}

if got, needed := talliedVotingPower, votingPowerNeeded; got <= needed {
if got, needed := talliedVotingPower, votingPowerNeeded; got <= needed && needed > 0 {
return ErrNotEnoughVotingPowerSigned{Got: got, Needed: needed}
}

Expand Down
30 changes: 26 additions & 4 deletions types/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,29 @@ func (vals *ValidatorSet) RescalePriorities(diffMax int64) {
}

func (vals *ValidatorSet) incrementProposerPriority() *Validator {
// Get number of validators.
for _, val := range vals.Validators {
votingPower := val.VotingPower
// If is zero, then set to 1.
if votingPower == 0 {
votingPower = 1
}

// Check for overflow for sum.
newPrio := safeAddClip(val.ProposerPriority, val.VotingPower)
val.ProposerPriority = newPrio
newPriority := safeAddClip(val.ProposerPriority, votingPower)

val.ProposerPriority = newPriority
}
// Decrement the validator with most ProposerPriority.
mostest := vals.getValWithMostPriority()

// Mind the underflow.
mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, vals.TotalVotingPower())
totalVotingPower := vals.TotalVotingPower()

if totalVotingPower == 0 {
totalVotingPower = 1
}
mostest.ProposerPriority = safeSubClip(mostest.ProposerPriority, totalVotingPower)
return mostest
}

Expand Down Expand Up @@ -379,6 +392,7 @@ func processChanges(origChanges []*Validator) (updates, removals []*Validator, e
updates = make([]*Validator, 0, len(changes))
var prevAddr Address

zeroVotingPowerCounter := 0
// Scan changes by address and append valid validators to updates or removals lists.
for _, valUpdate := range changes {
if bytes.Equal(valUpdate.Address, prevAddr) {
Expand All @@ -395,7 +409,15 @@ func processChanges(origChanges []*Validator) (updates, removals []*Validator, e
MaxTotalVotingPower, valUpdate.VotingPower)
return nil, nil, err
case valUpdate.VotingPower == 0:
removals = append(removals, valUpdate)
// TODO: uncomment and remove below after IDP.
// removals = append(removals, valUpdate)
zeroVotingPowerCounter++
// While we allow to have a 0 VP validator in the set, we don't allow more than 250 of them.
if zeroVotingPowerCounter < 250 {
updates = append(updates, valUpdate)
} else {
removals = append(removals, valUpdate)
}
default:
updates = append(updates, valUpdate)
}
Expand Down
17 changes: 15 additions & 2 deletions types/vote_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,16 @@ func (voteSet *VoteSet) addVerifiedVote(
origSum := votesByBlock.sum
quorum := voteSet.valSet.TotalVotingPower()*2/3 + 1

// Specific case for stake-free period
//TODO: remove this after IDP period
if voteSet.valSet.TotalVotingPower() == 0 {
quorum = 0
}
// Add vote to votesByBlock
votesByBlock.addVerifiedVote(vote, votingPower)

// If we just crossed the quorum threshold and have 2/3 majority...
if origSum < quorum && quorum <= votesByBlock.sum {
if (origSum == 0 && quorum == 0 || origSum < quorum) && quorum <= votesByBlock.sum {
// Only consider the first quorum reached
if voteSet.maj23 == nil {
maj23BlockID := vote.BlockID
Expand Down Expand Up @@ -456,6 +461,10 @@ func (voteSet *VoteSet) HasTwoThirdsAny() bool {
}
voteSet.mtx.Lock()
defer voteSet.mtx.Unlock()
if voteSet.valSet.TotalVotingPower()*2/3 == 0 {
// TODO: Remove after IDP
return true
}
return voteSet.sum > voteSet.valSet.TotalVotingPower()*2/3
}

Expand Down Expand Up @@ -621,7 +630,11 @@ func (voteSet *VoteSet) LogString() string {
// return the power voted, the total, and the fraction
func (voteSet *VoteSet) sumTotalFrac() (int64, int64, float64) {
voted, total := voteSet.sum, voteSet.valSet.TotalVotingPower()
fracVoted := float64(voted) / float64(total)
fracVoted := 0.0
// Only calculate the fraction if total is not zero
if total != 0 {
fracVoted = float64(voted) / float64(total)
}
return voted, total, fracVoted
}

Expand Down