From e51107406251a1e598b83d4ff2ed6e9a33354668 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Fri, 26 Apr 2024 20:25:34 +0700 Subject: [PATCH] refactor: add ibc-hooks migration --- app/upgrades/v6_6_0/upgrade.go | 3 +++ app/upgrades/v6_6_0/upgrades_test.go | 20 ++++++++++++++++++ bech32-migration/ibchooks/ibchooks.go | 29 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 bech32-migration/ibchooks/ibchooks.go diff --git a/app/upgrades/v6_6_0/upgrade.go b/app/upgrades/v6_6_0/upgrade.go index d056d823..588e5d77 100644 --- a/app/upgrades/v6_6_0/upgrade.go +++ b/app/upgrades/v6_6_0/upgrade.go @@ -13,12 +13,14 @@ import ( upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" routertypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types" icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types" + ibchookstypes "github.com/notional-labs/composable/v6/x/ibc-hooks/types" ibctransfermiddlewaretypes "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types" "github.com/notional-labs/composable/v6/app/keepers" "github.com/notional-labs/composable/v6/app/upgrades" bech32authmigration "github.com/notional-labs/composable/v6/bech32-migration/auth" bech32govmigration "github.com/notional-labs/composable/v6/bech32-migration/gov" + bech32IbcHooksMigration "github.com/notional-labs/composable/v6/bech32-migration/ibchooks" bench32ibctransfermiddleware "github.com/notional-labs/composable/v6/bech32-migration/ibctransfermiddleware" bech32icamigration "github.com/notional-labs/composable/v6/bech32-migration/ica" bech32mintmigration "github.com/notional-labs/composable/v6/bech32-migration/mint" @@ -52,6 +54,7 @@ func CreateUpgradeHandler( bech32WasmMigration.MigrateAddressBech32(ctx, keys[wasm.StoreKey], codec) bech32PfmMigration.MigrateAddressBech32(ctx, keys[routertypes.StoreKey], codec, keepers) bench32ibctransfermiddleware.MigrateAddressBech32(ctx, keys[ibctransfermiddlewaretypes.StoreKey], codec) + bech32IbcHooksMigration.MigrateAddressBech32(ctx, keys[ibchookstypes.StoreKey], codec) return mm.RunMigrations(ctx, configurator, vm) } } diff --git a/app/upgrades/v6_6_0/upgrades_test.go b/app/upgrades/v6_6_0/upgrades_test.go index 02e94f09..6f8762ec 100644 --- a/app/upgrades/v6_6_0/upgrades_test.go +++ b/app/upgrades/v6_6_0/upgrades_test.go @@ -2,6 +2,7 @@ package v6_6_0_test import ( "encoding/json" + ibchookskeeper "github.com/notional-labs/composable/v6/x/ibc-hooks/keeper" ibctransfermiddlewaretypes "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types" "strings" "testing" @@ -20,6 +21,7 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" routertypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types" + ibchookstypes "github.com/notional-labs/composable/v6/x/ibc-hooks/types" ibctransfemiddlewaretypes "github.com/notional-labs/composable/v6/x/ibctransfermiddleware/types" apptesting "github.com/notional-labs/composable/v6/app" @@ -64,6 +66,7 @@ func (s *UpgradeTestSuite) TestForMigratingNewPrefix() { prepareForTestingTransferMiddlewareModule(s) prepareForTestingPfmMiddlewareModule(s) prepareForTestingIbcTransferMiddlewareModule(s) + prepareForTestingIbcHooksModule(s) /* == UPGRADE == */ upgradeHeight := int64(5) @@ -80,6 +83,7 @@ func (s *UpgradeTestSuite) TestForMigratingNewPrefix() { checkUpgradeTransferMiddlewareModule(s) checkUpgradePfmMiddlewareModule(s) checkUpgradeIbcTransferMiddlewareModule(s) + checkUpgradeIbcHooksMiddlewareModule(s) } func prepareForTestingGovModule(s *UpgradeTestSuite) (sdk.AccAddress, govtypes.Proposal) { @@ -280,6 +284,12 @@ func prepareForTestingIbcTransferMiddlewareModule(s *UpgradeTestSuite) { store.Set(ibctransfemiddlewaretypes.ParamsKey, bz) } +func prepareForTestingIbcHooksModule(s *UpgradeTestSuite) { + store := s.Ctx.KVStore(s.App.GetKey(ibchookstypes.StoreKey)) + store.Set(ibchookskeeper.GetPacketKey("channel-2", 2), []byte("centauri1hj5fveer5cjtn4wd6wstzugjfdxzl0xpzxlwgs")) + store.Set(ibchookskeeper.GetPacketKey("channel-4", 2), []byte("centauri1wkjvpgkuchq0r8425g4z4sf6n85zj5wtmqzjv9")) +} + func checkUpgradeGovModule(s *UpgradeTestSuite, acc1 sdk.AccAddress, proposal govtypes.Proposal) { // CONVERT ACC TO NEW PREFIX _, bz, _ := bech32.DecodeAndConvert(acc1.String()) @@ -491,7 +501,17 @@ func checkUpgradeIbcTransferMiddlewareModule(s *UpgradeTestSuite) { s.Suite.Equal("pica1hj5fveer5cjtn4wd6wstzugjfdxzl0xpas3hgy", data) data = s.App.IbcTransferMiddlewareKeeper.GetChannelFeeAddress(s.Ctx, "channel-1") s.Suite.Equal("", data) +} +func checkUpgradeIbcHooksMiddlewareModule(s *UpgradeTestSuite) { + data := s.App.IBCHooksKeeper.GetPacketCallback(s.Ctx, "channel-2", 2) + s.Suite.Equal("pica1hj5fveer5cjtn4wd6wstzugjfdxzl0xpas3hgy", data) + + data = s.App.IBCHooksKeeper.GetPacketCallback(s.Ctx, "channel-4", 2) + s.Suite.Equal("pica1wkjvpgkuchq0r8425g4z4sf6n85zj5wtykvtv3", data) + + data = s.App.IBCHooksKeeper.GetPacketCallback(s.Ctx, "channel-2", 1) + s.Suite.Equal("", data) } func CreateVestingAccount(s *UpgradeTestSuite, diff --git a/bech32-migration/ibchooks/ibchooks.go b/bech32-migration/ibchooks/ibchooks.go new file mode 100644 index 00000000..0f62560f --- /dev/null +++ b/bech32-migration/ibchooks/ibchooks.go @@ -0,0 +1,29 @@ +package ibchooks + +import ( + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/notional-labs/composable/v6/bech32-migration/utils" +) + +func MigrateAddressBech32(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) { + ctx.Logger().Info("Migration of address bech32 for ibchooks module begin") + totalAddr := uint64(0) + store := ctx.KVStore(storeKey) + channelKey := []byte("channel") + iterator := sdk.KVStorePrefixIterator(store, channelKey) + for ; iterator.Valid(); iterator.Next() { + totalAddr++ + fullKey := iterator.Key() + contract := string(store.Get(fullKey)) + contract = utils.SafeConvertAddress(contract) + totalAddr++ + store.Set(fullKey, []byte(contract)) + } + + ctx.Logger().Info( + "Migration of address bech32 for ibchooks module done", + "totalAddr", totalAddr, + ) +}