Skip to content

Commit

Permalink
add v1.3.0 upgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
go7066 committed Oct 24, 2022
1 parent 44bc84a commit c0e74d2
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
),
)
}
}
38 changes: 38 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions app/upgrades/v130/constants.go
Original file line number Diff line number Diff line change
@@ -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{},
},
}
18 changes: 18 additions & 0 deletions app/upgrades/v130/upgrades.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit c0e74d2

Please sign in to comment.