-
Notifications
You must be signed in to change notification settings - Fork 174
/
validator.go
235 lines (204 loc) · 5.9 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package types
import (
"bytes"
"fmt"
"math/big"
"sort"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
)
// Validator heimdall validator
type Validator struct {
ID ValidatorID `json:"ID"`
StartEpoch uint64 `json:"startEpoch"`
EndEpoch uint64 `json:"endEpoch"`
Nonce uint64 `json:"nonce"`
VotingPower int64 `json:"power"` // TODO add 10^-18 here so that we dont overflow easily
PubKey PubKey `json:"pubKey"`
Signer HeimdallAddress `json:"signer"`
LastUpdated string `json:"last_updated"`
Jailed bool `json:"jailed"`
ProposerPriority int64 `json:"accum"`
}
// NewValidator func creates a new validator,
// the HeimdallAddress field is generated using Address i.e. [20]byte
func NewValidator(
id ValidatorID,
startEpoch uint64,
endEpoch uint64,
nonce uint64,
power int64,
pubKey PubKey,
signer HeimdallAddress,
) *Validator {
return &Validator{
ID: id,
StartEpoch: startEpoch,
EndEpoch: endEpoch,
Nonce: nonce,
VotingPower: power,
PubKey: pubKey,
Signer: signer,
}
}
// SortValidatorByAddress sorts a slice of validators by address
// to sort it we compare the values of the Signer(HeimdallAddress i.e. [20]byte)
func SortValidatorByAddress(a []Validator) []Validator {
sort.Slice(a, func(i, j int) bool {
return bytes.Compare(a[i].Signer.Bytes(), a[j].Signer.Bytes()) < 0
})
return a
}
// IsCurrentValidator checks if validator is in current validator set
func (v *Validator) IsCurrentValidator(ackCount uint64) bool {
// current epoch will be ack count + 1
currentEpoch := ackCount + 1
// validator hasnt initialised unstake
if !v.Jailed && v.StartEpoch <= currentEpoch && (v.EndEpoch == 0 || v.EndEpoch > currentEpoch) && v.VotingPower > 0 {
return true
}
return false
}
// Validates validator
func (v *Validator) ValidateBasic() bool {
if bytes.Equal(v.PubKey.Bytes(), ZeroPubKey.Bytes()) {
return false
}
if bytes.Equal(v.Signer.Bytes(), []byte("")) {
return false
}
return true
}
// amino marshall validator
func MarshallValidator(cdc *codec.Codec, validator Validator) (bz []byte, err error) {
bz, err = cdc.MarshalBinaryBare(validator)
if err != nil {
return bz, err
}
return bz, nil
}
// amono unmarshall validator
func UnmarshallValidator(cdc *codec.Codec, value []byte) (Validator, error) {
var validator Validator
// unmarshall validator and return
err := cdc.UnmarshalBinaryBare(value, &validator)
if err != nil {
return validator, err
}
return validator, nil
}
// Copy creates a new copy of the validator so we can mutate accum.
// Panics if the validator is nil.
func (v *Validator) Copy() *Validator {
vCopy := *v
return &vCopy
}
// Returns the one with higher ProposerPriority.
func (v *Validator) CompareProposerPriority(other *Validator) *Validator {
if v == nil {
return other
}
switch {
case v.ProposerPriority > other.ProposerPriority:
return v
case v.ProposerPriority < other.ProposerPriority:
return other
default:
result := bytes.Compare(v.Signer.Bytes(), other.Signer.Bytes())
switch {
case result < 0:
return v
case result > 0:
return other
default:
panic("Cannot compare identical validators")
}
}
}
func (v *Validator) String() string {
if v == nil {
return "nil-Validator"
}
return fmt.Sprintf("Validator{%v %v %v VP:%v A:%v}",
v.ID,
v.Signer,
v.PubKey,
v.VotingPower,
v.ProposerPriority)
}
// ValidatorListString returns a prettified validator list for logging purposes.
func ValidatorListString(vals []*Validator) string {
chunks := make([]string, len(vals))
for i, val := range vals {
chunks[i] = fmt.Sprintf("%s:%d", val.Signer, val.VotingPower)
}
return strings.Join(chunks, ",")
}
// Bytes computes the unique encoding of a validator with a given voting power.
// These are the bytes that gets hashed in consensus. It excludes address
// as its redundant with the pubkey. This also excludes ProposerPriority
// which changes every round.
func (v *Validator) Bytes() []byte {
result := make([]byte, 64)
copy(result[12:], v.Signer.Bytes())
copy(result[32:], new(big.Int).SetInt64(v.VotingPower).Bytes())
return result
}
// UpdatedAt returns block number of last validator update
func (v *Validator) UpdatedAt() string {
return v.LastUpdated
}
// MinimalVal returns block number of last validator update
func (v *Validator) MinimalVal() MinimalVal {
return MinimalVal{
ID: v.ID,
VotingPower: uint64(v.VotingPower),
Signer: v.Signer,
}
}
// --------
// ValidatorID validator ID and helper functions
type ValidatorID uint64
// NewValidatorID generate new validator ID
func NewValidatorID(id uint64) ValidatorID {
return ValidatorID(id)
}
// Bytes get bytes of validatorID
func (valID ValidatorID) Bytes() []byte {
return []byte(strconv.FormatUint(valID.Uint64(), 10))
}
// Int converts validator ID to int
func (valID ValidatorID) Int() int {
return int(valID)
}
// Uint64 converts validator ID to int
func (valID ValidatorID) Uint64() uint64 {
return uint64(valID)
}
// Uint64 converts validator ID to int
func (valID ValidatorID) String() string {
return strconv.FormatUint(valID.Uint64(), 10)
}
// --------
// MinimalVal is the minimal validator representation
// Used to send validator information to bor validator contract
type MinimalVal struct {
ID ValidatorID `json:"ID"`
VotingPower uint64 `json:"power"` // TODO add 10^-18 here so that we dont overflow easily
Signer HeimdallAddress `json:"signer"`
}
// SortMinimalValByAddress sorts validators
func SortMinimalValByAddress(a []MinimalVal) []MinimalVal {
sort.Slice(a, func(i, j int) bool {
return bytes.Compare(a[i].Signer.Bytes(), a[j].Signer.Bytes()) < 0
})
return a
}
// ValToMinVal converts array of validators to minimal validators
func ValToMinVal(vals []Validator) (minVals []MinimalVal) {
for _, val := range vals {
minVals = append(minVals, val.MinimalVal())
}
return
}