/
miss_counter.go
58 lines (46 loc) · 1.67 KB
/
miss_counter.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
gogotypes "github.com/gogo/protobuf/types"
"github.com/ojo-network/ojo/x/oracle/types"
)
// GetMissCounter retrieves the # of vote periods missed in this oracle slash
// window.
func (k Keeper) GetMissCounter(ctx sdk.Context, operator sdk.ValAddress) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetMissCounterKey(operator))
if bz == nil {
// by default the counter is zero
return 0
}
var missCounter gogotypes.UInt64Value
k.cdc.MustUnmarshal(bz, &missCounter)
return missCounter.Value
}
// SetMissCounter updates the # of vote periods missed in this oracle slash
// window.
func (k Keeper) SetMissCounter(ctx sdk.Context, operator sdk.ValAddress, missCounter uint64) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&gogotypes.UInt64Value{Value: missCounter})
store.Set(types.GetMissCounterKey(operator), bz)
}
// DeleteMissCounter removes miss counter for the validator.
func (k Keeper) DeleteMissCounter(ctx sdk.Context, operator sdk.ValAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetMissCounterKey(operator))
}
// IterateMissCounters iterates over the miss counters and performs a callback
// function.
func (k Keeper) IterateMissCounters(ctx sdk.Context, handler func(sdk.ValAddress, uint64) bool) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixMissCounter)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
operator := sdk.ValAddress(iter.Key()[2:])
var missCounter gogotypes.UInt64Value
k.cdc.MustUnmarshal(iter.Value(), &missCounter)
if handler(operator, missCounter.Value) {
break
}
}
}