-
Notifications
You must be signed in to change notification settings - Fork 127
/
keeper.go
83 lines (71 loc) · 2.24 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
package guardian
import (
"github.com/irisnet/irishub/codec"
sdk "github.com/irisnet/irishub/types"
)
type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
// codespace
codespace sdk.CodespaceType
}
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
storeKey: key,
cdc: cdc,
codespace: codespace,
}
return keeper
}
// 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 Guardian) sdk.Error {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(guardian)
store.Set(GetProfilerKey(guardian.Address), bz)
return nil
}
func (k Keeper) DeleteProfiler(ctx sdk.Context, address sdk.AccAddress) sdk.Error {
store := ctx.KVStore(k.storeKey)
store.Delete(GetProfilerKey(address))
return nil
}
func (k Keeper) GetProfiler(ctx sdk.Context, addr sdk.AccAddress) (guardian Guardian, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(GetProfilerKey(addr))
if bz != nil {
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &guardian)
return guardian, true
}
return guardian, false
}
// Gets all profilers
func (k Keeper) ProfilersIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, GetProfilersSubspaceKey())
}
// Add a trustee
func (k Keeper) AddTrustee(ctx sdk.Context, guardian Guardian) sdk.Error {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(guardian)
store.Set(GetTrusteeKey(guardian.Address), bz)
return nil
}
func (k Keeper) DeleteTrustee(ctx sdk.Context, address sdk.AccAddress) sdk.Error {
store := ctx.KVStore(k.storeKey)
store.Delete(GetTrusteeKey(address))
return nil
}
func (k Keeper) GetTrustee(ctx sdk.Context, addr sdk.AccAddress) (guardian Guardian, found bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(GetTrusteeKey(addr))
if bz != nil {
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &guardian)
return guardian, true
}
return guardian, false
}
// Gets all trustees
func (k Keeper) TrusteesIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, GetTrusteesSubspaceKey())
}