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.

66 lines
1.4 KiB

7 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/tendermint/go-wire/data"
  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. func (v Validators) Len() int {
  12. return len(v)
  13. }
  14. // XXX: doesn't distinguish same validator with different power
  15. func (v Validators) Less(i, j int) bool {
  16. return bytes.Compare(v[i].PubKey, v[j].PubKey) <= 0
  17. }
  18. func (v Validators) Swap(i, j int) {
  19. v1 := v[i]
  20. v[i] = v[j]
  21. v[j] = v1
  22. }
  23. func ValidatorsString(vs Validators) string {
  24. s := make([]validatorPretty, len(vs))
  25. for i, v := range vs {
  26. s[i] = validatorPretty{v.PubKey, v.Power}
  27. }
  28. b, err := json.Marshal(s)
  29. if err != nil {
  30. cmn.PanicSanity(err.Error())
  31. }
  32. return string(b)
  33. }
  34. type validatorPretty struct {
  35. PubKey data.Bytes `json:"pub_key"`
  36. Power int64 `json:"power"`
  37. }
  38. //------------------------------------------------------------------------------
  39. // KVPairInt is a helper method to build KV pair with an integer value.
  40. func KVPairInt(key string, val int64) *KVPair {
  41. return &KVPair{
  42. Key: key,
  43. ValueInt: val,
  44. ValueType: KVPair_INT,
  45. }
  46. }
  47. // KVPairString is a helper method to build KV pair with a string value.
  48. func KVPairString(key, val string) *KVPair {
  49. return &KVPair{
  50. Key: key,
  51. ValueString: val,
  52. ValueType: KVPair_STRING,
  53. }
  54. }