generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mint): add migration script to migrate x/param
- Loading branch information
Showing
7 changed files
with
139 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¶ms) | ||
|
||
if err := params.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
bz, err := cdc.Marshal(¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
store.Set(types.ParamsKey, bz) | ||
|
||
logger.Debug("module migration done") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |