/
epoch.go
87 lines (73 loc) · 2.37 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package keeper
import (
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/osmosis-labs/osmosis/v9/x/epochs/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// 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
}
// NumBlocksSinceEpochStart returns the number of blocks since the epoch started.
// if the epoch started on block N, then calling this during block N (after BeforeEpochStart)
// would return 0.
// Calling it any point in block N+1 (assuming the epoch doesn't increment) would return 1.
func (k Keeper) NumBlocksSinceEpochStart(ctx sdk.Context, identifier string) (int64, error) {
epoch := k.GetEpochInfo(ctx, identifier)
if (epoch == types.EpochInfo{}) {
return 0, fmt.Errorf("epoch with identifier %s not found", identifier)
}
return ctx.BlockHeight() - epoch.CurrentEpochStartHeight, nil
}