/
keeper.go
608 lines (510 loc) · 21.2 KB
/
keeper.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package slashing
import (
"fmt"
"strconv"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
gogotypes "github.com/gogo/protobuf/types"
"github.com/maticnetwork/heimdall/chainmanager"
"github.com/maticnetwork/heimdall/params/subspace"
"github.com/maticnetwork/heimdall/slashing/types"
"github.com/maticnetwork/heimdall/staking"
hmTypes "github.com/maticnetwork/heimdall/types"
)
// Keeper of the slashing store
type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
sk staking.Keeper
// codespace
codespace sdk.CodespaceType
paramSpace subspace.Subspace
// chain manager keeper
chainKeeper chainmanager.Keeper
}
// NewKeeper creates a slashing keeper
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, sk staking.Keeper, paramSpace subspace.Subspace, codespace sdk.CodespaceType, chainKeeper chainmanager.Keeper) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
sk: sk,
paramSpace: paramSpace.WithKeyTable(types.ParamKeyTable()),
codespace: codespace,
chainKeeper: chainKeeper,
}
}
// Codespace returns the codespace
func (k Keeper) Codespace() sdk.CodespaceType {
return k.codespace
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetValidatorSigningInfo retruns the ValidatorSigningInfo for a specific validator
// ConsAddress
func (k *Keeper) GetValidatorSigningInfo(ctx sdk.Context, valID hmTypes.ValidatorID) (info hmTypes.ValidatorSigningInfo, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetValidatorSigningInfoKey(valID.Bytes()))
if bz == nil {
found = false
return
}
k.cdc.MustUnmarshalBinaryBare(bz, &info)
found = true
return
}
// HasValidatorSigningInfo returns if a given validator has signing information
// persited.
func (k *Keeper) HasValidatorSigningInfo(ctx sdk.Context, valID hmTypes.ValidatorID) bool {
_, ok := k.GetValidatorSigningInfo(ctx, valID)
return ok
}
// SetValidatorSigningInfo sets the validator signing info to a consensus address key
func (k *Keeper) SetValidatorSigningInfo(ctx sdk.Context, valID hmTypes.ValidatorID, info hmTypes.ValidatorSigningInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&info)
store.Set(types.GetValidatorSigningInfoKey(valID.Bytes()), bz)
}
// IterateValidatorSigningInfos iterates over the stored ValidatorSigningInfo
func (k *Keeper) IterateValidatorSigningInfos(ctx sdk.Context,
handler func(valID hmTypes.ValidatorID, info hmTypes.ValidatorSigningInfo) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.ValidatorSigningInfoKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var info hmTypes.ValidatorSigningInfo
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &info)
if handler(info.ValID, info) {
break
}
}
}
// signing info bit array
// GetValidatorMissedBlockBitArray gets the bit for the missed blocks array
func (k *Keeper) GetValidatorMissedBlockBitArray(ctx sdk.Context, valID hmTypes.ValidatorID, index int64) bool {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetValidatorMissedBlockBitArrayKey(valID.Bytes(), index))
var missed gogotypes.BoolValue
if bz == nil {
// lazy: treat empty key as not missed
return false
}
k.cdc.MustUnmarshalBinaryBare(bz, &missed)
return missed.Value
}
// IterateValidatorMissedBlockBitArray iterates over the signed blocks window
// and performs a callback function
func (k *Keeper) IterateValidatorMissedBlockBitArray(ctx sdk.Context,
valID hmTypes.ValidatorID, handler func(index int64, missed bool) (stop bool)) {
store := ctx.KVStore(k.storeKey)
index := int64(0)
params := k.GetParams(ctx)
// Array may be sparse
for ; index < params.SignedBlocksWindow; index++ {
var missed gogotypes.BoolValue
bz := store.Get(types.GetValidatorMissedBlockBitArrayKey(valID.Bytes(), index))
if bz == nil {
continue
}
k.cdc.MustUnmarshalBinaryBare(bz, &missed)
if handler(index, missed.Value) {
break
}
}
}
// SetValidatorMissedBlockBitArray sets the bit that checks if the validator has
// missed a block in the current window
func (k *Keeper) SetValidatorMissedBlockBitArray(ctx sdk.Context, valID hmTypes.ValidatorID, index int64, missed bool) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&gogotypes.BoolValue{Value: missed})
store.Set(types.GetValidatorMissedBlockBitArrayKey(valID.Bytes(), index), bz)
}
// clearValidatorMissedBlockBitArray deletes every instance of ValidatorMissedBlockBitArray in the store
func (k *Keeper) clearValidatorMissedBlockBitArray(ctx sdk.Context, valID hmTypes.ValidatorID) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.GetValidatorMissedBlockBitArrayPrefixKey(valID.Bytes()))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
}
// MinSignedPerWindow - minimum blocks signed per window
func (k *Keeper) MinSignedPerWindow(ctx sdk.Context) int64 {
var minSignedPerWindow sdk.Dec
params := k.GetParams(ctx)
// minSignedPerWindow = percent
minSignedPerWindow = params.MinSignedPerWindow
signedBlocksWindow := params.SignedBlocksWindow
// NOTE: RoundInt64 will never panic as minSignedPerWindow is
// less than 1.
return minSignedPerWindow.MulInt64(signedBlocksWindow).RoundInt64()
}
// -----------------------------------------------------------------------------
// Params
// SetParams sets the slashing module's parameters.
func (k *Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, ¶ms)
}
// GetParams gets the slashing module's parameters.
func (k *Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, ¶ms)
return
}
//
// Tick count
//
// GetTickCount returns current Tick count
func (k Keeper) GetTickCount(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
// check if tick count is there
if store.Has(types.TickCountKey) {
// get current tick count
tickCount, err := strconv.ParseUint(string(store.Get(types.TickCountKey)), 10, 64)
if err != nil {
k.Logger(ctx).Error("Unable to convert key to int")
} else {
return tickCount
}
}
return 0
}
// UpdateTickCountWithValue updates TickCount with value
func (k Keeper) UpdateTickCountWithValue(ctx sdk.Context, value uint64) {
store := ctx.KVStore(k.storeKey)
// convert
tickCount := []byte(strconv.FormatUint(value, 10))
// update
store.Set(types.TickCountKey, tickCount)
}
// IncrementTickCount updates Tick count by 1
func (k Keeper) IncrementTickCount(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
// get current tick Count
tickCount := k.GetTickCount(ctx)
// increment by 1
tickCounts := []byte(strconv.FormatUint(tickCount+1, 10))
// update
store.Set(types.TickCountKey, tickCounts)
}
// Slashing Info api's
// SlashInterim - Add slash amounts to a buffer and emit <slash-limit> event if exceeded
func (k *Keeper) SlashInterim(ctx sdk.Context, valID hmTypes.ValidatorID, slashPercent sdk.Dec) uint64 {
if slashPercent.IsNegative() {
panic(fmt.Errorf("attempted to slash with a negative slash factor: %v", slashPercent))
}
validator, found := k.sk.GetValidatorFromValID(ctx, valID)
if !found {
k.Logger(ctx).Error("Interim slashing the validator. Validator not found", "valID", valID)
return uint64(0)
}
valPower := validator.VotingPower
slashAmountDec := sdk.NewDec(valPower).Mul(slashPercent)
slashAmountInt := slashAmountDec.TruncateInt().Int64()
k.Logger(ctx).Info("Interim slashing the validator", "valID", valID, "valPower", valPower, "slashPercent", slashPercent, "slashAmountDec", slashAmountDec, "slashAmountInt", slashAmountInt)
// Add slash to buffer
valSlashingInfo, found := k.GetBufferValSlashingInfo(ctx, valID)
if found {
// Add or Update Slash Amount
prevAmount := valSlashingInfo.SlashedAmount
updatedSlashAmount := prevAmount + uint64(slashAmountInt)
valSlashingInfo.SlashedAmount = updatedSlashAmount
} else {
// create slashing info
valSlashingInfo = hmTypes.NewValidatorSlashingInfo(valID, uint64(slashAmountInt), false)
}
// Check if jailLimit is exceeded and update the jail status.
if k.IsJailLimitExceeded(ctx, valSlashingInfo) {
valSlashingInfo.IsJailed = true
}
k.Logger(ctx).Debug("After interim slashing the validator status", "valID", valID, "updatedSlashAmount", valSlashingInfo.SlashedAmount, "jailStatus", valSlashingInfo.IsJailed)
// Update buffer with val slashing info
k.SetBufferValSlashingInfo(ctx, valID, valSlashingInfo)
// Update total slashed amount
k.UpdateTotalSlashedAmount(ctx, uint64(slashAmountInt))
totalSlashedAmount := k.GetTotalSlashedAmount(ctx)
// Check if slash limit is exceeded and emit `slash-limit` event
if k.IsSlashedLimitExceeded(ctx) {
k.Logger(ctx).Info("TotalSlashedAmount exceeded SlashLimit, Emitting event", types.EventTypeSlashLimit)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSlashLimit,
sdk.NewAttribute(types.AttributeKeySlashedAmount, fmt.Sprintf("%d", totalSlashedAmount)),
),
)
k.Logger(ctx).Info("Emitted SlashLimit event", "slashedAmountAttr", totalSlashedAmount)
}
return uint64(slashAmountInt)
}
func (k *Keeper) GetTotalSlashedAmount(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
if store.Has(types.TotalSlashedAmountKey) {
// get current Total slashed amount
totalSlashedAmountKey, err := strconv.ParseUint(string(store.Get(types.TotalSlashedAmountKey)), 10, 64)
if err != nil {
k.Logger(ctx).Error("Unable to convert key to int")
} else {
return totalSlashedAmountKey
}
}
return 0
}
// IsSlashedLimitExceeded - if total slashed amount exceeded slash limit or not
func (k *Keeper) IsSlashedLimitExceeded(ctx sdk.Context) bool {
params := k.GetParams(ctx)
slashedAmount := k.GetTotalSlashedAmount(ctx)
totalPower := k.sk.GetTotalPower(ctx)
slashLimitDec := sdk.NewDec(totalPower).Mul(params.SlashFractionLimit)
slashLimit := slashLimitDec.TruncateInt().Int64()
k.Logger(ctx).Info("checking if slash-limit exceeded", "totalPower", totalPower, "totalSlashedAmount", slashedAmount, "slashlimit", slashLimit)
if slashedAmount >= uint64(slashLimit) {
k.Logger(ctx).Debug("slash-limit exceeded", "totalPower", totalPower, "totalSlashedAmount", slashedAmount, "slashlimit", slashLimit)
return true
}
k.Logger(ctx).Debug("slash-limit not exceeded", "totalPower", totalPower, "totalSlashedAmount", slashedAmount, "slashlimit", slashLimit)
return false
}
// IsJailLimitExceeded - if jail limit is exceeded or not
func (k *Keeper) IsJailLimitExceeded(ctx sdk.Context, valSlashingInfo hmTypes.ValidatorSlashingInfo) bool {
params := k.GetParams(ctx)
valID := valSlashingInfo.ID
slashedAmount := valSlashingInfo.SlashedAmount
val, _ := k.sk.GetValidatorFromValID(ctx, valID)
jailLimitDec := sdk.NewDec(val.VotingPower).Mul(params.JailFractionLimit)
jailLimit := jailLimitDec.TruncateInt().Int64()
k.Logger(ctx).Info("Checking if jail limit is exceeded", "valId", valID, "power", val.VotingPower, "slashedAmount", slashedAmount, "jailLimit", jailLimit, "jailLimitDec", jailLimitDec)
if slashedAmount >= uint64(jailLimit) {
k.Logger(ctx).Debug("Jail limit exceeded", "valId", valID, "power", val.VotingPower, "slashedAmount", slashedAmount, "jailLimit", jailLimit, "jailLimitDec", jailLimitDec)
return true
}
k.Logger(ctx).Debug("Jail limit not exceeded", "valId", valID, "power", val.VotingPower, "slashedAmount", slashedAmount, "jailLimit", jailLimit, "jailLimitDec", jailLimitDec)
return false
}
// GetBufferValSlashingInfo gets the validator slashing info for a validator ID key
func (k *Keeper) GetBufferValSlashingInfo(ctx sdk.Context, valId hmTypes.ValidatorID) (info hmTypes.ValidatorSlashingInfo, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetBufferValSlashingInfoKey(valId.Bytes()))
if bz == nil {
found = false
return
}
k.cdc.MustUnmarshalBinaryBare(bz, &info)
found = true
return
}
// SetBufferValSlashingInfo sets the validator slashing info to a validator ID key
func (k *Keeper) SetBufferValSlashingInfo(ctx sdk.Context, valID hmTypes.ValidatorID, info hmTypes.ValidatorSlashingInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&info)
store.Set(types.GetBufferValSlashingInfoKey(valID.Bytes()), bz)
}
// RemoveBufferValSlashingInfo removes the validator slashing info for a validator ID key
func (k *Keeper) RemoveBufferValSlashingInfo(ctx sdk.Context, valID hmTypes.ValidatorID) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetBufferValSlashingInfoKey(valID.Bytes()))
}
// IterateBufferValSlashingInfos iterates over the stored ValidatorSlashingInfo
func (k *Keeper) IterateBufferValSlashingInfos(ctx sdk.Context,
handler func(slashingInfo hmTypes.ValidatorSlashingInfo) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.BufferValSlashingInfoKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var slashingInfo hmTypes.ValidatorSlashingInfo
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &slashingInfo)
if handler(slashingInfo) {
break
}
}
}
// FlushBufferValSlashingInfos removes all validator slashing infos in buffer
func (k *Keeper) FlushBufferValSlashingInfos(ctx sdk.Context) error {
// iterate through validator slashing info and create validator slashing info update array
err := k.IterateBufferValSlashingInfosAndApplyFn(ctx, func(valSlashingInfo hmTypes.ValidatorSlashingInfo) error {
// remove from buffer data
k.RemoveBufferValSlashingInfo(ctx, valSlashingInfo.ID)
return nil
})
return err
}
// FlushBufferValSlashingInfos removes all validator slashing infos in buffer
func (k *Keeper) FlushTotalSlashedAmount(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
// remove from store
if store.Has(types.TotalSlashedAmountKey) {
store.Delete(types.TotalSlashedAmountKey)
}
}
// IterateBufferValSlashingInfosAndApplyFn interate ValidatorSlashingInfo and apply the given function.
func (k *Keeper) IterateBufferValSlashingInfosAndApplyFn(ctx sdk.Context, f func(slashingInfo hmTypes.ValidatorSlashingInfo) error) error {
store := ctx.KVStore(k.storeKey)
// get validator iterator
iterator := sdk.KVStorePrefixIterator(store, types.BufferValSlashingInfoKey)
defer iterator.Close()
// loop through validators to get valid validators
for ; iterator.Valid(); iterator.Next() {
// unmarshall validator
slashingInfo, err := hmTypes.UnmarshallValSlashingInfo(k.cdc, iterator.Value())
if err != nil {
k.Logger(ctx).Error("Error unmarshalling val slashing info")
return err
}
// call function and return if required
if err := f(slashingInfo); err != nil {
return err
}
}
return nil
}
// GetBufferValSlashingInfos returns all validator slashing infos in buffer
func (k *Keeper) GetBufferValSlashingInfos(ctx sdk.Context) (valSlashingInfos []*hmTypes.ValidatorSlashingInfo, err error) {
// iterate through validators and create validator update array
err = k.IterateBufferValSlashingInfosAndApplyFn(ctx, func(valSlashingInfo hmTypes.ValidatorSlashingInfo) error {
// append to list of valSlashingInfos
valSlashingInfos = append(valSlashingInfos, &valSlashingInfo)
return nil
})
return
}
func (k *Keeper) UpdateTotalSlashedAmount(ctx sdk.Context, slashedAmount uint64) {
store := ctx.KVStore(k.storeKey)
current := k.GetTotalSlashedAmount(ctx)
updated := current + slashedAmount
// convert
totalSlashedAmount := []byte(strconv.FormatUint(updated, 10))
store.Set(types.TotalSlashedAmountKey, totalSlashedAmount)
k.Logger(ctx).Debug("Updated Total Slashed Amount ", "oldAmount", current, "newAmount", updated)
}
// GetTickValSlashingInfo gets the validator slashing info for a validator ID key
func (k *Keeper) GetTickValSlashingInfo(ctx sdk.Context, valId hmTypes.ValidatorID) (info hmTypes.ValidatorSlashingInfo, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTickValSlashingInfoKey(valId.Bytes()))
if bz == nil {
found = false
return
}
k.cdc.MustUnmarshalBinaryBare(bz, &info)
found = true
return
}
// GetTickValSlashingInfos returns all validator slashing infos in tick
func (k *Keeper) GetTickValSlashingInfos(ctx sdk.Context) (valSlashingInfos []*hmTypes.ValidatorSlashingInfo, err error) {
// iterate through validators and create slashing info update array
err = k.IterateTickValSlashingInfosAndApplyFn(ctx, func(valSlashingInfo hmTypes.ValidatorSlashingInfo) error {
// append to list of valSlashingInfos
valSlashingInfos = append(valSlashingInfos, &valSlashingInfo)
return nil
})
return
}
// SetTickValSlashingInfo sets the validator slashing info to a validator ID key
func (k *Keeper) SetTickValSlashingInfo(ctx sdk.Context, valID hmTypes.ValidatorID, info hmTypes.ValidatorSlashingInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&info)
store.Set(types.GetTickValSlashingInfoKey(valID.Bytes()), bz)
}
// RemoveTickValSlashingInfo removes the validator slashing info for a validator ID key
func (k *Keeper) RemoveTickValSlashingInfo(ctx sdk.Context, valID hmTypes.ValidatorID) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetTickValSlashingInfoKey(valID.Bytes()))
}
// IterateTickValSlashingInfos iterates over the stored ValidatorSlashingInfo
func (k *Keeper) IterateTickValSlashingInfos(ctx sdk.Context,
handler func(slashingInfo hmTypes.ValidatorSlashingInfo) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TickValSlashingInfoKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var slashingInfo hmTypes.ValidatorSlashingInfo
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &slashingInfo)
if handler(slashingInfo) {
break
}
}
}
// CopyValSlashingInfosToTickData copies all validator slashing infos in buffer to tickdata
func (k *Keeper) CopyBufferValSlashingInfosToTickData(ctx sdk.Context) error {
// iterate through validators and create validator slashing info update array
err := k.IterateBufferValSlashingInfosAndApplyFn(ctx, func(valSlashingInfo hmTypes.ValidatorSlashingInfo) error {
// store to tick data
k.SetTickValSlashingInfo(ctx, valSlashingInfo.ID, valSlashingInfo)
return nil
})
return err
}
// IterateTickValSlashingInfosAndApplyFn interate ValidatorSlashingInfo and apply the given function.
func (k *Keeper) IterateTickValSlashingInfosAndApplyFn(ctx sdk.Context, f func(slashingInfo hmTypes.ValidatorSlashingInfo) error) error {
store := ctx.KVStore(k.storeKey)
// get validator iterator
iterator := sdk.KVStorePrefixIterator(store, types.TickValSlashingInfoKey)
defer iterator.Close()
// loop through validators to get valid validators
for ; iterator.Valid(); iterator.Next() {
// unmarshall validator
slashingInfo, _ := hmTypes.UnmarshallValSlashingInfo(k.cdc, iterator.Value())
k.Logger(ctx).Debug("slashing the validator", "slashingInfo", slashingInfo)
// call function and return if required
if err := f(slashingInfo); err != nil {
// Error slashing validator
k.Logger(ctx).Error("Error slashing the validator", "error", err)
return err
}
}
return nil
}
// SlashAndJailTickValSlashingInfos reduces power of all validator slashing infos in tick data
func (k *Keeper) SlashAndJailTickValSlashingInfos(ctx sdk.Context) error {
// iterate through validator slashing info and create validator slashing info update array
err := k.IterateTickValSlashingInfosAndApplyFn(ctx, func(valSlashingInfo hmTypes.ValidatorSlashingInfo) error {
err := k.sk.Slash(ctx, valSlashingInfo)
return err
})
return err
}
// FlushTickValSlashingInfos removes all validator slashing infos in last Tick
func (k *Keeper) FlushTickValSlashingInfos(ctx sdk.Context) error {
// iterate through validator slashing info and create validator slashing info update array
err := k.IterateTickValSlashingInfosAndApplyFn(ctx, func(valSlashingInfo hmTypes.ValidatorSlashingInfo) error {
// remove from tick data
k.RemoveTickValSlashingInfo(ctx, valSlashingInfo.ID)
return nil
})
return err
}
//
// Slashing sequence
//
// SetSlashingSequence sets Slashing sequence
func (k *Keeper) SetSlashingSequence(ctx sdk.Context, sequence string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetSlashingSequenceKey(sequence), types.DefaultValue)
}
// HasSlashingSequence checks if Slashing sequence already exists
func (k *Keeper) HasSlashingSequence(ctx sdk.Context, sequence string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetSlashingSequenceKey(sequence))
}
// GetSlashingSequences checks if Slashing already exists
func (k *Keeper) GetSlashingSequences(ctx sdk.Context) (sequences []string) {
k.IterateSlashingSequencesAndApplyFn(ctx, func(sequence string) error {
sequences = append(sequences, sequence)
return nil
})
return
}
// IterateSlashingSequencesAndApplyFn interate validators and apply the given function.
func (k *Keeper) IterateSlashingSequencesAndApplyFn(ctx sdk.Context, f func(sequence string) error) {
store := ctx.KVStore(k.storeKey)
// get sequence iterator
iterator := sdk.KVStorePrefixIterator(store, types.SlashingSequenceKey)
defer iterator.Close()
// loop through validators to get valid validators
for ; iterator.Valid(); iterator.Next() {
sequence := string(iterator.Key()[len(types.SlashingSequenceKey):])
// call function and return if required
if err := f(sequence); err != nil {
return
}
}
}