cosmos-sdk/x/params/doc.go

112 lines
2.8 KiB
Go
Raw Normal View History

2018-09-17 08:28:13 -07:00
/*
2019-12-10 08:48:57 -08:00
Package params provides a namespaced module parameter store.
2018-09-17 08:28:13 -07:00
2019-12-10 08:48:57 -08:00
There are two core components, Keeper and Subspace. Subspace is an isolated
namespace for a parameter store, where keys are prefixed by pre-configured
subspace names which modules provide. The Keeper has a permission to access all
existing subspaces.
2018-09-17 08:28:13 -07:00
Subspace can be used by the individual keepers, who needs a private parameter store
that the other keeper cannot modify. Keeper can be used by the Governance keeper,
who need to modify any parameter in case of the proposal passes.
2018-09-17 08:28:13 -07:00
Basic Usage:
2019-12-10 08:48:57 -08:00
1. Declare constant module parameter keys and the globally unique Subspace name:
2018-09-17 08:28:13 -07:00
2018-09-18 04:16:20 -07:00
const (
2019-12-10 08:48:57 -08:00
ModuleSubspace = "mymodule"
2018-09-18 04:16:20 -07:00
)
const (
KeyParameter1 = "myparameter1"
KeyParameter2 = "myparameter2"
)
2019-12-10 08:48:57 -08:00
2. Create a concrete parameter struct and define the validation functions:
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
type MyParams struct {
MyParam1 int64 `json:"my_param1" yaml:"my_param1"`
MyParam2 bool `json:"my_param2" yaml:"my_param2"`
2019-02-04 18:13:04 -08:00
}
2019-12-10 08:48:57 -08:00
func validateMyParam1(i interface{}) error {
_, ok := i.(int64)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
2019-02-04 18:13:04 -08:00
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
// validate (if necessary)...
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
return nil
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func validateMyParam2(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
2019-02-04 18:13:04 -08:00
2019-12-10 08:48:57 -08:00
// validate (if necessary)...
2019-02-04 18:13:04 -08:00
2019-12-10 08:48:57 -08:00
return nil
2019-02-04 18:13:04 -08:00
}
2019-12-10 08:48:57 -08:00
3. Implement the params.ParamSet interface:
2019-02-04 18:13:04 -08:00
2019-12-10 08:48:57 -08:00
func (p *MyParams) ParamSetPairs() params.ParamSetPairs {
return params.ParamSetPairs{
{KeyParameter1, &p.MyParam1, validateMyParam1},
{KeyParameter2, &p.MyParam2, validateMyParam2},
}
2019-02-04 18:13:04 -08:00
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func paramKeyTable() params.KeyTable {
return params.NewKeyTable().RegisterParamSet(&MyParams{})
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
4. Have the module accept a Subspace in the constructor and set the KeyTable (if necessary):
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func NewKeeper(..., paramSpace params.Subspace, ...) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(paramKeyTable())
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
return Keeper {
// ...
paramSpace: paramSpace,
2018-09-18 04:16:20 -07:00
}
}
2019-12-10 08:48:57 -08:00
Now we have access to the module's parameters that are namespaced using the keys defined:
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func InitGenesis(ctx sdk.Context, k Keeper, gs GenesisState) {
// ...
k.SetParams(ctx, gs.Params)
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func (k Keeper) SetParams(ctx sdk.Context, params Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func (k Keeper) GetParams(ctx sdk.Context) (params Params) {
k.paramSpace.GetParamSet(ctx, &params)
return params
}
2018-09-18 04:16:20 -07:00
2019-12-10 08:48:57 -08:00
func (k Keeper) MyParam1(ctx sdk.Context) (res int64) {
k.paramSpace.Get(ctx, KeyParameter1, &res)
return res
2018-09-18 04:16:20 -07:00
}
2019-12-10 08:48:57 -08:00
func (k Keeper) MyParam2(ctx sdk.Context) (res bool) {
k.paramSpace.Get(ctx, KeyParameter2, &res)
return res
2018-09-18 04:16:20 -07:00
}
2019-12-10 08:48:57 -08:00
NOTE: Any call to SetParamSet will panic or any call to Update will error if any
given parameter value is invalid based on the registered value validation function.
2018-09-17 08:28:13 -07:00
*/
2019-12-10 08:48:57 -08:00
package params