/
keeper.go
executable file
·77 lines (64 loc) · 2.09 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
package keeper
import (
"fmt"
"github.com/tendermint/tendermint/libs/log"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/commitHub/commitBlockchain/codec"
"github.com/commitHub/commitBlockchain/modules/supply/exported"
"github.com/commitHub/commitBlockchain/modules/supply/internal/types"
)
// Keeper of the supply store
type Keeper struct {
cdc *codec.Codec
storeKey sdk.StoreKey
ak types.AccountKeeper
bk types.BankKeeper
permAddrs map[string]types.PermissionsForAddress
}
// NewKeeper creates a new Keeper instance
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ak types.AccountKeeper, bk types.BankKeeper,
codespace sdk.CodespaceType, maccPerms map[string][]string) Keeper {
// set the addresses
permAddrs := make(map[string]types.PermissionsForAddress)
for name, perms := range maccPerms {
permAddrs[name] = types.NewPermissionsForAddress(name, perms)
}
return Keeper{
cdc: cdc,
storeKey: key,
ak: ak,
bk: bk,
permAddrs: permAddrs,
}
}
// 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))
}
// GetSupply retrieves the Supply from store
func (k Keeper) GetSupply(ctx sdk.Context) (supply types.Supply) {
store := ctx.KVStore(k.storeKey)
b := store.Get(SupplyKey)
if b == nil {
panic("stored supply should not have been nil")
}
k.cdc.MustUnmarshalBinaryLengthPrefixed(b, &supply)
return
}
// SetSupply sets the Supply to store
func (k Keeper) SetSupply(ctx sdk.Context, supply types.Supply) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshalBinaryLengthPrefixed(supply)
store.Set(SupplyKey, b)
}
// ValidatePermissions validates that the module account has been granted
// permissions within its set of allowed permissions.
func (k Keeper) ValidatePermissions(macc exported.ModuleAccountI) error {
permAddr := k.permAddrs[macc.GetName()]
for _, perm := range macc.GetPermissions() {
if !permAddr.HasPermission(perm) {
return fmt.Errorf("invalid module permission %s", perm)
}
}
return nil
}