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.

106 lines
2.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
10 years ago
10 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-crypto"
  8. "github.com/tendermint/go-wire"
  9. )
  10. // Volatile state for each Validator
  11. // Also persisted with the state, but fields change
  12. // every height|round so they don't go in merkle.Tree
  13. type Validator struct {
  14. Address []byte `json:"address"`
  15. PubKey crypto.PubKey `json:"pub_key"`
  16. LastCommitHeight int `json:"last_commit_height"`
  17. VotingPower int64 `json:"voting_power"`
  18. Accum int64 `json:"accum"`
  19. }
  20. // Creates a new copy of the validator so we can mutate accum.
  21. // Panics if the validator is nil.
  22. func (v *Validator) Copy() *Validator {
  23. vCopy := *v
  24. return &vCopy
  25. }
  26. // Returns the one with higher Accum.
  27. func (v *Validator) CompareAccum(other *Validator) *Validator {
  28. if v == nil {
  29. return other
  30. }
  31. if v.Accum > other.Accum {
  32. return v
  33. } else if v.Accum < other.Accum {
  34. return other
  35. } else {
  36. if bytes.Compare(v.Address, other.Address) < 0 {
  37. return v
  38. } else if bytes.Compare(v.Address, other.Address) > 0 {
  39. return other
  40. } else {
  41. PanicSanity("Cannot compare identical validators")
  42. return nil
  43. }
  44. }
  45. }
  46. func (v *Validator) String() string {
  47. if v == nil {
  48. return "nil-Validator"
  49. }
  50. return fmt.Sprintf("Validator{%X %v %v VP:%v A:%v}",
  51. v.Address,
  52. v.PubKey,
  53. v.LastCommitHeight,
  54. v.VotingPower,
  55. v.Accum)
  56. }
  57. func (v *Validator) Hash() []byte {
  58. return wire.BinaryRipemd160(v)
  59. }
  60. //-------------------------------------
  61. var ValidatorCodec = validatorCodec{}
  62. type validatorCodec struct{}
  63. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int, err *error) {
  64. wire.WriteBinary(o.(*Validator), w, n, err)
  65. }
  66. func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} {
  67. return wire.ReadBinary(&Validator{}, r, 0, n, err)
  68. }
  69. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  70. PanicSanity("ValidatorCodec.Compare not implemented")
  71. return 0
  72. }
  73. //--------------------------------------------------------------------------------
  74. // For testing...
  75. func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) {
  76. privVal := GenPrivValidator()
  77. _, tempFilePath := Tempfile("priv_validator_")
  78. privVal.SetFile(tempFilePath)
  79. votePower := minPower
  80. if randPower {
  81. votePower += int64(RandUint32())
  82. }
  83. val := &Validator{
  84. Address: privVal.Address,
  85. PubKey: privVal.PubKey,
  86. LastCommitHeight: 0,
  87. VotingPower: votePower,
  88. Accum: 0,
  89. }
  90. return val, privVal
  91. }