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.

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