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.

116 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
10 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/tendermint/account"
  7. "github.com/tendermint/tendermint/binary"
  8. "github.com/tendermint/tendermint/types"
  9. )
  10. // Persistent (mostly) static data for each Validator
  11. type ValidatorInfo struct {
  12. Address []byte `json:"address"`
  13. PubKey account.PubKeyEd25519 `json:"pub_key"`
  14. UnbondTo []*types.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. binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
  27. }
  28. func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
  29. return binary.ReadBinary(&ValidatorInfo{}, r, n, err)
  30. }
  31. var ValidatorInfoCodec = binary.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 account.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. func (v *Validator) Copy() *Validator {
  50. vCopy := *v
  51. return &vCopy
  52. }
  53. // Returns the one with higher Accum.
  54. func (v *Validator) CompareAccum(other *Validator) *Validator {
  55. if v == nil {
  56. return other
  57. }
  58. if v.Accum > other.Accum {
  59. return v
  60. } else if v.Accum < other.Accum {
  61. return other
  62. } else {
  63. if bytes.Compare(v.Address, other.Address) < 0 {
  64. return v
  65. } else if bytes.Compare(v.Address, other.Address) > 0 {
  66. return other
  67. } else {
  68. // SANITY CHECK
  69. panic("Cannot compare identical validators")
  70. }
  71. }
  72. }
  73. func (v *Validator) String() string {
  74. return fmt.Sprintf("Validator{%X %v %v-%v-%v VP:%v A:%v}",
  75. v.Address,
  76. v.PubKey,
  77. v.BondHeight,
  78. v.LastCommitHeight,
  79. v.UnbondHeight,
  80. v.VotingPower,
  81. v.Accum)
  82. }
  83. func (v *Validator) Hash() []byte {
  84. return binary.BinarySha256(v)
  85. }
  86. //-------------------------------------
  87. var ValidatorCodec = validatorCodec{}
  88. type validatorCodec struct{}
  89. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
  90. binary.WriteBinary(o.(*Validator), w, n, err)
  91. }
  92. func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
  93. return binary.ReadBinary(&Validator{}, r, n, err)
  94. }
  95. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  96. panic("ValidatorCodec.Compare not implemented")
  97. }