Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add migration pfm #499

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/upgrades/v6_6_0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
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"

"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"
bech32icamigration "github.com/notional-labs/composable/v6/bech32-migration/ica"
bech32mintmigration "github.com/notional-labs/composable/v6/bech32-migration/mint"
bech32PfmMigration "github.com/notional-labs/composable/v6/bech32-migration/pfmmiddleware"
bech32slashingmigration "github.com/notional-labs/composable/v6/bech32-migration/slashing"
bech32stakingmigration "github.com/notional-labs/composable/v6/bech32-migration/staking"
bech32transfermiddlewaremigration "github.com/notional-labs/composable/v6/bech32-migration/transfermiddleware"
Expand Down Expand Up @@ -45,6 +48,7 @@ func CreateUpgradeHandler(
bech32mintmigration.MigrateAddressBech32(ctx, keys[minttypes.StoreKey], codec)
bech32transfermiddlewaremigration.MigrateAddressBech32(ctx, keys[transfermiddlewaretypes.StoreKey], codec)
bech32WasmMigration.MigrateAddressBech32(ctx, keys[wasm.StoreKey], codec)
bech32PfmMigration.MigrateAddressBech32(ctx, keys[routertypes.StoreKey], codec, keepers)
return mm.RunMigrations(ctx, configurator, vm)
}
}
32 changes: 32 additions & 0 deletions app/upgrades/v6_6_0/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
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"
apptesting "github.com/notional-labs/composable/v6/app"
"github.com/notional-labs/composable/v6/bech32-migration/utils"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -58,6 +59,7 @@ func (s *UpgradeTestSuite) TestForMigratingNewPrefix() {
prepareForTestingICAHostModule(s)
prepareForTestingMintModule(s)
prepareForTestingTransferMiddlewareModule(s)
prepareForTestingPfmMiddlewareModule(s)

/* == UPGRADE == */
upgradeHeight := int64(5)
Expand Down Expand Up @@ -214,6 +216,36 @@ func prepareForTestingTransferMiddlewareModule(s *UpgradeTestSuite) {
s.App.TransferMiddlewareKeeper.SetAllowRlyAddress(s.Ctx, acc1.String())
}

func prepareForTestingPfmMiddlewareModule(s *UpgradeTestSuite) {
store := s.Ctx.KVStore(s.App.GetKey(routertypes.StoreKey))
inFlightPacket := routertypes.InFlightPacket{
PacketData: []byte("{\"amount\":\"10000\",\"denom\":\"transfer/channel-6660/ppica\",\"memo\":\"{\\\"forward\\\":{\\\"receiver\\\":\\\"osmo1wkjvpgkuchq0r8425g4z4sf6n85zj5wth3u77y\\\",\\\"port\\\":\\\"transfer\\\",\\\"channel\\\":\\\"channel-9\\\",\\\"timeout\\\":600000000000,\\\"retries\\\":0}}\",\"receiver\":\"centauri1wkjvpgkuchq0r8425g4z4sf6n85zj5wtmqzjv9\",\"sender\":\"osmo1wkjvpgkuchq0r8425g4z4sf6n85zj5wth3u77y\"}"),
OriginalSenderAddress: "osmo1wkjvpgkuchq0r8425g4z4sf6n85zj5wth3u77y",
RefundChannelId: "channel-9",
RefundPortId: "transfer",
RefundSequence: 18,
PacketSrcPortId: "transfer",
PacketSrcChannelId: "channel-66660",

PacketTimeoutTimestamp: 1712153063084849609,
PacketTimeoutHeight: "5-123",

RetriesRemaining: int32(0),
Timeout: uint64(600000000000),
Nonrefundable: false,
}

encCdc := apptesting.MakeEncodingConfig()

key := routertypes.RefundPacketKey("channel-9", "transfer", 0)
bz := encCdc.Amino.MustMarshal(&inFlightPacket)
store.Set(key, bz)

key = routertypes.RefundPacketKey("channel-9", "transfer", 2)
bz = encCdc.Amino.MustMarshal(&inFlightPacket)
store.Set(key, bz)
}

func checkUpgradeGovModule(s *UpgradeTestSuite, acc1 sdk.AccAddress, proposal govtypes.Proposal) {
// CONVERT ACC TO NEW PREFIX
_, bz, _ := bech32.DecodeAndConvert(acc1.String())
Expand Down
77 changes: 77 additions & 0 deletions bech32-migration/pfmmiddleware/pfmmiddleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package pfmmiddleware

import (
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
routertypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/notional-labs/composable/v6/app/keepers"
"github.com/notional-labs/composable/v6/bech32-migration/utils"
)

func MigrateAddressBech32(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, keepers *keepers.AppKeepers) {
ctx.Logger().Info("Migration of address bech32 for pfmmiddleware module begin")
totalAddr := uint64(0)

store := ctx.KVStore(storeKey)

allChannels := keepers.IBCKeeper.ChannelKeeper.GetAllChannels(ctx)
// if len(allChannels) == 0 {
// allChannels = append(allChannels, types.IdentifiedChannel{
// ChannelId: "channel-9",
// })
//}
for _, channel := range allChannels {
channelKey := []byte(fmt.Sprintf("%s/", channel.ChannelId))
iterator := sdk.KVStorePrefixIterator(store, channelKey)
for ; iterator.Valid(); iterator.Next() {
totalAddr++
fullKey := iterator.Key()
if !store.Has(fullKey) {
continue
}
bz := store.Get(fullKey)
var inFlightPacket routertypes.InFlightPacket
cdc.MustUnmarshal(bz, &inFlightPacket)
inFlightPacket.OriginalSenderAddress = utils.SafeConvertAddress(inFlightPacket.OriginalSenderAddress)
var data transfertypes.FungibleTokenPacketData
if err := transfertypes.ModuleCdc.UnmarshalJSON(inFlightPacket.PacketData, &data); err != nil {
continue
hoank101 marked this conversation as resolved.
Show resolved Hide resolved
}
data.Receiver = utils.SafeConvertAddress(data.Receiver)
data.Sender = utils.SafeConvertAddress(data.Sender)

d := make(map[string]interface{})
err := json.Unmarshal([]byte(data.Memo), &d)
// parse memo
if err == nil && d["forward"] != nil {
var m routertypes.PacketMetadata
err = json.Unmarshal([]byte(data.Memo), &m)
if err != nil {
continue
}
m.Forward.Receiver = utils.SafeConvertAddress(m.Forward.Receiver)
bzM, err := json.Marshal(m)
if err != nil {
continue
}
data.Memo = string(bzM)
}
bz = cdc.MustMarshal(&data)
inFlightPacket.PacketData = bz
bz = cdc.MustMarshal(&inFlightPacket)
totalAddr++
store.Set(fullKey, bz)
}

}

ctx.Logger().Info(
"Migration of address bech32 for pfmmiddleware module done",
"totalAddr", totalAddr,
)
}
Loading