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

198 lines
4.9 KiB
Go
Raw Normal View History

package subspace
import (
"reflect"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Individual parameter store for each keeper
2018-09-27 11:12:52 -07:00
// Transient store persists for a block, so we use it for
// recording whether the parameter has been changed or not
type Subspace struct {
cdc *codec.Codec
2018-09-27 11:12:52 -07:00
key sdk.StoreKey // []byte -> []byte, stores parameter
tkey sdk.StoreKey // []byte -> bool, stores parameter change
name []byte
2018-10-06 08:32:41 -07:00
table TypeTable
}
// NewSubspace constructs a store with namestore
func NewSubspace(cdc *codec.Codec, key sdk.StoreKey, tkey sdk.StoreKey, name string) (res Subspace) {
res = Subspace{
cdc: cdc,
key: key,
tkey: tkey,
name: []byte(name),
table: TypeTable{
m: make(map[string]reflect.Type),
},
}
return
}
2018-10-06 08:32:41 -07:00
// WithTypeTable initializes TypeTable and returns modified Subspace
func (s Subspace) WithTypeTable(table TypeTable) Subspace {
if table.m == nil {
panic("SetTypeTable() called with nil TypeTable")
}
if len(s.table.m) != 0 {
panic("SetTypeTable() called on already initialized Subspace")
}
for k, v := range table.m {
s.table.m[k] = v
}
2018-10-06 06:50:58 -07:00
// Allocate additional capicity for Subspace.name
// So we don't have to allocate extra space each time appending to the key
name := s.name
s.name = make([]byte, len(name), len(name)+table.maxKeyLength())
copy(s.name, name)
return s
}
2018-10-06 06:50:58 -07:00
// Returns a KVStore identical with ctx.KVStore(s.key).Prefix()
func (s Subspace) kvStore(ctx sdk.Context) sdk.KVStore {
2018-10-06 06:50:58 -07:00
// append here is safe, appends within a function won't cause
// weird side effects when its singlethreaded
return ctx.KVStore(s.key).Prefix(append(s.name, '/'))
2018-09-18 10:16:51 -07:00
}
// Returns a KVStore identical with ctx.TransientStore(s.tkey).Prefix()
func (s Subspace) transientStore(ctx sdk.Context) sdk.KVStore {
2018-10-06 06:50:58 -07:00
// append here is safe, appends within a function won't cause
// weird side effects when its singlethreaded
return ctx.TransientStore(s.tkey).Prefix(append(s.name, '/'))
2018-09-18 10:16:51 -07:00
}
// Get parameter from store
func (s Subspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
2018-09-18 10:16:51 -07:00
store := s.kvStore(ctx)
2018-09-27 11:52:29 -07:00
bz := store.Get(key)
err := s.cdc.UnmarshalJSON(bz, ptr)
if err != nil {
panic(err)
}
}
// GetIfExists do not modify ptr if the stored parameter is nil
func (s Subspace) GetIfExists(ctx sdk.Context, key []byte, ptr interface{}) {
2018-09-18 10:16:51 -07:00
store := s.kvStore(ctx)
2018-09-27 11:52:29 -07:00
bz := store.Get(key)
if bz == nil {
return
}
err := s.cdc.UnmarshalJSON(bz, ptr)
if err != nil {
panic(err)
}
}
// Get raw bytes of parameter from store
func (s Subspace) GetRaw(ctx sdk.Context, key []byte) []byte {
2018-09-18 10:16:51 -07:00
store := s.kvStore(ctx)
2018-10-06 08:32:41 -07:00
return store.Get(key)
}
// Check if the parameter is set in the store
func (s Subspace) Has(ctx sdk.Context, key []byte) bool {
2018-09-18 10:16:51 -07:00
store := s.kvStore(ctx)
2018-09-27 11:52:29 -07:00
return store.Has(key)
}
// Returns true if the parameter is set in the block
func (s Subspace) Modified(ctx sdk.Context, key []byte) bool {
2018-09-18 10:16:51 -07:00
tstore := s.transientStore(ctx)
2018-09-27 11:52:29 -07:00
return tstore.Has(key)
}
// Set parameter, return error if stored parameter has different type from input
2018-09-26 11:30:57 -07:00
// Also set to the transient store to record change
func (s Subspace) Set(ctx sdk.Context, key []byte, param interface{}) {
2018-09-18 10:16:51 -07:00
store := s.kvStore(ctx)
ty, ok := s.table.m[string(key)]
2018-10-06 08:32:41 -07:00
if !ok {
panic("Parameter not registered")
}
2018-09-26 11:30:57 -07:00
2018-10-06 08:32:41 -07:00
pty := reflect.TypeOf(param)
if pty.Kind() == reflect.Ptr {
pty = pty.Elem()
}
2018-10-06 08:32:41 -07:00
if pty != ty {
panic("Type mismatch with registered table")
}
bz, err := s.cdc.MarshalJSON(param)
if err != nil {
2018-09-10 05:10:06 -07:00
panic(err)
}
2018-09-27 11:52:29 -07:00
store.Set(key, bz)
2018-09-18 10:16:51 -07:00
tstore := s.transientStore(ctx)
2018-09-27 11:52:29 -07:00
tstore.Set(key, []byte{})
}
// Get to ParamSet
func (s Subspace) GetParamSet(ctx sdk.Context, ps ParamSet) {
2018-10-06 06:50:58 -07:00
for _, pair := range ps.KeyValuePairs() {
2018-10-06 08:32:41 -07:00
s.Get(ctx, pair.Key, pair.Value)
2018-09-18 09:27:54 -07:00
}
}
// Set from ParamSet
func (s Subspace) SetParamSet(ctx sdk.Context, ps ParamSet) {
2018-10-06 06:50:58 -07:00
for _, pair := range ps.KeyValuePairs() {
2018-09-18 10:16:51 -07:00
// pair.Field is a pointer to the field, so indirecting the ptr.
// go-amino automatically handles it but just for sure,
// since SetStruct is meant to be used in InitGenesis
// so this method will not be called frequently
2018-10-06 08:32:41 -07:00
v := reflect.Indirect(reflect.ValueOf(pair.Value)).Interface()
2018-09-18 10:16:51 -07:00
s.Set(ctx, pair.Key, v)
2018-09-17 08:28:13 -07:00
}
}
// Returns name of Subspace
func (s Subspace) Name() string {
return string(s.name)
}
// Wrapper of Subspace, provides immutable functions only
type ReadOnlySubspace struct {
s Subspace
}
// Exposes Get
func (ros ReadOnlySubspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
ros.s.Get(ctx, key, ptr)
}
// Exposes GetRaw
func (ros ReadOnlySubspace) GetRaw(ctx sdk.Context, key []byte) []byte {
return ros.s.GetRaw(ctx, key)
}
// Exposes Has
func (ros ReadOnlySubspace) Has(ctx sdk.Context, key []byte) bool {
return ros.s.Has(ctx, key)
}
// Exposes Modified
func (ros ReadOnlySubspace) Modified(ctx sdk.Context, key []byte) bool {
return ros.s.Modified(ctx, key)
}
2018-09-18 10:16:51 -07:00
// Exposes Space
func (ros ReadOnlySubspace) Name() string {
return ros.s.Name()
2018-09-18 10:16:51 -07:00
}