tendermint/types/util.go

67 lines
1.4 KiB
Go
Raw Normal View History

2016-11-21 20:42:42 -08:00
package types
import (
"bytes"
"encoding/json"
2016-11-23 15:22:44 -08:00
"github.com/tendermint/go-wire/data"
2017-05-15 09:59:44 -07:00
cmn "github.com/tendermint/tmlibs/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
2016-11-21 20:42:42 -08:00
type Validators []*Validator
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 15:22:44 -08:00
func ValidatorsString(vs Validators) string {
s := make([]validatorPretty, len(vs))
for i, v := range vs {
s[i] = validatorPretty{v.PubKey, v.Power}
}
2017-05-15 09:59:44 -07:00
b, err := json.Marshal(s)
if err != nil {
cmn.PanicSanity(err.Error())
}
return string(b)
2016-11-23 15:22:44 -08:00
}
2017-11-30 21:51:20 -08:00
type validatorPretty struct {
PubKey data.Bytes `json:"pub_key"`
Power int64 `json:"power"`
}
//------------------------------------------------------------------------------
// KVPairInt is a helper method to build KV pair with an integer value.
func KVPairInt(key string, val int64) *KVPair {
return &KVPair{
Key: key,
ValueInt: val,
ValueType: KVPair_INT,
}
}
// KVPairString is a helper method to build KV pair with a string value.
func KVPairString(key, val string) *KVPair {
return &KVPair{
Key: key,
ValueString: val,
ValueType: KVPair_STRING,
}
}