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.

115 lines
3.2 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. "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 uint `json:"first_bond_height"`
  16. FirstBondAmount uint64 `json:"first_bond_amount"`
  17. DestroyedHeight uint `json:"destroyed_height"` // If destroyed
  18. DestroyedAmount uint64 `json:"destroyed_amount"` // If destroyed
  19. ReleasedHeight uint `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 uint `json:"bond_height"`
  43. UnbondHeight uint `json:"unbond_height"`
  44. LastCommitHeight uint `json:"last_commit_height"`
  45. VotingPower uint64 `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. panic("Cannot compare identical validators")
  69. }
  70. }
  71. }
  72. func (v *Validator) String() string {
  73. return fmt.Sprintf("Validator{%X %v %v-%v-%v VP:%v A:%v}",
  74. v.Address,
  75. v.PubKey,
  76. v.BondHeight,
  77. v.LastCommitHeight,
  78. v.UnbondHeight,
  79. v.VotingPower,
  80. v.Accum)
  81. }
  82. func (v *Validator) Hash() []byte {
  83. return binary.BinarySha256(v)
  84. }
  85. //-------------------------------------
  86. var ValidatorCodec = validatorCodec{}
  87. type validatorCodec struct{}
  88. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
  89. binary.WriteBinary(o.(*Validator), w, n, err)
  90. }
  91. func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
  92. return binary.ReadBinary(&Validator{}, r, n, err)
  93. }
  94. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  95. panic("ValidatorCodec.Compare not implemented")
  96. }