Upgrade handlers enable coordinated on-chain upgrades across all validators at specific block heights via governance proposals. They provide a mechanism for chains to perform data migrations, parameter updates, and module upgrades in a deterministic way.
Upgrade handlers are critical for maintaining consensus during chain upgrades. All validators must run the same upgrade logic at the same height.
The upgrade handler receives and returns a module.VersionMap that tracks module versions:
Copy
Ask AI
// fromVM contains the module versions before the upgrade// The returned VersionMap contains the new versions after migrationmigrations, err := app.ModuleManager.RunMigrations(ctx, app.configurator, fromVM)
func migrateParams(ctx sdk.Context, keeper paramskeeper.Keeper) error { // Get old params var oldParams v1.Params keeper.GetParamSet(ctx, &oldParams) // Convert to new format newParams := v2.Params{ Field1: oldParams.Field1, Field2: convertField(oldParams.Field2), // New field with default value Field3: "default", } // Set new params keeper.SetParams(ctx, newParams) return nil}