-
Notifications
You must be signed in to change notification settings - Fork 11
/
validator.go
307 lines (267 loc) · 7.54 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/rs/zerolog"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/crypto/bls12381"
ce "github.com/dashpay/tenderdash/crypto/encoding"
"github.com/dashpay/tenderdash/internal/jsontypes"
tmproto "github.com/dashpay/tenderdash/proto/tendermint/types"
)
// Validator Volatile state for each Validator
// NOTE: The ProposerPriority is not included in Validator.Hash();
// make sure to update that method if changes are made here
// The ProTxHash is part of Dash additions required for BLS threshold signatures
type Validator struct {
ProTxHash ProTxHash
PubKey crypto.PubKey
VotingPower int64
NodeAddress ValidatorAddress
ProposerPriority int64
}
type validatorJSON struct {
PubKey json.RawMessage `json:"pub_key,omitempty"`
VotingPower int64 `json:"voting_power,string"`
ProTxHash ProTxHash `json:"pro_tx_hash"`
NodeAddress ValidatorAddress `json:"address"`
ProposerPriority int64 `json:"proposer_priority,string"`
}
func (v Validator) MarshalJSON() ([]byte, error) {
val := validatorJSON{
ProTxHash: v.ProTxHash,
VotingPower: v.VotingPower,
ProposerPriority: v.ProposerPriority,
}
if v.PubKey != nil {
pk, err := jsontypes.Marshal(v.PubKey)
if err != nil {
return nil, err
}
val.PubKey = pk
}
return json.Marshal(val)
}
func (v *Validator) UnmarshalJSON(data []byte) error {
var val validatorJSON
if err := json.Unmarshal(data, &val); err != nil {
return err
}
if err := jsontypes.Unmarshal(val.PubKey, &v.PubKey); err != nil {
return err
}
v.ProTxHash = val.ProTxHash
v.VotingPower = val.VotingPower
v.ProposerPriority = val.ProposerPriority
return nil
}
func NewTestValidatorGeneratedFromProTxHash(proTxHash crypto.ProTxHash) *Validator {
return &Validator{
VotingPower: DefaultDashVotingPower,
ProposerPriority: 0,
ProTxHash: proTxHash,
}
}
func NewTestRemoveValidatorGeneratedFromProTxHash(proTxHash crypto.ProTxHash) *Validator {
return &Validator{
VotingPower: 0,
ProposerPriority: 0,
ProTxHash: proTxHash,
}
}
func NewValidatorDefaultVotingPower(pubKey crypto.PubKey, proTxHash []byte) *Validator {
return NewValidator(pubKey, DefaultDashVotingPower, proTxHash, "")
}
// NewValidator returns a new validator with the given pubkey and voting power.
func NewValidator(pubKey crypto.PubKey, votingPower int64, proTxHash ProTxHash, address string) *Validator {
var (
addr ValidatorAddress
err error
)
if address != "" {
addr, err = ParseValidatorAddress(address)
if err != nil {
panic(err.Error())
}
}
return &Validator{
PubKey: pubKey,
VotingPower: votingPower,
ProposerPriority: 0,
ProTxHash: proTxHash,
NodeAddress: addr,
}
}
// ValidateBasic performs basic validation.
func (v *Validator) ValidateBasic() error {
if v == nil {
return errors.New("nil validator")
}
if v.ProTxHash == nil {
return errors.New("validator does not have a provider transaction hash")
}
if v.VotingPower < 0 {
return errors.New("validator has negative voting power")
}
if len(v.ProTxHash) != crypto.DefaultHashSize {
return fmt.Errorf("validator proTxHash is the wrong size: %v", len(v.ProTxHash))
}
if !v.NodeAddress.Zero() {
if err := v.NodeAddress.Validate(); err != nil {
return fmt.Errorf("validator node address is invalid: %w", err)
}
}
return nil
}
// ValidatePubKey performs basic validation on the public key.
func (v *Validator) ValidatePubKey() error {
if v.PubKey == nil {
return errors.New("validator does not have a public key")
}
if len(v.PubKey.Bytes()) != bls12381.PubKeySize {
return fmt.Errorf("validator PubKey is the wrong size: %X", v.PubKey.Bytes())
}
return nil
}
// Copy creates a new copy of the validator so we can mutate ProposerPriority.
// Panics if the validator is nil.
func (v *Validator) Copy() *Validator {
vCopy := *v
return &vCopy
}
// CompareProposerPriority 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.ProTxHash, other.ProTxHash)
switch {
case result < 0:
return v
case result > 0:
return other
default:
panic("Cannot compare identical validators")
}
}
}
// String returns a string representation of String.
//
// 1. address
// 2. public key
// 3. voting power
// 4. proposer priority
// 5. node address
func (v *Validator) String() string {
if v == nil {
return "nil-Validator"
}
return fmt.Sprintf("Validator{%v %v VP:%v A:%v N:%s}",
v.ProTxHash,
v.PubKey,
v.VotingPower,
v.ProposerPriority,
v.NodeAddress.String())
}
func (v *Validator) ShortStringBasic() string {
if v == nil {
return "nil-Validator"
}
return fmt.Sprintf("Validator{%v %v}",
v.ProTxHash.ShortString(),
v.PubKey)
}
// MarshalZerologObject implements zerolog.LogObjectMarshaler
func (v *Validator) MarshalZerologObject(e *zerolog.Event) {
e.Str("protxhash", v.ProTxHash.ShortString())
e.Int64("voting_power", v.VotingPower)
e.Int64("proposer_priority", v.ProposerPriority)
e.Str("address", v.NodeAddress.String())
if v.PubKey != nil {
pubkey := v.PubKey.HexString()
if len(pubkey) > 8 {
pubkey = pubkey[:8]
}
e.Str("pub_key", pubkey)
e.Str("pub_key_type", v.PubKey.Type())
}
}
// 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:%s:%d", val.ProTxHash, val.PubKey, 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 {
pk := ce.MustPubKeyToProto(v.PubKey)
pbv := tmproto.SimpleValidator{
PubKey: &pk,
VotingPower: v.VotingPower,
}
bz, err := pbv.Marshal()
if err != nil {
panic(err)
}
return bz
}
// ToProto converts Validator to protobuf
func (v *Validator) ToProto() (*tmproto.Validator, error) {
if v == nil {
return nil, errors.New("nil validator")
}
if v.ProTxHash == nil {
return nil, errors.New("the validator must have a proTxHash")
}
vp := tmproto.Validator{
VotingPower: v.VotingPower,
ProposerPriority: v.ProposerPriority,
ProTxHash: v.ProTxHash,
}
if v.PubKey != nil && len(v.PubKey.Bytes()) > 0 {
pk, err := ce.PubKeyToProto(v.PubKey)
if err != nil {
return nil, err
}
vp.PubKey = &pk
}
vp.NodeAddress = v.NodeAddress.String()
return &vp, nil
}
// ValidatorFromProto sets a protobuf Validator to the given pointer.
// It returns an error if the public key is invalid.
func ValidatorFromProto(vp *tmproto.Validator) (*Validator, error) {
if vp == nil {
return nil, errors.New("nil validator")
}
v := new(Validator)
v.VotingPower = vp.GetVotingPower()
v.ProposerPriority = vp.GetProposerPriority()
v.ProTxHash = vp.ProTxHash
var err error
if vp.PubKey != nil && vp.PubKey.Sum != nil {
if v.PubKey, err = ce.PubKeyFromProto(*vp.PubKey); err != nil {
return nil, err
}
}
if vp.NodeAddress != "" {
if v.NodeAddress, err = ParseValidatorAddress(vp.NodeAddress); err != nil {
return nil, err
}
}
return v, nil
}