From ecf61d32edec775215ebd5145f9c15a6ab596b86 Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Mon, 30 May 2022 10:17:21 +0200 Subject: [PATCH 1/3] fix: Fix v0.45->v0.46 migration (#12028) ## Description closes #12027 - Add `tendermint key-migrate` subcommand - Fix in-place store migrations Test: - on v0.45 node, make a proposal to update software, make it pass - wait for node to halt - on v0.46 binary, run `simd tendermint key-migrate` - on v0.46 binary, run `simd start --mode validator` - make sure the v0.46 node runs correctly --- ### 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... - [ ] 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/main/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/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/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) (cherry picked from commit ca0b8f96b94135ae5692e6b3bccd8fac9339072e) # Conflicts: # simapp/upgrades.go --- CHANGELOG.md | 10 +++++ server/tm_cmds.go | 68 ++++++++++++++++++++++++++++++ server/util.go | 1 + simapp/app.go | 7 +-- simapp/upgrades.go | 5 +++ x/staking/migrations/v046/store.go | 10 +++-- 6 files changed, 95 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81085858f8bd..49dad77fb830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,16 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features +* (cli) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Add the `tendermint key-migrate` to perform Tendermint v0.35 DB key migration. + +### Bug Fixes + +* (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations. + +## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 + +### Features + * (types) [#11985](https://github.com/cosmos/cosmos-sdk/pull/11985) Add a `Priority` field on `sdk.Context`, which represents the CheckTx priority field. It is only used during CheckTx. * (gRPC) [#11889](https://github.com/cosmos/cosmos-sdk/pull/11889) Support custom read and write gRPC options in `app.toml`. See `max-recv-msg-size` and `max-send-msg-size` respectively. * (cli) [\#11738](https://github.com/cosmos/cosmos-sdk/pull/11738) Add `tx auth multi-sign` as alias of `tx auth multisign` for consistency with `multi-send`. diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 0f243e5a8f32..6cc63850ff74 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -3,10 +3,14 @@ package server // DONTCOVER import ( + "context" "fmt" "github.com/spf13/cobra" + cfg "github.com/tendermint/tendermint/config" pvm "github.com/tendermint/tendermint/privval" + "github.com/tendermint/tendermint/scripts/keymigrate" + "github.com/tendermint/tendermint/scripts/scmigrate" tversion "github.com/tendermint/tendermint/version" "sigs.k8s.io/yaml" @@ -123,3 +127,67 @@ func VersionCmd() *cobra.Command { }, } } + +// makeKeyMigrateCmd is ported from tendermint's key-migrate command, but +// uses the SDK's own server.Context. +// ref: https://github.com/tendermint/tendermint/blob/master/UPGRADING.md#database-key-format-changes +func makeKeyMigrateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "key-migrate", + Short: "Run Tendermint database key migration", + RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := context.WithCancel(cmd.Context()) + defer cancel() + + serverCtx := GetServerContextFromCmd(cmd) + config := serverCtx.Config + + contexts := []string{ + // this is ordered to put the + // (presumably) biggest/most important + // subsets first. + "blockstore", + "state", + "peerstore", + "tx_index", + "evidence", + "light", + } + + for idx, dbctx := range contexts { + serverCtx.Logger.Info("beginning a key migration", + "dbctx", dbctx, + "num", idx+1, + "total", len(contexts), + ) + + db, err := cfg.DefaultDBProvider(&cfg.DBContext{ + ID: dbctx, + Config: config, + }) + + if err != nil { + return fmt.Errorf("constructing database handle: %w", err) + } + + if err = keymigrate.Migrate(ctx, db); err != nil { + return fmt.Errorf("running migration for context %q: %w", + dbctx, err) + } + + if dbctx == "blockstore" { + if err := scmigrate.Migrate(ctx, db); err != nil { + return fmt.Errorf("running seen commit migration: %w", err) + + } + } + } + + serverCtx.Logger.Info("completed database migration successfully") + + return nil + }, + } + + return cmd +} diff --git a/server/util.go b/server/util.go index ca7d4c63cfd8..bea9e16bfc91 100644 --- a/server/util.go +++ b/server/util.go @@ -283,6 +283,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type tmcmd.ResetAllCmd, tmcmd.ResetStateCmd, tmcmd.InspectCmd, + makeKeyMigrateCmd(), ) startCmd := StartCmd(appCreator, defaultNodeHome) diff --git a/simapp/app.go b/simapp/app.go index cb50d352bdd4..e3bc604dd00b 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -321,9 +321,6 @@ func NewSimApp( // set the governance module account as the authority for conducting upgrades app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp, authtypes.NewModuleAddress(govtypes.ModuleName).String()) - // RegisterUpgradeHandlers is used for registering any on-chain upgrades - app.RegisterUpgradeHandlers() - app.NFTKeeper = nftkeeper.NewKeeper(keys[nftkeeper.StoreKey], appCodec, app.AccountKeeper, app.BankKeeper) // create evidence keeper with router @@ -408,6 +405,10 @@ func NewSimApp( app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) app.mm.RegisterServices(app.configurator) + // RegisterUpgradeHandlers is used for registering any on-chain upgrades. + // Make sure it's called after `app.mm` and `app.configurator` are set. + app.RegisterUpgradeHandlers() + // add test gRPC service for testing gRPC queries in isolation testdata_pulsar.RegisterQueryServer(app.GRPCQueryRouter(), testdata_pulsar.QueryImpl{}) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index 2a62ae5c881c..bcdc087665a0 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -17,6 +17,7 @@ const UpgradeName = "v045-to-v046" func (app SimApp) RegisterUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler(UpgradeName, +<<<<<<< HEAD func(ctx sdk.Context, plan upgradetypes.Plan, _ module.VersionMap) (module.VersionMap, error) { // We set fromVersion to 1 to avoid running InitGenesis for modules for // in-store migrations. @@ -47,6 +48,10 @@ func (app SimApp) RegisterUpgradeHandlers() { } return app.mm.RunMigrations(ctx, app.configurator, fromVM) +======= + func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { + return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) +>>>>>>> ca0b8f96b (fix: Fix v0.45->v0.46 migration (#12028)) }) upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() diff --git a/x/staking/migrations/v046/store.go b/x/staking/migrations/v046/store.go index 20825ec26029..e2be22cbb1ff 100644 --- a/x/staking/migrations/v046/store.go +++ b/x/staking/migrations/v046/store.go @@ -8,7 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/staking/types" ) -// MigrateStore performs in-place store migrations from v0.43/v0.44 to v0.45. +// MigrateStore performs in-place store migrations from v0.43/v0.44/v0.45 to v0.46. // The migration includes: // // - Setting the MinCommissionRate param in the paramstore @@ -19,6 +19,10 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Binar } func migrateParamsStore(ctx sdk.Context, paramstore paramtypes.Subspace) { - paramstore.WithKeyTable(types.ParamKeyTable()) - paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate) + if paramstore.HasKeyTable() { + paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate) + } else { + paramstore.WithKeyTable(types.ParamKeyTable()) + paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate) + } } From 79aa5fbe77813f1552f6707b3921d1862da533a6 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 30 May 2022 10:24:14 +0200 Subject: [PATCH 2/3] fix conflicts --- simapp/upgrades.go | 35 +---------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/simapp/upgrades.go b/simapp/upgrades.go index bcdc087665a0..d6bd283402e6 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -17,41 +17,8 @@ const UpgradeName = "v045-to-v046" func (app SimApp) RegisterUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler(UpgradeName, -<<<<<<< HEAD - func(ctx sdk.Context, plan upgradetypes.Plan, _ module.VersionMap) (module.VersionMap, error) { - // We set fromVersion to 1 to avoid running InitGenesis for modules for - // in-store migrations. - // - // If you wish to skip any module migrations, i.e. they were already migrated - // in an older version, you can use `modulename.AppModule{}.ConsensusVersion()` - // instead of `1` below. - // - // For example: - // "auth": auth.AppModule{}.ConsensusVersion() - fromVM := map[string]uint64{ - "auth": 1, - "authz": 1, - "bank": 1, - "capability": 1, - "crisis": 1, - "distribution": 1, - "evidence": 1, - "feegrant": 1, - "gov": 1, - "mint": 1, - "params": 1, - "slashing": 1, - "staking": 1, - "upgrade": 1, - "vesting": 1, - "genutil": 1, - } - - return app.mm.RunMigrations(ctx, app.configurator, fromVM) -======= func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { - return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) ->>>>>>> ca0b8f96b (fix: Fix v0.45->v0.46 migration (#12028)) + return app.mm.RunMigrations(ctx, app.configurator, fromVM) }) upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() From 25c8721695280e42ef70bd97a8955155236279b4 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Mon, 30 May 2022 10:25:57 +0200 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49dad77fb830..065c0768f06c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,8 +37,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] -## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23 - ### Features * (cli) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Add the `tendermint key-migrate` to perform Tendermint v0.35 DB key migration.