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.

121 lines
3.3 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
10 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. acm "github.com/tendermint/tendermint/account"
  7. . "github.com/tendermint/tendermint/common"
  8. "github.com/tendermint/tendermint/wire"
  9. )
  10. // Persistent (mostly) static data for each Validator
  11. type ValidatorInfo struct {
  12. Address []byte `json:"address"`
  13. PubKey acm.PubKeyEd25519 `json:"pub_key"`
  14. UnbondTo []*TxOutput `json:"unbond_to"`
  15. FirstBondHeight int `json:"first_bond_height"`
  16. FirstBondAmount int64 `json:"first_bond_amount"`
  17. DestroyedHeight int `json:"destroyed_height"` // If destroyed
  18. DestroyedAmount int64 `json:"destroyed_amount"` // If destroyed
  19. ReleasedHeight int `json:"released_height"` // If released
  20. }
  21. func (valInfo *ValidatorInfo) Copy() *ValidatorInfo {
  22. valInfoCopy := *valInfo
  23. return &valInfoCopy
  24. }
  25. func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  26. wire.WriteBinary(o.(*ValidatorInfo), w, n, err)
  27. }
  28. func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
  29. return wire.ReadBinary(&ValidatorInfo{}, r, n, err)
  30. }
  31. var ValidatorInfoCodec = wire.Codec{
  32. Encode: ValidatorInfoEncoder,
  33. Decode: ValidatorInfoDecoder,
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Volatile state for each Validator
  37. // Also persisted with the state, but fields change
  38. // every height|round so they don't go in merkle.Tree
  39. type Validator struct {
  40. Address []byte `json:"address"`
  41. PubKey acm.PubKeyEd25519 `json:"pub_key"`
  42. BondHeight int `json:"bond_height"`
  43. UnbondHeight int `json:"unbond_height"`
  44. LastCommitHeight int `json:"last_commit_height"`
  45. VotingPower int64 `json:"voting_power"`
  46. Accum int64 `json:"accum"`
  47. }
  48. // Creates a new copy of the validator so we can mutate accum.
  49. // Panics if the validator is nil.
  50. func (v *Validator) Copy() *Validator {
  51. vCopy := *v
  52. return &vCopy
  53. }
  54. // Returns the one with higher Accum.
  55. func (v *Validator) CompareAccum(other *Validator) *Validator {
  56. if v == nil {
  57. return other
  58. }
  59. if v.Accum > other.Accum {
  60. return v
  61. } else if v.Accum < other.Accum {
  62. return other
  63. } else {
  64. if bytes.Compare(v.Address, other.Address) < 0 {
  65. return v
  66. } else if bytes.Compare(v.Address, other.Address) > 0 {
  67. return other
  68. } else {
  69. PanicSanity("Cannot compare identical validators")
  70. return nil
  71. }
  72. }
  73. }
  74. func (v *Validator) String() string {
  75. if v == nil {
  76. return "nil-Validator"
  77. }
  78. return fmt.Sprintf("Validator{%X %v %v-%v-%v VP:%v A:%v}",
  79. v.Address,
  80. v.PubKey,
  81. v.BondHeight,
  82. v.LastCommitHeight,
  83. v.UnbondHeight,
  84. v.VotingPower,
  85. v.Accum)
  86. }
  87. func (v *Validator) Hash() []byte {
  88. return wire.BinaryRipemd160(v)
  89. }
  90. //-------------------------------------
  91. var ValidatorCodec = validatorCodec{}
  92. type validatorCodec struct{}
  93. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
  94. wire.WriteBinary(o.(*Validator), w, n, err)
  95. }
  96. func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
  97. return wire.ReadBinary(&Validator{}, r, n, err)
  98. }
  99. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  100. PanicSanity("ValidatorCodec.Compare not implemented")
  101. return 0
  102. }