-
Notifications
You must be signed in to change notification settings - Fork 38
/
validator.go
55 lines (41 loc) · 928 Bytes
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package loom
import (
"bytes"
"sort"
"github.com/loomnetwork/go-loom/types"
)
type (
Validator = types.Validator
ValidatorSet map[string]*Validator
)
func (vs ValidatorSet) Get(pubKey []byte) *Validator {
return vs[string(pubKey)]
}
func (vs ValidatorSet) Set(v *Validator) {
vs[string(v.PubKey)] = v
}
func (vs ValidatorSet) Slice() []*Validator {
vals := make([]*Validator, 0, len(vs))
for _, v := range vs {
vals = append(vals, v)
}
sort.Sort(validatorsByAddress(vals))
return vals
}
type validatorsByAddress []*Validator
func (s validatorsByAddress) Len() int {
return len(s)
}
func (s validatorsByAddress) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s validatorsByAddress) Less(i, j int) bool {
return bytes.Compare(s[i].PubKey, s[j].PubKey) < 0
}
func NewValidatorSet(vals ...*Validator) ValidatorSet {
vs := make(ValidatorSet)
for _, v := range vals {
vs.Set(v)
}
return vs
}