Skip to content

Commit

Permalink
fix upgrade from version without InitChainer
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera committed May 9, 2024
1 parent a3a76f0 commit f1b2d8d
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions app/setup_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package app
import (
"os"

"golang.org/x/exp/slices"

"github.com/cosmos/cosmos-sdk/baseapp"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/consensus"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand Down Expand Up @@ -53,6 +54,16 @@ func SetupHandlers(app *App) {
}
}
baseAppLegacySS := app.ParamsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
needsForcedMigration := []string{
authtypes.ModuleName,
banktypes.ModuleName,
stakingtypes.ModuleName,
distrtypes.ModuleName,
slashingtypes.ModuleName,
govtypes.ModuleName,
crisistypes.ModuleName,
emissionstypes.ModuleName,

Check warning on line 65 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L57-L65

Added lines #L57 - L65 were not covered by tests
}
allUpgrades := upgradeTracker{
upgrades: []upgradeTrackerItem{

Check warning on line 68 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L67-L68

Added lines #L67 - L68 were not covered by tests
{
Expand All @@ -65,8 +76,22 @@ func SetupHandlers(app *App) {
// https://docs.cosmos.network/main/build/migrations/upgrading#xconsensus
baseapp.MigrateParams(ctx, baseAppLegacySS, &app.ConsensusParamsKeeper)

Check warning on line 77 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L77

Added line #L77 was not covered by tests

// skip consensus genesis
app.mm.Modules[consensustypes.ModuleName] = consensus.ConsensusVersion
// empty version map happens when upgrading from old versions which did not correctly call
// app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap()) in InitChainer.
// we must populate the version map if we detect this scenario
//
// this will only happen on the first upgrade. mainnet and testnet will not require this condition.
if len(vm) == 0 {
for m, mb := range app.mm.Modules {
if module, ok := mb.(module.HasConsensusVersion); ok {
if slices.Contains(needsForcedMigration, m) {
vm[m] = module.ConsensusVersion() - 1
} else {
vm[m] = module.ConsensusVersion()

Check warning on line 90 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L84-L90

Added lines #L84 - L90 were not covered by tests
}
}
}
}
return vm, nil

Check warning on line 95 in app/setup_handlers.go

View check run for this annotation

Codecov / codecov/patch

app/setup_handlers.go#L95

Added line #L95 was not covered by tests
},
},
Expand Down

0 comments on commit f1b2d8d

Please sign in to comment.