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.6 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. // TODO: make non-volatile identity
  12. // - Remove LastCommitHeight, send bitarray of vals that signed in BeginBlock
  13. // - Remove Accum - it can be computed, and now valset becomes identifying
  14. type Validator struct {
  15. Address []byte `json:"address"`
  16. PubKey crypto.PubKey `json:"pub_key"`
  17. LastCommitHeight int `json:"last_commit_height"`
  18. VotingPower int64 `json:"voting_power"`
  19. Accum int64 `json:"accum"`
  20. }
  21. // Creates a new copy of the validator so we can mutate accum.
  22. // Panics if the validator is nil.
  23. func (v *Validator) Copy() *Validator {
  24. vCopy := *v
  25. return &vCopy
  26. }
  27. // Returns the one with higher Accum.
  28. func (v *Validator) CompareAccum(other *Validator) *Validator {
  29. if v == nil {
  30. return other
  31. }
  32. if v.Accum > other.Accum {
  33. return v
  34. } else if v.Accum < other.Accum {
  35. return other
  36. } else {
  37. if bytes.Compare(v.Address, other.Address) < 0 {
  38. return v
  39. } else if bytes.Compare(v.Address, other.Address) > 0 {
  40. return other
  41. } else {
  42. PanicSanity("Cannot compare identical validators")
  43. return nil
  44. }
  45. }
  46. }
  47. func (v *Validator) String() string {
  48. if v == nil {
  49. return "nil-Validator"
  50. }
  51. return fmt.Sprintf("Validator{%X %v %v VP:%v A:%v}",
  52. v.Address,
  53. v.PubKey,
  54. v.LastCommitHeight,
  55. v.VotingPower,
  56. v.Accum)
  57. }
  58. func (v *Validator) Hash() []byte {
  59. return wire.BinaryRipemd160(v)
  60. }
  61. //-------------------------------------
  62. var ValidatorCodec = validatorCodec{}
  63. type validatorCodec struct{}
  64. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int, err *error) {
  65. wire.WriteBinary(o.(*Validator), w, n, err)
  66. }
  67. func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} {
  68. return wire.ReadBinary(&Validator{}, r, 0, n, err)
  69. }
  70. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  71. PanicSanity("ValidatorCodec.Compare not implemented")
  72. return 0
  73. }
  74. //--------------------------------------------------------------------------------
  75. // For testing...
  76. func RandValidator(randPower bool, minPower int64) (*Validator, *PrivValidator) {
  77. privVal := GenPrivValidator()
  78. _, tempFilePath := Tempfile("priv_validator_")
  79. privVal.SetFile(tempFilePath)
  80. votePower := minPower
  81. if randPower {
  82. votePower += int64(RandUint32())
  83. }
  84. val := &Validator{
  85. Address: privVal.Address,
  86. PubKey: privVal.PubKey,
  87. LastCommitHeight: 0,
  88. VotingPower: votePower,
  89. Accum: 0,
  90. }
  91. return val, privVal
  92. }