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.

185 lines
4.3 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/tendermint/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. pk, err := ce.PubKeyToProto(v.PubKey)
  96. if err != nil {
  97. panic(err)
  98. }
  99. pbv := tmproto.SimpleValidator{
  100. PubKey: &pk,
  101. VotingPower: v.VotingPower,
  102. }
  103. bz, err := pbv.Marshal()
  104. if err != nil {
  105. panic(err)
  106. }
  107. return bz
  108. }
  109. // ToProto converts Valiator to protobuf
  110. func (v *Validator) ToProto() (*tmproto.Validator, error) {
  111. if v == nil {
  112. return nil, errors.New("nil validator")
  113. }
  114. pk, err := ce.PubKeyToProto(v.PubKey)
  115. if err != nil {
  116. return nil, err
  117. }
  118. vp := tmproto.Validator{
  119. Address: v.Address,
  120. PubKey: pk,
  121. VotingPower: v.VotingPower,
  122. ProposerPriority: v.ProposerPriority,
  123. }
  124. return &vp, nil
  125. }
  126. // FromProto sets a protobuf Validator to the given pointer.
  127. // It returns an error if the public key is invalid.
  128. func ValidatorFromProto(vp *tmproto.Validator) (*Validator, error) {
  129. if vp == nil {
  130. return nil, errors.New("nil validator")
  131. }
  132. pk, err := ce.PubKeyFromProto(vp.PubKey)
  133. if err != nil {
  134. return nil, err
  135. }
  136. v := new(Validator)
  137. v.Address = vp.GetAddress()
  138. v.PubKey = pk
  139. v.VotingPower = vp.GetVotingPower()
  140. v.ProposerPriority = vp.GetProposerPriority()
  141. return v, nil
  142. }
  143. //----------------------------------------
  144. // RandValidator
  145. // RandValidator returns a randomized validator, useful for testing.
  146. // UNSTABLE
  147. func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
  148. privVal := NewMockPV()
  149. votePower := minPower
  150. if randPower {
  151. votePower += int64(tmrand.Uint32())
  152. }
  153. pubKey, err := privVal.GetPubKey()
  154. if err != nil {
  155. panic(fmt.Errorf("could not retrieve pubkey %w", err))
  156. }
  157. val := NewValidator(pubKey, votePower)
  158. return val, privVal
  159. }