-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
81 lines (62 loc) · 2.04 KB
/
keeper.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
package keeper
import (
"encoding/json"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/corestario/modules/incubator/reseeding/internal/types"
)
const seedsKey = "seeds"
const currentSeedKey = "current_seed"
// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
storeKey sdk.StoreKey // Unexposed key to access store from sdk.Context
cdc *codec.Codec // The amino codec for binary encoding/decoding.
}
// NewKeeper creates new instances of the reseeding Keeper
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
}
}
func (k Keeper) StoreSeed(ctx sdk.Context, seeds types.Seeds) (err error) {
store := ctx.KVStore(k.storeKey)
bz, _ := json.Marshal(seeds)
store.Set([]byte(seedsKey), bz)
return nil
}
func (k Keeper) GetSeeds(ctx sdk.Context) (types.Seeds, error) {
store := ctx.KVStore(k.storeKey)
var seeds = make(types.Seeds)
seedsBytes := store.Get([]byte(seedsKey))
if seedsBytes == nil {
return seeds, nil
}
if err := json.Unmarshal(seedsBytes, &seeds); err != nil {
return nil, fmt.Errorf("failed to unmarshal seeds: %w", err)
}
return seeds, nil
}
func (k Keeper) ResetSeeds(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
bz, _ := json.Marshal(make(types.Seeds))
store.Set([]byte(seedsKey), bz)
}
func (k Keeper) SetCurrentSeed(ctx sdk.Context, seed []byte) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte(currentSeedKey), seed)
}
func (k Keeper) ResetCurrentSeed(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
store.Set([]byte(currentSeedKey), []byte{})
}
func (k Keeper) GetCurrentSeed(ctx sdk.Context) []byte {
store := ctx.KVStore(k.storeKey)
return store.Get([]byte(currentSeedKey))
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}