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.

119 lines
2.7 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
10 years ago
10 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/tendermint2/account"
  7. "github.com/tendermint/tendermint2/binary"
  8. "github.com/tendermint/tendermint2/types"
  9. )
  10. // Persistent (mostly) static data for each Validator
  11. type ValidatorInfo struct {
  12. Address []byte
  13. PubKey account.PubKeyEd25519
  14. UnbondTo []*types.TxOutput
  15. FirstBondHeight uint
  16. FirstBondAmount uint64
  17. // If destroyed:
  18. DestroyedHeight uint
  19. DestroyedAmount uint64
  20. // If released:
  21. ReleasedHeight uint
  22. }
  23. func (valInfo *ValidatorInfo) Copy() *ValidatorInfo {
  24. valInfoCopy := *valInfo
  25. return &valInfoCopy
  26. }
  27. func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
  28. binary.WriteBinary(o.(*ValidatorInfo), w, n, err)
  29. }
  30. func ValidatorInfoDecoder(r io.Reader, n *int64, err *error) interface{} {
  31. return binary.ReadBinary(&ValidatorInfo{}, r, n, err)
  32. }
  33. var ValidatorInfoCodec = binary.Codec{
  34. Encode: ValidatorInfoEncoder,
  35. Decode: ValidatorInfoDecoder,
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Volatile state for each Validator
  39. // Also persisted with the state, but fields change
  40. // every height|round so they don't go in merkle.Tree
  41. type Validator struct {
  42. Address []byte
  43. PubKey account.PubKeyEd25519
  44. BondHeight uint
  45. UnbondHeight uint
  46. LastCommitHeight uint
  47. VotingPower uint64
  48. Accum int64
  49. }
  50. // Creates a new copy of the validator so we can mutate accum.
  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. panic("Cannot compare identical validators")
  71. }
  72. }
  73. }
  74. func (v *Validator) String() string {
  75. return fmt.Sprintf("Validator{%X %v %v-%v-%v VP:%v A:%v}",
  76. v.Address,
  77. v.PubKey,
  78. v.BondHeight,
  79. v.LastCommitHeight,
  80. v.UnbondHeight,
  81. v.VotingPower,
  82. v.Accum)
  83. }
  84. func (v *Validator) Hash() []byte {
  85. return binary.BinarySha256(v)
  86. }
  87. //-------------------------------------
  88. var ValidatorCodec = validatorCodec{}
  89. type validatorCodec struct{}
  90. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
  91. binary.WriteBinary(o.(*Validator), w, n, err)
  92. }
  93. func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
  94. return binary.ReadBinary(&Validator{}, r, n, err)
  95. }
  96. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  97. panic("ValidatorCodec.Compare not implemented")
  98. }