From 836ce9fe5404b918c10862f58d8d84dc0a6e170c Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Mon, 4 Mar 2024 11:36:32 +0700 Subject: [PATCH] refactor: rollback --- app/app.go | 48 ++++++++++++++++++++++++++++ app/upgrades/v4_1_0/constants.go | 5 +++ app/upgrades/v4_1_0/upgrades.go | 42 ++++++++++++++++++++++++ app/upgrades/v4_1_0/upgrades_test.go | 45 ++++++++++++++++++++++++++ app/upgrades/v4_1_1/constants.go | 5 +++ app/upgrades/v4_1_1/upgrades.go | 22 +++++++++++++ app/upgrades/v4_1_2/forks.go | 3 +- 7 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 app/upgrades/v4_1_0/constants.go create mode 100644 app/upgrades/v4_1_0/upgrades.go create mode 100644 app/upgrades/v4_1_0/upgrades_test.go create mode 100644 app/upgrades/v4_1_1/constants.go create mode 100644 app/upgrades/v4_1_1/upgrades.go diff --git a/app/app.go b/app/app.go index 42071a24..b5db0a6c 100644 --- a/app/app.go +++ b/app/app.go @@ -154,6 +154,8 @@ import ( wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper" appparams "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/params" + v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_0" + // unnamed import of statik for swagger UI support _ "github.com/cosmos/cosmos-sdk/client/docs/statik" ) @@ -1117,6 +1119,52 @@ func RegisterSwaggerAPI(rtr *mux.Router) { // Setup Upgrade Handler func (app *MigalooApp) setupUpgradeHandlers() { + // !! ATTENTION !! + // v4 upgrade handler + // !! WHEN UPGRADING TO SDK v0.47 MAKE SURE TO INCLUDE THIS + // source: https://github.com/cosmos/cosmos-sdk/blob/release/v0.47.x/UPGRADING.md#xconsensus + app.UpgradeKeeper.SetUpgradeHandler( + v4.UpgradeName, + v4.CreateUpgradeHandler( + app.mm, + app.configurator, + app.IBCKeeper.ClientKeeper, + app.ParamsKeeper, + app.ConsensusParamsKeeper, + app.ICAControllerKeeper, + ), + ) + + // When a planned update height is reached, the old binary will panic + // writing on disk the height and name of the update that triggered it + // This will read that value, and execute the preparations for the upgrade. + upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() + if err != nil { + panic(fmt.Errorf("failed to read upgrade info from disk: %w", err)) + } + + if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + return + } + + if upgradeInfo.Name == v4.UpgradeName { + // !! ATTENTION !! + // !! WHEN UPGRADING TO SDK v0.47 MAKE SURE TO INCLUDE THIS + // source: https://github.com/cosmos/cosmos-sdk/blob/release/v0.47.x/UPGRADING.md + storeUpgrades := &storetypes.StoreUpgrades{ + Added: []string{ + consensusparamtypes.StoreKey, + crisistypes.StoreKey, + icqtypes.StoreKey, + feeburnmoduletypes.StoreKey, + }, + Deleted: []string{ + "intertx", + }, + } + // configure store loader that checks if version == upgradeHeight and applies store upgrades + app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, storeUpgrades)) + } } // GetMaccPerms returns a copy of the module account permissions diff --git a/app/upgrades/v4_1_0/constants.go b/app/upgrades/v4_1_0/constants.go new file mode 100644 index 00000000..c68f43a0 --- /dev/null +++ b/app/upgrades/v4_1_0/constants.go @@ -0,0 +1,5 @@ +package v4 + +// UpgradeName defines the on-chain upgrade name for the Migaloo v3.0.2 upgrade. +// this upgrade includes the fix for pfm +const UpgradeName = "v4.1.0" diff --git a/app/upgrades/v4_1_0/upgrades.go b/app/upgrades/v4_1_0/upgrades.go new file mode 100644 index 00000000..ea8585ba --- /dev/null +++ b/app/upgrades/v4_1_0/upgrades.go @@ -0,0 +1,42 @@ +package v4 + +import ( + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper" + paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper" + paramstypes "github.com/cosmos/cosmos-sdk/x/params/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + icacontrollerkeeper "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/keeper" + icacontrollertypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/controller/types" + clientkeeper "github.com/cosmos/ibc-go/v7/modules/core/02-client/keeper" + ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" +) + +// CreateUpgradeHandler small security fix, can be a no-op, running mm.RunMigarions just to be sure +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, + clientKeeper clientkeeper.Keeper, + paramsKeeper paramskeeper.Keeper, + consensusParamsKeeper consensuskeeper.Keeper, + icacontrollerKeeper icacontrollerkeeper.Keeper, +) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, _plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { + // READ: https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/UPGRADING.md#xconsensus + baseAppLegacySS := paramsKeeper.Subspace(baseapp.Paramspace). + WithKeyTable(paramstypes.ConsensusParamsKeyTable()) + baseapp.MigrateParams(ctx, baseAppLegacySS, &consensusParamsKeeper) + + // READ: https://github.com/cosmos/ibc-go/blob/v7.2.0/docs/migrations/v7-to-v7_1.md#chains + params := clientKeeper.GetParams(ctx) + params.AllowedClients = append(params.AllowedClients, ibcexported.Localhost) + clientKeeper.SetParams(ctx, params) + + // READ: https://github.com/terra-money/core/issues/166 + icacontrollerKeeper.SetParams(ctx, icacontrollertypes.DefaultParams()) + + return mm.RunMigrations(ctx, configurator, fromVM) + } +} diff --git a/app/upgrades/v4_1_0/upgrades_test.go b/app/upgrades/v4_1_0/upgrades_test.go new file mode 100644 index 00000000..9ea52f7d --- /dev/null +++ b/app/upgrades/v4_1_0/upgrades_test.go @@ -0,0 +1,45 @@ +package v4_test + +import ( + "testing" + + apptesting "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app" + abci "github.com/cometbft/cometbft/abci/types" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + "github.com/stretchr/testify/suite" +) + +const ( + v4UpgradeHeight = int64(10) +) + +type UpgradeTestSuite struct { + apptesting.KeeperTestHelper +} + +func TestUpgradeTestSuite(t *testing.T) { + suite.Run(t, new(UpgradeTestSuite)) +} + +func (suite *UpgradeTestSuite) TestUpgrade() { + suite.Setup(suite.T(), apptesting.SimAppChainID) + dummyUpgrade(suite) + feeBurnParam := suite.App.FeeBurnKeeper.GetParams(suite.Ctx) + suite.Require().Equal("0", feeBurnParam.GetTxFeeBurnPercent()) +} + +func dummyUpgrade(s *UpgradeTestSuite) { + s.Ctx = s.Ctx.WithBlockHeight(v4UpgradeHeight - 1) + plan := upgradetypes.Plan{Name: "v4.1.0", Height: v4UpgradeHeight} + err := s.App.UpgradeKeeper.ScheduleUpgrade(s.Ctx, plan) + s.Require().NoError(err) + _, exists := s.App.UpgradeKeeper.GetUpgradePlan(s.Ctx) + s.Require().True(exists) + + s.Ctx = s.Ctx.WithBlockHeight(v4UpgradeHeight) + + s.Require().NotPanics(func() { + beginBlockRequest := abci.RequestBeginBlock{} + s.App.BeginBlocker(s.Ctx, beginBlockRequest) + }) +} diff --git a/app/upgrades/v4_1_1/constants.go b/app/upgrades/v4_1_1/constants.go new file mode 100644 index 00000000..8c8c1d87 --- /dev/null +++ b/app/upgrades/v4_1_1/constants.go @@ -0,0 +1,5 @@ +package v4 + +const ( + UpgradeHeight = 226753 +) diff --git a/app/upgrades/v4_1_1/upgrades.go b/app/upgrades/v4_1_1/upgrades.go new file mode 100644 index 00000000..21df6e9b --- /dev/null +++ b/app/upgrades/v4_1_1/upgrades.go @@ -0,0 +1,22 @@ +package v4 + +import ( + feeburnkeeper "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/x/feeburn/keeper" + sdk "github.com/cosmos/cosmos-sdk/types" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +func UpdateAccountPermissionAndFeeBurnPercent(ctx sdk.Context, fk feeburnkeeper.Keeper, ak authkeeper.AccountKeeper) { + // Burning module permissions + moduleAccI := ak.GetModuleAccount(ctx, authtypes.FeeCollectorName) + moduleAcc := moduleAccI.(*authtypes.ModuleAccount) + moduleAcc.Permissions = []string{authtypes.Burner} + ak.SetModuleAccount(ctx, moduleAcc) + + // set default fee_burn_percent to 10 + feeBurnParams := fk.GetParams(ctx) + feeBurnParams.TxFeeBurnPercent = "10" + + _ = fk.SetParams(ctx, feeBurnParams) +} diff --git a/app/upgrades/v4_1_2/forks.go b/app/upgrades/v4_1_2/forks.go index 785e6784..5997ed9d 100644 --- a/app/upgrades/v4_1_2/forks.go +++ b/app/upgrades/v4_1_2/forks.go @@ -1,9 +1,10 @@ package v4 import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper" - "time" ) func UpdateAlliance(ctx sdk.Context, alk alliancekeeper.Keeper) {