Skip to content

Commit

Permalink
feat: implement migration from v2 to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Mar 22, 2023
1 parent e00048a commit 19717ce
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 5 deletions.
5 changes: 5 additions & 0 deletions x/logic/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/okp4/okp4d/x/logic/migrations/v2"
v3 "github.com/okp4/okp4d/x/logic/migrations/v3"
)

type Migrator struct {
Expand All @@ -16,3 +17,7 @@ func NewMigrator(keeper Keeper) Migrator {
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.paramstore, m.keeper.cdc)
}

func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v3.MigrateStore(ctx, m.keeper.paramstore, m.keeper.cdc)
}
8 changes: 6 additions & 2 deletions x/logic/migrations/v2/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
)

func MigrateStore(ctx sdk.Context, paramstore paramtypes.Subspace, cdc codec.BinaryCodec) error {
logger := ctx.Logger().With("migration", "logic")
logger := ctx.Logger().
With("module", "logic").
With("migration", "v2")

logger.Debug("start module migration")
logger.Debug("starting module migration")

// Add default params keys / values
logger.Debug("set params default values")
params := types.DefaultParams()
paramstore.SetParamSet(ctx, &params)

logger.Debug("module migration done")

return nil
}
18 changes: 18 additions & 0 deletions x/logic/migrations/v2/types/params.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package types

import paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

// Parameter store keys.
var (
KeyInterpreter = []byte("Interpreter")
KeyLimits = []byte("Limits")
)

// String implements the Stringer interface.
func (p *Params) String() string {
return p.Interpreter.String() + "\n" +
p.Limits.String()
}

// ParamSetPairs get the params.ParamSet.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
noopValidator := func(i interface{}) error { return nil }

return []paramtypes.ParamSetPair{
paramtypes.NewParamSetPair(KeyInterpreter, &p.Interpreter, noopValidator),
paramtypes.NewParamSetPair(KeyLimits, &p.Limits, noopValidator),
}
}
39 changes: 39 additions & 0 deletions x/logic/migrations/v3/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package v2

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
v2types "github.com/okp4/okp4d/x/logic/migrations/v2/types"
"github.com/okp4/okp4d/x/logic/types"
)

func MigrateStore(ctx sdk.Context, paramstore paramtypes.Subspace, cdc codec.BinaryCodec) error {
logger := ctx.Logger().
With("module", "logic").
With("migration", "v3")

logger.Debug("starting module migration")

logger.Debug("migrate logic params")
var oldParams v2types.Params
paramstore.GetParamSet(ctx, &oldParams)

newParams := types.Params{
Interpreter: types.Interpreter{
Bootstrap: oldParams.Interpreter.Bootstrap,
PredicatesWhitelist: oldParams.Interpreter.RegisteredPredicates,
PredicatesBlacklist: []string{},
},
Limits: types.Limits{
MaxGas: oldParams.Limits.MaxGas,
MaxSize: oldParams.Limits.MaxSize,
MaxResultCount: oldParams.Limits.MaxResultCount,
},
}
paramstore.SetParamSet(ctx, &newParams)

logger.Debug("module migration done")

return nil
}
17 changes: 14 additions & 3 deletions x/logic/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,19 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {

migrator := keeper.NewMigrator(am.keeper)

if err := cfg.RegisterMigration(types.ModuleName, 1, migrator.Migrate1to2); err != nil {
panic(fmt.Errorf("failed to migrate %s to v2: %w", types.ModuleName, err))
migrations := []struct {
fromVersion uint64
migrator func(ctx sdk.Context) error
}{
{1, migrator.Migrate1to2},
{2, migrator.Migrate2to3},
}

for _, migration := range migrations {
if err := cfg.RegisterMigration(types.ModuleName, migration.fromVersion, migration.migrator); err != nil {
panic(fmt.Errorf("failed to migrate %s from version %d to v%d: %w",
types.ModuleName, migration.fromVersion, migration.fromVersion+1, err))
}
}
}

Expand All @@ -159,7 +170,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 2 }
func (AppModule) ConsensusVersion() uint64 { return 3 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down

0 comments on commit 19717ce

Please sign in to comment.