Skip to content

Commit f15ad04

Browse files
mossidjackzampolin
authored andcommitted
Merge PR #2605: Paramstore Subkey
1 parent a2b73c8 commit f15ad04

File tree

24 files changed

+181
-142
lines changed

24 files changed

+181
-142
lines changed

PENDING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ IMPROVEMENTS
7070
* [\#3424](https://github.com/cosmos/cosmos-sdk/issues/3424) Allow generation of gentxs with empty memo field.
7171

7272
* SDK
73+
* [\#2605] x/params add subkey accessing
7374
* [\#2986](https://github.com/cosmos/cosmos-sdk/pull/2986) Store Refactor
7475
* \#3435 Test that store implementations do not allow nil values
7576

docs/spec/params/keeper.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
In the app initialization stage, `Keeper.Subspace(Paramspace)` is passed to the user modules, and the subspaces are stored in `Keeper.spaces`. Later it can be retrieved with `Keeper.GetSubspace`, so the keepers holding `Keeper` can access to any subspace. For example, Gov module can take `Keeper` as its argument and modify parameter of any subspace when a `ParameterChangeProposal` is accepted.
44

5+
Example:
6+
57
```go
68
type MasterKeeper struct {
79
pk params.Keeper

docs/spec/params/subspace.md

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,26 @@
11
# Subspace
22

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.
44

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
66

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.
118

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.
1610

17-
type Keeper struct {
18-
cdc *wire.Codec
19-
key sdk.StoreKey
11+
## KeyTable
2012

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.
2414

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.
2616

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.
3018

31-
`NewKeeper` should register a `TypeTable`, which defines a map from parameter keys from types.
19+
## ParamSet
3220

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:
4222

43-
Now we can access to the paramstore using Paramstore Keys
44-
45-
```go
46-
var param MyStruct
47-
k.ps.Get(KeyParameter1, &param)
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
7625

26+
The implementor should be a pointer in order to use `GetParamSet()`

x/auth/keeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewAccountKeeper(
4747
key: key,
4848
proto: proto,
4949
cdc: cdc,
50-
paramSubspace: paramstore.WithTypeTable(ParamTypeTable()),
50+
paramSubspace: paramstore.WithKeyTable(ParamKeyTable()),
5151
}
5252
}
5353

x/auth/params.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ type Params struct {
4242
}
4343

4444
// ParamTable for staking module
45-
func ParamTypeTable() params.TypeTable {
46-
return params.NewTypeTable().RegisterParamSet(&Params{})
45+
func ParamKeyTable() params.KeyTable {
46+
return params.NewKeyTable().RegisterParamSet(&Params{})
4747
}
4848

49-
// KeyValuePairs implements the ParamSet interface and returns all the key/value
49+
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs
5050
// pairs of auth module's parameters.
5151
// nolint
52-
func (p *Params) KeyValuePairs() params.KeyValuePairs {
53-
return params.KeyValuePairs{
52+
func (p *Params) ParamSetPairs() params.ParamSetPairs {
53+
return params.ParamSetPairs{
5454
{KeyMaxMemoCharacters, &p.MaxMemoCharacters},
5555
{KeyTxSigLimit, &p.TxSigLimit},
5656
{KeyTxSizeCostPerByte, &p.TxSizeCostPerByte},

x/bank/keeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewBaseKeeper(ak auth.AccountKeeper,
4242
paramSpace params.Subspace,
4343
codespace sdk.CodespaceType) BaseKeeper {
4444

45-
ps := paramSpace.WithTypeTable(ParamTypeTable())
45+
ps := paramSpace.WithKeyTable(ParamKeyTable())
4646
return BaseKeeper{
4747
BaseSendKeeper: NewBaseSendKeeper(ak, ps, codespace),
4848
ak: ak,

x/bank/params.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const (
1515
var ParamStoreKeySendEnabled = []byte("sendenabled")
1616

1717
// type declaration for parameters
18-
func ParamTypeTable() params.TypeTable {
19-
return params.NewTypeTable(
18+
func ParamKeyTable() params.KeyTable {
19+
return params.NewKeyTable(
2020
ParamStoreKeySendEnabled, false,
2121
)
2222
}

x/distribution/keeper/keeper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramSpace params.Subspace, c
2626
keeper := Keeper{
2727
storeKey: key,
2828
cdc: cdc,
29-
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
29+
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
3030
bankKeeper: ck,
3131
stakingKeeper: sk,
3232
feeCollectionKeeper: fck,

x/distribution/keeper/params.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
// type declaration for parameters
9-
func ParamTypeTable() params.TypeTable {
10-
return params.NewTypeTable(
9+
func ParamKeyTable() params.KeyTable {
10+
return params.NewKeyTable(
1111
ParamStoreKeyCommunityTax, sdk.Dec{},
1212
ParamStoreKeyBaseProposerReward, sdk.Dec{},
1313
ParamStoreKeyBonusProposerReward, sdk.Dec{},

x/gov/keeper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ var (
3535
BurnedDepositCoinsAccAddr = sdk.AccAddress(crypto.AddressHash([]byte("govBurnedDepositCoins")))
3636
)
3737

38-
// Type declaration for parameters
39-
func ParamTypeTable() params.TypeTable {
40-
return params.NewTypeTable(
38+
// Key declaration for parameters
39+
func ParamKeyTable() params.KeyTable {
40+
return params.NewKeyTable(
4141
ParamStoreKeyDepositParams, DepositParams{},
4242
ParamStoreKeyVotingParams, VotingParams{},
4343
ParamStoreKeyTallyParams, TallyParams{},
@@ -80,7 +80,7 @@ func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, paramsKeeper params.Keeper, p
8080
return Keeper{
8181
storeKey: key,
8282
paramsKeeper: paramsKeeper,
83-
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
83+
paramSpace: paramSpace.WithKeyTable(ParamKeyTable()),
8484
ck: ck,
8585
ds: ds,
8686
vs: ds.GetValidatorSet(),

0 commit comments

Comments
 (0)