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.

119 lines
2.8 KiB

8 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
10 years ago
10 years ago
10 years ago
7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/go-crypto"
  7. "github.com/tendermint/go-wire"
  8. cmn "github.com/tendermint/tmlibs/common"
  9. )
  10. // Volatile state for each Validator
  11. // NOTE: The Accum is not included in Validator.Hash();
  12. // make sure to update that method if changes are made here
  13. type Validator struct {
  14. Address Address `json:"address"`
  15. PubKey crypto.PubKey `json:"pub_key"`
  16. VotingPower int64 `json:"voting_power"`
  17. Accum int64 `json:"accum"`
  18. }
  19. func NewValidator(pubKey crypto.PubKey, votingPower int64) *Validator {
  20. return &Validator{
  21. Address: pubKey.Address(),
  22. PubKey: pubKey,
  23. VotingPower: votingPower,
  24. Accum: 0,
  25. }
  26. }
  27. // Creates a new copy of the validator so we can mutate accum.
  28. // Panics if the validator is nil.
  29. func (v *Validator) Copy() *Validator {
  30. vCopy := *v
  31. return &vCopy
  32. }
  33. // Returns the one with higher Accum.
  34. func (v *Validator) CompareAccum(other *Validator) *Validator {
  35. if v == nil {
  36. return other
  37. }
  38. if v.Accum > other.Accum {
  39. return v
  40. } else if v.Accum < other.Accum {
  41. return other
  42. } else {
  43. if bytes.Compare(v.Address, other.Address) < 0 {
  44. return v
  45. } else if bytes.Compare(v.Address, other.Address) > 0 {
  46. return other
  47. } else {
  48. cmn.PanicSanity("Cannot compare identical validators")
  49. return nil
  50. }
  51. }
  52. }
  53. func (v *Validator) String() string {
  54. if v == nil {
  55. return "nil-Validator"
  56. }
  57. return fmt.Sprintf("Validator{%v %v VP:%v A:%v}",
  58. v.Address,
  59. v.PubKey,
  60. v.VotingPower,
  61. v.Accum)
  62. }
  63. // Hash computes the unique ID of a validator with a given voting power.
  64. // It excludes the Accum value, which changes with every round.
  65. func (v *Validator) Hash() []byte {
  66. return wire.BinaryRipemd160(struct {
  67. Address Address
  68. PubKey crypto.PubKey
  69. VotingPower int64
  70. }{
  71. v.Address,
  72. v.PubKey,
  73. v.VotingPower,
  74. })
  75. }
  76. //-------------------------------------
  77. var ValidatorCodec = validatorCodec{}
  78. type validatorCodec struct{}
  79. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int, err *error) {
  80. wire.WriteBinary(o.(*Validator), w, n, err)
  81. }
  82. func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} {
  83. return wire.ReadBinary(&Validator{}, r, 0, n, err)
  84. }
  85. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  86. cmn.PanicSanity("ValidatorCodec.Compare not implemented")
  87. return 0
  88. }
  89. //--------------------------------------------------------------------------------
  90. // For testing...
  91. // RandValidator returns a randomized validator, useful for testing.
  92. // UNSTABLE
  93. func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidatorFS) {
  94. _, tempFilePath := cmn.Tempfile("priv_validator_")
  95. privVal := GenPrivValidatorFS(tempFilePath)
  96. votePower := minPower
  97. if randPower {
  98. votePower += int64(cmn.RandUint32())
  99. }
  100. val := NewValidator(privVal.GetPubKey(), votePower)
  101. return val, privVal
  102. }