Skip to content

Commit

Permalink
refactor(mint): add migration script to migrate x/param
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 29, 2023
1 parent d35928b commit 403b168
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ func New(
groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
crisis.NewAppModule(&app.CrisisKeeper, skipGenesisInvariants),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, app.GetSubspace(minttypes.ModuleName)),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
Expand Down Expand Up @@ -774,7 +774,7 @@ func New(
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, app.GetSubspace(minttypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
Expand Down
19 changes: 19 additions & 0 deletions x/mint/exported/exported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package exported

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

type (
ParamSet = paramtypes.ParamSet

// Subspace defines an interface that implements the legacy x/params Subspace
// type.
//
// NOTE: This is used solely for migration of x/params managed parameters.
Subspace interface {
GetParamSet(ctx sdk.Context, ps ParamSet)
WithKeyTable(table paramtypes.KeyTable) paramtypes.Subspace
}
)
23 changes: 23 additions & 0 deletions x/mint/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okp4/okp4d/x/mint/exported"
v2 "github.com/okp4/okp4d/x/mint/migrations/v2"
)

type Migrator struct {
keeper Keeper
legacySubspace exported.Subspace
}

func NewMigrator(keeper Keeper, legacySubspace exported.Subspace) Migrator {
return Migrator{
keeper: keeper,
legacySubspace: legacySubspace,
}
}

func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.legacySubspace)
}
48 changes: 48 additions & 0 deletions x/mint/migrations/v2/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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/okp4/okp4d/x/mint/exported"
"github.com/okp4/okp4d/x/mint/types"
)

// MigrateStore migrates the x/mint module state from the consensus version 1 to
// version 2.
// Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/mint
// module state.
func MigrateStore(ctx sdk.Context,
storeKey storetypes.StoreKey,
cdc codec.BinaryCodec,
legacySubspace exported.Subspace,
) error {
logger := ctx.Logger().
With("module", "mint").
With("migration", "v2")

logger.Debug("starting module migration")

logger.Debug("migrate mint params")

store := ctx.KVStore(storeKey)

var params types.Params
legacySubspace.WithKeyTable(types.ParamKeyTable()).
GetParamSet(ctx, &params)

if err := params.Validate(); err != nil {
return err
}

bz, err := cdc.Marshal(&params)
if err != nil {
return err
}
store.Set(types.ParamsKey, bz)

logger.Debug("module migration done")

return nil
}
22 changes: 21 additions & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/okp4/okp4d/x/mint/exported"
"github.com/spf13/cobra"
abci "github.com/tendermint/tendermint/abci/types"

Expand Down Expand Up @@ -85,14 +86,17 @@ type AppModule struct {

keeper keeper.Keeper
authKeeper types.AccountKeeper
// legacySubspace is used principally for migration
legacySubspace exported.Subspace
}

// NewAppModule creates a new AppModule object.
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper) AppModule {
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, legacySubspace exported.Subspace) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
authKeeper: ak,
legacySubspace: legacySubspace,
}
}

Expand Down Expand Up @@ -122,6 +126,22 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServiceServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

migrator := keeper.NewMigrator(am.keeper, am.legacySubspace)

migrations := []struct {
fromVersion uint64
migrator func(ctx sdk.Context) error
}{
{1, migrator.Migrate1to2},
}

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))
}
}
}

// InitGenesis performs genesis initialization for the mint module. It returns
Expand Down
17 changes: 1 addition & 16 deletions x/mint/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"sigs.k8s.io/yaml"

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

// Parameter store keys.
Expand All @@ -18,11 +17,6 @@ var (
KeyBlocksPerYear = []byte("BlocksPerYear")
)

// ParamTable for minting module.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

func NewParams(
mintDenom string, annualReductionFactor sdk.Dec, blocksPerYear uint64,
) Params {
Expand All @@ -38,7 +32,7 @@ func DefaultParams() Params {
return Params{
MintDenom: sdk.DefaultBondDenom,
AnnualReductionFactor: sdk.NewDecWithPrec(20, 2), // Tha annual reduction factor is configured to 20% per year
BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5 second block times
BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times
}
}

Expand All @@ -63,15 +57,6 @@ func (p Params) String() string {
return string(out)
}

// Implements params.ParamSet.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom),
paramtypes.NewParamSetPair(KeyAnnualReductionFactor, &p.AnnualReductionFactor, validateAnnualReductionFactor),
paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear),
}
}

func validateMintDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
Expand Down
25 changes: 25 additions & 0 deletions x/mint/types/params_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package types

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

// ParamKeyTable for minting module.
//
// Deprecated: kept for migration purpose,
// will be removed soon.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

var _ paramtypes.ParamSet = (*Params)(nil)

// ParamSetPairs Implements params.ParamSet.
//
// Deprecated: kept for migration purpose,
// will be removed soon.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom),
paramtypes.NewParamSetPair(KeyAnnualReductionFactor, &p.AnnualReductionFactor, validateAnnualReductionFactor),
paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear),
}
}

0 comments on commit 403b168

Please sign in to comment.