-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathepoch.go
88 lines (70 loc) · 2.19 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
88
package keeper
import (
"fmt"
"time"
"github.com/NibiruChain/collections"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/NibiruChain/nibiru/x/epochs/types"
)
// GetEpochInfo returns epoch info by identifier.
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (epoch types.EpochInfo, err error) {
epoch, err = k.Epochs.Get(ctx, identifier)
if err != nil {
err = fmt.Errorf("epoch with identifier %s not found", identifier)
return
}
return
}
// EpochExists checks if the epoch exists
func (k Keeper) EpochExists(ctx sdk.Context, identifier string) bool {
_, err := k.Epochs.Get(ctx, identifier)
return err == nil
}
// AddEpochInfo adds a new epoch info. Will return an error if the epoch fails validation,
// or re-uses an existing identifier.
// This method also sets the start time if left unset, and sets the epoch start height.
func (k Keeper) AddEpochInfo(ctx sdk.Context, epoch types.EpochInfo) error {
if err := epoch.Validate(); err != nil {
return err
}
if k.EpochExists(ctx, epoch.Identifier) {
return fmt.Errorf("epoch with identifier %s already exists", epoch.Identifier)
}
// Initialize empty and default epoch values
if epoch.StartTime.Equal(time.Time{}) {
epoch.StartTime = ctx.BlockTime()
}
epoch.CurrentEpochStartHeight = ctx.BlockHeight()
k.Epochs.Insert(ctx, epoch.Identifier, epoch)
return nil
}
// DeleteEpochInfo delete epoch info.
func (k Keeper) DeleteEpochInfo(ctx sdk.Context, identifier string) (err error) {
err = k.Epochs.Delete(ctx, identifier)
return
}
// IterateEpochInfo iterate through epochs.
func (k Keeper) IterateEpochInfo(
ctx sdk.Context,
fn func(index int64, epochInfo types.EpochInfo) (stop bool),
) {
iterate := k.Epochs.Iterate(ctx, &collections.Range[string]{})
i := int64(0)
for ; iterate.Valid(); iterate.Next() {
epoch := iterate.Value()
stop := fn(i, epoch)
if stop {
break
}
i++
}
}
// AllEpochInfos iterate through epochs to return all epochs info.
func (k Keeper) AllEpochInfos(ctx sdk.Context) []types.EpochInfo {
var epochs []types.EpochInfo
k.IterateEpochInfo(ctx, func(index int64, epochInfo types.EpochInfo) (stop bool) {
epochs = append(epochs, epochInfo)
return false
})
return epochs
}