forked from osmosis-labs/osmosis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoch.go
71 lines (61 loc) · 1.77 KB
/
epoch.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
package keeper
import (
"github.com/MonOsmosis/osmosis/v3/x/epochs/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
)
// GetEpochInfo returns epoch info by identifier
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) types.EpochInfo {
epoch := types.EpochInfo{}
store := ctx.KVStore(k.storeKey)
b := store.Get(append(types.KeyPrefixEpoch, []byte(identifier)...))
if b == nil {
return epoch
}
err := proto.Unmarshal(b, &epoch)
if err != nil {
panic(err)
}
return epoch
}
// SetEpochInfo set epoch info
func (k Keeper) SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo) {
store := ctx.KVStore(k.storeKey)
value, err := proto.Marshal(&epoch)
if err != nil {
panic(err)
}
store.Set(append(types.KeyPrefixEpoch, []byte(epoch.Identifier)...), value)
}
// DeleteEpochInfo delete epoch info
func (k Keeper) DeleteEpochInfo(ctx sdk.Context, identifier string) {
store := ctx.KVStore(k.storeKey)
store.Delete(append(types.KeyPrefixEpoch, []byte(identifier)...))
}
// IterateEpochInfo iterate through epochs
func (k Keeper) IterateEpochInfo(ctx sdk.Context, fn func(index int64, epochInfo types.EpochInfo) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixEpoch)
defer iterator.Close()
i := int64(0)
for ; iterator.Valid(); iterator.Next() {
epoch := types.EpochInfo{}
err := proto.Unmarshal(iterator.Value(), &epoch)
if err != nil {
panic(err)
}
stop := fn(i, epoch)
if stop {
break
}
i++
}
}
func (k Keeper) AllEpochInfos(ctx sdk.Context) []types.EpochInfo {
epochs := []types.EpochInfo{}
k.IterateEpochInfo(ctx, func(index int64, epochInfo types.EpochInfo) (stop bool) {
epochs = append(epochs, epochInfo)
return false
})
return epochs
}