Skip to content

Commit

Permalink
chore: migrate to core modules (#7866)
Browse files Browse the repository at this point in the history
* core modules

* fmt

* fix broken tests

* lint fix

* actually, we can do wasm for now. this will work fine.

* appmodulev2 -> appmodule

---------

Co-authored-by: Gjermund Garaba <[email protected]>
  • Loading branch information
technicallyty and gjermundgaraba authored Jan 25, 2025
1 parent 4e54444 commit dcd6a3e
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 102 deletions.
37 changes: 21 additions & 16 deletions modules/apps/27-interchain-accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
coreregistry "cosmossdk.io/core/registry"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

Expand All @@ -34,10 +34,10 @@ var (
_ appmodule.AppModule = (*AppModule)(nil)
_ appmodule.HasConsensusVersion = (*AppModule)(nil)
_ appmodule.HasRegisterInterfaces = (*AppModule)(nil)
_ appmodule.HasMigrations = (*AppModule)(nil)

_ module.AppModule = (*AppModule)(nil)
_ module.HasGenesis = (*AppModule)(nil)
_ module.HasServices = (*AppModule)(nil)
_ module.HasGRPCGateway = (*AppModule)(nil)

// Sims
Expand Down Expand Up @@ -123,28 +123,33 @@ func (AppModule) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}

// RegisterServices registers module services
func (am AppModule) RegisterServices(cfg module.Configurator) {
if am.controllerKeeper != nil {
controllertypes.RegisterMsgServer(cfg.MsgServer(), controllerkeeper.NewMsgServerImpl(am.controllerKeeper))
controllertypes.RegisterQueryServer(cfg.QueryServer(), am.controllerKeeper)
}

if am.hostKeeper != nil {
hosttypes.RegisterMsgServer(cfg.MsgServer(), hostkeeper.NewMsgServerImpl(am.hostKeeper))
hosttypes.RegisterQueryServer(cfg.QueryServer(), am.hostKeeper)
}

func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error {
controllerMigrator := controllerkeeper.NewMigrator(am.controllerKeeper)
hostMigrator := hostkeeper.NewMigrator(am.hostKeeper)
if err := cfg.RegisterMigration(types.ModuleName, 2, func(ctx sdk.Context) error {
if err := registrar.Register(types.ModuleName, 2, func(ctx context.Context) error {
if err := hostMigrator.MigrateParams(ctx); err != nil {
return err
}
return controllerMigrator.MigrateParams(ctx)
}); err != nil {
panic(fmt.Errorf("failed to migrate interchainaccounts app from version 2 to 3 (self-managed params migration): %v", err))
return fmt.Errorf("failed to migrate interchainaccounts app from version 2 to 3 (self-managed params migration): %w", err)
}
return nil
}

// RegisterServices registers module services
func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error {
if am.controllerKeeper != nil {
controllertypes.RegisterMsgServer(cfg, controllerkeeper.NewMsgServerImpl(am.controllerKeeper))
controllertypes.RegisterQueryServer(cfg, am.controllerKeeper)
}

if am.hostKeeper != nil {
hosttypes.RegisterMsgServer(cfg, hostkeeper.NewMsgServerImpl(am.hostKeeper))
hosttypes.RegisterQueryServer(cfg, am.hostKeeper)
}

return nil
}

// InitGenesis performs genesis initialization for the interchain accounts module.
Expand Down
6 changes: 4 additions & 2 deletions modules/apps/29-fee/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"context"

storetypes "cosmossdk.io/store/types"

"github.com/cosmos/cosmos-sdk/runtime"
Expand All @@ -24,10 +26,10 @@ func NewMigrator(keeper Keeper) Migrator {

// Migrate1to2 migrates ibc-fee module from ConsensusVersion 1 to 2
// by refunding leftover fees to the refund address.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
func (m Migrator) Migrate1to2(ctx context.Context) error {
store := runtime.KVStoreAdapter(m.keeper.KVStoreService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix))
defer coretypes.LogDeferred(ctx.Logger(), func() error { return iterator.Close() })
defer coretypes.LogDeferred(m.keeper.Logger, func() error { return iterator.Close() })

for ; iterator.Valid(); iterator.Next() {
feesInEscrow := m.keeper.MustUnmarshalFees(iterator.Value())
Expand Down
25 changes: 15 additions & 10 deletions modules/apps/29-fee/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
Expand All @@ -26,10 +27,10 @@ var (
_ appmodule.HasConsensusVersion = (*AppModule)(nil)
_ appmodule.HasAminoCodec = (*AppModule)(nil)
_ appmodule.HasRegisterInterfaces = (*AppModule)(nil)
_ appmodule.HasMigrations = (*AppModule)(nil)

_ module.AppModule = (*AppModule)(nil)
_ module.HasGenesis = (*AppModule)(nil)
_ module.HasServices = (*AppModule)(nil)
_ module.AppModule = (*AppModule)(nil)
_ module.HasGenesis = (*AppModule)(nil)

_ autocli.HasCustomTxCommand = (*AppModule)(nil)
_ autocli.HasCustomQueryCommand = (*AppModule)(nil)
Expand Down Expand Up @@ -104,15 +105,19 @@ func (AppModule) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error {
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
panic(fmt.Errorf("failed to migrate ibc-fee module from version 1 to 2 (refund leftover fees): %v", err))
if err := registrar.Register(types.ModuleName, 1, m.Migrate1to2); err != nil {
return fmt.Errorf("failed to migrate ibc-fee module from version 1 to 2 (refund leftover fees): %v", err)
}
return nil
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error {
types.RegisterMsgServer(cfg, am.keeper)
types.RegisterQueryServer(cfg, am.keeper)
return nil
}

// InitGenesis performs genesis initialization for the ibc-29-fee module. It returns
Expand Down
12 changes: 6 additions & 6 deletions modules/apps/transfer/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ func NewMigrator(keeper Keeper) Migrator {
}

// MigrateParams migrates the transfer module's parameters from the x/params to self store.
func (m Migrator) MigrateParams(ctx sdk.Context) error {
func (m Migrator) MigrateParams(ctx context.Context) error {
var params types.Params
m.keeper.legacySubspace.GetParamSet(ctx, &params)
m.keeper.legacySubspace.GetParamSet(sdk.UnwrapSDKContext(ctx), &params)

m.keeper.SetParams(ctx, params)
m.keeper.Logger.Info("successfully migrated transfer app self-manage params")
return nil
}

// MigrateDenomMetadata sets token metadata for all the IBC denom traces
func (m Migrator) MigrateDenomMetadata(ctx sdk.Context) error {
func (m Migrator) MigrateDenomMetadata(ctx context.Context) error {
m.keeper.iterateDenomTraces(ctx,
func(dt internaltypes.DenomTrace) (stop bool) {
// check if the metadata for the given denom trace does not already exist
Expand All @@ -55,7 +55,7 @@ func (m Migrator) MigrateDenomMetadata(ctx sdk.Context) error {
}

// MigrateTotalEscrowForDenom migrates the total amount of source chain tokens in escrow.
func (m Migrator) MigrateTotalEscrowForDenom(ctx sdk.Context) error {
func (m Migrator) MigrateTotalEscrowForDenom(ctx context.Context) error {
var totalEscrowed sdk.Coins
portID := m.keeper.GetPort(ctx)

Expand All @@ -76,7 +76,7 @@ func (m Migrator) MigrateTotalEscrowForDenom(ctx sdk.Context) error {
}

// MigrateDenomTraceToDenom migrates storage from using DenomTrace to Denom.
func (m Migrator) MigrateDenomTraceToDenom(ctx sdk.Context) error {
func (m Migrator) MigrateDenomTraceToDenom(ctx context.Context) error {
var (
denoms []types.Denom
denomTraces []internaltypes.DenomTrace
Expand Down Expand Up @@ -147,7 +147,7 @@ func (k Keeper) iterateDenomTraces(ctx context.Context, cb func(denomTrace inter
}

// setDenomMetadataWithDenomTrace sets an IBC token's denomination metadata
func (k Keeper) setDenomMetadataWithDenomTrace(ctx sdk.Context, denomTrace internaltypes.DenomTrace) {
func (k Keeper) setDenomMetadataWithDenomTrace(ctx context.Context, denomTrace internaltypes.DenomTrace) {
metadata := banktypes.Metadata{
Description: fmt.Sprintf("IBC token from %s", denomTrace.GetFullDenomPath()),
DenomUnits: []*banktypes.DenomUnit{
Expand Down
32 changes: 19 additions & 13 deletions modules/apps/transfer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"google.golang.org/grpc"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
Expand All @@ -28,11 +29,11 @@ var (
_ appmodule.AppModule = (*AppModule)(nil)
_ appmodule.HasConsensusVersion = (*AppModule)(nil)
_ appmodule.HasRegisterInterfaces = (*AppModule)(nil)
_ appmodule.HasMigrations = (*AppModule)(nil)

_ module.AppModule = (*AppModule)(nil)
_ module.HasGRPCGateway = (*AppModule)(nil)
_ module.HasGenesis = (*AppModule)(nil)
_ module.HasServices = (*AppModule)(nil)

// Sims
_ module.AppModuleSimulation = (*AppModule)(nil)
Expand Down Expand Up @@ -118,27 +119,32 @@ func (AppModule) GetQueryCmd() *cobra.Command {
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
types.RegisterQueryV2Server(cfg.QueryServer(), am.keeper)
func (am AppModule) RegisterServices(cfg grpc.ServiceRegistrar) error {
types.RegisterMsgServer(cfg, am.keeper)
types.RegisterQueryServer(cfg, am.keeper)
types.RegisterQueryV2Server(cfg, am.keeper)
return nil
}

func (am AppModule) RegisterMigrations(registrar appmodule.MigrationRegistrar) error {
m := keeper.NewMigrator(am.keeper)
if err := cfg.RegisterMigration(types.ModuleName, 2, m.MigrateTotalEscrowForDenom); err != nil {
panic(fmt.Errorf("failed to migrate transfer app from version 2 to 3 (total escrow entry migration): %v", err))
if err := registrar.Register(types.ModuleName, 2, m.MigrateTotalEscrowForDenom); err != nil {
return fmt.Errorf("failed to migrate transfer app from version 2 to 3 (total escrow entry migration): %w", err)
}

if err := cfg.RegisterMigration(types.ModuleName, 3, m.MigrateParams); err != nil {
panic(fmt.Errorf("failed to migrate transfer app version 3 to 4 (self-managed params migration): %v", err))
if err := registrar.Register(types.ModuleName, 3, m.MigrateParams); err != nil {
return fmt.Errorf("failed to migrate transfer app version 3 to 4 (self-managed params migration): %w", err)
}

if err := cfg.RegisterMigration(types.ModuleName, 4, m.MigrateDenomMetadata); err != nil {
panic(fmt.Errorf("failed to migrate transfer app from version 4 to 5 (set denom metadata migration): %v", err))
if err := registrar.Register(types.ModuleName, 4, m.MigrateDenomMetadata); err != nil {
return fmt.Errorf("failed to migrate transfer app from version 4 to 5 (set denom metadata migration): %w", err)
}

if err := cfg.RegisterMigration(types.ModuleName, 5, m.MigrateDenomTraceToDenom); err != nil {
panic(fmt.Errorf("failed to migrate transfer app from version 5 to 6 (migrate DenomTrace to Denom): %v", err))
if err := registrar.Register(types.ModuleName, 5, m.MigrateDenomTraceToDenom); err != nil {
return fmt.Errorf("failed to migrate transfer app from version 5 to 6 (migrate DenomTrace to Denom): %w", err)
}

return nil
}

// InitGenesis performs genesis initialization for the ibc-transfer module. It returns
Expand Down
12 changes: 7 additions & 5 deletions modules/core/02-client/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v9/modules/core/02-client/migrations/v7"
Expand All @@ -25,16 +27,16 @@ func NewMigrator(keeper *Keeper) Migrator {
// - prunes solo machine consensus states
// - removes the localhost client
// - asserts that existing tendermint clients are properly registered on the chain codec
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
return v7.MigrateStore(ctx, m.keeper.KVStoreService, m.keeper.cdc, m.keeper)
func (m Migrator) Migrate2to3(ctx context.Context) error {
return v7.MigrateStore(ctx, m.keeper.Logger, m.keeper.KVStoreService, m.keeper.cdc, m.keeper)
}

// MigrateParams migrates from consensus version 4 to 5.
// This migration takes the parameters that are currently stored and managed by x/params
// and stores them directly in the ibc module's state.
func (m Migrator) MigrateParams(ctx sdk.Context) error {
func (m Migrator) MigrateParams(ctx context.Context) error {
var params types.Params
m.keeper.legacySubspace.GetParamSet(ctx, &params)
m.keeper.legacySubspace.GetParamSet(sdk.UnwrapSDKContext(ctx), &params)
if err := params.Validate(); err != nil {
return err
}
Expand All @@ -46,7 +48,7 @@ func (m Migrator) MigrateParams(ctx sdk.Context) error {

// MigrateToStatelessLocalhost deletes the localhost client state. The localhost
// implementation is now stateless.
func (m Migrator) MigrateToStatelessLocalhost(ctx sdk.Context) error {
func (m Migrator) MigrateToStatelessLocalhost(ctx context.Context) error {
clientStore := m.keeper.ClientStore(ctx, exported.LocalhostClientID)

// delete the client state
Expand Down
4 changes: 3 additions & 1 deletion modules/core/02-client/migrations/v7/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package v7_test
import (
"encoding/json"

"cosmossdk.io/log"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"

Expand Down Expand Up @@ -107,7 +109,7 @@ func (suite *MigrationsV7TestSuite) TestMigrateGenesisSolomachine() {
// migrate store get expected genesis
// store migration and genesis migration should produce identical results
// NOTE: tendermint clients are not pruned in genesis so the test should not have expired tendermint clients
err = v7.MigrateStore(suite.chainA.GetContext(), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper)
err = v7.MigrateStore(suite.chainA.GetContext(), log.NewTestLogger(suite.T()), runtime.NewKVStoreService(suite.chainA.GetSimApp().GetKey(ibcexported.StoreKey)), suite.chainA.App.AppCodec(), suite.chainA.GetSimApp().IBCKeeper.ClientKeeper)
suite.Require().NoError(err)
expectedClientGenState, err := ibcclient.ExportGenesis(suite.chainA.GetContext(), suite.chainA.App.GetIBCKeeper().ClientKeeper)
suite.Require().NoError(err)
Expand Down
Loading

0 comments on commit dcd6a3e

Please sign in to comment.