github.com/cosmos/cosmos-sdk@v0.50.10/x/params/keeper/keeper.go (about) 1 package keeper 2 3 import ( 4 "cosmossdk.io/log" 5 storetypes "cosmossdk.io/store/types" 6 7 "github.com/cosmos/cosmos-sdk/codec" 8 sdk "github.com/cosmos/cosmos-sdk/types" 9 "github.com/cosmos/cosmos-sdk/x/params/types" 10 "github.com/cosmos/cosmos-sdk/x/params/types/proposal" 11 ) 12 13 // Keeper of the global paramstore 14 type Keeper struct { 15 cdc codec.BinaryCodec 16 legacyAmino *codec.LegacyAmino 17 key storetypes.StoreKey 18 tkey storetypes.StoreKey 19 spaces map[string]*types.Subspace 20 } 21 22 // NewKeeper constructs a params keeper 23 func NewKeeper(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key, tkey storetypes.StoreKey) Keeper { 24 return Keeper{ 25 cdc: cdc, 26 legacyAmino: legacyAmino, 27 key: key, 28 tkey: tkey, 29 spaces: make(map[string]*types.Subspace), 30 } 31 } 32 33 // Logger returns a module-specific logger. 34 func (k Keeper) Logger(ctx sdk.Context) log.Logger { 35 return ctx.Logger().With("module", "x/"+proposal.ModuleName) 36 } 37 38 // Allocate subspace used for keepers 39 func (k Keeper) Subspace(s string) types.Subspace { 40 _, ok := k.spaces[s] 41 if ok { 42 panic("subspace already occupied") 43 } 44 45 if s == "" { 46 panic("cannot use empty string for subspace") 47 } 48 49 space := types.NewSubspace(k.cdc, k.legacyAmino, k.key, k.tkey, s) 50 k.spaces[s] = &space 51 52 return space 53 } 54 55 // Get existing substore from keeper 56 func (k Keeper) GetSubspace(s string) (types.Subspace, bool) { 57 space, ok := k.spaces[s] 58 if !ok { 59 return types.Subspace{}, false 60 } 61 return *space, ok 62 } 63 64 // GetSubspaces returns all the registered subspaces. 65 func (k Keeper) GetSubspaces() []types.Subspace { 66 spaces := make([]types.Subspace, len(k.spaces)) 67 i := 0 68 for _, ss := range k.spaces { 69 spaces[i] = *ss 70 i++ 71 } 72 73 return spaces 74 }