/
store.go
82 lines (72 loc) · 1.99 KB
/
store.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
package keeper
import (
"encoding/json"
"fmt"
"github.com/c-osmosis/osmosis/x/incentives/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// getLastPotID returns ID used last time
func (k Keeper) getLastPotID(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyLastPotID)
if bz == nil {
return 0
}
return sdk.BigEndianToUint64(bz)
}
// setLastPotID save ID used by last pot
func (k Keeper) setLastPotID(ctx sdk.Context, ID uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.KeyLastPotID, sdk.Uint64ToBigEndian(ID))
}
// potStoreKey returns action store key from ID
func potStoreKey(ID uint64) []byte {
return combineKeys(types.KeyPrefixPeriodPot, sdk.Uint64ToBigEndian(ID))
}
// getPotRefs get pot IDs specified on the prefix and timestamp key
func (k Keeper) getPotRefs(ctx sdk.Context, key []byte) []uint64 {
store := ctx.KVStore(k.storeKey)
potIDs := []uint64{}
if store.Has(key) {
bz := store.Get(key)
err := json.Unmarshal(bz, &potIDs)
if err != nil {
panic(err)
}
}
return potIDs
}
// addPotRefByKey append pot ID into an array associated to provided key
func (k Keeper) addPotRefByKey(ctx sdk.Context, key []byte, potID uint64) error {
store := ctx.KVStore(k.storeKey)
potIDs := k.getPotRefs(ctx, key)
if findIndex(potIDs, potID) > -1 {
return fmt.Errorf("pot with same ID exist: %d", potID)
}
potIDs = append(potIDs, potID)
bz, err := json.Marshal(potIDs)
if err != nil {
return err
}
store.Set(key, bz)
return nil
}
// deletePotRefByKey removes pot ID from an array associated to provided key
func (k Keeper) deletePotRefByKey(ctx sdk.Context, key []byte, potID uint64) {
var index = -1
store := ctx.KVStore(k.storeKey)
potIDs := k.getPotRefs(ctx, key)
potIDs, index = removeValue(potIDs, potID)
if index < 0 {
panic(fmt.Sprintf("specific pot with ID %d not found", potID))
}
if len(potIDs) == 0 {
store.Delete(key)
} else {
bz, err := json.Marshal(potIDs)
if err != nil {
panic(err)
}
store.Set(key, bz)
}
}