From c8fb3eeb90cfc8f6e30e3f15c521e043a898c9ce Mon Sep 17 00:00:00 2001 From: jiujiteiro Date: Fri, 12 Aug 2022 10:57:54 -0400 Subject: [PATCH] upgrade tests --- app/app.go | 50 +++++++++++++++++++++++++++++++++--- app/upgrades/v2/constants.go | 10 ++++++++ app/upgrades/v2/upgrades.go | 20 +++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 app/upgrades/v2/constants.go create mode 100644 app/upgrades/v2/upgrades.go diff --git a/app/app.go b/app/app.go index 4263693f..45476584 100644 --- a/app/app.go +++ b/app/app.go @@ -1,8 +1,11 @@ package app import ( + "fmt" + storetypes "github.com/cosmos/cosmos-sdk/store/types" ibcclientclient "github.com/cosmos/ibc-go/v3/modules/core/02-client/client" ibcclienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" + v2 "github.com/realiotech/realio-network/v1/app/upgrades/v2" evmante "github.com/tharsis/ethermint/app/ante" srvflags "github.com/tharsis/ethermint/server/flags" @@ -239,6 +242,9 @@ type App struct { // sm is the simulation manager sm *module.SimulationManager + + // the configurator + configurator module.Configurator } // New returns a reference to an initialized blockchain app @@ -527,7 +533,8 @@ func New( app.mm.RegisterInvariants(&app.CrisisKeeper) app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino) - app.mm.RegisterServices(module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())) + app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) + app.mm.RegisterServices(app.configurator) // create the simulation manager and define the order of the modules for deterministic simulations app.sm = module.NewSimulationManager( @@ -583,6 +590,8 @@ func New( app.SetEndBlocker(app.EndBlocker) + app.setupUpgradeHandlers() + if loadLatest { if err := app.LoadLatestVersion(); err != nil { tmos.Exit(err.Error()) @@ -727,6 +736,11 @@ func GetMaccPerms() map[string][]string { return dupMaccPerms } +// SimulationManager implements the SimulationApp interface +func (app *App) SimulationManager() *module.SimulationManager { + return app.sm +} + // initParamsKeeper init params keeper and its subspaces func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino, key, tkey sdk.StoreKey) paramskeeper.Keeper { paramsKeeper := paramskeeper.NewKeeper(appCodec, legacyAmino, key, tkey) @@ -749,7 +763,35 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino return paramsKeeper } -// SimulationManager implements the SimulationApp interface -func (app *App) SimulationManager() *module.SimulationManager { - return app.sm +func (app *App) setupUpgradeHandlers() { + // add upgrade handlers here + // stub for v2 upgrade handler + app.UpgradeKeeper.SetUpgradeHandler( + v2.UpgradeName, + v2.CreateUpgradeHandler(app.mm, app.configurator), + ) + + // When a planned update height is reached, the old binary will panic + // writing on disk the height and name of the update that triggered it + // This will read that value, and execute the preparations for the upgrade. + upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() + if err != nil { + panic(fmt.Errorf("failed to read upgrade info from disk: %w", err)) + } + + if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) { + return + } + + var storeUpgrades *storetypes.StoreUpgrades + + switch upgradeInfo.Name { + case v2.UpgradeName: + // no store upgrades in v2 + } + + if storeUpgrades != nil { + // configure store loader that checks if version == upgradeHeight and applies store upgrades + app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, storeUpgrades)) + } } diff --git a/app/upgrades/v2/constants.go b/app/upgrades/v2/constants.go new file mode 100644 index 00000000..b1009b09 --- /dev/null +++ b/app/upgrades/v2/constants.go @@ -0,0 +1,10 @@ +package v2 + +const ( + // UpgradeName is the shared upgrade plan name for mainnet and testnet + UpgradeName = "v2.0.0" + // MainnetUpgradeHeight defines the Evmos mainnet block height on which the upgrade will take place + MainnetUpgradeHeight = 58700 // (24 * 60 * 60) / 6 + 44300 + // UpgradeInfo defines the binaries that will be used for the upgrade + UpgradeInfo = `'{"binaries":{"darwin/arm64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Darwin_arm64.tar.gz","darwin/x86_64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Darwin_x86_64.tar.gz","linux/arm64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Linux_arm64.tar.gz","linux/x86_64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Linux_x86_64.tar.gz","windows/x86_64":"https://github.com/realiotech/realio-network/releases/download/v0.1.0/realio-network_0.1.0_Windows_x86_64.zip"}}'` +) diff --git a/app/upgrades/v2/upgrades.go b/app/upgrades/v2/upgrades.go new file mode 100644 index 00000000..dc6e22f5 --- /dev/null +++ b/app/upgrades/v2/upgrades.go @@ -0,0 +1,20 @@ +package v2 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" +) + +// CreateUpgradeHandler creates an SDK upgrade handler for v2 +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, +) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + // Refs: + // - https://docs.cosmos.network/master/building-modules/upgrade.html#registering-migrations + // - https://docs.cosmos.network/master/migrations/chain-upgrade-guide-044.html#chain-upgrade + return mm.RunMigrations(ctx, configurator, vm) + } +}