Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add params migration #14992

Merged
merged 5 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ ClevelDB, BoltDB and BadgerDB are not supported anymore. To migrate from a unsup

GoLevelDB version has been pinned to `v1.0.1-0.20210819022825-2ae1ddf74ef7`, following versions might cause unexpected behavior.
See related issues:
- [issue #14949 on cosmos-sdk](https://github.com/cosmos/cosmos-sdk/issues/14949)
- [issue #25413 on go-ethereum](https://github.com/ethereum/go-ethereum/pull/25413)

* [issue #14949 on cosmos-sdk](https://github.com/cosmos/cosmos-sdk/issues/14949)
* [issue #25413 on go-ethereum](https://github.com/ethereum/go-ethereum/pull/25413)

### Protobuf

Expand Down Expand Up @@ -266,6 +267,15 @@ The module name is assumed by `baseapp` to be the second element of the message
In case a module does not follow the standard message path, (e.g. IBC), it is advised to keep emitting the module name event.
`Baseapp` only emits that event if the module have not already done so.

### `x/params`

The `params` module was deprecated since v0.46. The Cosmos SDK has migrated away from `x/params` for its own modules.
Cosmos SDK modules now store their parameters directly in its repective modules.
The `params` module will be removed in `v0.48`, as mentioned [in v0.46 release](https://github.com/cosmos/cosmos-sdk/blob/v0.46.1/UPGRADING.md#xparams). It is strongly encouraged to migrate away from `x/params` before `v0.48`.

When performing a chain migration, the params table must be initizalied manually. This was done in the modules keepers in previous versions.
Have a look at `simapp.RegisterUpgradeHandlers()` for an example.

#### `x/gov`

##### Minimum Proposal Deposit At Time of Submission
Expand Down Expand Up @@ -336,20 +346,17 @@ func (app SimApp) RegisterUpgradeHandlers() {

The old params module is required to still be imported in your app.go in order to handle this migration.

##### App.go Changes
##### `app.go` changes

Previous:
When using an `app.go` without App Wiring, the following changes are required:

```go
bApp.SetParamStore(app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()))
```diff
- bApp.SetParamStore(app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable()))
+ app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamstypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
+ bApp.SetParamStore(&app.ConsensusParamsKeeper)
```

After:

```go
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[upgradetypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
bApp.SetParamStore(&app.ConsensusParamsKeeper)
```
When using App Wiring, the paramater store is automatically set for you.

#### `x/nft`

Expand Down Expand Up @@ -442,7 +449,7 @@ mistakes.

#### `x/params`

* The `x/params` module has been depreacted in favour of each module housing and providing way to modify their parameters. Each module that has parameters that are changable during runtime have an authority, the authority can be a module or user account. The Cosmos-SDK team recommends migrating modules away from using the param module. An example of how this could look like can be found [here](https://github.com/cosmos/cosmos-sdk/pull/12363).
* The `x/params` module has been depreacted in favour of each module housing and providing way to modify their parameters. Each module that has parameters that are changable during runtime have an authority, the authority can be a module or user account. The Cosmos SDK team recommends migrating modules away from using the param module. An example of how this could look like can be found [here](https://github.com/cosmos/cosmos-sdk/pull/12363).
* The Param module will be maintained until April 18, 2023. At this point the module will reach end of life and be removed from the Cosmos SDK.

#### `x/gov`
Expand Down
15 changes: 6 additions & 9 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ import (
"github.com/spf13/cast"

simappparams "cosmossdk.io/simapp/params"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/evidence"
evidencekeeper "cosmossdk.io/x/evidence/keeper"
evidencetypes "cosmossdk.io/x/evidence/types"
"cosmossdk.io/x/feegrant"
feegrantkeeper "cosmossdk.io/x/feegrant/keeper"
feegrantmodule "cosmossdk.io/x/feegrant/module"
"cosmossdk.io/x/nft"
nftkeeper "cosmossdk.io/x/nft/keeper"
nftmodule "cosmossdk.io/x/nft/module"

"cosmossdk.io/x/upgrade"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"
upgradetypes "cosmossdk.io/x/upgrade/types"

storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/feegrant"
feegrantkeeper "cosmossdk.io/x/feegrant/keeper"
feegrantmodule "cosmossdk.io/x/feegrant/module"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand Down Expand Up @@ -85,7 +83,6 @@ import (
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/group"
groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper"
Expand Down Expand Up @@ -282,7 +279,7 @@ func NewSimApp(
app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])

// set the BaseApp's parameter store
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[upgradetypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
app.ConsensusParamsKeeper = consensusparamkeeper.NewKeeper(appCodec, keys[consensusparamtypes.StoreKey], authtypes.NewModuleAddress(govtypes.ModuleName).String())
bApp.SetParamStore(&app.ConsensusParamsKeeper)

app.CapabilityKeeper = capabilitykeeper.NewKeeper(appCodec, keys[capabilitytypes.StoreKey], memKeys[capabilitytypes.MemStoreKey])
Expand Down Expand Up @@ -741,7 +738,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(minttypes.ModuleName)
paramsKeeper.Subspace(distrtypes.ModuleName)
paramsKeeper.Subspace(slashingtypes.ModuleName)
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable())
paramsKeeper.Subspace(govtypes.ModuleName)
paramsKeeper.Subspace(crisistypes.ModuleName)

return paramsKeeper
Expand Down
51 changes: 47 additions & 4 deletions simapp/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

// UpgradeName defines the on-chain upgrade name for the sample SimApp upgrade
Expand All @@ -19,17 +29,45 @@ import (
const UpgradeName = "v046-to-v047"

func (app SimApp) RegisterUpgradeHandlers() {
// Set param key table for params module migration
for _, subspace := range app.ParamsKeeper.GetSubspaces() {
subspace := subspace

var keyTable paramstypes.KeyTable
switch subspace.Name() {
case authtypes.ModuleName:
keyTable = authtypes.ParamKeyTable()
case banktypes.ModuleName:
keyTable = banktypes.ParamKeyTable()
case stakingtypes.ModuleName:
keyTable = stakingtypes.ParamKeyTable()
case minttypes.ModuleName:
keyTable = minttypes.ParamKeyTable()
case distrtypes.ModuleName:
keyTable = distrtypes.ParamKeyTable()
case slashingtypes.ModuleName:
keyTable = slashingtypes.ParamKeyTable()
case govtypes.ModuleName:
keyTable = govv1.ParamKeyTable()
case crisistypes.ModuleName:
keyTable = crisistypes.ParamKeyTable()
}

if !subspace.HasKeyTable() {
subspace.WithKeyTable(keyTable)
}
}

baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())

app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Migrate CometBFT consensus parameters from x/params module to a
// dedicated x/consensus module.
// Migrate CometBFT consensus parameters from x/params module to a dedicated x/consensus module.
baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper)

// Note: this migration is optional,
// You can include x/gov proposal migration documented at [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md)
// You can include x/gov proposal migration documented in [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md)

return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
},
Expand All @@ -41,7 +79,12 @@ func (app SimApp) RegisterUpgradeHandlers() {
}

if upgradeInfo.Name == UpgradeName && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := storetypes.StoreUpgrades{}
storeUpgrades := storetypes.StoreUpgrades{
Added: []string{
consensustypes.ModuleName,
crisistypes.ModuleName,
},
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
Expand Down
2 changes: 1 addition & 1 deletion x/auth/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

var ParamsKey = []byte{0x01}
var ParamsKey = []byte{0x00}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well this is concerning

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need way better testing on migrations. This is too close to a release


// Migrate migrates the x/auth module state from the consensus version 3 to
// version 4. Specifically, it takes the parameters that are currently stored
Expand Down
4 changes: 1 addition & 3 deletions x/auth/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const (
)

// NewParams creates a new Params object
func NewParams(
maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64,
) Params {
func NewParams(maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64) Params {
return Params{
MaxMemoCharacters: maxMemoCharacters,
TxSigLimit: txSigLimit,
Expand Down
4 changes: 1 addition & 3 deletions x/auth/types/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ var (

var _ paramtypes.ParamSet = &Params{}

// ParamKeyTable for auth module
//
// Deprecated.
// Deprecated: ParamKeyTable for auth module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
Expand Down
2 changes: 1 addition & 1 deletion x/crisis/types/legacy_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// ParamStoreKeyConstantFee is the constant fee parameter
var ParamStoreKeyConstantFee = []byte("ConstantFee")

// type declaration for parameters
// Deprecated: Type declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable(
paramtypes.NewParamSetPair(ParamStoreKeyConstantFee, sdk.Coin{}, validateConstantFee),
Expand Down
4 changes: 2 additions & 2 deletions x/distribution/types/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ var (
ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled")
)

// ParamKeyTable returns the parameter key table.
// Deprecated: ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// ParamSetPairs returns the parameter set pairs.
// Deprecated: ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyCommunityTax, &p.CommunityTax, validateCommunityTax),
Expand Down
2 changes: 1 addition & 1 deletion x/gov/types/v1/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
ParamStoreKeyTallyParams = []byte("tallyparams")
)

// ParamKeyTable - Key declaration for parameters
// Deprecated: ParamKeyTable - Key declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable(
paramtypes.NewParamSetPair(ParamStoreKeyDepositParams, DepositParams{}, validateDepositParams),
Expand Down
4 changes: 1 addition & 3 deletions x/mint/types/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ var (
KeyBlocksPerYear = []byte("BlocksPerYear")
)

// ParamTable for minting module.
//
// Deprecated.
// Deprecated: ParamTable for minting module.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
Expand Down
4 changes: 2 additions & 2 deletions x/params/types/subspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func (s Subspace) HasKeyTable() bool {
// WithKeyTable initializes KeyTable and returns modified Subspace
func (s Subspace) WithKeyTable(table KeyTable) Subspace {
if table.m == nil {
panic("SetKeyTable() called with nil KeyTable")
panic("WithKeyTable() called with nil KeyTable")
}
if len(s.table.m) != 0 {
panic("SetKeyTable() called on already initialized Subspace")
panic("WithKeyTable() called on already initialized Subspace")
}

for k, v := range table.m {
Expand Down
8 changes: 2 additions & 6 deletions x/slashing/types/params_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ var (
KeySlashFractionDowntime = []byte("SlashFractionDowntime")
)

// ParamKeyTable for slashing module
//
// Deprecated.
// Deprecated: ParamKeyTable for slashing module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// ParamSetPairs - Implements params.ParamSet
//
// Deprecated.
// Deprecated: ParamSetPairs implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeySignedBlocksWindow, &p.SignedBlocksWindow, validateSignedBlocksWindow),
Expand Down