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.

59 lines
1.3 KiB

7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "sort"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. )
  8. //------------------------------------------------------------------------------
  9. // Validators is a list of validators that implements the Sort interface
  10. type Validators []Validator
  11. var _ sort.Interface = (Validators)(nil)
  12. // All these methods for Validators:
  13. // Len, Less and Swap
  14. // are for Validators to implement sort.Interface
  15. // which will be used by the sort package.
  16. // See Issue https://github.com/tendermint/abci/issues/212
  17. func (v Validators) Len() int {
  18. return len(v)
  19. }
  20. // XXX: doesn't distinguish same validator with different power
  21. func (v Validators) Less(i, j int) bool {
  22. return bytes.Compare(v[i].PubKey.Data, v[j].PubKey.Data) <= 0
  23. }
  24. func (v Validators) Swap(i, j int) {
  25. v1 := v[i]
  26. v[i] = v[j]
  27. v[j] = v1
  28. }
  29. func ValidatorsString(vs Validators) string {
  30. s := make([]validatorPretty, len(vs))
  31. for i, v := range vs {
  32. s[i] = validatorPretty{
  33. Address: v.Address,
  34. PubKey: v.PubKey.Data,
  35. Power: v.Power,
  36. }
  37. }
  38. b, err := json.Marshal(s)
  39. if err != nil {
  40. panic(err.Error())
  41. }
  42. return string(b)
  43. }
  44. type validatorPretty struct {
  45. Address cmn.HexBytes `json:"address"`
  46. PubKey []byte `json:"pub_key"`
  47. Power int64 `json:"power"`
  48. }