mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 14:34:17 +00:00
types: prevent temporary power overflows on validator updates (#4165)
Closes #4164
This commit is contained in:
committed by
Tess Rinearson
parent
2b906630fa
commit
fc0d5bca61
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user