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.

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