From e79f7c550297d963ac3d9715aa2a12b7f36cb29c Mon Sep 17 00:00:00 2001 From: Matt Kocubinski Date: Wed, 29 May 2024 13:55:32 -0500 Subject: [PATCH] refactor(x/upgrade): migrate to appmodule.VersionMap --- simapp/app_test.go | 4 ++-- simapp/upgrades.go | 4 ++-- types/module/module.go | 8 ++++---- x/upgrade/keeper/abci_test.go | 13 ++++++------- x/upgrade/keeper/grpc_query_test.go | 6 +++--- x/upgrade/keeper/keeper.go | 13 ++++++------- x/upgrade/keeper/keeper_test.go | 20 +++++++++++--------- x/upgrade/types/handler.go | 4 ++-- 8 files changed, 36 insertions(+), 36 deletions(-) diff --git a/simapp/app_test.go b/simapp/app_test.go index 20de441ef08b..62312557efaf 100644 --- a/simapp/app_test.go +++ b/simapp/app_test.go @@ -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(), @@ -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(), diff --git a/simapp/upgrades.go b/simapp/upgrades.go index ed6d35c0b490..01fe745d6e88 100644 --- a/simapp/upgrades.go +++ b/simapp/upgrades.go @@ -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" ) @@ -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) { return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM) }, ) diff --git a/types/module/module.go b/types/module/module.go index 306db89d4fbf..4248143dc7df 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -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) @@ -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] @@ -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 { diff --git a/x/upgrade/keeper/abci_test.go b/x/upgrade/keeper/abci_test.go index 1cf0793aba3f..f0de252394d2 100644 --- a/x/upgrade/keeper/abci_test.go +++ b/x/upgrade/keeper/abci_test.go @@ -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" ) @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) }, diff --git a/x/upgrade/keeper/grpc_query_test.go b/x/upgrade/keeper/grpc_query_test.go index dedfeff9b4e6..8e28c2ac136a 100644 --- a/x/upgrade/keeper/grpc_query_test.go +++ b/x/upgrade/keeper/grpc_query_test.go @@ -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" @@ -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" ) @@ -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) @@ -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) diff --git a/x/upgrade/keeper/keeper.go b/x/upgrade/keeper/keeper.go index e11904e1b4bd..89aa35c98d1c 100644 --- a/x/upgrade/keeper/keeper.go +++ b/x/upgrade/keeper/keeper.go @@ -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 { @@ -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: @@ -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 } @@ -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}) @@ -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)) @@ -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 diff --git a/x/upgrade/keeper/keeper_test.go b/x/upgrade/keeper/keeper_test.go index a1a572c7a437..0135c034fbd2 100644 --- a/x/upgrade/keeper/keeper_test.go +++ b/x/upgrade/keeper/keeper_test.go @@ -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" @@ -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" ) @@ -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{ @@ -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) @@ -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 @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) diff --git a/x/upgrade/types/handler.go b/x/upgrade/types/handler.go index 3af3043053a8..4c130046139c 100644 --- a/x/upgrade/types/handler.go +++ b/x/upgrade/types/handler.go @@ -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 @@ -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)