You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package state
  2. import (
  3. "github.com/tendermint/tendermint/account"
  4. . "github.com/tendermint/tendermint/common"
  5. "bytes"
  6. "testing"
  7. )
  8. func randValidator_() *Validator {
  9. return &Validator{
  10. Address: RandBytes(20),
  11. PubKey: account.PubKeyEd25519(RandBytes(64)),
  12. BondHeight: uint(RandUint32()),
  13. VotingPower: RandUint64(),
  14. Accum: int64(RandUint64()),
  15. }
  16. }
  17. func randValidatorSet(numValidators int) *ValidatorSet {
  18. validators := make([]*Validator, numValidators)
  19. for i := 0; i < numValidators; i++ {
  20. validators[i] = randValidator_()
  21. }
  22. return NewValidatorSet(validators)
  23. }
  24. func TestCopy(t *testing.T) {
  25. vset := randValidatorSet(10)
  26. vsetHash := vset.Hash()
  27. if len(vsetHash) == 0 {
  28. t.Fatalf("ValidatorSet had unexpected zero hash")
  29. }
  30. vsetCopy := vset.Copy()
  31. vsetCopyHash := vsetCopy.Hash()
  32. if !bytes.Equal(vsetHash, vsetCopyHash) {
  33. t.Fatalf("ValidatorSet copy had wrong hash. Orig: %X, Copy: %X", vsetHash, vsetCopyHash)
  34. }
  35. }
  36. func BenchmarkValidatorSetCopy(b *testing.B) {
  37. b.StopTimer()
  38. vset := NewValidatorSet([]*Validator{})
  39. for i := 0; i < 1000; i++ {
  40. privAccount := account.GenPrivAccount()
  41. val := &Validator{
  42. Address: privAccount.Address,
  43. PubKey: privAccount.PubKey.(account.PubKeyEd25519),
  44. }
  45. if !vset.Add(val) {
  46. panic("Failde to add validator")
  47. }
  48. }
  49. b.StartTimer()
  50. for i := 0; i < b.N; i++ {
  51. vset.Copy()
  52. }
  53. }