cosmos-sdk/x/params/keeper.go

58 lines
1.1 KiB
Go
Raw Normal View History

package params
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-09-18 04:16:20 -07:00
"github.com/cosmos/cosmos-sdk/x/params/store"
)
// Keeper of the global paramstore
type Keeper struct {
cdc *codec.Codec
key sdk.StoreKey
tkey sdk.StoreKey
2018-09-18 04:16:20 -07:00
stores map[string]*Store
}
2018-09-26 08:36:26 -07:00
// NewKeeper constructs a params keeper
func NewKeeper(cdc *codec.Codec, key *sdk.KVStoreKey, tkey *sdk.TransientStoreKey) (k Keeper) {
k = Keeper{
cdc: cdc,
key: key,
tkey: tkey,
2018-09-18 04:16:20 -07:00
stores: make(map[string]*Store),
}
return k
}
// Allocate substore used for keepers
2018-10-06 08:32:41 -07:00
func (k Keeper) Substore(storename string, table Table) Store {
2018-09-18 04:16:20 -07:00
_, ok := k.stores[storename]
if ok {
2018-09-18 04:16:20 -07:00
panic("substore already occupied")
}
2018-09-18 04:16:20 -07:00
if storename == "" {
panic("cannot use empty string for substore")
}
2018-10-06 08:32:41 -07:00
store := store.NewStore(k.cdc, k.key, k.tkey, storename, table)
2018-09-18 04:16:20 -07:00
k.stores[storename] = &store
2018-09-18 04:16:20 -07:00
return store
}
2018-09-10 04:59:05 -07:00
2018-09-18 04:16:20 -07:00
// Get existing substore from keeper
func (k Keeper) GetSubstore(storename string) (Store, bool) {
store, ok := k.stores[storename]
2018-09-10 04:59:05 -07:00
if !ok {
2018-09-18 04:16:20 -07:00
return Store{}, false
2018-09-10 04:59:05 -07:00
}
2018-09-18 04:16:20 -07:00
return *store, ok
2018-09-10 04:59:05 -07:00
}