Skip to content

Commit

Permalink
add & register params migration
Browse files Browse the repository at this point in the history
  • Loading branch information
harish551 committed Sep 23, 2023
1 parent c54f7c9 commit 5f0797e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 6 deletions.
7 changes: 4 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,15 @@ func New(
app.AccountKeeper,
app.BankKeeper,
app.DistrKeeper,
app.GetSubspace(onfttypes.ModuleName),
govModAddress,
)
onftModule := onft.NewAppModule(
appCodec,
app.ONFTKeeper,
app.AccountKeeper,
app.BankKeeper,
app.DistrKeeper,
app.GetSubspace(onfttypes.ModuleName),
)

// Create static IBC router, add transfer route, then set and seal it
Expand Down Expand Up @@ -739,7 +740,7 @@ func (app *App) GetSubspace(moduleName string) paramstypes.Subspace {

// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
func (app *App) RegisterAPIRoutes(apiSvr *api.Server, _ config.APIConfig) {
clientCtx := apiSvr.ClientCtx

// Register new tx routes from grpc-gateway.
Expand Down Expand Up @@ -797,7 +798,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino

paramsKeeper.Subspace(authtypes.ModuleName)
paramsKeeper.Subspace(banktypes.ModuleName)
paramsKeeper.Subspace(stakingtypes.ModuleName).WithKeyTable(stakingtypes.ParamKeyTable())
paramsKeeper.Subspace(stakingtypes.ModuleName)
paramsKeeper.Subspace(minttypes.ModuleName)
paramsKeeper.Subspace(distrtypes.ModuleName)
paramsKeeper.Subspace(slashingtypes.ModuleName)
Expand Down
13 changes: 13 additions & 0 deletions exported/onft.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package exported

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

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -20,3 +21,15 @@ type ONFTI interface {
GetCreatedTime() time.Time
GetRoyaltyShare() sdk.Dec
}

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)
}
)
28 changes: 28 additions & 0 deletions keeper/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package keeper

import (
"github.com/OmniFlix/onft/exported"
v2 "github.com/OmniFlix/onft/migrations/v2"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Migrator is a struct for handling in-place state migrations.
type Migrator struct {
keeper Keeper
legacySubspace exported.Subspace
}

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

// Migrate1to2 migrates the onft 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 onft
// module state.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc)
}
37 changes: 37 additions & 0 deletions migrations/v2/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package v2

import (
"github.com/OmniFlix/onft/exported"
"github.com/OmniFlix/onft/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
ModuleName = "onft"
)

var ParamsKey = []byte{0x07}

// Migrate migrates the onft 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 onft
// module state.
func Migrate(
ctx sdk.Context,
store sdk.KVStore,
legacySubspace exported.Subspace,
cdc codec.BinaryCodec,
) error {
var currParams types.Params
legacySubspace.GetParamSet(ctx, &currParams)

if err := currParams.ValidateBasic(); err != nil {
return err
}

bz := cdc.MustMarshal(&currParams)
store.Set(ParamsKey, bz)

return nil
}
45 changes: 45 additions & 0 deletions migrations/v2/migrator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v2_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/OmniFlix/onft"
"github.com/OmniFlix/onft/exported"
v2 "github.com/OmniFlix/onft/migrations/v2"
"github.com/OmniFlix/onft/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
)

type mockSubspace struct {
ps types.Params
}

func newMockSubspace(ps types.Params) mockSubspace {
return mockSubspace{ps: ps}
}

func (ms mockSubspace) GetParamSet(ctx sdk.Context, ps exported.ParamSet) {
*ps.(*types.Params) = ms.ps
}

func TestMigrate(t *testing.T) {
encCfg := moduletestutil.MakeTestEncodingConfig(onft.AppModuleBasic{})
cdc := encCfg.Codec

storeKey := sdk.NewKVStoreKey(v2.ModuleName)
tKey := sdk.NewTransientStoreKey("transient_test")
ctx := testutil.DefaultContext(storeKey, tKey)
store := ctx.KVStore(storeKey)

legacySubspace := newMockSubspace(types.DefaultParams())
require.NoError(t, v2.Migrate(ctx, store, legacySubspace, cdc))

var res types.Params
bz := store.Get(v2.ParamsKey)
require.NoError(t, cdc.Unmarshal(bz, &res))
require.Equal(t, legacySubspace.ps, res)
}
22 changes: 19 additions & 3 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/OmniFlix/onft/exported"

"github.com/OmniFlix/onft/simulation"
abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -28,6 +29,9 @@ var (
_ module.AppModuleSimulation = AppModule{}
)

// ConsensusVersion defines the current onft module consensus version.
const ConsensusVersion = 2

type AppModuleBasic struct {
cdc codec.Codec
}
Expand Down Expand Up @@ -73,21 +77,23 @@ type AppModule struct {
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distributionKeeper types.DistributionKeeper
legacySubspace exported.Subspace
}

func (am AppModule) RegisterQueryService(server grpc.Server) {
types.RegisterQueryServer(server, am.keeper)
}

func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper, distrKeeper types.DistributionKeeper,
bankKeeper types.BankKeeper, distrKeeper types.DistributionKeeper, ss exported.Subspace,
) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc},
keeper: keeper,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distributionKeeper: distrKeeper,
legacySubspace: ss,
}
}

Expand All @@ -99,12 +105,20 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
func (AppModule) QuerierRoute() string { return types.RouterKey }

func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx))
if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}

func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
// x/params migration
m := keeper.NewMigrator(am.keeper, am.legacySubspace)

if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Sprintf("failed to migrate %s from version 1 to 2: %v", types.ModuleName, err))
}
}

func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
Expand All @@ -120,7 +134,9 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
gs := ExportGenesis(ctx, am.keeper)
return cdc.MustMarshalJSON(gs)
}
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 {
return ConsensusVersion
}

func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}

Expand Down

0 comments on commit 5f0797e

Please sign in to comment.