|
1 | 1 | # Subspace |
2 | 2 |
|
3 | | -## Basic Usage |
| 3 | +`Subspace` is a prefixed subspace of the parameter store. Each module who use the parameter store will take a `Subspace`, not the `Keeper`, to isolate permission to access. |
4 | 4 |
|
5 | | -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 recommended to use the same name with the module's. |
| 5 | +## Key |
6 | 6 |
|
7 | | -```go |
8 | | -const ( |
9 | | - DefaultParamspace = "mymodule" |
10 | | -) |
| 7 | +Parameter keys are human readable alphanumeric strings. A parameter for the key `"ExampleParameter"` is stored under `[]byte("SubspaceName" + "/" + "ExampleParameter")`, where `"SubspaceName"` is the name of the subspace. |
11 | 8 |
|
12 | | -const ( |
13 | | - KeyParameter1 = "myparameter1" |
14 | | - KeyParameter2 = "myparameter2" |
15 | | -) |
| 9 | +Subkeys are secondary parameter keys those are used along with a primary parameter key. Subkeys can be used for grouping or dynamic parameter key generation during runtime. |
16 | 10 |
|
17 | | -type Keeper struct { |
18 | | - cdc *wire.Codec |
19 | | - key sdk.StoreKey |
| 11 | +## KeyTable |
20 | 12 |
|
21 | | - ps params.Subspace |
22 | | -} |
23 | | -``` |
| 13 | +All of the paramter keys that will be used should be registered at the compile time. `KeyTable` is essentially a `map[string]attribute`, where the `string` is a parameter key. |
24 | 14 |
|
25 | | -Pass a params.Subspace to NewKeeper with DefaultParamSubspace (or another) |
| 15 | +Currently, `attribute` only consists of `reflect.Type`, which indicates the parameter type. It is needed even if the state machine has no error, because the paraeter can be modified externally, for example via the governance. |
26 | 16 |
|
27 | | -```go |
28 | | -app.myKeeper = mymodule.NewKeeper(cdc, key, app.paramStore.SubStore(mymodule.DefaultParamspace)) |
29 | | -``` |
| 17 | +Only primary keys have to be registered on the `KeyTable`. Subkeys inherit the attribute of the primary key. |
30 | 18 |
|
31 | | -`NewKeeper` should register a `TypeTable`, which defines a map from parameter keys from types. |
| 19 | +## ParamSet |
32 | 20 |
|
33 | | -```go |
34 | | -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, space params.Subspace) Keeper { |
35 | | - return Keeper { |
36 | | - cdc: cdc, |
37 | | - key: key, |
38 | | - ps: space.WithTypeTable(ParamTypeTable()), |
39 | | - } |
40 | | -} |
41 | | -``` |
| 21 | +Modules often define a struct of parameters. Instead of calling methods with each of those parameters, when the struct implements `ParamSet`, it can be used with the following methods: |
42 | 22 |
|
43 | | -Now we can access to the paramstore using Paramstore Keys |
44 | | - |
45 | | -```go |
46 | | -var param MyStruct |
47 | | -k.ps.Get(KeyParameter1, ¶m) |
48 | | -k.ps.Set(KeyParameter2, param) |
49 | | -``` |
50 | | - |
51 | | -# Genesis Usage |
52 | | - |
53 | | -Declare a struct for parameters and make it implement params.ParamSet. It will then be able to be passed to SetParamSet. |
54 | | - |
55 | | -```go |
56 | | -type MyParams struct { |
57 | | - Parameter1 uint64 |
58 | | - Parameter2 string |
59 | | -} |
60 | | - |
61 | | -// Implements params.ParamSet |
62 | | -// KeyValuePairs must return the list of (ParamKey, PointerToTheField) |
63 | | -func (p *MyParams) KeyValuePairs() params.KeyValuePairs { |
64 | | - return params.KeyFieldPairs { |
65 | | - {KeyParameter1, &p.Parameter1}, |
66 | | - {KeyParameter2, &p.Parameter2}, |
67 | | - } |
68 | | -} |
69 | | - |
70 | | -func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) { |
71 | | - k.ps.SetParamSet(ctx, &data.params) |
72 | | -} |
73 | | -``` |
74 | | - |
75 | | -The method is pointer receiver because there could be a case that we read from the store and set the result to the struct. |
| 23 | +* `KeyTable.RegisterParamSet()`: registers all parameters in the struct |
| 24 | +* `Subspace.{Get, Set}ParamSet()`: Get to & Set from the struct |
76 | 25 |
|
| 26 | +The implementor should be a pointer in order to use `GetParamSet()` |
0 commit comments