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.

177 lines
4.2 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/tendermint/tendermint/crypto"
  8. ce "github.com/tendermint/tendermint/crypto/encoding"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. tmproto "github.com/tendermint/tendermint/proto/types"
  11. )
  12. // Volatile state for each Validator
  13. // NOTE: The ProposerPriority is not included in Validator.Hash();
  14. // make sure to update that method if changes are made here
  15. type Validator struct {
  16. Address Address `json:"address"`
  17. PubKey crypto.PubKey `json:"pub_key"`
  18. VotingPower int64 `json:"voting_power"`
  19. ProposerPriority int64 `json:"proposer_priority"`
  20. }
  21. func NewValidator(pubKey crypto.PubKey, votingPower int64) *Validator {
  22. return &Validator{
  23. Address: pubKey.Address(),
  24. PubKey: pubKey,
  25. VotingPower: votingPower,
  26. ProposerPriority: 0,
  27. }
  28. }
  29. func (v *Validator) ValidateBasic() error {
  30. if v == nil {
  31. return errors.New("nil validator")
  32. }
  33. if v.PubKey == nil {
  34. return errors.New("validator does not have a public key")
  35. }
  36. if v.VotingPower < 0 {
  37. return errors.New("validator has negative voting power")
  38. }
  39. if len(v.Address) != crypto.AddressSize {
  40. return fmt.Errorf("validator address is the wrong size: %v", v.Address)
  41. }
  42. return nil
  43. }
  44. // Creates a new copy of the validator so we can mutate ProposerPriority.
  45. // Panics if the validator is nil.
  46. func (v *Validator) Copy() *Validator {
  47. vCopy := *v
  48. return &vCopy
  49. }
  50. // Returns the one with higher ProposerPriority.
  51. func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
  52. if v == nil {
  53. return other
  54. }
  55. switch {
  56. case v.ProposerPriority > other.ProposerPriority:
  57. return v
  58. case v.ProposerPriority < other.ProposerPriority:
  59. return other
  60. default:
  61. result := bytes.Compare(v.Address, other.Address)
  62. switch {
  63. case result < 0:
  64. return v
  65. case result > 0:
  66. return other
  67. default:
  68. panic("Cannot compare identical validators")
  69. }
  70. }
  71. }
  72. func (v *Validator) String() string {
  73. if v == nil {
  74. return "nil-Validator"
  75. }
  76. return fmt.Sprintf("Validator{%v %v VP:%v A:%v}",
  77. v.Address,
  78. v.PubKey,
  79. v.VotingPower,
  80. v.ProposerPriority)
  81. }
  82. // ValidatorListString returns a prettified validator list for logging purposes.
  83. func ValidatorListString(vals []*Validator) string {
  84. chunks := make([]string, len(vals))
  85. for i, val := range vals {
  86. chunks[i] = fmt.Sprintf("%s:%d", val.Address, val.VotingPower)
  87. }
  88. return strings.Join(chunks, ",")
  89. }
  90. // Bytes computes the unique encoding of a validator with a given voting power.
  91. // These are the bytes that gets hashed in consensus. It excludes address
  92. // as its redundant with the pubkey. This also excludes ProposerPriority
  93. // which changes every round.
  94. func (v *Validator) Bytes() []byte {
  95. return cdcEncode(struct {
  96. PubKey crypto.PubKey
  97. VotingPower int64
  98. }{
  99. v.PubKey,
  100. v.VotingPower,
  101. })
  102. }
  103. // ToProto converts Valiator to protobuf
  104. func (v *Validator) ToProto() (*tmproto.Validator, error) {
  105. if v == nil {
  106. return nil, errors.New("nil validator")
  107. }
  108. pk, err := ce.PubKeyToProto(v.PubKey)
  109. if err != nil {
  110. return nil, err
  111. }
  112. vp := tmproto.Validator{
  113. Address: v.Address,
  114. PubKey: pk,
  115. VotingPower: v.VotingPower,
  116. ProposerPriority: v.ProposerPriority,
  117. }
  118. return &vp, nil
  119. }
  120. // FromProto sets a protobuf Validator to the given pointer.
  121. // It returns an error if the public key is invalid.
  122. func ValidatorFromProto(vp *tmproto.Validator) (*Validator, error) {
  123. if vp == nil {
  124. return nil, errors.New("nil validator")
  125. }
  126. pk, err := ce.PubKeyFromProto(vp.PubKey)
  127. if err != nil {
  128. return nil, err
  129. }
  130. v := new(Validator)
  131. v.Address = vp.GetAddress()
  132. v.PubKey = pk
  133. v.VotingPower = vp.GetVotingPower()
  134. v.ProposerPriority = vp.GetProposerPriority()
  135. return v, nil
  136. }
  137. //----------------------------------------
  138. // RandValidator
  139. // RandValidator returns a randomized validator, useful for testing.
  140. // UNSTABLE
  141. func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
  142. privVal := NewMockPV()
  143. votePower := minPower
  144. if randPower {
  145. votePower += int64(tmrand.Uint32())
  146. }
  147. pubKey, err := privVal.GetPubKey()
  148. if err != nil {
  149. panic(fmt.Errorf("could not retrieve pubkey %w", err))
  150. }
  151. val := NewValidator(pubKey, votePower)
  152. return val, privVal
  153. }