/
badge_used_cu.go
95 lines (81 loc) · 3.08 KB
/
badge_used_cu.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
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
"github.com/lavanet/lava/x/pairing/types"
timertypes "github.com/lavanet/lava/x/timerstore/types"
)
// SetBadgeUsedCu set a specific badgeUsedCu in the store from its index
func (k Keeper) SetBadgeUsedCu(ctx sdk.Context, badgeUsedCu types.BadgeUsedCu) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BadgeUsedCuKeyPrefix))
b := k.cdc.MustMarshal(&badgeUsedCu)
store.Set(badgeUsedCu.BadgeUsedCuKey, b)
}
// GetBadgeUsedCu returns a badgeUsedCu from its index
func (k Keeper) GetBadgeUsedCu(
ctx sdk.Context,
badgeUsedCuKey []byte,
) (val types.BadgeUsedCu, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BadgeUsedCuKeyPrefix))
b := store.Get(badgeUsedCuKey)
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveBadgeUsedCu removes a badgeUsedCu from the store
func (k Keeper) RemoveBadgeUsedCu(
ctx sdk.Context,
badgeUsedCuKey []byte,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BadgeUsedCuKeyPrefix))
if store.Has(badgeUsedCuKey) {
store.Delete(badgeUsedCuKey)
} else {
// badge (epoch) timer has expired for an unknown badge: either the
// timer was set wrongly, or the badge was incorrectly removed; and
// we cannot even return an error about it.
utils.LavaFormatError("critical: epoch expiry for unknown badge, skipping",
fmt.Errorf("badge not found"),
utils.Attribute{Key: "badge", Value: string(badgeUsedCuKey)},
utils.Attribute{Key: "block", Value: ctx.BlockHeight()},
)
return
}
}
// GetAllBadgeUsedCu returns all badgeUsedCu
func (k Keeper) GetAllBadgeUsedCu(ctx sdk.Context) (list []types.BadgeUsedCu) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.BadgeUsedCuKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.BadgeUsedCu
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) BadgeUsedCuExpiry(ctx sdk.Context, badge types.Badge) uint64 {
blocksToSave, err := k.epochStorageKeeper.BlocksToSave(ctx, badge.Epoch)
if err != nil {
utils.LavaFormatError("critical: BadgeUsedCuExpiry failed to get BlocksToSave", err,
utils.LogAttr("badge", badge.Address),
utils.LogAttr("block", ctx.BlockHeight()),
)
// on error, blocksToSave will be zero, so to avoid immediate expiry (and user
// discontent) use a reasonable default: the current EpochToSave * EpochBlocks
blocksToSave = k.epochStorageKeeper.BlocksToSaveRaw(ctx)
}
return badge.Epoch + blocksToSave
}
// InitBadgeTimers imports badges timers data (from genesis)
func (k Keeper) InitBadgeTimers(ctx sdk.Context, gs timertypes.GenesisState) {
k.badgeTimerStore.Init(ctx, gs)
}
// ExportBadgesTimers exports badges timers data (for genesis)
func (k Keeper) ExportBadgesTimers(ctx sdk.Context) timertypes.GenesisState {
return k.badgeTimerStore.Export(ctx)
}