Skip to content

Commit

Permalink
refactor: add ibc-hooks migration (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed Apr 26, 2024
1 parent 2577afd commit 7e1d398
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/upgrades/v6_6_0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
20 changes: 20 additions & 0 deletions app/upgrades/v6_6_0/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -64,6 +66,7 @@ func (s *UpgradeTestSuite) TestForMigratingNewPrefix() {
prepareForTestingTransferMiddlewareModule(s)
prepareForTestingPfmMiddlewareModule(s)
prepareForTestingIbcTransferMiddlewareModule(s)
prepareForTestingIbcHooksModule(s)

/* == UPGRADE == */
upgradeHeight := int64(5)
Expand All @@ -80,6 +83,7 @@ func (s *UpgradeTestSuite) TestForMigratingNewPrefix() {
checkUpgradeTransferMiddlewareModule(s)
checkUpgradePfmMiddlewareModule(s)
checkUpgradeIbcTransferMiddlewareModule(s)
checkUpgradeIbcHooksMiddlewareModule(s)
}

func prepareForTestingGovModule(s *UpgradeTestSuite) (sdk.AccAddress, govtypes.Proposal) {
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions bech32-migration/ibchooks/ibchooks.go
Original file line number Diff line number Diff line change
@@ -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,
)
}

0 comments on commit 7e1d398

Please sign in to comment.