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.

187 lines
4.6 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/encoding"
  10. "github.com/tendermint/tendermint/internal/jsontypes"
  11. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  12. )
  13. // Volatile state for each Validator
  14. // NOTE: The ProposerPriority is not included in Validator.Hash();
  15. // make sure to update that method if changes are made here
  16. type Validator struct {
  17. Address Address `json:"address"`
  18. PubKey crypto.PubKey `json:"pub_key"`
  19. VotingPower int64 `json:"voting_power,string"`
  20. ProposerPriority int64 `json:"proposer_priority,string"`
  21. }
  22. func (v Validator) MarshalJSON() ([]byte, error) {
  23. pk, err := jsontypes.Marshal(v.PubKey)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return json.Marshal(struct {
  28. Addr Address `json:"address"`
  29. PubKey json.RawMessage `json:"pub_key"`
  30. Power int64 `json:"voting_power,string"`
  31. Priority int64 `json:"proposer_priority,string"`
  32. }{Addr: v.Address, PubKey: pk, Power: v.VotingPower, Priority: v.ProposerPriority})
  33. }
  34. // NewValidator returns a new validator with the given pubkey and voting power.
  35. func NewValidator(pubKey crypto.PubKey, votingPower int64) *Validator {
  36. return &Validator{
  37. Address: pubKey.Address(),
  38. PubKey: pubKey,
  39. VotingPower: votingPower,
  40. ProposerPriority: 0,
  41. }
  42. }
  43. // ValidateBasic performs basic validation.
  44. func (v *Validator) ValidateBasic() error {
  45. if v == nil {
  46. return errors.New("nil validator")
  47. }
  48. if v.PubKey == nil {
  49. return errors.New("validator does not have a public key")
  50. }
  51. if v.VotingPower < 0 {
  52. return errors.New("validator has negative voting power")
  53. }
  54. if len(v.Address) != crypto.AddressSize {
  55. return fmt.Errorf("validator address is the wrong size: %v", v.Address)
  56. }
  57. return nil
  58. }
  59. // Creates a new copy of the validator so we can mutate ProposerPriority.
  60. // Panics if the validator is nil.
  61. func (v *Validator) Copy() *Validator {
  62. vCopy := *v
  63. return &vCopy
  64. }
  65. // Returns the one with higher ProposerPriority.
  66. func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
  67. if v == nil {
  68. return other
  69. }
  70. switch {
  71. case v.ProposerPriority > other.ProposerPriority:
  72. return v
  73. case v.ProposerPriority < other.ProposerPriority:
  74. return other
  75. default:
  76. result := bytes.Compare(v.Address, other.Address)
  77. switch {
  78. case result < 0:
  79. return v
  80. case result > 0:
  81. return other
  82. default:
  83. panic("Cannot compare identical validators")
  84. }
  85. }
  86. }
  87. // String returns a string representation of String.
  88. //
  89. // 1. address
  90. // 2. public key
  91. // 3. voting power
  92. // 4. proposer priority
  93. func (v *Validator) String() string {
  94. if v == nil {
  95. return "nil-Validator"
  96. }
  97. return fmt.Sprintf("Validator{%v %v VP:%v A:%v}",
  98. v.Address,
  99. v.PubKey,
  100. v.VotingPower,
  101. v.ProposerPriority)
  102. }
  103. // ValidatorListString returns a prettified validator list for logging purposes.
  104. func ValidatorListString(vals []*Validator) string {
  105. chunks := make([]string, len(vals))
  106. for i, val := range vals {
  107. chunks[i] = fmt.Sprintf("%s:%d", val.Address, val.VotingPower)
  108. }
  109. return strings.Join(chunks, ",")
  110. }
  111. // Bytes computes the unique encoding of a validator with a given voting power.
  112. // These are the bytes that gets hashed in consensus. It excludes address
  113. // as its redundant with the pubkey. This also excludes ProposerPriority
  114. // which changes every round.
  115. func (v *Validator) Bytes() []byte {
  116. pk, err := encoding.PubKeyToProto(v.PubKey)
  117. if err != nil {
  118. panic(err)
  119. }
  120. pbv := tmproto.SimpleValidator{
  121. PubKey: &pk,
  122. VotingPower: v.VotingPower,
  123. }
  124. bz, err := pbv.Marshal()
  125. if err != nil {
  126. panic(err)
  127. }
  128. return bz
  129. }
  130. // ToProto converts Valiator to protobuf
  131. func (v *Validator) ToProto() (*tmproto.Validator, error) {
  132. if v == nil {
  133. return nil, errors.New("nil validator")
  134. }
  135. pk, err := encoding.PubKeyToProto(v.PubKey)
  136. if err != nil {
  137. return nil, err
  138. }
  139. vp := tmproto.Validator{
  140. Address: v.Address,
  141. PubKey: pk,
  142. VotingPower: v.VotingPower,
  143. ProposerPriority: v.ProposerPriority,
  144. }
  145. return &vp, nil
  146. }
  147. // FromProto sets a protobuf Validator to the given pointer.
  148. // It returns an error if the public key is invalid.
  149. func ValidatorFromProto(vp *tmproto.Validator) (*Validator, error) {
  150. if vp == nil {
  151. return nil, errors.New("nil validator")
  152. }
  153. pk, err := encoding.PubKeyFromProto(vp.PubKey)
  154. if err != nil {
  155. return nil, err
  156. }
  157. v := new(Validator)
  158. v.Address = vp.GetAddress()
  159. v.PubKey = pk
  160. v.VotingPower = vp.GetVotingPower()
  161. v.ProposerPriority = vp.GetProposerPriority()
  162. return v, nil
  163. }