Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(x/upgrade): migrate to appmodule.VersionMap #20485

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions simapp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ It is an exautive list of changes that completes the SimApp section in the [UPGR
Always refer to the [UPGRADING.md](https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md) to understand the changes.

* [#20409](https://github.com/cosmos/cosmos-sdk/pull/20409) Add `tx` as `SkipStoreKeys` in `app_config.go`.
* [#20485](https://github.com/cosmos/cosmos-sdk/pull/20485) The signature of `x/upgrade/types.UpgradeHandler` has changed to accept `appmodule.VersionMap` from `module.VersionMap`. These types are interchangeable, but usages of `UpradeKeeper.SetUpgradeHandler` may need to adjust their usages to match the new signature.

<!-- TODO: move changelog.md elements to here -->

Expand Down
4 changes: 2 additions & 2 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestRunMigrations(t *testing.T) {
// their latest ConsensusVersion.
_, err = app.ModuleManager.RunMigrations(
app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight()}), configurator,
module.VersionMap{
appmodule.VersionMap{
"accounts": accounts.AppModule{}.ConsensusVersion(),
"bank": 1,
"auth": auth.AppModule{}.ConsensusVersion(),
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestInitGenesisOnMigration(t *testing.T) {
// Run migrations only for "mock" module. We exclude it from
// the VersionMap to simulate upgrading with a new module.
_, err := app.ModuleManager.RunMigrations(ctx, app.Configurator(),
module.VersionMap{
appmodule.VersionMap{
"bank": bank.AppModule{}.ConsensusVersion(),
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authzmodule.AppModule{}.ConsensusVersion(),
Expand Down
4 changes: 2 additions & 2 deletions simapp/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package simapp
import (
"context"

"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/accounts"
epochstypes "cosmossdk.io/x/epochs/types"
protocolpooltypes "cosmossdk.io/x/protocolpool/types"
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/types/module"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
)

Expand All @@ -24,7 +24,7 @@ const UpgradeName = "v050-to-v051"
func (app SimApp) RegisterUpgradeHandlers() {
app.UpgradeKeeper.SetUpgradeHandler(
UpgradeName,
func(ctx context.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
func(ctx context.Context, _ upgradetypes.Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a changelog under simapp/CHANGELOD.md, as this wiring changes needs to happen for users as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I also make a CHANGELOG entry in x/upgrade? It's not breaking except for usages of the function delegate UpgradeHandler. In other words module.VersionMap is interchangeable with appmodule.VersionMap but function type with each of those as parameters is not.

return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
},
)
Expand Down
8 changes: 4 additions & 4 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []
// })
//
// Please also refer to https://docs.cosmos.network/main/core/upgrade for more information.
func (m Manager) RunMigrations(ctx context.Context, cfg Configurator, fromVM VersionMap) (VersionMap, error) {
func (m Manager) RunMigrations(ctx context.Context, cfg Configurator, fromVM appmodule.VersionMap) (appmodule.VersionMap, error) {
c, ok := cfg.(*configurator)
if !ok {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", &configurator{}, cfg)
Expand All @@ -668,7 +668,7 @@ func (m Manager) RunMigrations(ctx context.Context, cfg Configurator, fromVM Ver
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
updatedVM := VersionMap{}
updatedVM := appmodule.VersionMap{}
for _, moduleName := range modules {
module := m.Modules[moduleName]
fromVersion, exists := fromVM[moduleName]
Expand Down Expand Up @@ -824,8 +824,8 @@ func (m *Manager) PrepareCheckState(ctx sdk.Context) error {
}

// GetVersionMap gets consensus version from all modules
func (m *Manager) GetVersionMap() VersionMap {
vermap := make(VersionMap)
func (m *Manager) GetVersionMap() appmodule.VersionMap {
vermap := make(appmodule.VersionMap)
for name, v := range m.Modules {
version := uint64(0)
if v, ok := v.(appmodule.HasConsensusVersion); ok {
Expand Down
13 changes: 6 additions & 7 deletions x/upgrade/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

Expand All @@ -52,7 +51,7 @@ func (s *TestSuite) VerifyDoUpgrade(t *testing.T) {
require.ErrorContains(t, err, "UPGRADE \"test\" NEEDED at height: 11: ")

t.Log("Verify that the upgrade can be successfully applied with a handler")
s.keeper.SetUpgradeHandler("test", func(ctx context.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.keeper.SetUpgradeHandler("test", func(ctx context.Context, plan types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand All @@ -70,7 +69,7 @@ func (s *TestSuite) VerifyDoUpgradeWithCtx(t *testing.T, newCtx sdk.Context, pro
require.ErrorContains(t, err, "UPGRADE \""+proposalName+"\" NEEDED at height: ")

t.Log("Verify that the upgrade can be successfully applied with a handler")
s.keeper.SetUpgradeHandler(proposalName, func(ctx context.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.keeper.SetUpgradeHandler(proposalName, func(ctx context.Context, plan types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand Down Expand Up @@ -175,7 +174,7 @@ func TestHaltIfTooNew(t *testing.T) {
s := setupTest(t, 10, map[int64]bool{})
t.Log("Verify that we don't panic with registered plan not in database at all")
var called int
s.keeper.SetUpgradeHandler("future", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.keeper.SetUpgradeHandler("future", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
called++
return vm, nil
})
Expand Down Expand Up @@ -421,7 +420,7 @@ func TestBinaryVersion(t *testing.T) {
{
"test not panic: upgrade handler is present for last applied upgrade",
func() sdk.Context {
s.keeper.SetUpgradeHandler("test0", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.keeper.SetUpgradeHandler("test0", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand Down Expand Up @@ -472,7 +471,7 @@ func TestDowngradeVerification(t *testing.T) {
s.ctx = s.ctx.WithHeaderInfo(header.Info{Height: s.ctx.HeaderInfo().Height + 1})

// set the handler.
s.keeper.SetUpgradeHandler(planName, func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.keeper.SetUpgradeHandler(planName, func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand All @@ -487,7 +486,7 @@ func TestDowngradeVerification(t *testing.T) {
}{
"valid binary": {
preRun: func(k *keeper.Keeper, ctx sdk.Context, name string) {
k.SetUpgradeHandler(planName, func(ctx context.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
k.SetUpgradeHandler(planName, func(ctx context.Context, plan types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})
},
Expand Down
6 changes: 3 additions & 3 deletions x/upgrade/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/suite"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/header"
"cosmossdk.io/core/log"
storetypes "cosmossdk.io/store/types"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

Expand All @@ -48,7 +48,7 @@ func (suite *UpgradeTestSuite) SetupTest() {
suite.Require().NoError(err)
suite.encodedAuthority = authority
suite.upgradeKeeper = keeper.NewKeeper(env, skipUpgradeHeights, suite.encCfg.Codec, suite.T().TempDir(), nil, authority)
err = suite.upgradeKeeper.SetModuleVersionMap(suite.ctx, module.VersionMap{
err = suite.upgradeKeeper.SetModuleVersionMap(suite.ctx, appmodule.VersionMap{
"bank": 0,
})
suite.Require().NoError(err)
Expand Down Expand Up @@ -136,7 +136,7 @@ func (suite *UpgradeTestSuite) TestAppliedCurrentPlan() {
err := suite.upgradeKeeper.ScheduleUpgrade(suite.ctx, plan)
suite.Require().NoError(err)
suite.ctx = suite.ctx.WithHeaderInfo(header.Info{Height: expHeight})
suite.upgradeKeeper.SetUpgradeHandler(planName, func(ctx context.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
suite.upgradeKeeper.SetUpgradeHandler(planName, func(ctx context.Context, plan types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})
err = suite.upgradeKeeper.ApplyUpgrade(suite.ctx, plan)
Expand Down
13 changes: 6 additions & 7 deletions x/upgrade/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cosmos/cosmos-sdk/telemetry"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/kv"
"github.com/cosmos/cosmos-sdk/types/module"
)

type Keeper struct {
Expand All @@ -39,7 +38,7 @@ type Keeper struct {
versionModifier app.VersionModifier // implements setting the protocol version field on BaseApp
downgradeVerified bool // tells if we've already sanity checked that this binary version isn't being used against an old state.
authority string // the address capable of executing and canceling an upgrade. Usually the gov module account
initVersionMap module.VersionMap // the module version map at init genesis
initVersionMap appmodule.VersionMap // the module version map at init genesis
}

// NewKeeper constructs an upgrade Keeper which requires the following arguments:
Expand Down Expand Up @@ -75,13 +74,13 @@ func NewKeeper(

// SetInitVersionMap sets the initial version map.
// This is only used in app wiring and should not be used in any other context.
func (k *Keeper) SetInitVersionMap(vm module.VersionMap) {
func (k *Keeper) SetInitVersionMap(vm appmodule.VersionMap) {
k.initVersionMap = vm
}

// GetInitVersionMap gets the initial version map
// This is only used in upgrade InitGenesis and should not be used in any other context.
func (k *Keeper) GetInitVersionMap() module.VersionMap {
func (k *Keeper) GetInitVersionMap() appmodule.VersionMap {
return k.initVersionMap
}

Expand All @@ -93,7 +92,7 @@ func (k Keeper) SetUpgradeHandler(name string, upgradeHandler types.UpgradeHandl
}

// SetModuleVersionMap saves a given version map to state
func (k Keeper) SetModuleVersionMap(ctx context.Context, vm module.VersionMap) error {
func (k Keeper) SetModuleVersionMap(ctx context.Context, vm appmodule.VersionMap) error {
if len(vm) > 0 {
store := runtime.KVStoreAdapter(k.KVStoreService.OpenKVStore(ctx))
versionStore := prefix.NewStore(store, []byte{types.VersionMapByte})
Expand Down Expand Up @@ -121,7 +120,7 @@ func (k Keeper) SetModuleVersionMap(ctx context.Context, vm module.VersionMap) e

// GetModuleVersionMap returns a map of key module name and value module consensus version
// as defined in ADR-041.
func (k Keeper) GetModuleVersionMap(ctx context.Context) (module.VersionMap, error) {
func (k Keeper) GetModuleVersionMap(ctx context.Context) (appmodule.VersionMap, error) {
store := k.KVStoreService.OpenKVStore(ctx)
prefix := []byte{types.VersionMapByte}
it, err := store.Iterator(prefix, storetypes.PrefixEndBytes(prefix))
Expand All @@ -130,7 +129,7 @@ func (k Keeper) GetModuleVersionMap(ctx context.Context) (module.VersionMap, err
}
defer it.Close()

vm := make(module.VersionMap)
vm := make(appmodule.VersionMap)
for ; it.Valid(); it.Next() {
moduleBytes := it.Key()
// first byte is prefix key, so we remove it here
Expand Down
20 changes: 11 additions & 9 deletions x/upgrade/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cmttypes "github.com/cometbft/cometbft/types"
"github.com/stretchr/testify/suite"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/header"
"cosmossdk.io/core/log"
storetypes "cosmossdk.io/store/types"
Expand All @@ -24,7 +25,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

Expand Down Expand Up @@ -167,7 +167,7 @@ func (s *KeeperTestSuite) TestScheduleUpgrade() {
Height: 123450000,
},
setup: func() {
s.upgradeKeeper.SetUpgradeHandler("all-good", func(ctx context.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.upgradeKeeper.SetUpgradeHandler("all-good", func(ctx context.Context, plan types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})
err := s.upgradeKeeper.ApplyUpgrade(s.ctx, types.Plan{
Expand Down Expand Up @@ -289,7 +289,9 @@ func (s *KeeperTestSuite) TestIncrementProtocolVersion() {
err = s.upgradeKeeper.ApplyUpgrade(s.ctx, dummyPlan)
s.Require().EqualError(err, "ApplyUpgrade should never be called without first checking HasHandler")

s.upgradeKeeper.SetUpgradeHandler("dummy", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) { return vm, nil })
s.upgradeKeeper.SetUpgradeHandler("dummy", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})
err = s.upgradeKeeper.ApplyUpgrade(s.ctx, dummyPlan)
s.Require().NoError(err)
upgradedProtocolVersion, err := s.baseApp.AppVersion(s.ctx)
Expand All @@ -301,13 +303,13 @@ func (s *KeeperTestSuite) TestIncrementProtocolVersion() {
// Tests that the underlying state of x/upgrade is set correctly after
// an upgrade.
func (s *KeeperTestSuite) TestMigrations() {
initialVM := module.VersionMap{"bank": uint64(1)}
initialVM := appmodule.VersionMap{"bank": uint64(1)}
err := s.upgradeKeeper.SetModuleVersionMap(s.ctx, initialVM)
s.Require().NoError(err)
vmBefore, err := s.upgradeKeeper.GetModuleVersionMap(s.ctx)
s.Require().NoError(err)

s.upgradeKeeper.SetUpgradeHandler("dummy", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
s.upgradeKeeper.SetUpgradeHandler("dummy", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
// simulate upgrading the bank module
vm["bank"]++
return vm, nil
Expand All @@ -334,7 +336,7 @@ func (s *KeeperTestSuite) TestLastCompletedUpgrade() {
require.Equal(int64(0), height)
require.NoError(err)

keeper.SetUpgradeHandler("test0", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
keeper.SetUpgradeHandler("test0", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand All @@ -351,7 +353,7 @@ func (s *KeeperTestSuite) TestLastCompletedUpgrade() {
require.Equal(int64(10), height)
require.NoError(err)

keeper.SetUpgradeHandler("test1", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
keeper.SetUpgradeHandler("test1", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand All @@ -376,7 +378,7 @@ func (s *KeeperTestSuite) TestLastCompletedUpgradeOrdering() {
require := s.Require()

// apply first upgrade
keeper.SetUpgradeHandler("test-v0.9", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
keeper.SetUpgradeHandler("test-v0.9", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand All @@ -392,7 +394,7 @@ func (s *KeeperTestSuite) TestLastCompletedUpgradeOrdering() {
require.NoError(err)

// apply second upgrade
keeper.SetUpgradeHandler("test-v0.10", func(_ context.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
keeper.SetUpgradeHandler("test-v0.10", func(_ context.Context, _ types.Plan, vm appmodule.VersionMap) (appmodule.VersionMap, error) {
return vm, nil
})

Expand Down
4 changes: 2 additions & 2 deletions x/upgrade/types/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package types
import (
"context"

"github.com/cosmos/cosmos-sdk/types/module"
"cosmossdk.io/core/appmodule"
)

// UpgradeHandler specifies the type of function that is called when an upgrade
Expand All @@ -24,4 +24,4 @@ import (
// function.
//
// Please also refer to docs/core/upgrade.md for more information.
type UpgradeHandler func(ctx context.Context, plan Plan, fromVM module.VersionMap) (module.VersionMap, error)
type UpgradeHandler func(ctx context.Context, plan Plan, fromVM appmodule.VersionMap) (appmodule.VersionMap, error)
Loading