types: NewValidatorSet doesn't panic on empty valz list (#2938)

* types: NewValidatorSet doesn't panic on empty valz list

* changelog
This commit is contained in:
Ethan Buchman
2018-11-28 13:09:29 -05:00
committed by GitHub
parent 9adcfe2804
commit b11788d36d
3 changed files with 10 additions and 5 deletions

View File

@@ -30,3 +30,5 @@ program](https://hackerone.com/tendermint).
- [consensus] [\#2871](https://github.com/tendermint/tendermint/issues/2871) Remove *ProposalHeartbeat* infrastructure as it serves no real purpose
### BUG FIXES:
- [types] \#2938 Fix regression in v0.26.4 where we panic on empty
genDoc.Validators

View File

@@ -29,10 +29,10 @@ type ValidatorSet struct {
totalVotingPower int64
}
// NewValidatorSet initializes a ValidatorSet by copying over the
// values from `valz`, a list of Validators. If valz is nil or empty,
// the new ValidatorSet will have an empty list of Validators.
func NewValidatorSet(valz []*Validator) *ValidatorSet {
if valz != nil && len(valz) == 0 {
panic("validator set initialization slice cannot be an empty slice (but it can be nil)")
}
validators := make([]*Validator, len(valz))
for i, val := range valz {
validators[i] = val.Copy()

View File

@@ -17,9 +17,12 @@ import (
)
func TestValidatorSetBasic(t *testing.T) {
assert.Panics(t, func() { NewValidatorSet([]*Validator{}) })
// empty or nil validator lists are allowed,
// but attempting to IncrementAccum on them will panic.
vset := NewValidatorSet([]*Validator{})
assert.Panics(t, func() { vset.IncrementAccum(1) })
vset := NewValidatorSet(nil)
vset = NewValidatorSet(nil)
assert.Panics(t, func() { vset.IncrementAccum(1) })
assert.EqualValues(t, vset, vset.Copy())