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

fix: set upgrade handler for v0.2.1 with migrations for intrechain modules #67

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (

"github.com/Nolus-Protocol/nolus-core/app/keepers"
appparams "github.com/Nolus-Protocol/nolus-core/app/params"
"github.com/Nolus-Protocol/nolus-core/app/upgrades"
v0 "github.com/Nolus-Protocol/nolus-core/app/upgrades/v0"
"github.com/Nolus-Protocol/nolus-core/docs"

"github.com/CosmWasm/wasmd/x/wasm"
Expand All @@ -49,8 +51,12 @@ const (
Name = "nolus"
)

// DefaultNodeHome default home directories for the application daemon.
var DefaultNodeHome string
var (
// DefaultNodeHome default home directories for the application daemon.
DefaultNodeHome string

Upgrades = []upgrades.Upgrade{v0.Upgrade}
)

var (
_ cosmoscmd.CosmosApp = (*App)(nil)
Expand Down Expand Up @@ -161,7 +167,7 @@ func New(
app.mm.RegisterInvariants(&app.CrisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(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
//
Expand Down Expand Up @@ -201,6 +207,9 @@ func New(
app.SetBeginBlocker(app.BeginBlocker)
app.SetInitChainer(app.InitChainer)

// TODO start using those functions to follow upgrades patterns from gaia and osmosis.
// app.setupUpgradeHandlers()
// app.setupUpgradeStoreLoaders()
// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
// Make sure it's called after `app.mm` and `app.configurator` are set.
app.RegisterUpgradeHandlers()
Expand Down Expand Up @@ -338,6 +347,38 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
}

// TODO
// configure store loader that checks if version == upgradeHeight and applies store upgrades.
// func (app *App) 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
// }

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

// func (app *App) setupUpgradeHandlers() {
// for _, upgrade := range Upgrades {
// app.UpgradeKeeper.SetUpgradeHandler(
// upgrade.UpgradeName,
// upgrade.CreateUpgradeHandler(
// app.mm,
// app.configurator,
// &app.AppKeepers,
// ),
// )
// }
// }

// GetMaccPerms returns a copy of the module account permissions.
func GetMaccPerms() map[string][]string {
dupMaccPerms := make(map[string][]string)
Expand Down
57 changes: 56 additions & 1 deletion app/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package app

import (
"encoding/json"

storetypes "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/neutron-org/neutron/x/interchainqueries"
interchainqueriestypes "github.com/neutron-org/neutron/x/interchainqueries/types"
"github.com/neutron-org/neutron/x/interchaintxs"
interchaintxstypes "github.com/neutron-org/neutron/x/interchaintxs/types"
)

func (app *App) RegisterUpgradeHandlers() {
Expand All @@ -16,6 +23,7 @@ func (app *App) RegisterUpgradeHandlers() {
app.registerUpgradeV1_43(upgradeInfo)
app.registerUpgradeV1_44(upgradeInfo)
app.registerUpgradeV2_0(upgradeInfo)
app.registerUpgradeV2_1(upgradeInfo)
}

// performs upgrade from v0.1.39 -> v0.1.43.
Expand All @@ -36,11 +44,58 @@ func (app *App) registerUpgradeV1_44(_ storetypes.UpgradeInfo) {
})
}

// performs upgrade from v0.1.43 -> v0.2.2.
// performs upgrade from v0.1.43 -> v0.2.0.
func (app *App) registerUpgradeV2_0(_ storetypes.UpgradeInfo) {
const UpgradeV2_0Plan = "v0.2.0"
app.UpgradeKeeper.SetUpgradeHandler(UpgradeV2_0Plan, func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Upgrade handler execution", "name", UpgradeV2_0Plan)
return fromVM, nil
})
}

// performs upgrade from v0.2.0 -> v0.2.1.
func (app *App) registerUpgradeV2_1(_ storetypes.UpgradeInfo) {
const UpgradeV2_0Plan = "v0.2.1"
app.UpgradeKeeper.SetUpgradeHandler(UpgradeV2_0Plan, func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Upgrade handler execution", "name", UpgradeV2_0Plan)
appCodec := app.appCodec
// Register the consensus version in the version map
// to avoid the SDK from triggering the default
// InitGenesis function.
fromVM["interchainqueries"] = interchainqueries.AppModule{}.ConsensusVersion()

// Make custom genesis state and run InitGenesis for interchainqueries
interchainQueriesCustomGenesis := interchainqueriestypes.GenesisState{
Params: interchainqueriestypes.Params{
QuerySubmitTimeout: 1036800,
QueryDeposit: sdk.NewCoins(sdk.NewCoin("unls", sdk.NewInt(1000000))),
},
RegisteredQueries: []*interchainqueriestypes.RegisteredQuery{},
}
interchainQueriesCustomGenesisJSON, err := json.Marshal(interchainQueriesCustomGenesis)
if err != nil {
return nil, err
}
app.mm.Modules["interchainqueries"].InitGenesis(ctx, appCodec, interchainQueriesCustomGenesisJSON)

// Register the consensus version in the version map
// to avoid the SDK from triggering the default
// InitGenesis function.
fromVM["interchaintxs"] = interchaintxs.AppModule{}.ConsensusVersion()

// Make custom genesis state and run InitGenesis for interchaintxs
interchainTxsCustomGenesis := interchaintxstypes.GenesisState{
Params: interchaintxstypes.Params{
MsgSubmitTxMaxMessages: 16,
},
}
interchainTxsCustomGenesisJSON, err := json.Marshal(interchainTxsCustomGenesis)
if err != nil {
return nil, err
}
app.mm.Modules["interchaintxs"].InitGenesis(ctx, appCodec, interchainTxsCustomGenesisJSON)

ctx.Logger().Info("Running migrations")
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})
}
43 changes: 43 additions & 0 deletions app/upgrades/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Nolus Upgrades

This folder contains sub-folders for every nolus upgrade. (Both state
migrations, and hard forks) It also defines upgrade & hard fork structs,
that each upgrade implements. These then get included in the application
app.go to run the upgrade.

## Version History


## Upgrade types

Currently in Nolus we support only one upgrade type, `Upgrade`.
If we need to support more upgrade types, like `Fork`, we can add them.
An `Upgrade` defines an upgrade that is to be acted upon by state migrations from the
SDK `x/upgrade` module.

<!-- A `Fork` defines a hard fork that changes some
logic at a block height. If the goal is to have a new binary be
compatible with the old binary prior to the upgrade height, as is the
case for all osmosis `Fork`s, then all logic changes must be
height-gated or in the `BeginForkLogic` code. -->

```go
type Upgrade struct {
// Upgrade version name, for the upgrade handler, e.g. `v7`
UpgradeName string
// Function that creates an upgrade handler
CreateUpgradeHandler func(mm *module.Manager, configurator module.Configurator, keepers *keepers.AppKeepers) upgradetypes.UpgradeHandler
// Store upgrades, should be used for any new modules introduced, new modules deleted, or store names renamed.
StoreUpgrades store.StoreUpgrades
}

// 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)
// }
```
39 changes: 39 additions & 0 deletions app/upgrades/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package upgrades

import (
"github.com/Nolus-Protocol/nolus-core/app/keepers"

store "github.com/cosmos/cosmos-sdk/store/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. `v7`
UpgradeName string

// CreateUpgradeHandler defines the function that creates an upgrade handler
CreateUpgradeHandler func(*module.Manager, module.Configurator, *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)
// }
26 changes: 26 additions & 0 deletions app/upgrades/v0/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v0

import (
"github.com/Nolus-Protocol/nolus-core/app/upgrades"

store "github.com/cosmos/cosmos-sdk/store/types"
interchainqueries "github.com/neutron-org/neutron/x/interchainqueries/types"
interchaintxs "github.com/neutron-org/neutron/x/interchaintxs/types"
)

// TODO Start using this method to upgrade the app after export app state is fixed.
const (
// UpgradeName defines the on-chain upgrade name.
UpgradeName = "v0.2.1"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{
interchainqueries.ModuleName,
interchaintxs.ModuleName,
},
},
}
20 changes: 20 additions & 0 deletions app/upgrades/v0/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package v0

import (
"github.com/Nolus-Protocol/nolus-core/app/keepers"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

// TODO Start using this method to upgrade the app after export app state is fixed.
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
keepers *keepers.AppKeepers,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
return mm.RunMigrations(ctx, configurator, fromVM)
}
}