-
Notifications
You must be signed in to change notification settings - Fork 844
/
epoch_infos.go
82 lines (68 loc) · 2.54 KB
/
epoch_infos.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
// Copyright 2022 Evmos Foundation
// This file is part of the Evmos Network packages.
//
// Evmos is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Evmos packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/evmos/v12/x/epochs/types"
)
// GetEpochInfo returns epoch info by identifier
func (k Keeper) GetEpochInfo(ctx sdk.Context, identifier string) (types.EpochInfo, bool) {
epoch := types.EpochInfo{}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch)
bz := store.Get([]byte(identifier))
if len(bz) == 0 {
return epoch, false
}
k.cdc.MustUnmarshal(bz, &epoch)
return epoch, true
}
// SetEpochInfo set epoch info
func (k Keeper) SetEpochInfo(ctx sdk.Context, epoch types.EpochInfo) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch)
bz := k.cdc.MustMarshal(&epoch)
store.Set([]byte(epoch.Identifier), bz)
}
// DeleteEpochInfo delete epoch info
func (k Keeper) DeleteEpochInfo(ctx sdk.Context, identifier string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch)
store.Delete([]byte(identifier))
}
// IterateEpochInfo iterate through 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.KeyPrefixEpoch)
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++
}
}
// AllEpochInfos returns every epochInfo in the store
func (k Keeper) AllEpochInfos(ctx sdk.Context) []types.EpochInfo {
epochs := []types.EpochInfo{}
k.IterateEpochInfo(ctx, func(_ int64, epochInfo types.EpochInfo) (stop bool) {
epochs = append(epochs, epochInfo)
return false
})
return epochs
}