-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
executable file
·297 lines (246 loc) · 9.14 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
package types
import (
"bytes"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
"github.com/ci123chain/ci123chain/pkg/util"
"github.com/tendermint/go-amino"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
cryptoEncod "github.com/tendermint/tendermint/crypto/encoding"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
"sort"
"time"
"fmt"
)
type Validator struct {
OperatorAddress sdk.AccAddress `json:"operator_address"`
ConsensusKey string `json:"pub_key"`
ConsensusAddress string `json:"consensus_address"`
Jailed bool `json:"jailed"`
Tokens sdk.Int `json:"tokens"`
Status sdk.BondStatus `json:"status"`
DelegatorShares sdk.Dec `json:"delegator_shares"`
Description Description `json:"description"`
UnbondingHeight int64 `json:"unbonding_height"`
UnbondingTime time.Time `json:"unbonding_time"`
BondedHeight int64 `json:"bonded_height"`
Commission Commission `json:"commission"`
MinSelfDelegation sdk.Int `json:"min_self_delegation"`
}
//crypto pubKey
func NewValidator(operator sdk.AccAddress, pubKey string, description Description)( Validator, error) {
v := Validator{
OperatorAddress: operator,
ConsensusKey: pubKey,
Jailed: false,
Status: sdk.Unbonded,
Tokens: sdk.ZeroInt(),
DelegatorShares: sdk.ZeroDec(),
Description: description,
UnbondingHeight: int64(0),
UnbondingTime: time.Unix(0,0).UTC(),
BondedHeight: int64(-1),
Commission: NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()),
MinSelfDelegation: sdk.OneInt(),
}
v.ConsensusAddress = v.GetConsPubKey().Address().String()
return v, nil
}
func (v Validator) SetInitialCommission(commission Commission) (Validator, error) {
if err := commission.CommissionRates.Validate(); err != nil {
return v, err
}
v.Commission = commission
return v, nil
}
// 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)
}
// In some situations, the exchange rate becomes invalid, e.g. if
// Validator loses all tokens due to slashing. In this case,
// make all future delegations invalid.
func (v Validator) InvalidExRate() bool {
return v.Tokens.IsZero() && v.DelegatorShares.IsPositive()
}
// RemoveTokens removes tokens from a validator
func (v Validator) RemoveTokens(tokens sdk.Int) Validator {
if tokens.IsNegative() {
panic(fmt.Sprintf("should not happen: trying to remove negative tokens %v", tokens))
}
if v.Tokens.LT(tokens) {
panic(fmt.Sprintf("should not happen: only have %v tokens, trying to remove %v", v.Tokens, tokens))
}
v.Tokens = v.Tokens.Sub(tokens)
return v
}
// AddTokensFromDel adds tokens to a validator
func (v Validator) AddTokensFromDel(amount sdk.Int) (Validator, sdk.Dec) {
// calculate the shares to issue
var issuedShares sdk.Dec
if v.DelegatorShares.IsZero() {
// the first delegation to a validator sets the exchange rate to one
issuedShares = amount.ToDec()
} else {
shares, err := v.SharesFromTokens(amount)
if err != nil {
panic(err)
}
issuedShares = shares
}
//issuedShares = amount.ToDec()
v.Tokens = v.Tokens.Add(amount)
v.DelegatorShares = v.DelegatorShares.Add(issuedShares)
return v, issuedShares
}
// RemoveDelShares removes delegator shares from a validator.
// NOTE: because token fractions are left in the valiadator,
// the exchange rate of future shares of this validator can increase.
func (v Validator) RemoveDelShares(delShares sdk.Dec) (Validator, sdk.Int) {
remainingShares := v.DelegatorShares.Sub(delShares)
var issuedTokens sdk.Int
if remainingShares.IsZero() {
// last delegation share gets any trimmings
issuedTokens = v.Tokens
v.Tokens = sdk.ZeroInt()
} else {
// leave excess tokens in the validator
// however fully use all the delegator shares
issuedTokens = v.TokensFromShares(delShares).TruncateInt()
v.Tokens = v.Tokens.Sub(issuedTokens)
if v.Tokens.IsNegative() {
panic("attempting to remove more tokens than available in validator")
}
}
v.DelegatorShares = remainingShares
return v, issuedTokens
}
// calculate the token worth of provided shares
func (v Validator) TokensFromShares(shares sdk.Dec) sdk.Dec {
return (shares.MulInt(v.Tokens)).Quo(v.DelegatorShares)
}
// SharesFromTokens returns the shares of a delegation given a bond amount. It
// returns an error if the validator has no tokens.
func (v Validator) SharesFromTokens(amt sdk.Int) (sdk.Dec, error) {
if v.Tokens.IsZero() {
return sdk.ZeroDec(), ErrInsufficientShares
}
return v.GetDelegatorShares().MulInt(amt).QuoInt(v.GetTokens()), nil
}
func (v Validator) SharesFromTokensTruncated(amt sdk.Int) (sdk.Dec, error) {
if v.Tokens.IsZero() {
return sdk.ZeroDec(), ErrInsufficientShares
}
return v.GetDelegatorShares().MulInt(amt).QuoTruncate(v.GetTokens().ToDec()), nil
}
// get the bonded tokens which the validator holds
func (v Validator) BondedTokens() sdk.Int {
if v.IsBonded() {
return v.Tokens
}
return sdk.ZeroInt()
}
// get the consensus-engine power
// a reduction of 10^6 from validator tokens is applied
func (v Validator) ConsensusPower() int64 {
if v.IsBonded() {
return v.PotentialConsensusPower()
}
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
}
// ABCIValidatorUpdateZero returns an abci.ValidatorUpdate from a staking validator types
// with zero power used for validator updates.
func (v Validator) ABCIValidatorUpdateZero() abci.ValidatorUpdate {
key, _ := cryptoEncod.PubKeyToProto(v.GetConsPubKey())
return abci.ValidatorUpdate{
PubKey: key,
Power: 0,
}
}
// ABCIValidatorUpdate returns an abci.ValidatorUpdate from a staking validator types
// with the full validator power
func (v Validator) ABCIValidatorUpdate() abci.ValidatorUpdate {
key, _ := cryptoEncod.PubKeyToProto(v.GetConsPubKey())
return abci.ValidatorUpdate{
PubKey: key,
Power: v.ConsensusPower(),
}
}
// potential consensus-engine power
func (v Validator) PotentialConsensusPower() int64 {
return sdk.TokensToConsensusPower(v.Tokens)
}
// 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.AccAddress { return v.OperatorAddress }
func (v Validator) GetConsAddress() sdk.AccAddress { return sdk.ToAccAddress(v.GetConsPubKey().Address())}//公钥
func (v Validator) GetTokens() sdk.Int { return v.Tokens }
func (v Validator) GetBondedTokens() sdk.Int { return v.BondedTokens() }
func (v Validator) GetConsensusPower() int64 { return v.ConsensusPower() }
func (v Validator) GetCommission() sdk.Dec { return v.Commission.CommissionRates.Rate }
func (v Validator) GetMinSelfDelegation() sdk.Int { return v.MinSelfDelegation }
func (v Validator) GetDelegatorShares() sdk.Dec { return v.DelegatorShares }
func (v Validator) GetConsAddr() sdk.AccAddress {return sdk.ToAccAddress(v.GetConsPubKey().Address())}
func (v Validator) GetConsPubKey() crypto.PubKey {
var pubKey crypto.PubKey
pubKey, _ = util.ParsePubKey(v.ConsensusKey)
return pubKey
}
// calculate 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.
func (v Validator) TokensFromSharesRoundUp(shares sdk.Dec) sdk.Dec {
return (shares.MulInt(v.Tokens)).QuoRoundUp(v.DelegatorShares)
}
/*// calculate 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.
func (v Validator) TokensFromSharesRoundUp(shares sdk.Dec) sdk.Dec {
return (shares.MulInt(v.Tokens)).QuoRoundUp(v.DelegatorShares)
}*/
// Validators is a collection of Validator
type Validators []Validator
// Sort Validators sorts validator array in ascending operator address order
func (v Validators) Sort() {
sort.Sort(v)
}
// Implements sort interface
func (v Validators) Len() int {
return len(v)
}
// Implements sort interface
func (v Validators) Less(i, j int) bool {
return bytes.Compare(v[i].OperatorAddress.Bytes(), v[j].OperatorAddress.Bytes()) == -1
}
// Implements sort interface
func (v Validators) Swap(i, j int) {
it := v[i]
v[i] = v[j]
v[j] = it
}
var cdc = amino.NewCodec()
func init() {
cryptoAmino.RegisterAmino(cdc)
}