-
Notifications
You must be signed in to change notification settings - Fork 102
/
validator.go
283 lines (261 loc) · 9.48 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
package keeper
import (
"bytes"
"sort"
sdk "github.com/pokt-network/pocket-core/types"
"github.com/pokt-network/pocket-core/x/nodes/exported"
"github.com/pokt-network/pocket-core/x/nodes/types"
)
func (k Keeper) MarshalValidator(ctx sdk.Ctx, validator types.Validator) ([]byte, error) {
if k.Cdc.IsAfterNonCustodialUpgrade(ctx.BlockHeight()) {
if !k.Cdc.IsAfterRewardDelegatorUpgrade(ctx.BlockHeight()) {
validator.RewardDelegators = nil
}
bz, err := k.Cdc.MarshalBinaryLengthPrefixed(&validator, ctx.BlockHeight())
if err != nil {
ctx.Logger().Error("could not marshal validator: " + err.Error())
}
return bz, err
}
v := validator.ToLegacy()
bz, err := k.Cdc.MarshalBinaryLengthPrefixed(&v, ctx.BlockHeight())
if err != nil {
ctx.Logger().Error("could not marshal validator: " + err.Error())
}
return bz, err
}
func (k Keeper) UnmarshalValidator(ctx sdk.Ctx, valBytes []byte) (val types.Validator, err error) {
if k.Cdc.IsAfterNonCustodialUpgrade(ctx.BlockHeight()) {
err = k.Cdc.UnmarshalBinaryLengthPrefixed(valBytes, &val, ctx.BlockHeight())
if err == nil {
if !k.Cdc.IsAfterRewardDelegatorUpgrade(ctx.BlockHeight()) {
val.RewardDelegators = nil
}
} else {
ctx.Logger().Error("could not unmarshal validator: " + err.Error())
}
return val, err
}
v := types.LegacyValidator{}
err = k.Cdc.UnmarshalBinaryLengthPrefixed(valBytes, &v, ctx.BlockHeight())
if err != nil {
ctx.Logger().Error("could not unmarshal validator: " + err.Error())
}
return v.ToValidator(), err
}
// GetValidator - Retrieve validator with address from the main store
func (k Keeper) GetValidator(ctx sdk.Ctx, addr sdk.Address) (validator types.Validator, found bool) {
// NOTE: Removed validator caching due to chain halt on 74620 as an extra measure to guarantee
// no state inconsistencies. Safe to re-enable in the future.
//val, found := k.validatorCache.GetWithCtx(ctx, addr.String())
//if found {
// return val.(types.Validator), found
//}
store := ctx.KVStore(k.storeKey)
value, _ := store.Get(types.KeyForValByAllVals(addr))
if value == nil {
return validator, false
}
validator, err := k.UnmarshalValidator(ctx, value)
if err != nil {
ctx.Logger().Error("can't get validator: " + err.Error())
return validator, false
}
_ = k.validatorCache.AddWithCtx(ctx, addr.String(), validator)
return validator, true
}
// SetValidator - Store validator in the main store and state stores (stakingset/ unstakingset)
func (k Keeper) SetValidator(ctx sdk.Ctx, validator types.Validator) {
store := ctx.KVStore(k.storeKey)
bz, err := k.MarshalValidator(ctx, validator)
if err != nil {
ctx.Logger().Error("could not marshal validator: " + err.Error())
}
err = store.Set(types.KeyForValByAllVals(validator.Address), bz)
if err != nil {
ctx.Logger().Error("could not set validator: " + err.Error())
}
if validator.IsUnstaking() {
// Adds to unstaking validator queue
k.SetUnstakingValidator(ctx, validator)
}
if validator.IsStaked() {
if !validator.IsJailed() {
// save in the staked store
k.SetStakedValidator(ctx, validator)
}
}
_ = k.validatorCache.AddWithCtx(ctx, validator.Address.String(), validator)
}
func (k Keeper) SetValidators(ctx sdk.Ctx, validators types.Validators) {
for _, val := range validators {
k.SetValidator(ctx, val)
}
}
func (k Keeper) GetValidatorOutputAddress(ctx sdk.Ctx, operatorAddress sdk.Address) (sdk.Address, bool) {
val, found := k.GetValidator(ctx, operatorAddress)
return k.GetOutputAddressFromValidator(val), found
}
func (k Keeper) GetOutputAddressFromValidator(val types.Validator) sdk.Address {
if val.OutputAddress == nil {
return val.Address
}
return val.OutputAddress
}
// SetValidator - Store validator in the main store
func (k Keeper) DeleteValidator(ctx sdk.Ctx, addr sdk.Address) {
store := ctx.KVStore(k.storeKey)
_ = store.Delete(types.KeyForValByAllVals(addr))
k.DeleteValidatorSigningInfo(ctx, addr)
k.validatorCache.RemoveWithCtx(ctx, addr.String())
}
// GetAllValidators - Retrieve set of all validators with no limits from the main store
func (k Keeper) GetAllValidators(ctx sdk.Ctx) (validators []types.Validator) {
validators = make([]types.Validator, 0)
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
validator, err := k.UnmarshalValidator(ctx, iterator.Value())
if err != nil {
ctx.Logger().Error("can't get validator in GetAllValidators: " + err.Error())
continue
}
validators = append(validators, validator)
}
return validators
}
// GetAllValidators - Retrieve set of all validators with no limits from the main store
func (k Keeper) GetAllValidatorsAddrs(ctx sdk.Ctx) (validators []sdk.Address) {
validators = make([]sdk.Address, 0)
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
validators = append(validators, iterator.Key())
}
return validators
}
// GetAllValidators - - Retrieve the set of all validators with no limits from the main store
func (k Keeper) GetAllValidatorsWithOpts(ctx sdk.Ctx, opts types.QueryValidatorsParams) (validators []types.Validator) {
validators = make([]types.Validator, 0)
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
validator, err := k.UnmarshalValidator(ctx, iterator.Value())
if err != nil {
ctx.Logger().Error("could not unmarshal validator in GetAllValidatorsWithOpts: ", err.Error())
continue
}
if opts.IsValid(validator) {
validators = append(validators, validator)
}
}
return validators
}
// GetValidators - Retrieve a given amount of all the validators
func (k Keeper) GetValidators(ctx sdk.Ctx, maxRetrieve uint16) (validators []types.Validator) {
store := ctx.KVStore(k.storeKey)
validators = make([]types.Validator, maxRetrieve)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
i := 0
for ; iterator.Valid() && i < int(maxRetrieve); iterator.Next() {
validator, err := k.UnmarshalValidator(ctx, iterator.Value())
if err != nil {
ctx.Logger().Error("could not unmarshal validator in GetValidators: ", err.Error())
continue
}
validators[i] = validator
i++
}
return validators[:i] // trim if the array length < maxRetrieve
}
func (k Keeper) ClearSessionCache() {
if k.PocketKeeper != nil {
k.PocketKeeper.ClearSessionCache()
}
}
// IterateAndExecuteOverVals - Goes through the validator set and executes handler
func (k Keeper) IterateAndExecuteOverVals(
ctx sdk.Ctx, fn func(index int64, validator exported.ValidatorI) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
validator, err := k.UnmarshalValidator(ctx, iterator.Value())
if err != nil {
ctx.Logger().Error("could not unmarshal validator in IterateAndExecuteOverVals: ", err.Error())
continue
}
stop := fn(i, validator) // XXX is this safe will the validator unexposed fields be able to get written to?
if stop {
break
}
i++
}
}
// Validator - wrapper for GetValidator call
func (k Keeper) Validator(ctx sdk.Ctx, address sdk.Address) exported.ValidatorI {
val, found := k.GetValidator(ctx, address)
if !found {
return nil
}
return val
}
// AllValidators - Retrieve a list of all validators
func (k Keeper) AllValidators(ctx sdk.Ctx) (validators []exported.ValidatorI) {
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.AllValidatorsKey)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
validator, err := k.UnmarshalValidator(ctx, iterator.Value())
if err != nil {
ctx.Logger().Error("could not unmarshal validator in AllValidators: ", err.Error())
continue
}
validators = append(validators, validator)
}
return validators
}
// map of validator addresses to serialized power
type valPowerMap map[[sdk.AddrLen]byte][]byte
// getPrevStatePowerMap - Retrieve the prevState validator set
func (k Keeper) getPrevStatePowerMap(ctx sdk.Ctx) valPowerMap {
prevState := make(valPowerMap)
store := ctx.KVStore(k.storeKey)
iterator, _ := sdk.KVStorePrefixIterator(store, types.PrevStateValidatorsPowerKey)
defer iterator.Close()
// iterate over the prevState validator set index
for ; iterator.Valid(); iterator.Next() {
var valAddr [sdk.AddrLen]byte
// extract the validator address from the key (prefix is 1-byte)
copy(valAddr[:], iterator.Key()[1:])
// power bytes is just the value
powerBytes := iterator.Value()
prevState[valAddr] = make([]byte, len(powerBytes))
copy(prevState[valAddr], powerBytes)
}
return prevState
}
// sortNoLongerStakedValidators - Given a map of remaining validators to previous staked power
// returns the list of validators to be unbstaked, sorted by operator address
func sortNoLongerStakedValidators(prevState valPowerMap) [][]byte {
// sort the map keys for determinism
noLongerStaked := make([][]byte, len(prevState))
index := 0
for valAddrBytes := range prevState {
valAddr := make([]byte, sdk.AddrLen)
copy(valAddr, valAddrBytes[:])
noLongerStaked[index] = valAddr
index++
}
// sorted by address - order doesn't matter
sort.SliceStable(noLongerStaked, func(i, j int) bool {
// -1 means strictly less than
return bytes.Compare(noLongerStaked[i], noLongerStaked[j]) == -1
})
return noLongerStaked
}