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.

74 lines
1.5 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. const (
  9. PubKeyEd25519 = "ed25519"
  10. )
  11. func Ed25519Validator(pubkey []byte, power int64) Validator {
  12. return Validator{
  13. // Address:
  14. PubKey: PubKey{
  15. Type: PubKeyEd25519,
  16. Data: pubkey,
  17. },
  18. Power: power,
  19. }
  20. }
  21. //------------------------------------------------------------------------------
  22. // Validators is a list of validators that implements the Sort interface
  23. type Validators []Validator
  24. var _ sort.Interface = (Validators)(nil)
  25. // All these methods for Validators:
  26. // Len, Less and Swap
  27. // are for Validators to implement sort.Interface
  28. // which will be used by the sort package.
  29. // See Issue https://github.com/tendermint/abci/issues/212
  30. func (v Validators) Len() int {
  31. return len(v)
  32. }
  33. // XXX: doesn't distinguish same validator with different power
  34. func (v Validators) Less(i, j int) bool {
  35. return bytes.Compare(v[i].PubKey.Data, v[j].PubKey.Data) <= 0
  36. }
  37. func (v Validators) Swap(i, j int) {
  38. v1 := v[i]
  39. v[i] = v[j]
  40. v[j] = v1
  41. }
  42. func ValidatorsString(vs Validators) string {
  43. s := make([]validatorPretty, len(vs))
  44. for i, v := range vs {
  45. s[i] = validatorPretty{
  46. Address: v.Address,
  47. PubKey: v.PubKey.Data,
  48. Power: v.Power,
  49. }
  50. }
  51. b, err := json.Marshal(s)
  52. if err != nil {
  53. panic(err.Error())
  54. }
  55. return string(b)
  56. }
  57. type validatorPretty struct {
  58. Address cmn.HexBytes `json:"address"`
  59. PubKey []byte `json:"pub_key"`
  60. Power int64 `json:"power"`
  61. }