From f9921ae3625791083f1c3b54dac218ab67d4a038 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 2 Mar 2018 03:52:44 -0500 Subject: [PATCH] types/validator_set_test: move funcs around --- types/validator_set_test.go | 120 +++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/types/validator_set_test.go b/types/validator_set_test.go index b9f0f3adb..e34c71533 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -13,26 +13,6 @@ import ( cmn "github.com/tendermint/tmlibs/common" ) -func randPubKey() crypto.PubKey { - var pubKey [32]byte - copy(pubKey[:], cmn.RandBytes(32)) - return crypto.PubKeyEd25519(pubKey).Wrap() -} - -func randValidator_() *Validator { - val := NewValidator(randPubKey(), cmn.RandInt64()) - val.Accum = cmn.RandInt64() - return val -} - -func randValidatorSet(numValidators int) *ValidatorSet { - validators := make([]*Validator, numValidators) - for i := 0; i < numValidators; i++ { - validators[i] = randValidator_() - } - return NewValidatorSet(validators) -} - func TestCopy(t *testing.T) { vset := randValidatorSet(10) vsetHash := vset.Hash() @@ -48,6 +28,26 @@ func TestCopy(t *testing.T) { } } +func BenchmarkValidatorSetCopy(b *testing.B) { + b.StopTimer() + vset := NewValidatorSet([]*Validator{}) + for i := 0; i < 1000; i++ { + privKey := crypto.GenPrivKeyEd25519() + pubKey := privKey.PubKey() + val := NewValidator(pubKey, 0) + if !vset.Add(val) { + panic("Failed to add validator") + } + } + b.StartTimer() + + for i := 0; i < b.N; i++ { + vset.Copy() + } +} + +//------------------------------------------------------------------- + func TestProposerSelection1(t *testing.T) { vset := NewValidatorSet([]*Validator{ newValidator([]byte("foo"), 1000), @@ -66,10 +66,6 @@ func TestProposerSelection1(t *testing.T) { } } -func newValidator(address []byte, power int64) *Validator { - return &Validator{Address: address, VotingPower: power} -} - func TestProposerSelection2(t *testing.T) { addr0 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} addr1 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} @@ -193,6 +189,48 @@ func TestProposerSelection3(t *testing.T) { } } +func newValidator(address []byte, power int64) *Validator { + return &Validator{Address: address, VotingPower: power} +} + +func randPubKey() crypto.PubKey { + var pubKey [32]byte + copy(pubKey[:], cmn.RandBytes(32)) + return crypto.PubKeyEd25519(pubKey).Wrap() +} + +func randValidator_() *Validator { + val := NewValidator(randPubKey(), cmn.RandInt64()) + val.Accum = cmn.RandInt64() + return val +} + +func randValidatorSet(numValidators int) *ValidatorSet { + validators := make([]*Validator, numValidators) + for i := 0; i < numValidators; i++ { + validators[i] = randValidator_() + } + return NewValidatorSet(validators) +} + +func (valSet *ValidatorSet) toBytes() []byte { + bz, err := wire.MarshalBinary(valSet) + if err != nil { + panic(err) + } + return bz +} + +func (valSet *ValidatorSet) fromBytes(b []byte) { + err := wire.UnmarshalBinary(b, &valSet) + if err != nil { + // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED + panic(err) + } +} + +//------------------------------------------------------------------- + func TestValidatorSetTotalVotingPowerOverflows(t *testing.T) { vset := NewValidatorSet([]*Validator{ {Address: []byte("a"), VotingPower: math.MaxInt64, Accum: 0}, @@ -271,37 +309,3 @@ func TestSafeSubClip(t *testing.T) { assert.EqualValues(t, math.MinInt64, safeSubClip(math.MinInt64, math.MaxInt64)) assert.EqualValues(t, math.MaxInt64, safeSubClip(math.MaxInt64, -10)) } - -func BenchmarkValidatorSetCopy(b *testing.B) { - b.StopTimer() - vset := NewValidatorSet([]*Validator{}) - for i := 0; i < 1000; i++ { - privKey := crypto.GenPrivKeyEd25519() - pubKey := privKey.PubKey() - val := NewValidator(pubKey, 0) - if !vset.Add(val) { - panic("Failed to add validator") - } - } - b.StartTimer() - - for i := 0; i < b.N; i++ { - vset.Copy() - } -} - -func (valSet *ValidatorSet) toBytes() []byte { - bz, err := wire.MarshalBinary(valSet) - if err != nil { - panic(err) - } - return bz -} - -func (valSet *ValidatorSet) fromBytes(b []byte) { - err := wire.UnmarshalBinary(b, &valSet) - if err != nil { - // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED - panic(err) - } -}