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.

43 lines
848 B

7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. )
  6. //------------------------------------------------------------------------------
  7. // Validators is a list of validators that implements the Sort interface
  8. type Validators []Validator
  9. func (v Validators) Len() int {
  10. return len(v)
  11. }
  12. // XXX: doesn't distinguish same validator with different power
  13. func (v Validators) Less(i, j int) bool {
  14. return bytes.Compare(v[i].PubKey, v[j].PubKey) <= 0
  15. }
  16. func (v Validators) Swap(i, j int) {
  17. v1 := v[i]
  18. v[i] = v[j]
  19. v[j] = v1
  20. }
  21. func ValidatorsString(vs Validators) string {
  22. s := make([]validatorPretty, len(vs))
  23. for i, v := range vs {
  24. s[i] = validatorPretty(v)
  25. }
  26. b, err := json.Marshal(s)
  27. if err != nil {
  28. panic(err.Error())
  29. }
  30. return string(b)
  31. }
  32. type validatorPretty struct {
  33. PubKey []byte `json:"pub_key"`
  34. Power int64 `json:"power"`
  35. }