Skip to content

Commit ca0b8f9

Browse files
authored
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)
1 parent fdf081f commit ca0b8f9

File tree

6 files changed

+91
-35
lines changed

6 files changed

+91
-35
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939

4040
### Features
4141

42+
* (cli) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Add the `tendermint key-migrate` to perform Tendermint v0.35 DB key migration.
43+
44+
### Bug Fixes
45+
46+
* (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations.
47+
48+
## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23
49+
50+
### Features
51+
4252
* (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.
4353
* (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.
4454
* (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`.

server/tm_cmds.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package server
33
// DONTCOVER
44

55
import (
6+
"context"
67
"fmt"
78

89
"github.com/spf13/cobra"
10+
cfg "github.com/tendermint/tendermint/config"
911
pvm "github.com/tendermint/tendermint/privval"
12+
"github.com/tendermint/tendermint/scripts/keymigrate"
13+
"github.com/tendermint/tendermint/scripts/scmigrate"
1014
tversion "github.com/tendermint/tendermint/version"
1115
"sigs.k8s.io/yaml"
1216

@@ -123,3 +127,67 @@ func VersionCmd() *cobra.Command {
123127
},
124128
}
125129
}
130+
131+
// makeKeyMigrateCmd is ported from tendermint's key-migrate command, but
132+
// uses the SDK's own server.Context.
133+
// ref: https://github.com/tendermint/tendermint/blob/master/UPGRADING.md#database-key-format-changes
134+
func makeKeyMigrateCmd() *cobra.Command {
135+
cmd := &cobra.Command{
136+
Use: "key-migrate",
137+
Short: "Run Tendermint database key migration",
138+
RunE: func(cmd *cobra.Command, args []string) error {
139+
ctx, cancel := context.WithCancel(cmd.Context())
140+
defer cancel()
141+
142+
serverCtx := GetServerContextFromCmd(cmd)
143+
config := serverCtx.Config
144+
145+
contexts := []string{
146+
// this is ordered to put the
147+
// (presumably) biggest/most important
148+
// subsets first.
149+
"blockstore",
150+
"state",
151+
"peerstore",
152+
"tx_index",
153+
"evidence",
154+
"light",
155+
}
156+
157+
for idx, dbctx := range contexts {
158+
serverCtx.Logger.Info("beginning a key migration",
159+
"dbctx", dbctx,
160+
"num", idx+1,
161+
"total", len(contexts),
162+
)
163+
164+
db, err := cfg.DefaultDBProvider(&cfg.DBContext{
165+
ID: dbctx,
166+
Config: config,
167+
})
168+
169+
if err != nil {
170+
return fmt.Errorf("constructing database handle: %w", err)
171+
}
172+
173+
if err = keymigrate.Migrate(ctx, db); err != nil {
174+
return fmt.Errorf("running migration for context %q: %w",
175+
dbctx, err)
176+
}
177+
178+
if dbctx == "blockstore" {
179+
if err := scmigrate.Migrate(ctx, db); err != nil {
180+
return fmt.Errorf("running seen commit migration: %w", err)
181+
182+
}
183+
}
184+
}
185+
186+
serverCtx.Logger.Info("completed database migration successfully")
187+
188+
return nil
189+
},
190+
}
191+
192+
return cmd
193+
}

server/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type
283283
tmcmd.ResetAllCmd,
284284
tmcmd.ResetStateCmd,
285285
tmcmd.InspectCmd,
286+
makeKeyMigrateCmd(),
286287
)
287288

288289
startCmd := StartCmd(appCreator, defaultNodeHome)

simapp/app.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,6 @@ func NewSimApp(
328328
// set the governance module account as the authority for conducting upgrades
329329
app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp, authtypes.NewModuleAddress(govtypes.ModuleName).String())
330330

331-
// RegisterUpgradeHandlers is used for registering any on-chain upgrades
332-
app.RegisterUpgradeHandlers()
333-
334331
app.NFTKeeper = nftkeeper.NewKeeper(keys[nftkeeper.StoreKey], appCodec, app.AccountKeeper, app.BankKeeper)
335332

336333
// create evidence keeper with router
@@ -393,6 +390,10 @@ func NewSimApp(
393390
app.ModuleManager.RegisterInvariants(&app.CrisisKeeper)
394391
app.ModuleManager.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
395392

393+
// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
394+
// Make sure it's called after `app.mm` and `app.configurator` are set.
395+
app.RegisterUpgradeHandlers()
396+
396397
// add test gRPC service for testing gRPC queries in isolation
397398
testdata_pulsar.RegisterQueryServer(app.GRPCQueryRouter(), testdata_pulsar.QueryImpl{})
398399

simapp/upgrades.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,7 @@ const UpgradeName = "v045-to-v046"
1717

1818
func (app SimApp) RegisterUpgradeHandlers() {
1919
app.UpgradeKeeper.SetUpgradeHandler(UpgradeName,
20-
func(ctx sdk.Context, plan upgradetypes.Plan, _ module.VersionMap) (module.VersionMap, error) {
21-
// We set fromVersion to 1 to avoid running InitGenesis for modules for
22-
// in-store migrations.
23-
//
24-
// If you wish to skip any module migrations, i.e. they were already migrated
25-
// in an older version, you can use `modulename.AppModule{}.ConsensusVersion()`
26-
// instead of `1` below.
27-
//
28-
// For example:
29-
// "auth": auth.AppModule{}.ConsensusVersion()
30-
fromVM := map[string]uint64{
31-
"auth": 1,
32-
"authz": 1,
33-
"bank": 1,
34-
"capability": 1,
35-
"crisis": 1,
36-
"distribution": 1,
37-
"evidence": 1,
38-
"feegrant": 1,
39-
"gov": 1,
40-
"mint": 1,
41-
"params": 1,
42-
"slashing": 1,
43-
"staking": 1,
44-
"upgrade": 1,
45-
"vesting": 1,
46-
"genutil": 1,
47-
}
48-
20+
func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
4921
return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
5022
})
5123

x/staking/migrations/v046/store.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/cosmos/cosmos-sdk/x/staking/types"
99
)
1010

11-
// MigrateStore performs in-place store migrations from v0.43/v0.44 to v0.45.
11+
// MigrateStore performs in-place store migrations from v0.43/v0.44/v0.45 to v0.46.
1212
// The migration includes:
1313
//
1414
// - Setting the MinCommissionRate param in the paramstore
@@ -19,6 +19,10 @@ func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.Binar
1919
}
2020

2121
func migrateParamsStore(ctx sdk.Context, paramstore paramtypes.Subspace) {
22-
paramstore.WithKeyTable(types.ParamKeyTable())
23-
paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate)
22+
if paramstore.HasKeyTable() {
23+
paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate)
24+
} else {
25+
paramstore.WithKeyTable(types.ParamKeyTable())
26+
paramstore.Set(ctx, types.KeyMinCommissionRate, types.DefaultMinCommissionRate)
27+
}
2428
}

0 commit comments

Comments
 (0)