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.

97 lines
2.5 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
10 years ago
  1. package state
  2. import (
  3. "fmt"
  4. "io"
  5. . "github.com/tendermint/tendermint/binary"
  6. )
  7. // Holds state for a Validator at a given height+round.
  8. // Meant to be discarded every round of the consensus protocol.
  9. // TODO consider moving this to another common types package.
  10. type Validator struct {
  11. Account
  12. BondHeight uint32
  13. UnbondHeight uint32
  14. LastCommitHeight uint32
  15. VotingPower uint64
  16. Accum int64
  17. }
  18. // Used to persist the state of ConsensusStateControl.
  19. func ReadValidator(r io.Reader, n *int64, err *error) *Validator {
  20. return &Validator{
  21. Account: ReadAccount(r, n, err),
  22. BondHeight: ReadUInt32(r, n, err),
  23. UnbondHeight: ReadUInt32(r, n, err),
  24. LastCommitHeight: ReadUInt32(r, n, err),
  25. VotingPower: ReadUInt64(r, n, err),
  26. Accum: ReadInt64(r, n, err),
  27. }
  28. }
  29. // Creates a new copy of the validator so we can mutate accum.
  30. func (v *Validator) Copy() *Validator {
  31. return &Validator{
  32. Account: v.Account,
  33. BondHeight: v.BondHeight,
  34. UnbondHeight: v.UnbondHeight,
  35. LastCommitHeight: v.LastCommitHeight,
  36. VotingPower: v.VotingPower,
  37. Accum: v.Accum,
  38. }
  39. }
  40. // Used to persist the state of ConsensusStateControl.
  41. func (v *Validator) WriteTo(w io.Writer) (n int64, err error) {
  42. WriteBinary(w, v.Account, &n, &err)
  43. WriteUInt32(w, v.BondHeight, &n, &err)
  44. WriteUInt32(w, v.UnbondHeight, &n, &err)
  45. WriteUInt32(w, v.LastCommitHeight, &n, &err)
  46. WriteUInt64(w, v.VotingPower, &n, &err)
  47. WriteInt64(w, v.Accum, &n, &err)
  48. return
  49. }
  50. // Returns the one with higher Accum.
  51. func (v *Validator) CompareAccum(other *Validator) *Validator {
  52. if v == nil {
  53. return other
  54. }
  55. if v.Accum > other.Accum {
  56. return v
  57. } else if v.Accum < other.Accum {
  58. return other
  59. } else {
  60. if v.Id < other.Id {
  61. return v
  62. } else if v.Id > other.Id {
  63. return other
  64. } else {
  65. panic("Cannot compare identical validators")
  66. }
  67. }
  68. }
  69. func (v *Validator) String() string {
  70. return fmt.Sprintf("Validator{%v VP:%v A:%v}", v.Account, v.VotingPower, v.Accum)
  71. }
  72. //-------------------------------------
  73. var ValidatorCodec = validatorCodec{}
  74. type validatorCodec struct{}
  75. func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
  76. WriteBinary(w, o.(*Validator), n, err)
  77. }
  78. func (vc validatorCodec) Decode(r io.Reader, n *int64, err *error) interface{} {
  79. return ReadValidator(r, n, err)
  80. }
  81. func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int {
  82. panic("ValidatorCodec.Compare not implemented")
  83. }