tendermint/state/validator_set_test.go

106 lines
2.7 KiB
Go
Raw Normal View History

2014-10-12 21:14:10 -07:00
package state
import (
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/common"
2014-10-12 21:14:10 -07:00
"bytes"
2015-07-04 15:12:00 -07:00
"strings"
2014-10-12 21:14:10 -07:00
"testing"
)
2015-07-17 14:19:16 -07:00
func randPubKey() account.PubKeyEd25519 {
var pubKey [32]byte
copy(pubKey[:], RandBytes(32))
return account.PubKeyEd25519(pubKey)
}
2014-12-17 01:37:13 -08:00
func randValidator_() *Validator {
2014-10-12 21:14:10 -07:00
return &Validator{
2014-12-17 01:37:13 -08:00
Address: RandBytes(20),
2015-07-17 14:19:16 -07:00
PubKey: randPubKey(),
BondHeight: RandInt(),
VotingPower: RandInt64(),
Accum: RandInt64(),
2014-10-12 21:14:10 -07:00
}
}
func randValidatorSet(numValidators int) *ValidatorSet {
validators := make([]*Validator, numValidators)
for i := 0; i < numValidators; i++ {
2014-12-17 01:37:13 -08:00
validators[i] = randValidator_()
2014-10-12 21:14:10 -07:00
}
return NewValidatorSet(validators)
}
func TestCopy(t *testing.T) {
vset := randValidatorSet(10)
vsetHash := vset.Hash()
if len(vsetHash) == 0 {
t.Fatalf("ValidatorSet had unexpected zero hash")
}
vsetCopy := vset.Copy()
vsetCopyHash := vsetCopy.Hash()
if !bytes.Equal(vsetHash, vsetCopyHash) {
t.Fatalf("ValidatorSet copy had wrong hash. Orig: %X, Copy: %X", vsetHash, vsetCopyHash)
}
}
2015-03-19 13:47:24 -07:00
2015-04-18 23:08:02 -07:00
func TestProposerSelection(t *testing.T) {
2015-07-04 15:12:00 -07:00
vset := NewValidatorSet([]*Validator{
&Validator{
Address: []byte("foo"),
2015-07-17 14:19:16 -07:00
PubKey: randPubKey(),
2015-07-04 15:12:00 -07:00
BondHeight: RandInt(),
VotingPower: 1000,
Accum: 0,
},
&Validator{
Address: []byte("bar"),
2015-07-17 14:19:16 -07:00
PubKey: randPubKey(),
2015-07-04 15:12:00 -07:00
BondHeight: RandInt(),
VotingPower: 300,
Accum: 0,
},
&Validator{
Address: []byte("baz"),
2015-07-17 14:19:16 -07:00
PubKey: randPubKey(),
2015-07-04 15:12:00 -07:00
BondHeight: RandInt(),
VotingPower: 330,
Accum: 0,
},
})
proposers := []string{}
2015-04-18 23:08:02 -07:00
for i := 0; i < 100; i++ {
val := vset.Proposer()
2015-07-04 15:12:00 -07:00
proposers = append(proposers, string(val.Address))
2015-04-18 23:08:02 -07:00
vset.IncrementAccum(1)
}
2015-07-04 15:12:00 -07:00
expected := `bar foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo foo baz bar foo foo foo baz foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo bar foo foo baz foo foo bar foo baz foo foo bar foo baz foo foo bar foo baz foo foo`
if expected != strings.Join(proposers, " ") {
t.Errorf("Expected sequence of proposers was\n%v\nbut got \n%v", expected, strings.Join(proposers, " "))
}
2015-04-18 23:08:02 -07:00
}
2015-03-19 13:47:24 -07:00
func BenchmarkValidatorSetCopy(b *testing.B) {
b.StopTimer()
vset := NewValidatorSet([]*Validator{})
for i := 0; i < 1000; i++ {
privAccount := account.GenPrivAccount()
val := &Validator{
Address: privAccount.Address,
PubKey: privAccount.PubKey.(account.PubKeyEd25519),
}
if !vset.Add(val) {
2015-04-18 23:08:02 -07:00
panic("Failed to add validator")
2015-03-19 13:47:24 -07:00
}
}
b.StartTimer()
for i := 0; i < b.N; i++ {
vset.Copy()
}
}