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.

52 lines
1.1 KiB

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