feat: support in-place migration ordering (#10614)
## Description Closes: #10604 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
+44
-40
@@ -22,27 +22,13 @@ This document provides steps to use the In-Place Store Migrations upgrade method
|
||||
|
||||
Each module gets assigned a consensus version by the module developer. The consensus version serves as the breaking change version of the module. The Cosmos SDK keeps track of all module consensus versions in the x/upgrade `VersionMap` store. During an upgrade, the difference between the old `VersionMap` stored in state and the new `VersionMap` is calculated by the Cosmos SDK. For each identified difference, the module-specific migrations are run and the respective consensus version of each upgraded module is incremented.
|
||||
|
||||
## Genesis State
|
||||
|
||||
When starting a new chain, the consensus version of each module must be saved to state during the application's genesis. To save the consensus version, add the following line to the `InitChainer` method in `app.go`:
|
||||
|
||||
```diff
|
||||
func (app *MyApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
||||
...
|
||||
+ app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
This information is used by the Cosmos SDK to detect when modules with newer versions are introduced to the app.
|
||||
|
||||
### Consensus Version
|
||||
|
||||
The consensus version is defined on each app module by the module developer and serves as the breaking change version of the module. The consensus version informs the Cosmos SDK on which modules need to be upgraded. For example, if the bank module was version 2 and an upgrade introduces bank module 3, the Cosmos SDK upgrades the bank module and runs the "version 2 to 3" migration script.
|
||||
|
||||
### Version Map
|
||||
|
||||
The version map is a mapping of module names to consensus versions. The map is persisted to x/upgrade's state for use during in-place migrations. When migrations finish, the updated version map is persisted to state.
|
||||
The version map is a mapping of module names to consensus versions. The map is persisted to x/upgrade's state for use during in-place migrations. When migrations finish, the updated version map is persisted in the state.
|
||||
|
||||
## Upgrade Handlers
|
||||
|
||||
@@ -60,24 +46,29 @@ Inside these functions, you must perform any upgrade logic to include in the pro
|
||||
|
||||
## Running Migrations
|
||||
|
||||
Migrations are run inside of an `UpgradeHandler` using `app.mm.RunMigrations(ctx, cfg, vm)`. The `UpgradeHandler` functions describe the functionality to occur during an upgrade. The `RunMigration` function loops through the `VersionMap` argument and runs the migration scripts for all versions that are less than the versions of the new binary app module. After the migrations are finished, a new `VersionMap` is returned to persist the upgraded module versions to state.
|
||||
Migrations are run inside of an `UpgradeHandler` using `app.mm.RunMigrations(ctx, cfg, vm)`. The `UpgradeHandler` functions describe the functionality to occur during an upgrade. The `RunMigration` function loops through the `VersionMap` argument and runs the migration scripts for all versions that are less than the versions of the new binary app module. After the migrations are finished, a new `VersionMap` is returned to persist the upgraded module versions to state.
|
||||
|
||||
```go
|
||||
cfg := module.NewConfigurator(...)
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
|
||||
|
||||
// ...
|
||||
// do upgrade logic
|
||||
// additional upgrade logic
|
||||
// ...
|
||||
|
||||
// RunMigrations returns the VersionMap
|
||||
// with the updated module ConsensusVersions
|
||||
return app.mm.RunMigrations(ctx, vm)
|
||||
// returns a VersionMap with the updated module ConsensusVersions
|
||||
return app.mm.RunMigrations(ctx, fromVM)
|
||||
})
|
||||
```
|
||||
|
||||
To learn more about configuring migration scripts for your modules, see the [Module Upgrade Guide](../building-modules/upgrade.md).
|
||||
|
||||
### Order Of Migrations
|
||||
|
||||
By default, all migrations are run in module name alphabetical ascending order, except `x/auth` which is run last. The reason is state dependencies between x/auth and other modules (you can read more in [issue #10606](https://github.com/cosmos/cosmos-sdk/issues/10606)).
|
||||
|
||||
If you want to change the order of migration then you should call `app.mm.SetOrderMigrations(module1, module2, ...)` in your app.go file. The function will panic if you forget to include a module in the argument list.
|
||||
|
||||
## Adding New Modules During Upgrades
|
||||
|
||||
You can introduce entirely new modules to the application during an upgrade. New modules are recognized because they have not yet been registered in `x/upgrade`'s `VersionMap` store. In this case, `RunMigrations` calls the `InitGenesis` function from the corresponding module to set up its initial state.
|
||||
@@ -105,7 +96,35 @@ if upgradeInfo.Name == "my-plan" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.
|
||||
}
|
||||
```
|
||||
|
||||
## Overwriting Genesis Functions
|
||||
## Genesis State
|
||||
|
||||
When starting a new chain, the consensus version of each module MUST be saved to state during the application's genesis. To save the consensus version, add the following line to the `InitChainer` method in `app.go`:
|
||||
|
||||
```diff
|
||||
func (app *MyApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
||||
...
|
||||
+ app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
This information is used by the Cosmos SDK to detect when modules with newer versions are introduced to the app.
|
||||
|
||||
For a new module `foo`, `InitGenesis` is called by `RunMigration` only when `foo` is registered in the module manager but it's not set in the `fromVM`. Therefore, if you want to skip `InitGenesis` when a new module is added to the app, then you should set its module version in `fromVM` to the module consensus version:
|
||||
|
||||
```go
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
|
||||
// ...
|
||||
|
||||
// Set foo's version to the latest ConsensusVersion in the VersionMap.
|
||||
// This will skip running InitGenesis on Foo
|
||||
fromVM[foo.ModuleName] = foo.AppModule{}.ConsensusVersion()
|
||||
|
||||
return app.mm.RunMigrations(ctx, fromVM)
|
||||
})
|
||||
```
|
||||
|
||||
### Overwriting Genesis Functions
|
||||
|
||||
The Cosmos SDK offers modules that the application developer can import in their app. These modules often have an `InitGenesis` function already defined.
|
||||
|
||||
@@ -118,32 +137,17 @@ You MUST manually set the consensus version in the version map passed to the `Up
|
||||
```go
|
||||
import foo "github.com/my/module/foo"
|
||||
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
|
||||
|
||||
// Register the consensus version in the version map
|
||||
// to avoid the SDK from triggering the default
|
||||
// InitGenesis function.
|
||||
vm["foo"] = foo.AppModule{}.ConsensusVersion()
|
||||
fromVM["foo"] = foo.AppModule{}.ConsensusVersion()
|
||||
|
||||
// Run custom InitGenesis for foo
|
||||
app.mm["foo"].InitGenesis(ctx, app.appCodec, myCustomGenesisState)
|
||||
|
||||
return app.mm.RunMigrations(ctx, cfg, vm)
|
||||
})
|
||||
```
|
||||
|
||||
If you do not have a custom genesis function and want to skip the module's default genesis function, you can simply register the module with the version map in the `UpgradeHandler` as shown in the example:
|
||||
|
||||
```go
|
||||
import foo "github.com/my/module/foo"
|
||||
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
|
||||
|
||||
// Set foo's version to the latest ConsensusVersion in the VersionMap.
|
||||
// This will skip running InitGenesis on Foo
|
||||
vm["foo"] = foo.AppModule{}.ConsensusVersion()
|
||||
|
||||
return app.mm.RunMigrations(ctx, cfg, vm)
|
||||
return app.mm.RunMigrations(ctx, cfg, fromVM)
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user