-
Notifications
You must be signed in to change notification settings - Fork 125
/
keeper.go
118 lines (97 loc) · 3.2 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/irisnet/irishub/modules/guardian/types"
)
// Keeper of the guardian store
type Keeper struct {
cdc codec.Marshaler
storeKey sdk.StoreKey
}
// NewKeeper returns a guardian keeper
func NewKeeper(cdc codec.Marshaler, key sdk.StoreKey) Keeper {
keeper := Keeper{
storeKey: key,
cdc: cdc,
}
return keeper
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("%s", types.ModuleName))
}
// Add a profiler, only a existing profiler can add a new and the profiler is not existed
func (k Keeper) AddProfiler(ctx sdk.Context, guardian types.Guardian) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&guardian)
store.Set(types.GetProfilerKey(guardian.Address), bz)
}
// DeleteProfiler delete the stored profiler
func (k Keeper) DeleteProfiler(ctx sdk.Context, address sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetProfilerKey(address))
}
// GetProfiler retrieves the profiler by specified address
func (k Keeper) GetProfiler(ctx sdk.Context, addr sdk.AccAddress) (guardian types.Guardian, found bool) {
store := ctx.KVStore(k.storeKey)
if bz := store.Get(types.GetProfilerKey(addr)); bz != nil {
k.cdc.MustUnmarshalBinaryBare(bz, &guardian)
return guardian, true
}
return guardian, false
}
// IterateProfilers iterates through all profilers
func (k Keeper) IterateProfilers(
ctx sdk.Context,
op func(profiler types.Guardian) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetProfilersSubspaceKey())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var profiler types.Guardian
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &profiler)
if stop := op(profiler); stop {
break
}
}
}
// AddTrustee add a trustee
func (k Keeper) AddTrustee(ctx sdk.Context, guardian types.Guardian) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(&guardian)
store.Set(types.GetTrusteeKey(guardian.GetAddress()), bz)
}
// DeleteTrustee delete the stored trustee
func (k Keeper) DeleteTrustee(ctx sdk.Context, address sdk.AccAddress) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetTrusteeKey(address))
}
// GetTrustee retrieves the trustee by specified address
func (k Keeper) GetTrustee(ctx sdk.Context, addr sdk.AccAddress) (guardian types.Guardian, found bool) {
store := ctx.KVStore(k.storeKey)
if bz := store.Get(types.GetTrusteeKey(addr)); bz != nil {
k.cdc.MustUnmarshalBinaryBare(bz, &guardian)
return guardian, true
}
return guardian, false
}
// IterateTrustees iterates through all trustees
func (k Keeper) IterateTrustees(
ctx sdk.Context,
op func(trustee types.Guardian) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetTrusteesSubspaceKey())
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var trustee types.Guardian
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &trustee)
if stop := op(trustee); stop {
break
}
}
}