-
Notifications
You must be signed in to change notification settings - Fork 5
/
iterator.go
58 lines (50 loc) · 1.55 KB
/
iterator.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
package keeper
import (
"encoding/json"
db "github.com/cometbft/cometbft-db"
"github.com/duality-labs/duality/x/incentives/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// iterator returns an iterator over all gauges in the {prefix} space of state.
func (k Keeper) iterator(ctx sdk.Context, prefix []byte) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, prefix)
}
// iterator returns an iterator over all gauges in the {prefix} space of state.
func (k Keeper) iteratorStartEnd(ctx sdk.Context, start []byte, end []byte) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return store.Iterator(start, end)
}
func UnmarshalRefArray(bz []byte) []uint64 {
ids := []uint64{}
err := json.Unmarshal(bz, &ids)
if err != nil {
panic(err)
}
return ids
}
// getStakesFromIterator returns an array of single stake units by period defined by the x/stake module.
func (k Keeper) getStakesFromIterator(ctx sdk.Context, iterator db.Iterator) types.Stakes {
stakes := types.Stakes{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
stakeIDs := UnmarshalRefArray(iterator.Value())
for _, stakeID := range stakeIDs {
stake, err := k.GetStakeByID(ctx, stakeID)
if err != nil {
panic(err)
}
stakes = append(stakes, stake)
}
}
return stakes
}
func (k Keeper) getIDsFromIterator(iterator db.Iterator) []uint64 {
allIds := []uint64{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
ids := UnmarshalRefArray(iterator.Value())
allIds = append(allIds, ids...)
}
return allIds
}