cosmos-sdk/x/params/store/store.go

158 lines
3.7 KiB
Go
Raw Normal View History

2018-09-18 04:16:20 -07:00
package store
import (
"fmt"
"reflect"
tmlibs "github.com/tendermint/tendermint/libs/common"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Individual parameter store for each keeper
2018-09-18 04:16:20 -07:00
type Store struct {
cdc *codec.Codec
key sdk.StoreKey
tkey sdk.StoreKey
2018-09-18 04:16:20 -07:00
store []byte
}
2018-09-18 04:16:20 -07:00
// NewStore constructs a store with namestore
func NewStore(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, store string) Store {
if !tmlibs.IsASCIIText(store) {
panic("paramstore store expressions can only contain alphanumeric characters")
}
2018-09-18 04:16:20 -07:00
return Store{
cdc: cdc,
key: key,
tkey: tkey,
2018-09-18 04:16:20 -07:00
store: append([]byte(store), '/'),
}
}
// Get parameter from store
2018-09-18 04:16:20 -07:00
func (s Store) Get(ctx sdk.Context, key string, ptr interface{}) {
store := ctx.KVStore(s.key).Prefix(s.store)
2018-09-17 08:28:13 -07:00
bz := store.Get([]byte(key))
err := s.cdc.UnmarshalJSON(bz, ptr)
if err != nil {
panic(err)
}
}
// GetIfExists do not modify ptr if the stored parameter is nil
2018-09-18 04:16:20 -07:00
func (s Store) GetIfExists(ctx sdk.Context, key string, ptr interface{}) {
store := ctx.KVStore(s.key).Prefix(s.store)
2018-09-17 08:28:13 -07:00
bz := store.Get([]byte(key))
if bz == nil {
return
}
err := s.cdc.UnmarshalJSON(bz, ptr)
if err != nil {
panic(err)
}
}
// Get raw bytes of parameter from store
2018-09-18 04:16:20 -07:00
func (s Store) GetRaw(ctx sdk.Context, key string) []byte {
store := ctx.KVStore(s.key).Prefix(s.store)
2018-09-17 08:28:13 -07:00
res := store.Get([]byte(key))
return res
}
// Check if the parameter is set in the store
2018-09-18 04:16:20 -07:00
func (s Store) Has(ctx sdk.Context, key string) bool {
store := ctx.KVStore(s.key).Prefix(s.store)
2018-09-17 08:28:13 -07:00
return store.Has([]byte(key))
}
// Returns true if the parameter is set in the block
2018-09-18 04:16:20 -07:00
func (s Store) Modified(ctx sdk.Context, key string) bool {
tstore := ctx.KVStore(s.tkey).Prefix(s.store)
2018-09-17 08:28:13 -07:00
return tstore.Has([]byte(key))
}
// Set parameter, return error if stored parameter has different type from input
2018-09-18 04:16:20 -07:00
func (s Store) Set(ctx sdk.Context, key string, param interface{}) {
store := ctx.KVStore(s.key).Prefix(s.store)
2018-09-17 08:28:13 -07:00
keybz := []byte(key)
bz := store.Get(keybz)
if bz != nil {
ptrty := reflect.PtrTo(reflect.TypeOf(param))
ptr := reflect.New(ptrty).Interface()
if s.cdc.UnmarshalJSON(bz, ptr) != nil {
2018-09-10 05:10:06 -07:00
panic(fmt.Errorf("Type mismatch with stored param and provided param"))
}
}
bz, err := s.cdc.MarshalJSON(param)
if err != nil {
2018-09-10 05:10:06 -07:00
panic(err)
}
store.Set(keybz, bz)
2018-09-18 04:16:20 -07:00
tstore := ctx.KVStore(s.tkey).Prefix(s.store)
tstore.Set(keybz, []byte{})
}
// Set raw bytes of parameter
2018-09-18 04:16:20 -07:00
func (s Store) SetRaw(ctx sdk.Context, key string, param []byte) {
2018-09-17 08:28:13 -07:00
keybz := []byte(key)
2018-09-18 04:16:20 -07:00
store := ctx.KVStore(s.key).Prefix(s.store)
store.Set(keybz, param)
2018-09-18 04:16:20 -07:00
tstore := ctx.KVStore(s.tkey).Prefix(s.store)
tstore.Set(keybz, []byte{})
}
2018-09-18 09:27:54 -07:00
// Get to ParamStruct
func (s Store) GetStruct(ctx sdk.Context, ps ParamStruct) {
for _, pair := range ps.KeyFieldPairs() {
s.Get(ctx, pair.Key, pair.Field)
}
}
2018-09-17 08:28:13 -07:00
// Set from ParamStruct
2018-09-18 09:27:54 -07:00
func (s Store) SetStruct(ctx sdk.Context, ps ParamStruct) {
2018-09-17 08:28:13 -07:00
for _, pair := range ps.KeyFieldPairs() {
s.Set(ctx, pair.Key, pair.Field)
}
}
2018-09-18 04:16:20 -07:00
// Returns a KVStore identical with the paramstore
func (s Store) KVStore(ctx sdk.Context) sdk.KVStore {
return ctx.KVStore(s.key).Prefix(s.store)
}
2018-09-18 04:16:20 -07:00
// Wrapper of Store, provides immutable functions only
type ReadOnlyStore struct {
s Store
}
// Exposes Get
2018-09-18 04:16:20 -07:00
func (ros ReadOnlyStore) Get(ctx sdk.Context, key string, ptr interface{}) {
ros.s.Get(ctx, key, ptr)
}
// Exposes GetRaw
2018-09-18 04:16:20 -07:00
func (ros ReadOnlyStore) GetRaw(ctx sdk.Context, key string) []byte {
return ros.s.GetRaw(ctx, key)
}
// Exposes Has
2018-09-18 04:16:20 -07:00
func (ros ReadOnlyStore) Has(ctx sdk.Context, key string) bool {
return ros.s.Has(ctx, key)
}
// Exposes Modified
2018-09-18 04:16:20 -07:00
func (ros ReadOnlyStore) Modified(ctx sdk.Context, key string) bool {
return ros.s.Modified(ctx, key)
}