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.

211 lines
5.0 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
  18. PubKey crypto.PubKey
  19. VotingPower int64
  20. ProposerPriority int64
  21. }
  22. type validatorJSON struct {
  23. Address Address `json:"address"`
  24. PubKey json.RawMessage `json:"pub_key,omitempty"`
  25. VotingPower int64 `json:"voting_power,string"`
  26. ProposerPriority int64 `json:"proposer_priority,string"`
  27. }
  28. func (v Validator) MarshalJSON() ([]byte, error) {
  29. val := validatorJSON{
  30. Address: v.Address,
  31. VotingPower: v.VotingPower,
  32. ProposerPriority: v.ProposerPriority,
  33. }
  34. if v.PubKey != nil {
  35. pk, err := jsontypes.Marshal(v.PubKey)
  36. if err != nil {
  37. return nil, err
  38. }
  39. val.PubKey = pk
  40. }
  41. return json.Marshal(val)
  42. }
  43. func (v *Validator) UnmarshalJSON(data []byte) error {
  44. var val validatorJSON
  45. if err := json.Unmarshal(data, &val); err != nil {
  46. return err
  47. }
  48. if err := jsontypes.Unmarshal(val.PubKey, &v.PubKey); err != nil {
  49. return err
  50. }
  51. v.Address = val.Address
  52. v.VotingPower = val.VotingPower
  53. v.ProposerPriority = val.ProposerPriority
  54. return nil
  55. }
  56. // NewValidator returns a new validator with the given pubkey and voting power.
  57. func NewValidator(pubKey crypto.PubKey, votingPower int64) *Validator {
  58. return &Validator{
  59. Address: pubKey.Address(),
  60. PubKey: pubKey,
  61. VotingPower: votingPower,
  62. ProposerPriority: 0,
  63. }
  64. }
  65. // ValidateBasic performs basic validation.
  66. func (v *Validator) ValidateBasic() error {
  67. if v == nil {
  68. return errors.New("nil validator")
  69. }
  70. if v.PubKey == nil {
  71. return errors.New("validator does not have a public key")
  72. }
  73. if v.VotingPower < 0 {
  74. return errors.New("validator has negative voting power")
  75. }
  76. if len(v.Address) != crypto.AddressSize {
  77. return fmt.Errorf("validator address is the wrong size: %v", v.Address)
  78. }
  79. return nil
  80. }
  81. // Creates a new copy of the validator so we can mutate ProposerPriority.
  82. // Panics if the validator is nil.
  83. func (v *Validator) Copy() *Validator {
  84. vCopy := *v
  85. return &vCopy
  86. }
  87. // Returns the one with higher ProposerPriority.
  88. func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
  89. if v == nil {
  90. return other
  91. }
  92. switch {
  93. case v.ProposerPriority > other.ProposerPriority:
  94. return v
  95. case v.ProposerPriority < other.ProposerPriority:
  96. return other
  97. default:
  98. result := bytes.Compare(v.Address, other.Address)
  99. switch {
  100. case result < 0:
  101. return v
  102. case result > 0:
  103. return other
  104. default:
  105. panic("Cannot compare identical validators")
  106. }
  107. }
  108. }
  109. // String returns a string representation of String.
  110. //
  111. // 1. address
  112. // 2. public key
  113. // 3. voting power
  114. // 4. proposer priority
  115. func (v *Validator) String() string {
  116. if v == nil {
  117. return "nil-Validator"
  118. }
  119. return fmt.Sprintf("Validator{%v %v VP:%v A:%v}",
  120. v.Address,
  121. v.PubKey,
  122. v.VotingPower,
  123. v.ProposerPriority)
  124. }
  125. // ValidatorListString returns a prettified validator list for logging purposes.
  126. func ValidatorListString(vals []*Validator) string {
  127. chunks := make([]string, len(vals))
  128. for i, val := range vals {
  129. chunks[i] = fmt.Sprintf("%s:%d", val.Address, val.VotingPower)
  130. }
  131. return strings.Join(chunks, ",")
  132. }
  133. // Bytes computes the unique encoding of a validator with a given voting power.
  134. // These are the bytes that gets hashed in consensus. It excludes address
  135. // as its redundant with the pubkey. This also excludes ProposerPriority
  136. // which changes every round.
  137. func (v *Validator) Bytes() []byte {
  138. pk, err := encoding.PubKeyToProto(v.PubKey)
  139. if err != nil {
  140. panic(err)
  141. }
  142. pbv := tmproto.SimpleValidator{
  143. PubKey: &pk,
  144. VotingPower: v.VotingPower,
  145. }
  146. bz, err := pbv.Marshal()
  147. if err != nil {
  148. panic(err)
  149. }
  150. return bz
  151. }
  152. // ToProto converts Valiator to protobuf
  153. func (v *Validator) ToProto() (*tmproto.Validator, error) {
  154. if v == nil {
  155. return nil, errors.New("nil validator")
  156. }
  157. pk, err := encoding.PubKeyToProto(v.PubKey)
  158. if err != nil {
  159. return nil, err
  160. }
  161. vp := tmproto.Validator{
  162. Address: v.Address,
  163. PubKey: pk,
  164. VotingPower: v.VotingPower,
  165. ProposerPriority: v.ProposerPriority,
  166. }
  167. return &vp, nil
  168. }
  169. // FromProto sets a protobuf Validator to the given pointer.
  170. // It returns an error if the public key is invalid.
  171. func ValidatorFromProto(vp *tmproto.Validator) (*Validator, error) {
  172. if vp == nil {
  173. return nil, errors.New("nil validator")
  174. }
  175. pk, err := encoding.PubKeyFromProto(vp.PubKey)
  176. if err != nil {
  177. return nil, err
  178. }
  179. v := new(Validator)
  180. v.Address = vp.GetAddress()
  181. v.PubKey = pk
  182. v.VotingPower = vp.GetVotingPower()
  183. v.ProposerPriority = vp.GetProposerPriority()
  184. return v, nil
  185. }