Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dang/upgrade handler #200

Merged
merged 12 commits into from
Jul 19, 2023
Merged
62 changes: 54 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ import (
icq "github.com/strangelove-ventures/async-icq/v7"
icqtypes "github.com/strangelove-ventures/async-icq/v7/types"

reward "github.com/notional-labs/centauri/v3/app/upgrade/reward"
custombankmodule "github.com/notional-labs/centauri/v3/custom/bank"
"github.com/strangelove-ventures/packet-forward-middleware/v7/router"
routertypes "github.com/strangelove-ventures/packet-forward-middleware/v7/router/types"
Expand All @@ -104,12 +103,14 @@ import (
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

upgrades "github.com/notional-labs/centauri/v3/app/upgrades"
)

const (
Name = "centauri"
dirName = "banksy"
ForkHeight = 244008
Name = "centauri"
dirName = "banksy"
ForkHeight = 244008
)

var (
Expand All @@ -120,6 +121,8 @@ var (
// of "EnableAllProposals" (takes precedence over ProposalsEnabled)
// https://github.com/CosmWasm/wasmd/blob/02a54d33ff2c064f3539ae12d75d027d9c665f05/x/wasm/internal/types/proposal.go#L28-L34
EnableSpecificProposals = ""

Upgrades = []upgrades.Upgrade{}
)

// GetEnabledProposals parses the ProposalsEnabled / EnableSpecificProposals values to
Expand Down Expand Up @@ -287,7 +290,7 @@ func NewCentauriApp(
skipUpgradeHeights,
homePath,
)

app.setupUpgradeStoreLoaders()
app.InitNormalKeepers(
appCodec,
cdc,
Expand Down Expand Up @@ -445,6 +448,8 @@ func NewCentauriApp(
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
// app.sm = module.NewSimulationManager(
// auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
Expand Down Expand Up @@ -505,9 +510,6 @@ func NewCentauriApp(
}
}

// app.ScopedMonitoringKeeper = scopedMonitoringKeeper
app.UpgradeKeeper.SetUpgradeHandler(reward.UpgradeName, reward.CreateUpgradeHandler(app.mm, app.configurator, app.TransferMiddlewareKeeper, app.MintKeeper))

return app
}

Expand Down Expand Up @@ -646,3 +648,47 @@ func GetMaccPerms() map[string][]string {
func (app *CentauriApp) SimulationManager() *module.SimulationManager {
return app.sm
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
func (app *CentauriApp) setupUpgradeStoreLoaders() {
upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(fmt.Sprintf("failed to read upgrade info from disk %s", err))
}

if app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
return
}

currentHeight := app.CommitMultiStore().LastCommitID().Version

if upgradeInfo.Height == currentHeight+1 {
app.customPreUpgradeHandler(upgradeInfo)

Check warning

Code scanning / CodeQL

Expression has no effect Warning

This expression has no effect.
}

for _, upgrade := range Upgrades {
if upgradeInfo.Name == upgrade.UpgradeName {
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &upgrade.StoreUpgrades))
}
}
}

func (app *CentauriApp) customPreUpgradeHandler(upgradeInfo upgradetypes.Plan) {
switch upgradeInfo.Name {
default:
}
}

func (app *CentauriApp) setupUpgradeHandlers() {
for _, upgrade := range Upgrades {
app.UpgradeKeeper.SetUpgradeHandler(
upgrade.UpgradeName,
upgrade.CreateUpgradeHandler(
app.mm,
app.configurator,
app.BaseApp,
&app.AppKeepers,
),
)
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package upgrades

import (
types "github.com/cometbft/cometbft/proto/tendermint/types"
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"

"github.com/notional-labs/centauri/v3/app/keepers"
)

// BaseAppParamManager defines an interrace that BaseApp is expected to fullfil
// that allows upgrade handlers to modify BaseApp parameters.
type BaseAppParamManager interface {
GetConsensusParams(ctx sdk.Context) *types.ConsensusParams
StoreConsensusParams(ctx sdk.Context, cp *types.ConsensusParams)
}

// 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. `v7`
UpgradeName string

// CreateUpgradeHandler defines the function that creates an upgrade handler
CreateUpgradeHandler func(*module.Manager, module.Configurator, BaseAppParamManager, *keepers.AppKeepers) 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, keepers *keepers.AppKeepers)
}