From c0e74d294f36882aa2c18a785d753349c2a322ef Mon Sep 17 00:00:00 2001 From: gopherjk121 Date: Mon, 24 Oct 2022 18:02:01 +0800 Subject: [PATCH] add v1.3.0 upgrade handler --- app/app.go | 18 ++++++++++++++++ app/upgrades/types.go | 38 ++++++++++++++++++++++++++++++++++ app/upgrades/v130/constants.go | 20 ++++++++++++++++++ app/upgrades/v130/upgrades.go | 18 ++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 app/upgrades/types.go create mode 100644 app/upgrades/v130/constants.go create mode 100644 app/upgrades/v130/upgrades.go diff --git a/app/app.go b/app/app.go index 3817ec7..4903d45 100644 --- a/app/app.go +++ b/app/app.go @@ -9,6 +9,8 @@ import ( "path/filepath" "strings" + "github.com/TERITORI/teritori-chain/app/upgrades" + v130 "github.com/TERITORI/teritori-chain/app/upgrades/v130" airdrop "github.com/TERITORI/teritori-chain/x/airdrop" airdropkeeper "github.com/TERITORI/teritori-chain/x/airdrop/keeper" airdroptypes "github.com/TERITORI/teritori-chain/x/airdrop/types" @@ -178,6 +180,8 @@ var ( // DefaultNodeHome default home directories for the application daemon DefaultNodeHome string + Upgrades = []upgrades.Upgrade{v130.Upgrade} + // ModuleBasics defines the module BasicManager is in charge of setting up basic, // non-dependant module elements, such as codec registration // and genesis verification. @@ -674,6 +678,8 @@ func NewTeritoriApp( app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()) app.mm.RegisterServices(app.configurator) + app.setupUpgradeHandlers() + // create the simulation manager and define the order of the modules for deterministic simulations // // NOTE: this is not required apps that don't use the simulator for fuzz testing @@ -920,3 +926,15 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino return paramsKeeper } + +func (app *TeritoriApp) setupUpgradeHandlers() { + for _, upgrade := range Upgrades { + app.UpgradeKeeper.SetUpgradeHandler( + upgrade.UpgradeName, + upgrade.CreateUpgradeHandler( + app.mm, + app.configurator, + ), + ) + } +} diff --git a/app/upgrades/types.go b/app/upgrades/types.go new file mode 100644 index 0000000..21b0280 --- /dev/null +++ b/app/upgrades/types.go @@ -0,0 +1,38 @@ +package upgrades + +import ( + store "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" +) + +// Upgrade defines a struct containing necessary fields that a SoftwareUpgradeProposal +// must have written, in order for the state migration to go smoothly. +// An upgrade must implement this struct, and then set it in the app.go. +// The app.go will then define the handler. +type Upgrade struct { + // Upgrade version name, for the upgrade handler, e.g. `v1.3.0` + UpgradeName string + + // CreateUpgradeHandler defines the function that creates an upgrade handler + CreateUpgradeHandler func(*module.Manager, module.Configurator) upgradetypes.UpgradeHandler + + // Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed. + StoreUpgrades store.StoreUpgrades +} + +// Fork defines a struct containing the requisite fields for a non-software upgrade proposal +// Hard Fork at a given height to implement. +// There is one time code that can be added for the start of the Fork, in `BeginForkLogic`. +// Any other change in the code should be height-gated, if the goal is to have old and new binaries +// to be compatible prior to the upgrade height. +type Fork struct { + // Upgrade version name, for the upgrade handler, e.g. `v7` + UpgradeName string + // height the upgrade occurs at + UpgradeHeight int64 + + // Function that runs some custom state transition code at the beginning of a fork. + BeginForkLogic func(ctx sdk.Context) +} diff --git a/app/upgrades/v130/constants.go b/app/upgrades/v130/constants.go new file mode 100644 index 0000000..507f793 --- /dev/null +++ b/app/upgrades/v130/constants.go @@ -0,0 +1,20 @@ +package v130 + +import ( + store "github.com/cosmos/cosmos-sdk/store/types" + + "github.com/TERITORI/teritori-chain/app/upgrades" +) + +const ( + // UpgradeName defines the on-chain upgrade name. + UpgradeName = "v1.3.0" +) + +var Upgrade = upgrades.Upgrade{ + UpgradeName: UpgradeName, + CreateUpgradeHandler: CreateUpgradeHandler, + StoreUpgrades: store.StoreUpgrades{ + Added: []string{}, + }, +} diff --git a/app/upgrades/v130/upgrades.go b/app/upgrades/v130/upgrades.go new file mode 100644 index 0000000..c74a951 --- /dev/null +++ b/app/upgrades/v130/upgrades.go @@ -0,0 +1,18 @@ +package v130 + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" +) + +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, +) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + ctx.Logger().Info("start to run module migrations...") + + return mm.RunMigrations(ctx, configurator, vm) + } +}