From 44b5eab5ecec9141aae1a49c7da3883ba263cd53 Mon Sep 17 00:00:00 2001 From: Elliot Ng Date: Thu, 10 Feb 2022 15:18:02 +0800 Subject: [PATCH] Migrate app.go to sdk 0.44 and ibc-go v2 Ref oursky/likecoin-chain#20 SDK migration guide: https://docs.cosmos.network/master/migrations/chain-upgrade-guide-044.html#chain-upgrade IBC migration guide: https://github.com/cosmos/ibc-go/blob/main/docs/migrations/sdk-to-v1.md --- app/app.go | 115 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 86 insertions(+), 29 deletions(-) diff --git a/app/app.go b/app/app.go index 96ece65466..8b99b923b6 100644 --- a/app/app.go +++ b/app/app.go @@ -11,7 +11,6 @@ import ( abci "github.com/tendermint/tendermint/abci/types" "github.com/tendermint/tendermint/libs/log" tmos "github.com/tendermint/tendermint/libs/os" - tmproto "github.com/tendermint/tendermint/proto/tendermint/types" dbm "github.com/tendermint/tm-db" "github.com/cosmos/cosmos-sdk/baseapp" @@ -26,6 +25,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/api" "github.com/cosmos/cosmos-sdk/server/config" servertypes "github.com/cosmos/cosmos-sdk/server/types" + storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/x/auth" @@ -55,14 +55,6 @@ import ( "github.com/cosmos/cosmos-sdk/x/gov" govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - transfer "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer" - ibctransferkeeper "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/keeper" - ibctransfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types" - ibc "github.com/cosmos/cosmos-sdk/x/ibc/core" - ibcclient "github.com/cosmos/cosmos-sdk/x/ibc/core/02-client" - porttypes "github.com/cosmos/cosmos-sdk/x/ibc/core/05-port/types" - ibchost "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host" - ibckeeper "github.com/cosmos/cosmos-sdk/x/ibc/core/keeper" "github.com/cosmos/cosmos-sdk/x/mint" mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" @@ -81,6 +73,16 @@ import ( upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client" upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + transfer "github.com/cosmos/ibc-go/v2/modules/apps/transfer" + ibctransferkeeper "github.com/cosmos/ibc-go/v2/modules/apps/transfer/keeper" + ibctransfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types" + ibc "github.com/cosmos/ibc-go/v2/modules/core" + ibcclient "github.com/cosmos/ibc-go/v2/modules/core/02-client" + ibcclientclient "github.com/cosmos/ibc-go/v2/modules/core/02-client/client" + ibcclienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" + ibchost "github.com/cosmos/ibc-go/v2/modules/core/24-host" + ibckeeper "github.com/cosmos/ibc-go/v2/modules/core/keeper" "github.com/likecoin/likechain/x/iscn" iscnkeeper "github.com/likecoin/likechain/x/iscn/keeper" @@ -109,6 +111,8 @@ var ( distrclient.ProposalHandler, upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler, + ibcclientclient.UpdateClientProposalHandler, + ibcclientclient.UpgradeProposalHandler, ), params.AppModuleBasic{}, crisis.AppModuleBasic{}, @@ -135,7 +139,7 @@ var ( // TODO: move to other places type EncodingConfig struct { InterfaceRegistry types.InterfaceRegistry - Marshaler codec.Marshaler + Codec codec.Codec TxConfig client.TxConfig Amino *codec.LegacyAmino } @@ -144,7 +148,7 @@ type EncodingConfig struct { func MakeEncodingConfig() EncodingConfig { cdc := codec.NewLegacyAmino() interfaceRegistry := types.NewInterfaceRegistry() - marshaler := codec.NewProtoCodec(interfaceRegistry) + codec := codec.NewProtoCodec(interfaceRegistry) std.RegisterLegacyAminoCodec(cdc) std.RegisterInterfaces(interfaceRegistry) ModuleBasics.RegisterLegacyAminoCodec(cdc) @@ -152,8 +156,8 @@ func MakeEncodingConfig() EncodingConfig { return EncodingConfig{ InterfaceRegistry: interfaceRegistry, - Marshaler: marshaler, - TxConfig: authtx.NewTxConfig(marshaler, authtx.DefaultSignModes), + Codec: codec, + TxConfig: authtx.NewTxConfig(codec, authtx.DefaultSignModes), Amino: cdc, } } @@ -164,7 +168,7 @@ var _ servertypes.Application = (*LikeApp)(nil) type LikeApp struct { *baseapp.BaseApp cdc *codec.LegacyAmino - appCodec codec.Marshaler + appCodec codec.Codec interfaceRegistry types.InterfaceRegistry invCheckPeriod uint @@ -193,6 +197,9 @@ type LikeApp struct { // the module manager mm *module.Manager + + // module configurator + configurator module.Configurator } // NewLikeApp returns a reference to an initialized LikeApp. @@ -202,13 +209,13 @@ func NewLikeApp( invCheckPeriod uint, encodingConfig EncodingConfig, appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp), ) *LikeApp { - appCodec := encodingConfig.Marshaler + appCodec := encodingConfig.Codec cdc := encodingConfig.Amino interfaceRegistry := encodingConfig.InterfaceRegistry bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...) bApp.SetCommitMultiStoreTracer(traceStore) - bApp.SetAppVersion(version.Version) + bApp.SetVersion(version.Version) bApp.SetInterfaceRegistry(interfaceRegistry) keys := sdk.NewKVStoreKeys( @@ -283,7 +290,8 @@ func NewLikeApp( app.CrisisKeeper = crisiskeeper.NewKeeper( crisisSubspace, invCheckPeriod, app.BankKeeper, authtypes.FeeCollectorName, ) - app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath) + app.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, keys[upgradetypes.StoreKey], appCodec, homePath, app.BaseApp) + app.registerUpgradeHandlers() app.IscnKeeper = iscnkeeper.NewKeeper(appCodec, keys[iscntypes.StoreKey], app.AccountKeeper, app.BankKeeper, iscnSubspace) // register the staking hooks @@ -294,7 +302,7 @@ func NewLikeApp( // Create IBC Keeper app.IbcKeeper = ibckeeper.NewKeeper( - appCodec, keys[ibchost.StoreKey], ibcHostSubspace, app.StakingKeeper, scopedIBCKeeper, + appCodec, keys[ibchost.StoreKey], ibcHostSubspace, app.StakingKeeper, app.UpgradeKeeper, scopedIBCKeeper, ) // register the proposal types @@ -303,7 +311,7 @@ func NewLikeApp( AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)). AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)). AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)). - AddRoute(ibchost.RouterKey, ibcclient.NewClientUpdateProposalHandler(app.IbcKeeper.ClientKeeper)) + AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IbcKeeper.ClientKeeper)) app.GovKeeper = govkeeper.NewKeeper( appCodec, keys[govtypes.StoreKey], govSubspace, app.AccountKeeper, app.BankKeeper, &stakingKeeper, govRouter, @@ -384,8 +392,8 @@ func NewLikeApp( app.mm.RegisterInvariants(&app.CrisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), cdc) - cfg := module.NewConfigurator(app.MsgServiceRouter(), app.GRPCQueryRouter()) - app.mm.RegisterServices(cfg) + app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) + app.mm.RegisterServices(app.configurator) // initialize stores app.MountKVStores(keys) @@ -395,12 +403,20 @@ func NewLikeApp( // initialize BaseApp app.SetInitChainer(app.InitChainer) app.SetBeginBlocker(app.BeginBlocker) - app.SetAnteHandler( - ante.NewAnteHandler( - app.AccountKeeper, app.BankKeeper, ante.DefaultSigVerificationGasConsumer, - encodingConfig.TxConfig.SignModeHandler(), - ), + + anteHandler, err := ante.NewAnteHandler( + ante.HandlerOptions{ + AccountKeeper: app.AccountKeeper, + BankKeeper: app.BankKeeper, + SignModeHandler: encodingConfig.TxConfig.SignModeHandler(), + SigGasConsumer: ante.DefaultSigVerificationGasConsumer, + }, ) + if err != nil { + panic(err) + } + app.SetAnteHandler(anteHandler) + app.SetEndBlocker(app.EndBlocker) if loadLatest { @@ -408,13 +424,54 @@ func NewLikeApp( if err != nil { tmos.Exit(err.Error()) } - ctx := app.BaseApp.NewUncachedContext(true, tmproto.Header{}) - app.CapabilityKeeper.InitializeAndSeal(ctx) + app.CapabilityKeeper.Seal() } return app } +// Upgrade Handler +func (app *LikeApp) registerUpgradeHandlers() { + app.UpgradeKeeper.SetUpgradeHandler("v2.0.0", func(ctx sdk.Context, plan upgradetypes.Plan, _ module.VersionMap) (module.VersionMap, error) { + // 1st-time running in-store migrations, using 1 as fromVersion to + // avoid running InitGenesis. + fromVM := map[string]uint64{ + "auth": 1, + "bank": 1, + "capability": 1, + "crisis": 1, + "distribution": 1, + "evidence": 1, + "gov": 1, + "mint": 1, + "params": 1, + "slashing": 1, + "staking": 1, + "upgrade": 1, + "vesting": 1, + "ibc": 1, + "genutil": 1, + "transfer": 1, + } + + return app.mm.RunMigrations(ctx, app.configurator, fromVM) + }) + + upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() + if err != nil { + panic(err) + } + + if upgradeInfo.Name == "v2.0.0" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + storeUpgrades := storetypes.StoreUpgrades{ + Added: []string{"authz", "feegrant"}, + } + + // configure store loader that checks if version == upgradeHeight and applies store upgrades + app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades)) + } +} + // application updates every begin block func (app *LikeApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock { return app.mm.BeginBlock(ctx, req) @@ -448,7 +505,7 @@ func (app *LikeApp) ModuleAccountAddrs() map[string]bool { return modAccAddrs } -func (app *LikeApp) AppCodec() codec.Marshaler { +func (app *LikeApp) AppCodec() codec.Codec { return app.appCodec }