2016-11-21 23:42:42 -05:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-04-27 19:18:53 -04:00
|
|
|
"encoding/json"
|
2018-03-12 14:43:15 -07:00
|
|
|
"sort"
|
2016-11-21 23:42:42 -05:00
|
|
|
)
|
|
|
|
|
2017-12-01 00:51:20 -05:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2017-12-01 00:41:07 -05:00
|
|
|
// Validators is a list of validators that implements the Sort interface
|
2017-12-22 19:41:19 -08:00
|
|
|
type Validators []Validator
|
2016-11-21 23:42:42 -05:00
|
|
|
|
2018-03-12 14:43:15 -07:00
|
|
|
var _ sort.Interface = (Validators)(nil)
|
|
|
|
|
|
|
|
// All these methods for Validators:
|
|
|
|
// Len, Less and Swap
|
|
|
|
// are for Validators to implement sort.Interface
|
|
|
|
// which will be used by the sort package.
|
|
|
|
// See Issue https://github.com/tendermint/abci/issues/212
|
|
|
|
|
2016-11-21 23:42:42 -05:00
|
|
|
func (v Validators) Len() int {
|
|
|
|
return len(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: doesn't distinguish same validator with different power
|
|
|
|
func (v Validators) Less(i, j int) bool {
|
|
|
|
return bytes.Compare(v[i].PubKey, v[j].PubKey) <= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validators) Swap(i, j int) {
|
|
|
|
v1 := v[i]
|
|
|
|
v[i] = v[j]
|
|
|
|
v[j] = v1
|
|
|
|
}
|
2016-11-23 18:22:44 -05:00
|
|
|
|
|
|
|
func ValidatorsString(vs Validators) string {
|
|
|
|
s := make([]validatorPretty, len(vs))
|
|
|
|
for i, v := range vs {
|
2017-12-26 15:46:06 -08:00
|
|
|
s[i] = validatorPretty(v)
|
2016-11-23 18:22:44 -05:00
|
|
|
}
|
2017-05-15 12:59:44 -04:00
|
|
|
b, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
2017-12-26 04:52:02 -08:00
|
|
|
panic(err.Error())
|
2017-05-15 12:59:44 -04:00
|
|
|
}
|
2017-04-27 19:18:53 -04:00
|
|
|
return string(b)
|
2016-11-23 18:22:44 -05:00
|
|
|
}
|
2017-12-01 00:51:20 -05:00
|
|
|
|
|
|
|
type validatorPretty struct {
|
2017-12-26 04:52:02 -08:00
|
|
|
PubKey []byte `json:"pub_key"`
|
|
|
|
Power int64 `json:"power"`
|
2017-12-01 00:51:20 -05:00
|
|
|
}
|