-
Notifications
You must be signed in to change notification settings - Fork 4
/
validator.go
453 lines (407 loc) · 16 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package types
import (
"bytes"
"fmt"
"strings"
"time"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
"gopkg.in/yaml.v2"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zenchainprotocol/zenchain-node/x/staking/exported"
)
// nolint
const (
// TODO: Why can't we just have one string description which can be JSON by convention
MaxMonikerLength = 70
MaxIdentityLength = 3000
MaxWebsiteLength = 140
MaxDetailsLength = 280
)
// Implements Validator interface
var _ exported.ValidatorI = Validator{}
// Validator defines the total amount of bond shares and their exchange rate to
// coins. Slashing results in a decrease in the exchange rate, allowing correct
// calculation of future undelegations without iterating over delegators.
// When coins are delegated to this validator, the validator is credited with a
// delegation whose number of bond shares is based on the amount of coins delegated
// divided by the current exchange rate. Voting power can be calculated as total
// bonded shares multiplied by exchange rate.
type Validator struct {
// address of the validator's operator; bech encoded in JSON
OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"`
// the consensus public key of the validator; bech encoded in JSON
ConsPubKey crypto.PubKey `json:"consensus_pubkey" yaml:"consensus_pubkey"`
// has the validator been jailed from bonded status?
Jailed bool `json:"jailed" yaml:"jailed"`
// validator status (bonded/unbonding/unbonded)
Status sdk.BondStatus `json:"status" yaml:"status"`
// delegated tokens (incl. self-delegation)
Tokens sdk.Int `json:"tokens" yaml:"tokens"`
// total shares added to a validator
DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"`
// description terms for the validator
Description Description `json:"description" yaml:"description"`
// if unbonding, height at which this validator has begun unbonding
UnbondingHeight int64 `json:"unbonding_height" yaml:"unbonding_height"`
// if unbonding, min time for the validator to complete unbonding
UnbondingCompletionTime time.Time `json:"unbonding_time" yaml:"unbonding_time"`
// commission parameters
Commission Commission `json:"commission" yaml:"commission"`
// validator's self declared minimum self delegation
MinSelfDelegation sdk.Dec `json:"min_self_delegation" yaml:"min_self_delegation"`
}
// MarshalYAML implements the text format for yaml marshaling due to consensus pubkey
func (v Validator) MarshalYAML() (interface{}, error) {
bs, err := yaml.Marshal(struct {
Status sdk.BondStatus
Jailed bool
UnbondingHeight int64
ConsPubKey string
OperatorAddress sdk.ValAddress
Tokens sdk.Int
DelegatorShares sdk.Dec
Description Description
UnbondingCompletionTime time.Time
Commission Commission
MinSelfDelegation sdk.Dec
}{
OperatorAddress: v.OperatorAddress,
ConsPubKey: MustBech32ifyConsPub(v.ConsPubKey),
Jailed: v.Jailed,
Status: v.Status,
Tokens: v.Tokens,
DelegatorShares: v.DelegatorShares,
Description: v.Description,
UnbondingHeight: v.UnbondingHeight,
UnbondingCompletionTime: v.UnbondingCompletionTime,
Commission: v.Commission,
MinSelfDelegation: v.MinSelfDelegation,
})
if err != nil {
return nil, err
}
return string(bs), nil
}
// Validators is a collection of Validator
type Validators []Validator
// String returns a human readable string representation of Validators
func (v Validators) String() (out string) {
for _, val := range v {
out += val.String() + "\n"
}
return strings.TrimSpace(out)
}
// ToSDKValidators converts []Validators to []sdk.Validators
func (v Validators) ToSDKValidators() (validators []exported.ValidatorI) {
for _, val := range v {
validators = append(validators, val)
}
return validators
}
// NewValidator initializes a new validator
func NewValidator(operator sdk.ValAddress, pubKey crypto.PubKey, description Description, minSelfDelegation sdk.Dec) Validator {
return Validator{
OperatorAddress: operator,
ConsPubKey: pubKey,
Jailed: false,
Status: sdk.Unbonded,
Tokens: sdk.ZeroInt(),
DelegatorShares: sdk.ZeroDec(),
Description: description,
UnbondingHeight: int64(0),
UnbondingCompletionTime: time.Unix(0, 0).UTC(),
Commission: NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: minSelfDelegation,
}
}
// MustMarshalValidator must return the marshaling bytes of a validator
func MustMarshalValidator(cdc *codec.Codec, validator Validator) []byte {
return cdc.MustMarshalBinaryLengthPrefixed(validator)
}
// MustUnmarshalValidator must return the validator entity by unmarshaling
func MustUnmarshalValidator(cdc *codec.Codec, value []byte) Validator {
validator, err := UnmarshalValidator(cdc, value)
if err != nil {
panic(err)
}
return validator
}
// UnmarshalValidator unmarshals a validator from a store value
func UnmarshalValidator(cdc *codec.Codec, value []byte) (validator Validator, err error) {
err = cdc.UnmarshalBinaryLengthPrefixed(value, &validator)
return validator, err
}
// String returns a human readable string representation of a validator.
func (v Validator) String() string {
bechConsPubKey, err := Bech32ifyConsPub(v.ConsPubKey)
if err != nil {
panic(err)
}
return fmt.Sprintf(`Validator
Operator Address: %s
Validator Consensus Pubkey: %s
Jailed: %v
Status: %s
Tokens: %s
Delegator Shares: %s
Description: %s
Unbonding Height: %d
Unbonding Completion Time: %v
Minimum Self Delegation: %v
Commission: %s`,
v.OperatorAddress, bechConsPubKey,
v.Jailed, v.Status, v.Tokens,
v.DelegatorShares, v.Description,
v.UnbondingHeight, v.UnbondingCompletionTime, v.MinSelfDelegation,
v.Commission)
}
// this is a helper struct used for JSON de- and encoding only
type bechValidator struct {
// the bech32 address of the validator's operator
OperatorAddress sdk.ValAddress `json:"operator_address" yaml:"operator_address"`
// the bech32 consensus public key of the validator
ConsPubKey string `json:"consensus_pubkey" yaml:"consensus_pubkey"`
// has the validator been jailed from bonded status?
Jailed bool `json:"jailed" yaml:"jailed"`
// validator status (bonded/unbonding/unbonded)
Status sdk.BondStatus `json:"status" yaml:"status"`
// delegated tokens (incl. self-delegation)
Tokens sdk.Int `json:"tokens" yaml:"tokens"`
// total shares on a validator
DelegatorShares sdk.Dec `json:"delegator_shares" yaml:"delegator_shares"`
// description terms for the validator
Description Description `json:"description" yaml:"description"`
// if unbonding, height at which this validator has begun unbonding
UnbondingHeight int64 `json:"unbonding_height" yaml:"unbonding_height"`
// if unbonding, min time for the validator to complete unbonding
UnbondingCompletionTime time.Time `json:"unbonding_time" yaml:"unbonding_time"`
// commission parameters
Commission Commission `json:"commission" yaml:"commission"`
// minimum self delegation
MinSelfDelegation sdk.Dec `json:"min_self_delegation" yaml:"min_self_delegation"`
}
// MarshalJSON marshals the validator to JSON using Bech32
func (v Validator) MarshalJSON() ([]byte, error) {
bechConsPubKey, err := Bech32ifyConsPub(v.ConsPubKey)
if err != nil {
return nil, err
}
return codec.Cdc.MarshalJSON(bechValidator{
OperatorAddress: v.OperatorAddress,
ConsPubKey: bechConsPubKey,
Jailed: v.Jailed,
Status: v.Status,
Tokens: v.Tokens,
DelegatorShares: v.DelegatorShares,
Description: v.Description,
UnbondingHeight: v.UnbondingHeight,
UnbondingCompletionTime: v.UnbondingCompletionTime,
MinSelfDelegation: v.MinSelfDelegation,
Commission: v.Commission,
})
}
// UnmarshalJSON unmarshals the validator from JSON using Bech32
func (v *Validator) UnmarshalJSON(data []byte) error {
bv := &bechValidator{}
if err := codec.Cdc.UnmarshalJSON(data, bv); err != nil {
return err
}
consPubKey, err := GetConsPubKeyBech32(bv.ConsPubKey)
if err != nil {
return err
}
*v = Validator{
OperatorAddress: bv.OperatorAddress,
ConsPubKey: consPubKey,
Jailed: bv.Jailed,
Tokens: bv.Tokens,
Status: bv.Status,
DelegatorShares: bv.DelegatorShares,
Description: bv.Description,
UnbondingHeight: bv.UnbondingHeight,
UnbondingCompletionTime: bv.UnbondingCompletionTime,
Commission: bv.Commission,
MinSelfDelegation: bv.MinSelfDelegation,
}
return nil
}
// TestEquivalent is only for the ut
func (v Validator) TestEquivalent(v2 Validator) bool {
return v.ConsPubKey.Equals(v2.ConsPubKey) &&
bytes.Equal(v.OperatorAddress, v2.OperatorAddress) &&
v.Status.Equal(v2.Status) &&
v.Tokens.Equal(v2.Tokens) &&
v.DelegatorShares.Equal(v2.DelegatorShares) &&
v.Description == v2.Description &&
v.Commission.Equal(v2.Commission)
}
// ConsAddress returns the TM validator address
func (v Validator) ConsAddress() sdk.ConsAddress {
return sdk.ConsAddress(v.ConsPubKey.Address())
}
// IsBonded checks if the validator status equals Bonded
func (v Validator) IsBonded() bool {
return v.GetStatus().Equal(sdk.Bonded)
}
// IsUnbonded checks if the validator status equals Unbonded
func (v Validator) IsUnbonded() bool {
return v.GetStatus().Equal(sdk.Unbonded)
}
// IsUnbonding checks if the validator status equals Unbonding
func (v Validator) IsUnbonding() bool {
return v.GetStatus().Equal(sdk.Unbonding)
}
// DoNotModifyDesc is the constant used in flags to indicate that description field should not be updated
const DoNotModifyDesc = "[do-not-modify]"
// Description - description fields for a validator
type Description struct {
Moniker string `json:"moniker" yaml:"moniker"` // name
Identity string `json:"identity" yaml:"identity"` // optional identity signature (ex. UPort or Keybase)
Website string `json:"website" yaml:"website"` // optional website link
Details string `json:"details" yaml:"details"` // optional details
}
// NewDescription returns a new Description with the provided values.
func NewDescription(moniker, identity, website, details string) Description {
return Description{
Moniker: moniker,
Identity: identity,
Website: website,
Details: details,
}
}
// UpdateDescription updates the fields of a given description. An error is
// returned if the resulting description contains an invalid length.
func (d Description) UpdateDescription(d2 Description) (Description, error) {
if d2.Moniker == DoNotModifyDesc {
d2.Moniker = d.Moniker
}
if d2.Identity == DoNotModifyDesc {
d2.Identity = d.Identity
}
if d2.Website == DoNotModifyDesc {
d2.Website = d.Website
}
if d2.Details == DoNotModifyDesc {
d2.Details = d.Details
}
return Description{
Moniker: d2.Moniker,
Identity: d2.Identity,
Website: d2.Website,
Details: d2.Details,
}.EnsureLength()
}
// EnsureLength ensures the length of a validator's description.
func (d Description) EnsureLength() (Description, error) {
if len(d.Moniker) > MaxMonikerLength {
return d, ErrDescriptionLength("moniker", len(d.Moniker), MaxMonikerLength)
}
if len(d.Identity) > MaxIdentityLength {
return d, ErrDescriptionLength("identity", len(d.Identity), MaxIdentityLength)
}
if len(d.Website) > MaxWebsiteLength {
return d, ErrDescriptionLength("website", len(d.Website), MaxWebsiteLength)
}
if len(d.Details) > MaxDetailsLength {
return d, ErrDescriptionLength("details", len(d.Details), MaxDetailsLength)
}
return d, nil
}
// ABCIValidatorUpdate returns an abci.ValidatorUpdate from a staking validator type
// with the full validator power
func (v Validator) ABCIValidatorUpdate() abci.ValidatorUpdate {
return abci.ValidatorUpdate{
PubKey: tmtypes.TM2PB.PubKey(v.ConsPubKey),
Power: v.ConsensusPower(),
}
}
// ABCIValidatorUpdateZero returns an abci.ValidatorUpdate from a staking validator type
// with zero power used for validator updates.
func (v Validator) ABCIValidatorUpdateZero() abci.ValidatorUpdate {
return abci.ValidatorUpdate{
PubKey: tmtypes.TM2PB.PubKey(v.ConsPubKey),
Power: 0,
}
}
// SetInitialCommission attempts to set a validator's initial commission. An
// error is returned if the commission is invalid.
func (v Validator) SetInitialCommission(commission Commission) (Validator, error) {
if err := commission.Validate(); err != nil {
return v, err
}
v.Commission = commission
return v, nil
}
// TODO : remove these functions that implements some origin interface later
// TokensFromShares calculates the token worth of provided shares
func (v Validator) TokensFromShares(shares sdk.Dec) sdk.Dec {
return (shares.MulInt(v.Tokens)).Quo(v.DelegatorShares)
}
// TokensFromSharesTruncated calculates the token worth of provided shares, truncated
func (v Validator) TokensFromSharesTruncated(shares sdk.Dec) sdk.Dec {
return (shares.MulInt(v.Tokens)).QuoTruncate(v.DelegatorShares)
}
// TokensFromSharesRoundUp returns the token worth of provided shares, rounded up
// No usage found in All Places
func (v Validator) TokensFromSharesRoundUp(shares sdk.Dec) sdk.Dec {
return sdk.ZeroDec()
//return (shares.MulInt(v.Tokens)).QuoRoundUp(v.DelegatorShares)
}
// SharesFromTokens returns the shares of a delegation given a bond amount
// It returns an error if the validator has no tokens
// No usage found in All Places
func (v Validator) SharesFromTokens(amt sdk.Int) (sdk.Dec, error) {
return sdk.ZeroDec(), nil
//if v.Tokens.IsZero() {
// return sdk.ZeroDec(), ErrInsufficientShares(DefaultCodespace)
//}
//
//return v.GetDelegatorShares().MulInt(amt).QuoInt(v.GetTokens()), nil
}
// SharesFromTokensTruncated returns the truncated shares of a delegation given a bond amount
// It returns an error if the validator has no tokens
// No usage found in All Places
func (v Validator) SharesFromTokensTruncated(amt sdk.Int) (sdk.Dec, error) {
return sdk.ZeroDec(), nil
//if v.Tokens.IsZero() {
// return sdk.ZeroDec(), ErrInsufficientShares(DefaultCodespace)
//}
//
//return v.GetDelegatorShares().MulInt(amt).QuoTruncate(v.GetTokens().ToDec()), nil
}
// BondedTokens gets the bonded tokens which the validator holds
func (v Validator) BondedTokens() sdk.Int {
if v.IsBonded() {
return v.Tokens
}
return sdk.ZeroInt()
}
// ConsensusPower gets the consensus-engine power
func (v Validator) ConsensusPower() int64 {
if v.IsBonded() {
return v.PotentialConsensusPowerByShares()
}
return 0
}
// UpdateStatus updates the location of the shares within a validator
// to reflect the new status
func (v Validator) UpdateStatus(newStatus sdk.BondStatus) Validator {
v.Status = newStatus
return v
}
// nolint - for ValidatorI
func (v Validator) IsJailed() bool { return v.Jailed }
func (v Validator) GetMoniker() string { return v.Description.Moniker }
func (v Validator) GetStatus() sdk.BondStatus { return v.Status }
func (v Validator) GetOperator() sdk.ValAddress { return v.OperatorAddress }
func (v Validator) GetConsPubKey() crypto.PubKey { return v.ConsPubKey }
func (v Validator) GetConsAddr() sdk.ConsAddress { return sdk.ConsAddress(v.ConsPubKey.Address()) }
func (v Validator) GetTokens() sdk.Int { return v.Tokens }
func (v Validator) GetBondedTokens() sdk.Int { return sdk.ZeroInt() }
func (v Validator) GetConsensusPower() int64 { return v.ConsensusPower() }
func (v Validator) GetCommission() sdk.Dec { return v.Commission.Rate }
func (v Validator) GetMinSelfDelegation() sdk.Dec { return v.MinSelfDelegation }
func (v Validator) GetDelegatorShares() sdk.Dec { return v.DelegatorShares }