x/upgrade: added consensus version tracking (part of ADR-041) (#8743)
* -added consensus version tracking to x/upgrade * -added interface to module manager -added e2e test for migrations using consensus version store in x/upgrade -cleaned up x/upgrade Keeper -handler in apply upgrade now handles errors and setting consensus versions -cleaned up migration map keys -removed init chainer method -simapp now implements GetConsensusVersions to assist with testing * Changed MigrationMap identifier to VersionMap removed module_test * updated docs * forgot this * added line to changelog for this PR * Change set consensus version function to match adr 041 spec * add documentation * remove newline from changelog unnecessary newline removed * updated example in simapp for RunMigrations, SetCurrentConsensusVersions now returns an error * switch TestMigrations to use Require instead of t.Fatal * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * docs for SetVersionManager * -init genesis method added -removed panics/fails from setting consensus versions * update identifiers to be more go-like * update docs and UpgradeHandler fnc sig * Upgrade Keeper now takes a VersionMap instead of a VersionManager interface * upgrade keeper transition to Version Map * cleanup, added versionmap return to RunMigrations * quick fix * Update docs/architecture/adr-041-in-place-store-migrations.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * remove support for versionmap field on upgrade keeper * cleanup * rename get/set version map keeper functions * update adr doc to match name changes * remove redudant line Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Aaron Craelius
Amaury
technicallyty
mergify[bot]
parent
129267cc92
commit
5bd93bfe7b
@@ -82,48 +82,34 @@ Each module's migration functions are specific to the module's store evolutions,
|
||||
We introduce a new prefix store in `x/upgrade`'s store. This store will track each module's current version, it can be modelized as a `map[string]uint64` of module name to module ConsensusVersion, and will be used when running the migrations (see next section for details). The key prefix used is `0x1`, and the key/value format is:
|
||||
|
||||
```
|
||||
0x2 | {bytes(module_name)} => LittleEndian(module_consensus_version)
|
||||
0x2 | {bytes(module_name)} => BigEndian(module_consensus_version)
|
||||
```
|
||||
The initial state of the store is set from `app.go`'s `InitChainer` method.
|
||||
|
||||
s
|
||||
We add a new private field `versionManager` of type `VersionManager` to `x/upgrade`'s keeper, where `VersionManager` is:
|
||||
|
||||
```go
|
||||
type VersionManager interface {
|
||||
GetConsensusVersions() VersionMap
|
||||
}
|
||||
|
||||
// Map of module name => new module Consensus Version.
|
||||
type VersionMap map[string]uint64
|
||||
```
|
||||
|
||||
This `versionManager` field can be modified via the `SetVersionManager` field, and will allow the upgrade keeper to know the current versions of loaded modules. `SetVersionManager` MUST be called as early as possible in the app initialization; in the SDK's `simapp`, it is called in the `NewSimApp` constructor function.
|
||||
|
||||
The UpgradeHandler signature needs to be updated to take a `VersionMap`, as well as return an error:
|
||||
The UpgradeHandler signature needs to be updated to take a `VersionMap`, as well as return an upgraded `VersionMap` and an error:
|
||||
|
||||
```diff
|
||||
- type UpgradeHandler func(ctx sdk.Context, plan Plan)
|
||||
+ type UpgradeHandler func(ctx sdk.Context, plan Plan, versionMap VersionMap) error
|
||||
+ type UpgradeHandler func(ctx sdk.Context, plan Plan, versionMap VersionMap) (VersionMap, error)
|
||||
```
|
||||
|
||||
To apply an upgrade, we query the `VersionMap` from the `x/upgrade` store and pass it into the handler. The handler runs the actual migration functions (see next section), and if successful, the current ConsensusVersions of all loaded modules will be stored into state.
|
||||
To apply an upgrade, we query the `VersionMap` from the `x/upgrade` store and pass it into the handler. The handler runs the actual migration functions (see next section), and if successful, returns an updated `VersionMap` to be stored in state.
|
||||
|
||||
```diff
|
||||
func (k UpgradeKeeper) ApplyUpgrade(ctx sdk.Context, plan types.Plan) {
|
||||
// --snip--
|
||||
- handler(ctx, plan)
|
||||
+ err := handler(ctx, plan, k.GetConsensusVersions()) // k.GetConsensusVersions() fetches the VersionMap stored in state.
|
||||
+ updatedVM, err := handler(ctx, plan, k.GetModuleVersionMap(ctx)) // k.GetModuleVersionMap() fetches the VersionMap stored in state.
|
||||
+ if err != nil {
|
||||
+ return err
|
||||
+ }
|
||||
+
|
||||
+ // Get the current ConsensusVersions of the loaded modules (retrieved from
|
||||
+ // `k.versionManager`), and save them to state.
|
||||
+ k.SetCurrentConsensusVersions()
|
||||
+ // Set the updated consensus versions to state
|
||||
+ k.SetModuleVersionMap(ctx, updatedVM)
|
||||
}
|
||||
```
|
||||
|
||||
An gRPC query endpoint to query the `VersionMap` stored in `x/upgrade`'s state will also be added, so that app developers can double-check the `VersionMap` before the upgrade handler runs.
|
||||
A gRPC query endpoint to query the `VersionMap` stored in `x/upgrade`'s state will also be added, so that app developers can double-check the `VersionMap` before the upgrade handler runs.
|
||||
|
||||
### Running Migrations
|
||||
|
||||
@@ -139,8 +125,8 @@ If a required migration is missing (e.g. if it has not been registered in the `C
|
||||
In practice, the `RunMigrations` method should be called from inside an `UpgradeHandler`.
|
||||
|
||||
```go
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, versionMap VersionMap) error {
|
||||
return app.mm.RunMigrations(ctx, versionMap)
|
||||
app.UpgradeKeeper.SetUpgradeHandler("my-plan", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
|
||||
return app.mm.RunMigrations(ctx, vm)
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user