/
epoch_info.go
84 lines (67 loc) · 2.08 KB
/
epoch_info.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mycel-domain/mycel/x/epochs/types"
)
// SetEpochInfo set a specific epochInfo in the store from its index
func (k Keeper) SetEpochInfo(ctx sdk.Context, epochInfo types.EpochInfo) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EpochInfoKeyPrefix))
b := k.cdc.MustMarshal(&epochInfo)
store.Set(types.EpochInfoKey(
epochInfo.Identifier,
), b)
}
// GetEpochInfo returns a epochInfo from its index
func (k Keeper) GetEpochInfo(
ctx sdk.Context,
identifier string,
) (val types.EpochInfo, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EpochInfoKeyPrefix))
b := store.Get(types.EpochInfoKey(
identifier,
))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveEpochInfo removes a epochInfo from the store
func (k Keeper) RemoveEpochInfo(
ctx sdk.Context,
identifier string,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EpochInfoKeyPrefix))
store.Delete(types.EpochInfoKey(
identifier,
))
}
// GetAllEpochInfo returns all epochInfo
func (k Keeper) GetAllEpochInfo(ctx sdk.Context) (list []types.EpochInfo) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EpochInfoKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.EpochInfo
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// Iterate though epochs
func (k Keeper) IterateEpochInfo(ctx sdk.Context, fn func(index int64, epochInfo types.EpochInfo) (stop bool)) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.EpochInfoKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, nil)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
epoch := types.EpochInfo{}
k.cdc.MustUnmarshal(iterator.Value(), &epoch)
stop := fn(i, epoch)
if stop {
break
}
i++
}
}