Browse Source

test validator set more thoroughly

Refs #693
pull/1936/head
Anton Kaliaev 6 years ago
parent
commit
17e1df0cbd
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 57 additions and 2 deletions
  1. +3
    -2
      types/validator_set.go
  2. +54
    -0
      types/validator_set_test.go

+ 3
- 2
types/validator_set.go View File

@ -39,14 +39,15 @@ func NewValidatorSet(vals []*Validator) *ValidatorSet {
Validators: validators, Validators: validators,
} }
if vals != nil {
if vals != nil && len(vals) > 0 {
vs.IncrementAccum(1) vs.IncrementAccum(1)
} }
return vs return vs
} }
// incrementAccum and update the proposer
// IncrementAccum increments accum of each validator and updates the
// proposer. Panics if validator set is empty.
func (valSet *ValidatorSet) IncrementAccum(times int) { func (valSet *ValidatorSet) IncrementAccum(times int) {
// Add VotingPower * times to each validator and order into heap. // Add VotingPower * times to each validator and order into heap.
validatorsHeap := cmn.NewHeap() validatorsHeap := cmn.NewHeap()


+ 54
- 0
types/validator_set_test.go View File

@ -14,6 +14,60 @@ import (
cmn "github.com/tendermint/tendermint/libs/common" cmn "github.com/tendermint/tendermint/libs/common"
) )
func TestValidatorSetBasic(t *testing.T) {
for _, vset := range []*ValidatorSet{NewValidatorSet([]*Validator{}), NewValidatorSet(nil)} {
assert.Panics(t, func() { vset.IncrementAccum(1) })
assert.EqualValues(t, vset, vset.Copy())
assert.False(t, vset.HasAddress([]byte("some val")))
idx, val := vset.GetByAddress([]byte("some val"))
assert.Equal(t, -1, idx)
assert.Nil(t, val)
addr, val := vset.GetByIndex(-100)
assert.Nil(t, addr)
assert.Nil(t, val)
addr, val = vset.GetByIndex(0)
assert.Nil(t, addr)
assert.Nil(t, val)
addr, val = vset.GetByIndex(100)
assert.Nil(t, addr)
assert.Nil(t, val)
assert.Zero(t, vset.Size())
assert.Equal(t, int64(0), vset.TotalVotingPower())
assert.Nil(t, vset.GetProposer())
assert.Nil(t, vset.Hash())
// add
val = randValidator_()
assert.True(t, vset.Add(val))
assert.True(t, vset.HasAddress(val.Address))
idx, val2 := vset.GetByAddress(val.Address)
assert.Equal(t, 0, idx)
assert.Equal(t, val, val2)
addr, val2 = vset.GetByIndex(0)
assert.Equal(t, []byte(val.Address), addr)
assert.Equal(t, val, val2)
assert.Equal(t, 1, vset.Size())
assert.Equal(t, val.VotingPower, vset.TotalVotingPower())
assert.Equal(t, val, vset.GetProposer())
assert.NotNil(t, vset.Hash())
assert.NotPanics(t, func() { vset.IncrementAccum(1) })
// update
assert.False(t, vset.Update(randValidator_()))
val.VotingPower = 100
assert.True(t, vset.Update(val))
// remove
val2, removed := vset.Remove(randValidator_().Address)
assert.Nil(t, val2)
assert.False(t, removed)
val2, removed = vset.Remove(val.Address)
assert.Equal(t, val.Address, val2.Address)
assert.True(t, removed)
}
}
func TestCopy(t *testing.T) { func TestCopy(t *testing.T) {
vset := randValidatorSet(10) vset := randValidatorSet(10)
vsetHash := vset.Hash() vsetHash := vset.Hash()


Loading…
Cancel
Save