cosmos-sdk/x/params/doc.go

119 lines
3.1 KiB
Go
Raw Normal View History

2018-09-17 08:28:13 -07:00
package params
/*
2018-09-26 08:32:41 -07:00
Package params provides a globally available parameter store.
2018-09-17 08:28:13 -07:00
There are two main types, Keeper and Subspace. Subspace is an isolated namespace for a
2018-09-26 08:32:41 -07:00
paramstore, where keys are prefixed by preconfigured spacename. Keeper has a
permission to access all existing spaces.
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:
2018-09-26 08:32:41 -07:00
First, declare parameter space and parameter keys for the module. Then include
params.Subspace in the keeper. Since we prefix the keys with the spacename, it is
2018-09-26 08:32:41 -07:00
recommended to use the same name with the module's.
2018-09-17 08:28:13 -07:00
2018-09-18 04:16:20 -07:00
const (
DefaultParamspace = "mymodule"
)
const (
KeyParameter1 = "myparameter1"
KeyParameter2 = "myparameter2"
)
type Keeper struct {
2019-02-04 18:13:04 -08:00
cdc *codec.Codec
2018-09-18 04:16:20 -07:00
key sdk.StoreKey
ps params.Subspace
2018-09-18 04:16:20 -07:00
}
2019-02-04 18:13:04 -08:00
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable(
KeyParameter1, MyStruct{},
KeyParameter2, MyStruct{},
)
}
func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, ps params.Subspace) Keeper {
return Keeper {
cdc: cdc,
key: key,
ps: ps.WithKeyTable(ParamKeyTable()),
}
}
2018-10-13 10:50:32 -07:00
Pass a params.Subspace to NewKeeper with DefaultParamspace (or another)
2018-09-18 04:16:20 -07:00
app.myKeeper = mymodule.NewKeeper(app.paramStore.SubStore(mymodule.DefaultParamspace))
Now we can access to the paramstore using Paramstore Keys
var param MyStruct
2019-02-04 18:13:04 -08:00
k.ps.Get(ctx, KeyParameter1, &param)
k.ps.Set(ctx, KeyParameter2, param)
If you want to store an unknown number of parameters, or want to store a mapping,
you can use subkeys. Subkeys can be used with a main key, where the subkeys are
inheriting the key properties.
func ParamKeyTable() params.KeyTable {
return params.NewKeyTable(
KeyParamMain, MyStruct{},
)
}
func (k Keeper) GetDynamicParameter(ctx sdk.Context, subkey []byte) (res MyStruct) {
k.ps.GetWithSubkey(ctx, KeyParamMain, subkey, &res)
}
2018-09-18 04:16:20 -07:00
Genesis Usage:
Declare a struct for parameters and make it implement params.ParamSet. It will then
be able to be passed to SetParamSet.
2018-09-18 04:16:20 -07:00
type MyParams struct {
Parameter1 uint64
Parameter2 string
}
// Implements params.ParamSet
// KeyValuePairs must return the list of (ParamKey, PointerToTheField)
func (p *MyParams) KeyValuePairs() params.KeyValuePairs {
2018-09-18 04:16:20 -07:00
return params.KeyFieldPairs {
{KeyParameter1, &p.Parameter1},
{KeyParameter2, &p.Parameter2},
}
}
func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) {
k.ps.SetParamSet(ctx, &data.params)
2018-09-18 04:16:20 -07:00
}
2018-09-26 08:32:41 -07:00
The method is pointer receiver because there could be a case that we read from
the store and set the result to the struct.
2018-09-18 04:16:20 -07:00
Master Keeper Usage:
2018-09-18 04:16:20 -07:00
2018-09-26 08:32:41 -07:00
Keepers that require master permission to the paramstore, such as gov, can take
params.Keeper itself to access all subspace(using GetSubspace)
2018-09-18 04:16:20 -07:00
type MasterKeeper struct {
pk params.Keeper
2018-09-18 04:16:20 -07:00
}
func (k MasterKeeper) SetParam(ctx sdk.Context, space string, key string, param interface{}) {
2018-10-13 10:50:32 -07:00
space, ok := k.pk.GetSubspace(space)
2018-09-18 04:16:20 -07:00
if !ok {
return
}
space.Set(ctx, key, param)
2018-09-18 04:16:20 -07:00
}
2018-09-17 08:28:13 -07:00
*/