From 21b563b2621730feb776832508de6625803d60ae Mon Sep 17 00:00:00 2001
From: Khanh Hoa <hoa@notional.ventures>
Date: Thu, 14 Mar 2024 14:36:46 +0700
Subject: [PATCH] feat: add handler upgrade v4.1.3 in v4.1.2

---
 app/app.go                       |  2 +-
 app/upgrades/v4_1_3/constants.go |  7 +++
 app/upgrades/v4_1_3/readme.md    | 97 --------------------------------
 app/upgrades/v4_1_3/upgrades.go  | 51 +++++++++++++++++
 4 files changed, 59 insertions(+), 98 deletions(-)
 create mode 100644 app/upgrades/v4_1_3/constants.go
 delete mode 100644 app/upgrades/v4_1_3/readme.md
 create mode 100644 app/upgrades/v4_1_3/upgrades.go

diff --git a/app/app.go b/app/app.go
index 8fe2067f..1d2bcced 100644
--- a/app/app.go
+++ b/app/app.go
@@ -158,7 +158,7 @@ import (
 	"github.com/rakyll/statik/fs"
 
 	v3_0_2 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v3_0_2"
-	v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_2"
+	v4 "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/app/upgrades/v4_1_3"
 
 	// unnamed import of statik for swagger UI support
 	_ "github.com/White-Whale-Defi-Platform/migaloo-chain/v4/client/docs/statik"
diff --git a/app/upgrades/v4_1_3/constants.go b/app/upgrades/v4_1_3/constants.go
new file mode 100644
index 00000000..36db1968
--- /dev/null
+++ b/app/upgrades/v4_1_3/constants.go
@@ -0,0 +1,7 @@
+package v4
+
+// UpgradeName defines the on-chain upgrade name for the Migaloo v4.1.2 upgrade.
+// this upgrade includes the fix for pfm
+const (
+	UpgradeName = "v4.1.2"
+)
diff --git a/app/upgrades/v4_1_3/readme.md b/app/upgrades/v4_1_3/readme.md
deleted file mode 100644
index c8993852..00000000
--- a/app/upgrades/v4_1_3/readme.md
+++ /dev/null
@@ -1,97 +0,0 @@
-# Upgrade from Migaloo v4.1.2 to v4.1.3
-
-> ## This is an important security update. IT IS CONSENSUS BREAKING, so please apply the fix only on height 5962800.
-
-### Release Details
-* https://github.com/White-Whale-Defi-Platform/migaloo-chain/releases/tag/v4.1.3
-* Chain upgrade height : `5962800`. Exact upgrade time can be checked [here](https://ping.pub/whitewhale/block/5962800).
-
-
-# To upgrade migaloo-chain
-
-## Step 1: Alter systemd service configuration
-
-We need to disable automatic restart of the node service. To do so please alter your `cosmovisor.service` file configuration and set appropriate lines to following values.
-
-```
-Restart=no 
-
-Environment="DAEMON_ALLOW_DOWNLOAD_BINARIES=false"
-Environment="DAEMON_RESTART_AFTER_UPGRADE=false"
-```
-
-After that you will need to run `sudo systemctl daemon-reload` to apply changes in the service configuration.
-
-There is no need to restart the node yet; these changes will get applied during the node restart in the next step.
-
-## Step 2: Restart migalood with a configured `halt-height`.
-
-This upgrade requires `migalood` to have knowledge of the planned halt height. Please be aware that there is an extra step at the end to revert to `migalood`'s original configurations.
-
-There are two mutually exclusive options for this stage:
-
-### Option 1: Set the halt height by modifying `app.toml`
-
-* Stop the `migalood` process.
-
-* Edit the application configuration file at `~/.migalood/config/app.toml` so that `halt-height` reflects the upgrade plan:
-
-```toml
-# Note: Commitment of state will be attempted on the corresponding block.
-halt-height = 5962800
-```
-* Start `migalood` process
-
-* Wait for the upgrade height and confirm that the node has halted
-
-### Option 2: Restart the `migalood` binary with command line flags
-
-* Stop the `migalood` process.
-
-* Do not modify `app.toml`. Restart the `migalood` process with the flag `--halt-height`:
-```shell
-migalood --halt-height 7818500
-```
-
-* Wait for the upgrade height and confirm that the node has halted
-
-After performing these steps, the upgrade will proceed as usual using Cosmovisor.
-
-# Setup Cosmovisor
-## Create the updated migaloo binary of v4.1.3
-
-### Go to migaloo-chain directory if present else clone the repository
-
-```shell
-   git clone https://github.com/White-Whale-Defi-Platform/migaloo-chain.git
-```
-
-### Follow these steps if migaloo-chain repo already present
-
-```shell
-   cd migaloo-chain
-   git pull
-   git fetch --tags
-   git checkout v4.1.3
-   make install
-```
-
-## Copy the new migaloo (v4.1.3) binary to cosmovisor current directory
-```shell
-   cp $GOPATH/bin/migalood ~/.migalood/cosmovisor/current/bin
-```
-
-## Restore service file settings
-
-If you are using a service file, restore the previous `Restart` settings in your service file:
-```
-Restart=On-failure 
-```
-Reload the service control `sudo systemctl daemon-reload`.
-
-# Revert `migalood` configurations
-
-Depending on which path you chose for Step 1, either:
-
-* Reset `halt-height = 0` option in the `app.toml` or
-* Remove it from start parameters of the `migalood` binary and start node again
\ No newline at end of file
diff --git a/app/upgrades/v4_1_3/upgrades.go b/app/upgrades/v4_1_3/upgrades.go
new file mode 100644
index 00000000..484cee2b
--- /dev/null
+++ b/app/upgrades/v4_1_3/upgrades.go
@@ -0,0 +1,51 @@
+package v4
+
+import (
+	"github.com/cosmos/cosmos-sdk/baseapp"
+	sdk "github.com/cosmos/cosmos-sdk/types"
+	"github.com/cosmos/cosmos-sdk/types/module"
+	authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
+	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
+	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 that migrates the chain from v3.0.2 to v4.1.3
+func CreateUpgradeHandler(
+	mm *module.Manager,
+	configurator module.Configurator,
+	clientKeeper clientkeeper.Keeper,
+	paramsKeeper paramskeeper.Keeper,
+	consensusParamsKeeper consensuskeeper.Keeper,
+	icacontrollerKeeper icacontrollerkeeper.Keeper,
+	accountKeeper authkeeper.AccountKeeper,
+) upgradetypes.UpgradeHandler {
+	return func(ctx sdk.Context, _ 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())
+
+		// Burning module permissions
+		moduleAccI := accountKeeper.GetModuleAccount(ctx, authtypes.FeeCollectorName)
+		moduleAcc := moduleAccI.(*authtypes.ModuleAccount)
+		moduleAcc.Permissions = []string{authtypes.Burner}
+		accountKeeper.SetModuleAccount(ctx, moduleAcc)
+
+		return mm.RunMigrations(ctx, configurator, fromVM)
+	}
+}