From 412e15449b9ae77894748324475a872c736c7d02 Mon Sep 17 00:00:00 2001 From: Julian Strobl Date: Mon, 26 Feb 2024 08:20:05 +0100 Subject: [PATCH] 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 --- app/app.go | 12 ++++++++++++ x/dao/keeper/migrations.go | 21 +++++++++++++++++++++ x/dao/migrations/v2/migrate.go | 25 +++++++++++++++++++++++++ x/dao/module.go | 7 ++++++- x/machine/keeper/migrations.go | 21 +++++++++++++++++++++ x/machine/migrations/v2/migrate.go | 25 +++++++++++++++++++++++++ x/machine/module.go | 7 ++++++- 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 x/dao/keeper/migrations.go create mode 100644 x/dao/migrations/v2/migrate.go create mode 100644 x/machine/keeper/migrations.go create mode 100644 x/machine/migrations/v2/migrate.go diff --git a/app/app.go b/app/app.go index d092654..a970c25 100644 --- a/app/app.go +++ b/app/app.go @@ -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) + }) +} diff --git a/x/dao/keeper/migrations.go b/x/dao/keeper/migrations.go new file mode 100644 index 0000000..bb4862e --- /dev/null +++ b/x/dao/keeper/migrations.go @@ -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) +} diff --git a/x/dao/migrations/v2/migrate.go b/x/dao/migrations/v2/migrate.go new file mode 100644 index 0000000..82dbd3c --- /dev/null +++ b/x/dao/migrations/v2/migrate.go @@ -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(¶ms) + if err != nil { + return err + } + + store.Set(types.KeyPrefix(types.ParamsKey), bz) + + return nil +} diff --git a/x/dao/module.go b/x/dao/module.go index 40f18c4..842f58e 100644 --- a/x/dao/module.go +++ b/x/dao/module.go @@ -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) { diff --git a/x/machine/keeper/migrations.go b/x/machine/keeper/migrations.go new file mode 100644 index 0000000..decb5a1 --- /dev/null +++ b/x/machine/keeper/migrations.go @@ -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) +} diff --git a/x/machine/migrations/v2/migrate.go b/x/machine/migrations/v2/migrate.go new file mode 100644 index 0000000..f9fa4ed --- /dev/null +++ b/x/machine/migrations/v2/migrate.go @@ -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(¶ms) + if err != nil { + return err + } + + store.Set(types.KeyPrefix(types.ParamsKey), bz) + + return nil +} diff --git a/x/machine/module.go b/x/machine/module.go index f6c2112..c028031 100644 --- a/x/machine/module.go +++ b/x/machine/module.go @@ -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) {