tendermint/state/validator_set_test.go

73 lines
1.5 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-04-18 23:08:02 -07:00
"fmt"
2014-10-12 21:14:10 -07:00
"testing"
)
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),
PubKey: account.PubKeyEd25519(RandBytes(64)),
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) {
vset := randValidatorSet(10)
for i := 0; i < 100; i++ {
val := vset.Proposer()
fmt.Printf("Proposer: %v\n", val)
vset.IncrementAccum(1)
}
}
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()
}
}