types: prevent temporary power overflows on validator updates (#4165)

Closes #4164
This commit is contained in:
Gustavo Chaín
2019-11-20 12:31:14 +01:00
committed by Tess Rinearson
parent 2b906630fa
commit fc0d5bca61
3 changed files with 25 additions and 8 deletions
+8 -7
View File
@@ -399,13 +399,14 @@ func verifyUpdates(
// Updated validator, add the difference in power to the total.
updatedTotalVotingPower += valUpdate.VotingPower - val.VotingPower
}
overflow := updatedTotalVotingPower > MaxTotalVotingPower
if overflow {
err = fmt.Errorf(
"failed to add/update validator %v, total voting power would exceed the max allowed %v",
valUpdate, MaxTotalVotingPower)
return 0, 0, err
}
}
overflow := updatedTotalVotingPower > MaxTotalVotingPower
if overflow {
err = fmt.Errorf(
"failed to add/update validator, total voting power would exceed the max allowed %v",
MaxTotalVotingPower)
return 0, 0, err
}
return updatedTotalVotingPower, numNewValidators, nil
+15
View File
@@ -354,6 +354,21 @@ func TestValidatorSetTotalVotingPowerPanicsOnOverflow(t *testing.T) {
assert.Panics(t, shouldPanic)
}
func TestValidatorSetShouldNotErrorOnTemporalOverflow(t *testing.T) {
// Updating the validator set might trigger an Overflow error during the update process
valSet := NewValidatorSet([]*Validator{
{Address: []byte("b"), VotingPower: MaxTotalVotingPower - 1, ProposerPriority: 0},
{Address: []byte("a"), VotingPower: 1, ProposerPriority: 0},
})
err := valSet.UpdateWithChangeSet([]*Validator{
{Address: []byte("b"), VotingPower: 1, ProposerPriority: 0},
{Address: []byte("a"), VotingPower: MaxTotalVotingPower - 1, ProposerPriority: 0},
})
assert.NoError(t, err)
}
func TestAvgProposerPriority(t *testing.T) {
// Create Validator set without calling IncrementProposerPriority:
tcs := []struct {