feat: add migration to v0.8.0

Take x/dao and x/machine default parameters and store them into the
respective module state.

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
Julian Strobl 2024-02-26 08:20:05 +01:00
parent 4ab279033c
commit 412e15449b
7 changed files with 116 additions and 2 deletions

View File

@ -791,6 +791,8 @@ func New(
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.setupUpgradeHandlers()
if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
@ -982,3 +984,13 @@ func (app *App) SimulationManager() *module.SimulationManager {
func (app *App) ModuleManager() *module.Manager {
return app.mm
}
func (app *App) setupUpgradeHandlers() {
app.UpgradeKeeper.SetUpgradeHandler("v0.8.0", func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// Set versions to the latest ConsensusVersion in the VersionMap.
// This will skip running InitGenesis on Dao
fromVM[daomoduletypes.ModuleName] = daomodule.AppModule{}.ConsensusVersion()
fromVM[machinemoduletypes.ModuleName] = machinemodule.AppModule{}.ConsensusVersion()
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})
}

View File

@ -0,0 +1,21 @@
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/planetmint/planetmint-go/x/dao/migrations/v2"
)
// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}
// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}
// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}

View File

@ -0,0 +1,25 @@
package v2
import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/x/dao/types"
)
// MigrateStore migrates the x/dao module state from the consensus version 1 to
// version 2. Specifically, it takes the default params and stores them
// directly into the x/dao module state.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
params := types.DefaultParams()
bz, err := cdc.Marshal(&params)
if err != nil {
return err
}
store.Set(types.KeyPrefix(types.ParamsKey), bz)
return nil
}

View File

@ -117,6 +117,11 @@ func NewAppModule(
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate1to2); err != nil {
panic(fmt.Errorf("failed to register migration of %s to v2: %w", types.ModuleName, err))
}
}
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
@ -140,7 +145,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}
// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }
// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {

View File

@ -0,0 +1,21 @@
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/planetmint/planetmint-go/x/machine/migrations/v2"
)
// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}
// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}
// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}

View File

@ -0,0 +1,25 @@
package v2
import (
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/x/machine/types"
)
// MigrateStore migrates the x/machine module state from the consensus version
// 1 to version 2. Specifically, it takes the default params and stores them
// directly into the x/machine module state.
func MigrateStore(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
params := types.DefaultParams()
bz, err := cdc.Marshal(&params)
if err != nil {
return err
}
store.Set(types.KeyPrefix(types.ParamsKey), bz)
return nil
}

View File

@ -116,6 +116,11 @@ func NewAppModule(
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate1to2); err != nil {
panic(fmt.Errorf("failed to register migration of %s to v2: %w", types.ModuleName, err))
}
}
// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
@ -141,7 +146,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}
// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }
// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {