2020-02-19 03:52:20 -08:00
|
|
|
package types
|
2018-08-31 02:03:43 -07:00
|
|
|
|
|
|
|
import (
|
2019-08-28 07:58:25 -07:00
|
|
|
"fmt"
|
2018-08-31 02:03:43 -07:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-02-01 17:03:09 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
2021-10-04 09:36:38 -07:00
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
2020-02-19 03:52:20 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-31 02:03:43 -07:00
|
|
|
)
|
|
|
|
|
2018-12-19 11:58:30 -08:00
|
|
|
const (
|
|
|
|
// StoreKey is the string store key for the param store
|
|
|
|
StoreKey = "params"
|
|
|
|
|
|
|
|
// TStoreKey is the string store key for the param transient store
|
|
|
|
TStoreKey = "transient_params"
|
|
|
|
)
|
|
|
|
|
2018-08-31 02:03:43 -07:00
|
|
|
// 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
|
2018-10-10 13:01:30 -07:00
|
|
|
type Subspace struct {
|
2021-04-29 03:46:22 -07:00
|
|
|
cdc codec.BinaryCodec
|
2020-08-13 06:20:02 -07:00
|
|
|
legacyAmino *codec.LegacyAmino
|
2021-10-04 09:36:38 -07:00
|
|
|
key storetypes.StoreKey // []byte -> []byte, stores parameter
|
|
|
|
tkey storetypes.StoreKey // []byte -> bool, stores parameter change
|
2020-08-13 06:20:02 -07:00
|
|
|
name []byte
|
|
|
|
table KeyTable
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
// NewSubspace constructs a store with namestore
|
2021-10-04 09:36:38 -07:00
|
|
|
func NewSubspace(cdc codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key storetypes.StoreKey, tkey storetypes.StoreKey, name string) Subspace {
|
2019-12-10 08:48:57 -08:00
|
|
|
return Subspace{
|
2020-08-13 06:20:02 -07:00
|
|
|
cdc: cdc,
|
|
|
|
legacyAmino: legacyAmino,
|
|
|
|
key: key,
|
|
|
|
tkey: tkey,
|
|
|
|
name: []byte(name),
|
|
|
|
table: NewKeyTable(),
|
2018-10-10 13:01:30 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-06 08:32:41 -07:00
|
|
|
|
2019-12-02 17:58:14 -08:00
|
|
|
// HasKeyTable returns if the Subspace has a KeyTable registered.
|
|
|
|
func (s Subspace) HasKeyTable() bool {
|
|
|
|
return len(s.table.m) > 0
|
|
|
|
}
|
|
|
|
|
2019-02-04 18:13:04 -08:00
|
|
|
// WithKeyTable initializes KeyTable and returns modified Subspace
|
|
|
|
func (s Subspace) WithKeyTable(table KeyTable) Subspace {
|
2018-10-13 10:29:59 -07:00
|
|
|
if table.m == nil {
|
2019-02-04 18:13:04 -08:00
|
|
|
panic("SetKeyTable() called with nil KeyTable")
|
2018-10-10 13:01:30 -07:00
|
|
|
}
|
2018-10-13 10:29:59 -07:00
|
|
|
if len(s.table.m) != 0 {
|
2019-02-04 18:13:04 -08:00
|
|
|
panic("SetKeyTable() called on already initialized Subspace")
|
2018-10-10 13:01:30 -07:00
|
|
|
}
|
|
|
|
|
2018-10-13 10:29:59 -07:00
|
|
|
for k, v := range table.m {
|
|
|
|
s.table.m[k] = v
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
2018-10-06 06:50:58 -07:00
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Allocate additional capacity for Subspace.name
|
2018-10-13 10:29:59 -07:00
|
|
|
// 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-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2018-10-06 06:50:58 -07:00
|
|
|
// Returns a KVStore identical with ctx.KVStore(s.key).Prefix()
|
2018-10-10 13:01:30 -07:00
|
|
|
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
|
2019-02-01 17:03:09 -08:00
|
|
|
return prefix.NewStore(ctx.KVStore(s.key), append(s.name, '/'))
|
2018-09-18 10:16:51 -07:00
|
|
|
}
|
|
|
|
|
2019-02-04 18:13:04 -08:00
|
|
|
// Returns a transient store for modification
|
2018-10-10 13:01:30 -07:00
|
|
|
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
|
2019-02-01 17:03:09 -08:00
|
|
|
return prefix.NewStore(ctx.TransientStore(s.tkey), append(s.name, '/'))
|
2018-09-18 10:16:51 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Validate attempts to validate a parameter value by its key. If the key is not
|
|
|
|
// registered or if the validation of the value fails, an error is returned.
|
|
|
|
func (s Subspace) Validate(ctx sdk.Context, key []byte, value interface{}) error {
|
|
|
|
attr, ok := s.table.m[string(key)]
|
|
|
|
if !ok {
|
2021-10-14 01:53:38 -07:00
|
|
|
return fmt.Errorf("parameter %s not registered", key)
|
2019-12-10 08:48:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := attr.vfn(value); err != nil {
|
|
|
|
return fmt.Errorf("invalid parameter value: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-02-04 18:13:04 -08:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Get queries for a parameter by key from the Subspace's KVStore and sets the
|
|
|
|
// value to the provided pointer. If the value does not exist, it will panic.
|
2019-04-05 13:20:58 -07:00
|
|
|
func (s Subspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
|
2020-02-12 08:34:09 -08:00
|
|
|
s.checkType(key, ptr)
|
|
|
|
|
2018-09-18 10:16:51 -07:00
|
|
|
store := s.kvStore(ctx)
|
2018-09-27 11:52:29 -07:00
|
|
|
bz := store.Get(key)
|
2019-12-10 08:48:57 -08:00
|
|
|
|
2020-08-13 06:20:02 -07:00
|
|
|
if err := s.legacyAmino.UnmarshalJSON(bz, ptr); err != nil {
|
2019-04-05 13:20:58 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// GetIfExists queries for a parameter by key from the Subspace's KVStore and
|
|
|
|
// sets the value to the provided pointer. If the value does not exist, it will
|
|
|
|
// perform a no-op.
|
2019-04-05 13:20:58 -07:00
|
|
|
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)
|
2018-08-31 02:03:43 -07:00
|
|
|
if bz == nil {
|
2019-04-05 13:20:58 -07:00
|
|
|
return
|
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
|
2020-04-16 08:10:39 -07:00
|
|
|
s.checkType(key, ptr)
|
|
|
|
|
2020-08-13 06:20:02 -07:00
|
|
|
if err := s.legacyAmino.UnmarshalJSON(bz, ptr); err != nil {
|
2019-04-05 13:20:58 -07:00
|
|
|
panic(err)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 05:44:58 -07:00
|
|
|
// IterateKeys iterates over all the keys in the subspace and executes the
|
|
|
|
// provided callback. If the callback returns true for a given key, iteration
|
|
|
|
// will halt.
|
|
|
|
func (s Subspace) IterateKeys(ctx sdk.Context, cb func(key []byte) bool) {
|
|
|
|
store := s.kvStore(ctx)
|
|
|
|
|
|
|
|
iter := sdk.KVStorePrefixIterator(store, nil)
|
|
|
|
defer iter.Close()
|
|
|
|
|
|
|
|
for ; iter.Valid(); iter.Next() {
|
|
|
|
if cb(iter.Key()) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// GetRaw queries for the raw values bytes for a parameter by key.
|
2018-10-10 13:01:30 -07:00
|
|
|
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)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Has returns if a parameter key exists or not in the Subspace's KVStore.
|
2018-10-10 13:01:30 -07:00
|
|
|
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)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Modified returns true if the parameter key is set in the Subspace's transient
|
|
|
|
// KVStore.
|
2018-10-10 13:01:30 -07:00
|
|
|
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)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// checkType verifies that the provided key and value are comptable and registered.
|
|
|
|
func (s Subspace) checkType(key []byte, value interface{}) {
|
2019-02-04 18:13:04 -08:00
|
|
|
attr, ok := s.table.m[string(key)]
|
2018-10-06 08:32:41 -07:00
|
|
|
if !ok {
|
2021-10-14 01:53:38 -07:00
|
|
|
panic(fmt.Sprintf("parameter %s not registered", key))
|
2018-10-06 08:32:41 -07:00
|
|
|
}
|
2018-09-26 11:30:57 -07:00
|
|
|
|
2019-02-04 18:13:04 -08:00
|
|
|
ty := attr.ty
|
2019-12-10 08:48:57 -08:00
|
|
|
pty := reflect.TypeOf(value)
|
2018-10-06 08:32:41 -07:00
|
|
|
if pty.Kind() == reflect.Ptr {
|
|
|
|
pty = pty.Elem()
|
|
|
|
}
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-10-06 08:32:41 -07:00
|
|
|
if pty != ty {
|
2019-12-10 08:48:57 -08:00
|
|
|
panic("type mismatch with registered table")
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
2019-02-04 18:13:04 -08:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Set stores a value for given a parameter key assuming the parameter type has
|
|
|
|
// been registered. It will panic if the parameter type has not been registered
|
|
|
|
// or if the value cannot be encoded. A change record is also set in the Subspace's
|
|
|
|
// transient KVStore to mark the parameter as modified.
|
|
|
|
func (s Subspace) Set(ctx sdk.Context, key []byte, value interface{}) {
|
|
|
|
s.checkType(key, value)
|
2019-02-04 18:13:04 -08:00
|
|
|
store := s.kvStore(ctx)
|
|
|
|
|
2020-08-13 06:20:02 -07:00
|
|
|
bz, err := s.legacyAmino.MarshalJSON(value)
|
2018-08-31 02:03:43 -07:00
|
|
|
if err != nil {
|
2019-04-05 13:20:58 -07:00
|
|
|
panic(err)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
2019-12-10 08:48:57 -08:00
|
|
|
|
2018-09-27 11:52:29 -07:00
|
|
|
store.Set(key, bz)
|
2018-08-31 02:03:43 -07:00
|
|
|
|
2018-09-18 10:16:51 -07:00
|
|
|
tstore := s.transientStore(ctx)
|
2018-09-27 11:52:29 -07:00
|
|
|
tstore.Set(key, []byte{})
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Update stores an updated raw value for a given parameter key assuming the
|
|
|
|
// parameter type has been registered. It will panic if the parameter type has
|
|
|
|
// not been registered or if the value cannot be encoded. An error is returned
|
|
|
|
// if the raw value is not compatible with the registered type for the parameter
|
|
|
|
// key or if the new value is invalid as determined by the registered type's
|
|
|
|
// validation function.
|
|
|
|
func (s Subspace) Update(ctx sdk.Context, key, value []byte) error {
|
2019-04-30 09:31:38 -07:00
|
|
|
attr, ok := s.table.m[string(key)]
|
|
|
|
if !ok {
|
2021-10-14 01:53:38 -07:00
|
|
|
panic(fmt.Sprintf("parameter %s not registered", key))
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ty := attr.ty
|
|
|
|
dest := reflect.New(ty).Interface()
|
2019-05-27 17:47:12 -07:00
|
|
|
s.GetIfExists(ctx, key, dest)
|
2019-09-04 10:33:32 -07:00
|
|
|
|
2020-08-13 06:20:02 -07:00
|
|
|
if err := s.legacyAmino.UnmarshalJSON(value, dest); err != nil {
|
2019-12-10 08:48:57 -08:00
|
|
|
return err
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// destValue contains the dereferenced value of dest so validation function do
|
|
|
|
// not have to operate on pointers.
|
|
|
|
destValue := reflect.Indirect(reflect.ValueOf(dest)).Interface()
|
|
|
|
if err := s.Validate(ctx, key, destValue); err != nil {
|
2019-04-30 09:31:38 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
s.Set(ctx, key, dest)
|
2019-04-30 09:31:38 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// GetParamSet iterates through each ParamSetPair where for each pair, it will
|
|
|
|
// retrieve the value and set it to the corresponding value pointer provided
|
|
|
|
// in the ParamSetPair by calling Subspace#Get.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (s Subspace) GetParamSet(ctx sdk.Context, ps ParamSet) {
|
2019-02-04 18:13:04 -08:00
|
|
|
for _, pair := range ps.ParamSetPairs() {
|
2019-04-05 13:20:58 -07:00
|
|
|
s.Get(ctx, pair.Key, pair.Value)
|
2018-09-18 09:27:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// SetParamSet iterates through each ParamSetPair and sets the value with the
|
|
|
|
// corresponding parameter key in the Subspace's KVStore.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (s Subspace) SetParamSet(ctx sdk.Context, ps ParamSet) {
|
2019-02-04 18:13:04 -08:00
|
|
|
for _, pair := range ps.ParamSetPairs() {
|
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()
|
2019-12-10 08:48:57 -08:00
|
|
|
|
|
|
|
if err := pair.ValidatorFn(v); err != nil {
|
|
|
|
panic(fmt.Sprintf("value from ParamSetPair is invalid: %s", err))
|
|
|
|
}
|
|
|
|
|
2019-04-05 13:20:58 -07:00
|
|
|
s.Set(ctx, pair.Key, v)
|
2018-09-17 08:28:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Name returns the name of the Subspace.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (s Subspace) Name() string {
|
|
|
|
return string(s.name)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 13:01:30 -07:00
|
|
|
// Wrapper of Subspace, provides immutable functions only
|
|
|
|
type ReadOnlySubspace struct {
|
|
|
|
s Subspace
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Get delegates a read-only Get call to the Subspace.
|
2019-04-05 13:20:58 -07:00
|
|
|
func (ros ReadOnlySubspace) Get(ctx sdk.Context, key []byte, ptr interface{}) {
|
|
|
|
ros.s.Get(ctx, key, ptr)
|
2018-08-31 02:03:43 -07:00
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// GetRaw delegates a read-only GetRaw call to the Subspace.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (ros ReadOnlySubspace) GetRaw(ctx sdk.Context, key []byte) []byte {
|
2018-08-31 02:03:43 -07:00
|
|
|
return ros.s.GetRaw(ctx, key)
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Has delegates a read-only Has call to the Subspace.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (ros ReadOnlySubspace) Has(ctx sdk.Context, key []byte) bool {
|
2018-08-31 02:03:43 -07:00
|
|
|
return ros.s.Has(ctx, key)
|
|
|
|
}
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Modified delegates a read-only Modified call to the Subspace.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (ros ReadOnlySubspace) Modified(ctx sdk.Context, key []byte) bool {
|
2018-08-31 02:03:43 -07:00
|
|
|
return ros.s.Modified(ctx, key)
|
|
|
|
}
|
2018-09-18 10:16:51 -07:00
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
// Name delegates a read-only Name call to the Subspace.
|
2018-10-10 13:01:30 -07:00
|
|
|
func (ros ReadOnlySubspace) Name() string {
|
|
|
|
return ros.s.Name()
|
2018-09-18 10:16:51 -07:00
|
|
|
}
|