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.

107 lines
2.5 KiB

8 years ago
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
  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. . "github.com/tendermint/tmlibs/common"
  7. "github.com/tendermint/go-crypto"
  8. "github.com/tendermint/go-wire"
  9. )
  10. // Volatile state for each Validator
  11. // TODO: make non-volatile identity
  12. // - Remove Accum - it can be computed, and now valset becomes identifying
  13. type Validator struct {
  14. Address []byte `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. 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{%X %v VP:%v A:%v}",
  58. v.Address,
  59. v.PubKey,
  60. v.VotingPower,
  61. v.Accum)
  62. }
  63. func (v *Validator) Hash() []byte {
  64. return wire.BinaryRipemd160(v)
  65. }
  66. //-------------------------------------
  67. var ValidatorCodec = validatorCodec{}
  68. type validatorCodec struct{}
  69. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int, err *error) {
  70. wire.WriteBinary(o.(*Validator), w, n, err)
  71. }
  72. func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} {
  73. return wire.ReadBinary(&Validator{}, r, 0, n, err)
  74. }
  75. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  76. PanicSanity("ValidatorCodec.Compare not implemented")
  77. return 0
  78. }
  79. //--------------------------------------------------------------------------------
  80. // For testing...
  81. func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) {
  82. privVal := GenPrivValidator()
  83. _, tempFilePath := Tempfile("priv_validator_")
  84. privVal.SetFile(tempFilePath)
  85. votePower := minPower
  86. if randPower {
  87. votePower += int64(RandUint32())
  88. }
  89. val := NewValidator(privVal.PubKey, votePower)
  90. return val, privVal
  91. }