tendermint/abci/types/util.go

60 lines
1.3 KiB
Go
Raw Normal View History

2016-11-21 20:42:42 -08:00
package types
import (
"bytes"
"encoding/json"
"sort"
2018-05-23 19:20:35 -07:00
2018-07-01 19:36:49 -07:00
cmn "github.com/tendermint/tendermint/libs/common"
2016-11-21 20:42:42 -08:00
)
2017-11-30 21:51:20 -08:00
//------------------------------------------------------------------------------
2017-11-30 21:41:07 -08:00
// Validators is a list of validators that implements the Sort interface
type Validators []Validator
2016-11-21 20:42:42 -08: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 20:42:42 -08: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 {
2018-05-23 19:20:35 -07:00
return bytes.Compare(v[i].PubKey.Data, v[j].PubKey.Data) <= 0
2016-11-21 20:42:42 -08:00
}
func (v Validators) Swap(i, j int) {
v1 := v[i]
v[i] = v[j]
v[j] = v1
}
2016-11-23 15:22:44 -08:00
func ValidatorsString(vs Validators) string {
s := make([]validatorPretty, len(vs))
for i, v := range vs {
2018-05-23 19:20:35 -07:00
s[i] = validatorPretty{
Address: v.Address,
PubKey: v.PubKey.Data,
Power: v.Power,
}
2016-11-23 15:22:44 -08:00
}
2017-05-15 09:59:44 -07:00
b, err := json.Marshal(s)
if err != nil {
panic(err.Error())
2017-05-15 09:59:44 -07:00
}
return string(b)
2016-11-23 15:22:44 -08:00
}
2017-11-30 21:51:20 -08:00
type validatorPretty struct {
2018-05-23 19:20:35 -07:00
Address cmn.HexBytes `json:"address"`
PubKey []byte `json:"pub_key"`
Power int64 `json:"power"`
2017-11-30 21:51:20 -08:00
}