From fe12d661aa581223de6e59f724a24a0960796701 Mon Sep 17 00:00:00 2001 From: fx0x55 <80245546+fx0x55@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:33:02 +0800 Subject: [PATCH] refactor(x/crosschain): migrate module data --- app/app.go | 1 + app/modules.go | 42 +++++++++++++++++++++++++++ app/upgrades/store/store.go | 28 ++++++++++++++++++ app/upgrades/v8/upgrade.go | 37 +++-------------------- x/arbitrum/module.go | 4 +-- x/avalanche/module.go | 4 +-- x/bsc/module.go | 4 +-- x/crosschain/keeper/legacy.go | 3 +- x/crosschain/keeper/migrations.go | 22 ++++++-------- x/crosschain/migrations/v8/migrate.go | 37 +++++++++++++++++++++++ x/crosschain/types/key.go | 26 ----------------- x/eth/module.go | 4 +-- x/layer2/module.go | 4 +-- x/optimism/module.go | 4 +-- x/polygon/module.go | 4 +-- x/tron/module.go | 4 +-- 16 files changed, 139 insertions(+), 89 deletions(-) create mode 100644 app/upgrades/store/store.go create mode 100644 x/crosschain/migrations/v8/migrate.go diff --git a/app/app.go b/app/app.go index 0898cbda..0ce6aa61 100644 --- a/app/app.go +++ b/app/app.go @@ -202,6 +202,7 @@ func New( // can do so safely. app.mm.SetOrderInitGenesis(orderInitBlockers()...) app.mm.SetOrderExportGenesis(orderInitBlockers()...) + app.mm.SetOrderMigrations(orderMigrations()...) app.mm.RegisterInvariants(app.CrisisKeeper) app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) diff --git a/app/modules.go b/app/modules.go index 4d950aaf..8fc9b3e1 100644 --- a/app/modules.go +++ b/app/modules.go @@ -280,6 +280,48 @@ func orderInitBlockers() []string { } } +func orderMigrations() []string { + return []string{ + capabilitytypes.ModuleName, + authtypes.ModuleName, + banktypes.ModuleName, + distrtypes.ModuleName, + stakingtypes.ModuleName, + slashingtypes.ModuleName, + govtypes.ModuleName, + minttypes.ModuleName, + crisistypes.ModuleName, + genutiltypes.ModuleName, + evidencetypes.ModuleName, + authz.ModuleName, + feegrant.ModuleName, + paramstypes.ModuleName, + upgradetypes.ModuleName, + vestingtypes.ModuleName, + + erc20types.ModuleName, + + bsctypes.ModuleName, + trontypes.ModuleName, + polygontypes.ModuleName, + avalanchetypes.ModuleName, + ethtypes.ModuleName, + arbitrumtypes.ModuleName, + optimismtypes.ModuleName, + layer2types.ModuleName, + + feemarkettypes.ModuleName, + evmtypes.ModuleName, + migratetypes.ModuleName, + + ibctm.ModuleName, + ibctransfertypes.ModuleName, + ibcexported.ModuleName, + + consensustypes.ModuleName, + } +} + func GetMaccPerms() map[string][]string { dupMaccPerms := make(map[string][]string) for k, v := range maccPerms { diff --git a/app/upgrades/store/store.go b/app/upgrades/store/store.go new file mode 100644 index 00000000..2e5c0709 --- /dev/null +++ b/app/upgrades/store/store.go @@ -0,0 +1,28 @@ +package store + +import ( + storetypes "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func RemoveStoreKeys(ctx sdk.Context, storeKey storetypes.StoreKey, prefixKeys [][]byte) { + store := ctx.KVStore(storeKey) + + deleteFn := func(prefixKey []byte) { + iterator := storetypes.KVStorePrefixIterator(store, prefixKey) + defer iterator.Close() + deleteCount := 0 + for ; iterator.Valid(); iterator.Next() { + store.Delete(iterator.Key()) + deleteCount++ + } + if deleteCount > 0 { + ctx.Logger().Info("remove store key", "kvStore", storeKey.Name(), + "prefix", prefixKey[0], "deleteKeyCount", deleteCount) + } + } + + for _, key := range prefixKeys { + deleteFn(key) + } +} diff --git a/app/upgrades/v8/upgrade.go b/app/upgrades/v8/upgrade.go index c19286b5..0669122c 100644 --- a/app/upgrades/v8/upgrade.go +++ b/app/upgrades/v8/upgrade.go @@ -2,7 +2,6 @@ package v8 import ( "context" - "encoding/hex" "strings" storetypes "cosmossdk.io/store/types" @@ -19,8 +18,8 @@ import ( ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types" "github.com/functionx/fx-core/v8/app/keepers" + "github.com/functionx/fx-core/v8/app/upgrades/store" fxtypes "github.com/functionx/fx-core/v8/types" - crosschainkeeper "github.com/functionx/fx-core/v8/x/crosschain/keeper" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" erc20v8 "github.com/functionx/fx-core/v8/x/erc20/migrations/v8" erc20types "github.com/functionx/fx-core/v8/x/erc20/types" @@ -43,23 +42,19 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, return fromVM, err } - removeStoreKeys(cacheCtx, app.GetKey(stakingtypes.StoreKey), fxstakingv8.GetRemovedStoreKeys()) + store.RemoveStoreKeys(cacheCtx, app.GetKey(stakingtypes.StoreKey), fxstakingv8.GetRemovedStoreKeys()) if err = migrationGovCustomParam(cacheCtx, app.GovKeeper, app.GetKey(govtypes.StoreKey)); err != nil { return fromVM, err } - if err = updateArbitrumParams(cacheCtx, app.ArbitrumKeeper); err != nil { - return fromVM, err - } - if err = migrateBridgeBalance(cacheCtx, app.BankKeeper, app.AccountKeeper); err != nil { return fromVM, err } updateMetadata(cacheCtx, app.BankKeeper) - removeStoreKeys(cacheCtx, app.GetKey(erc20types.StoreKey), erc20v8.GetRemovedStoreKeys()) + store.RemoveStoreKeys(cacheCtx, app.GetKey(erc20types.StoreKey), erc20v8.GetRemovedStoreKeys()) commit() cacheCtx.Logger().Info("upgrade complete", "module", "upgrade") @@ -67,38 +62,14 @@ func CreateUpgradeHandler(mm *module.Manager, configurator module.Configurator, } } -func updateArbitrumParams(ctx sdk.Context, keeper crosschainkeeper.Keeper) error { - params := keeper.GetParams(ctx) - params.AverageExternalBlockTime = 250 - return keeper.SetParams(ctx, ¶ms) -} - func migrationGovCustomParam(ctx sdk.Context, keeper *keeper.Keeper, storeKey *storetypes.KVStoreKey) error { // 1. delete fxParams key - removeStoreKeys(ctx, storeKey, fxgovv8.GetRemovedStoreKeys()) + store.RemoveStoreKeys(ctx, storeKey, fxgovv8.GetRemovedStoreKeys()) // 2. init custom params return keeper.InitCustomParams(ctx) } -func removeStoreKeys(ctx sdk.Context, storeKey *storetypes.KVStoreKey, keys [][]byte) { - store := ctx.KVStore(storeKey) - - deleteFn := func(key []byte) { - iterator := storetypes.KVStorePrefixIterator(store, key) - defer iterator.Close() - for ; iterator.Valid(); iterator.Next() { - store.Delete(iterator.Key()) - ctx.Logger().Info("remove store key", "kvStore", storeKey.Name(), - "prefix", hex.EncodeToString(key), "key", string(iterator.Key())) - } - } - - for _, key := range keys { - deleteFn(key) - } -} - func migrateCrosschainModuleAccount(ctx sdk.Context, ak authkeeper.AccountKeeper) error { addr, perms := ak.GetModuleAddressAndPermissions(crosschaintypes.ModuleName) if addr == nil { diff --git a/x/arbitrum/module.go b/x/arbitrum/module.go index 796b4467..7ebb306e 100644 --- a/x/arbitrum/module.go +++ b/x/arbitrum/module.go @@ -106,7 +106,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 1, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate7to8WithArbExternalBlockTime); err != nil { panic(err) } } @@ -128,7 +128,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 2 + return 3 } // EndBlock implements app module diff --git a/x/avalanche/module.go b/x/avalanche/module.go index a26da9d6..0e39b434 100644 --- a/x/avalanche/module.go +++ b/x/avalanche/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 3, migrator.Migrate7to8); err != nil { panic(err) } } @@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 3 + return 4 } // EndBlock implements app module diff --git a/x/bsc/module.go b/x/bsc/module.go index 850d8e5f..3e39bf4f 100644 --- a/x/bsc/module.go +++ b/x/bsc/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 4, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 5, migrator.Migrate7to8); err != nil { panic(err) } } @@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 5 + return 6 } // EndBlock implements app module diff --git a/x/crosschain/keeper/legacy.go b/x/crosschain/keeper/legacy.go index a6f74c7e..13568502 100644 --- a/x/crosschain/keeper/legacy.go +++ b/x/crosschain/keeper/legacy.go @@ -3,13 +3,14 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/functionx/fx-core/v8/x/crosschain/migrations/v8" "github.com/functionx/fx-core/v8/x/crosschain/types" ) // Deprecated: do not use, remove in v8 func (k Keeper) LegacyGetDenomBridgeToken(ctx sdk.Context, denom string) (*types.BridgeToken, bool) { store := ctx.KVStore(k.storeKey) - data := store.Get(types.GetTokenToDenomKey(denom)) + data := store.Get(v8.GetTokenToDenomKey(denom)) if len(data) == 0 { return nil, false } diff --git a/x/crosschain/keeper/migrations.go b/x/crosschain/keeper/migrations.go index 21d41d28..523820b4 100644 --- a/x/crosschain/keeper/migrations.go +++ b/x/crosschain/keeper/migrations.go @@ -3,8 +3,7 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - fxtypes "github.com/functionx/fx-core/v8/types" - "github.com/functionx/fx-core/v8/x/crosschain/types" + "github.com/functionx/fx-core/v8/x/crosschain/migrations/v8" ) type Migrator struct { @@ -17,18 +16,15 @@ func NewMigrator(k Keeper) Migrator { } } -func (m Migrator) Migrate(ctx sdk.Context) error { - params := m.keeper.GetParams(ctx) - - params.BridgeCallTimeout = types.DefBridgeCallTimeout - params.BridgeCallMaxGasLimit = types.MaxGasLimit +func (m Migrator) Migrate7to8(ctx sdk.Context) error { + return v8.Migrate(ctx, m.keeper.storeKey) +} - enablePending := false - if ctx.ChainID() == fxtypes.TestnetChainId { - enablePending = true +func (m Migrator) Migrate7to8WithArbExternalBlockTime(ctx sdk.Context) error { + if err := m.Migrate7to8(ctx); err != nil { + return err } - params.EnableSendToExternalPending = enablePending - params.EnableBridgeCallPending = enablePending - + params := m.keeper.GetParams(ctx) + params.AverageExternalBlockTime = 250 return m.keeper.SetParams(ctx, ¶ms) } diff --git a/x/crosschain/migrations/v8/migrate.go b/x/crosschain/migrations/v8/migrate.go new file mode 100644 index 00000000..3519f26f --- /dev/null +++ b/x/crosschain/migrations/v8/migrate.go @@ -0,0 +1,37 @@ +package v8 + +import ( + storetypes "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/functionx/fx-core/v8/app/upgrades/store" +) + +var ( + // Deprecated: DenomToTokenKey prefixes the index of asset denom to external token + DenomToTokenKey = []byte{0x26} + + // Deprecated: TokenToDenomKey prefixes the index of assets external token to denom + TokenToDenomKey = []byte{0x27} + + // Deprecated: remove data in upgrade + BridgeCallFromMsgKey = []byte{0x51} +) + +// Deprecated: GetTokenToDenomKey returns the following key format +func GetTokenToDenomKey(denom string) []byte { + return append(TokenToDenomKey, []byte(denom)...) +} + +func GetRemovedStoreKeys() [][]byte { + return [][]byte{ + DenomToTokenKey, + TokenToDenomKey, + BridgeCallFromMsgKey, + } +} + +func Migrate(ctx sdk.Context, storeKey storetypes.StoreKey) error { + store.RemoveStoreKeys(ctx, storeKey, GetRemovedStoreKeys()) + return nil +} diff --git a/x/crosschain/types/key.go b/x/crosschain/types/key.go index b0135050..4c2a9e72 100644 --- a/x/crosschain/types/key.go +++ b/x/crosschain/types/key.go @@ -66,12 +66,6 @@ var ( // KeyLastOutgoingBatchID indexes the lastBatchID KeyLastOutgoingBatchID = append(SequenceKeyPrefix, []byte("lastBatchId")...) - // Deprecated: DenomToTokenKey prefixes the index of asset denom to external token - DenomToTokenKey = []byte{0x26} - - // Deprecated: TokenToDenomKey prefixes the index of assets external token to denom - TokenToDenomKey = []byte{0x27} - // LastSlashedOracleSetNonce indexes the latest slashed oracleSet nonce LastSlashedOracleSetNonce = []byte{0x28} @@ -121,13 +115,7 @@ var ( OutgoingBridgeCallNonceKey = []byte{0x48} OutgoingBridgeCallAddressAndNonceKey = []byte{0x49} - // Deprecated: remove data in upgrade - BridgeCallFromMsgKey = []byte{0x51} - PendingExecuteClaimKey = []byte{0x54} - - // BridgeDenomKey -> value BridgeDenom - BridgeDenomKey = []byte{0x60} ) // GetOracleKey returns the following key format @@ -202,16 +190,6 @@ func GetLastEventBlockHeightByOracleKey(oracleAddr sdk.AccAddress) []byte { return append(LastEventBlockHeightByOracleKey, oracleAddr.Bytes()...) } -// Deprecated: GetDenomToTokenKey returns the following key format -func GetDenomToTokenKey(tokenContract string) []byte { - return append(DenomToTokenKey, []byte(tokenContract)...) -} - -// Deprecated: GetTokenToDenomKey returns the following key format -func GetTokenToDenomKey(denom string) []byte { - return append(TokenToDenomKey, []byte(denom)...) -} - func GetBridgeCallConfirmKey(nonce uint64, addr sdk.AccAddress) []byte { return append(BridgeCallConfirmKey, append(sdk.Uint64ToBigEndian(nonce), addr.Bytes()...)...) } @@ -241,7 +219,3 @@ func ParseOutgoingBridgeCallNonce(key []byte, address string) (nonce uint64) { func GetPendingExecuteClaimKey(nonce uint64) []byte { return append(PendingExecuteClaimKey, sdk.Uint64ToBigEndian(nonce)...) } - -func GetBridgeDenomKey(denom string) []byte { - return append(BridgeDenomKey, []byte(denom)...) -} diff --git a/x/eth/module.go b/x/eth/module.go index 93d79507..eb9f1571 100644 --- a/x/eth/module.go +++ b/x/eth/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 3, migrator.Migrate7to8); err != nil { panic(err) } } @@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 3 + return 4 } // EndBlock implements app module diff --git a/x/layer2/module.go b/x/layer2/module.go index 4cc98e09..88be7711 100644 --- a/x/layer2/module.go +++ b/x/layer2/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 1, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate7to8); err != nil { panic(err) } } @@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 2 + return 3 } // EndBlock implements app module diff --git a/x/optimism/module.go b/x/optimism/module.go index 624bc495..54f4802f 100644 --- a/x/optimism/module.go +++ b/x/optimism/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 1, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 2, migrator.Migrate7to8); err != nil { panic(err) } } @@ -130,7 +130,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 2 + return 3 } // EndBlock implements app module diff --git a/x/polygon/module.go b/x/polygon/module.go index 4bfa4ee3..16879c2b 100644 --- a/x/polygon/module.go +++ b/x/polygon/module.go @@ -104,7 +104,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 4, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 5, migrator.Migrate7to8); err != nil { panic(err) } } @@ -126,7 +126,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 5 + return 6 } // EndBlock implements app module diff --git a/x/tron/module.go b/x/tron/module.go index a95fbbbf..a66d97ad 100644 --- a/x/tron/module.go +++ b/x/tron/module.go @@ -104,7 +104,7 @@ func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { migrator := crosschainkeeper.NewMigrator(am.keeper) - if err := cfg.RegisterMigration(am.Name(), 4, migrator.Migrate); err != nil { + if err := cfg.RegisterMigration(am.Name(), 5, migrator.Migrate7to8); err != nil { panic(err) } } @@ -126,7 +126,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw // ConsensusVersion implements AppModule/ConsensusVersion. func (am AppModule) ConsensusVersion() uint64 { - return 5 + return 6 } // EndBlock implements app module