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

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

Merged
merged 2 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions types/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions types/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down