From f7f181b35192e136f467b1f47f3d94817ff3c64e Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:06:53 +0200 Subject: [PATCH 1/7] fix: Fix v0.45->v0.46 migration --- server/tm_cmds.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++ server/util.go | 1 + 2 files changed, 70 insertions(+) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 0f243e5a8f3..f1039713c0a 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,68 @@ func VersionCmd() *cobra.Command { }, } } + +// MakeKeyMigrateCommand is ported from tendermint's key-migrate command, but +// uses the SDK's own server.Context. +func MakeKeyMigrateCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "key-migrate", + Short: "Run TendermintDatabase key migration", + RunE: func(cmd *cobra.Command, args []string) error { + ctx, cancel := context.WithCancel(cmd.Context()) + defer cancel() + + serverCtx := GetServerContextFromCmd(cmd) + config := serverCtx.Config + fmt.Println(config) + fmt.Println(config.DBDir()) + + 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 ca7d4c63cfd..6271e15b31e 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, + MakeKeyMigrateCommand(), ) startCmd := StartCmd(appCreator, defaultNodeHome) From 543fe08c6d24b1cfb75091fbe124309d3222e88b Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:09:03 +0200 Subject: [PATCH 2/7] Add comment --- server/tm_cmds.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index f1039713c0a..0e1b575cebb 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -130,6 +130,7 @@ func VersionCmd() *cobra.Command { // MakeKeyMigrateCommand 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 MakeKeyMigrateCommand() *cobra.Command { cmd := &cobra.Command{ Use: "key-migrate", From 9884ac57f2b2ee39eae37a1f014dbc066eb7ce6d Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:25:45 +0200 Subject: [PATCH 3/7] Make migration work --- server/tm_cmds.go | 2 -- simapp/app.go | 6 +++--- simapp/upgrades.go | 30 +----------------------------- x/staking/migrations/v046/store.go | 10 +++++++--- 4 files changed, 11 insertions(+), 37 deletions(-) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 0e1b575cebb..a997fc07f1a 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -141,8 +141,6 @@ func MakeKeyMigrateCommand() *cobra.Command { serverCtx := GetServerContextFromCmd(cmd) config := serverCtx.Config - fmt.Println(config) - fmt.Println(config.DBDir()) contexts := []string{ // this is ordered to put the diff --git a/simapp/app.go b/simapp/app.go index cb50d352bdd..6feaf6c6bcb 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,9 @@ 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 + 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 2a62ae5c881..d6bd283402e 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -17,35 +17,7 @@ const UpgradeName = "v045-to-v046" func (app SimApp) RegisterUpgradeHandlers() { app.UpgradeKeeper.SetUpgradeHandler(UpgradeName, - 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, - } - + func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { return app.mm.RunMigrations(ctx, app.configurator, fromVM) }) diff --git a/x/staking/migrations/v046/store.go b/x/staking/migrations/v046/store.go index 20825ec2602..e2be22cbb1f 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 fbb5babc725ddc461c3b6454b2d29175348b1700 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:34:34 +0200 Subject: [PATCH 4/7] changelog --- CHANGELOG.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a5ddac6ac2..065c0768f06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,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`. From 3146574b9f886d687994e7bdf730f2ee2ad0e348 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:35:37 +0200 Subject: [PATCH 5/7] Add comment --- simapp/app.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simapp/app.go b/simapp/app.go index 6feaf6c6bcb..e3bc604dd00 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -405,7 +405,8 @@ 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 + // 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 From 8a8b57b257dc62aa793f06334af5927aa92c8a84 Mon Sep 17 00:00:00 2001 From: Amaury M <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:38:52 +0200 Subject: [PATCH 6/7] private function --- server/tm_cmds.go | 4 ++-- server/util.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index a997fc07f1a..4f528dc2fc7 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -128,10 +128,10 @@ func VersionCmd() *cobra.Command { } } -// MakeKeyMigrateCommand is ported from tendermint's key-migrate command, but +// 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 MakeKeyMigrateCommand() *cobra.Command { +func makeKeyMigrateCmd() *cobra.Command { cmd := &cobra.Command{ Use: "key-migrate", Short: "Run TendermintDatabase key migration", diff --git a/server/util.go b/server/util.go index 6271e15b31e..bea9e16bfc9 100644 --- a/server/util.go +++ b/server/util.go @@ -283,7 +283,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type tmcmd.ResetAllCmd, tmcmd.ResetStateCmd, tmcmd.InspectCmd, - MakeKeyMigrateCommand(), + makeKeyMigrateCmd(), ) startCmd := StartCmd(appCreator, defaultNodeHome) From 73dfe7985deb6c31dbb9824e86dffa440ad6c9da Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Tue, 24 May 2022 16:46:25 +0200 Subject: [PATCH 7/7] Update server/tm_cmds.go --- server/tm_cmds.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/tm_cmds.go b/server/tm_cmds.go index 4f528dc2fc7..6cc63850ff7 100644 --- a/server/tm_cmds.go +++ b/server/tm_cmds.go @@ -134,7 +134,7 @@ func VersionCmd() *cobra.Command { func makeKeyMigrateCmd() *cobra.Command { cmd := &cobra.Command{ Use: "key-migrate", - Short: "Run TendermintDatabase key migration", + Short: "Run Tendermint database key migration", RunE: func(cmd *cobra.Command, args []string) error { ctx, cancel := context.WithCancel(cmd.Context()) defer cancel()