Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement in-place store migration #334

Merged
merged 14 commits into from
Nov 29, 2021
24 changes: 23 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
ibctransfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/modules/core"
ibcclient "github.com/cosmos/ibc-go/modules/core/02-client"
ibcconnectiontypes "github.com/cosmos/ibc-go/modules/core/03-connection/types"
porttypes "github.com/cosmos/ibc-go/modules/core/05-port/types"
ibchost "github.com/cosmos/ibc-go/modules/core/24-host"
ibckeeper "github.com/cosmos/ibc-go/modules/core/keeper"
Expand Down Expand Up @@ -112,6 +113,7 @@ import (
const (
// AppName specifies the global application name.
AppName = "Shentu"
upgradeName = "Shentu-upgrade"

// DefaultKeyPass for certik node daemon.
DefaultKeyPass = "12345678"
Expand Down Expand Up @@ -213,6 +215,7 @@ type ShentuApp struct {

// simulation manager
sm *module.SimulationManager
configurator module.Configurator
}

// NewShentuApp returns a reference to an initialized ShentuApp.
Expand Down Expand Up @@ -524,7 +527,9 @@ func NewShentuApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
)

app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
app.mm.RegisterServices(cfg)

app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(app.configurator)

app.sm = module.NewSimulationManager(
auth.NewAppModule(appCodec, app.authKeeper, app.accountKeeper, app.bankKeeper, app.certKeeper, authsims.RandomGenesisAccounts),
Expand Down Expand Up @@ -570,6 +575,23 @@ func NewShentuApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
app.SetAnteHandler(anteHandler)
app.SetEndBlocker(app.EndBlocker)

app.upgradeKeeper.SetUpgradeHandler(
upgradeName,
func(ctx sdk.Context, _ upgradetypes.Plan, _ module.VersionMap) (module.VersionMap, error) {
app.ibcKeeper.ConnectionKeeper.SetParams(ctx, ibcconnectiontypes.DefaultParams())

fromVM := make(map[string]uint64)
for moduleName := range app.mm.Modules {
fromVM[moduleName] = 1
}
// override versions for _new_ modules as to not skip InitGenesis
fromVM[sdkauthz.ModuleName] = 0
fromVM[sdkfeegrant.ModuleName] = 0

return app.mm.RunMigrations(ctx, app.configurator, fromVM)
},
)

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
Expand Down
44 changes: 44 additions & 0 deletions x/auth/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package keeper

import (
"github.com/gogo/protobuf/grpc"

sdk "github.com/cosmos/cosmos-sdk/types"
v043 "github.com/cosmos/cosmos-sdk/x/auth/legacy/v043"
sdktypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/certikfoundation/shentu/v2/x/auth/types"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper types.AccountKeeper
queryServer grpc.Server
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper, queryServer grpc.Server) Migrator {
return Migrator{keeper: keeper.ak, queryServer: queryServer}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
var iterErr error

m.keeper.IterateAccounts(ctx, func(account sdktypes.AccountI) (stop bool) {
wb, err := v043.MigrateAccount(ctx, account, m.queryServer)
if err != nil {
iterErr = err
return true
}

if wb == nil {
return false
}

m.keeper.SetAccount(ctx, wb)
return false
})

return iterErr
}
8 changes: 7 additions & 1 deletion x/auth/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ func (am AppModule) LegacyQuerierHandler(cdc *codec.LegacyAmino) sdk.Querier {
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
authtypes.RegisterQueryServer(cfg.QueryServer(), am.authKeeper)

m := keeper.NewMigrator(am.keeper, cfg.QueryServer())
err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the auth module. It returns no validator updates.
Expand All @@ -150,7 +156,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock returns the begin blocker for the auth module.
func (am AppModule) BeginBlock(ctx sdk.Context, rbb abci.RequestBeginBlock) {
Expand Down
2 changes: 2 additions & 0 deletions x/auth/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ type CertKeeper interface {
type AccountKeeper interface {
GetAccount(ctx sdk.Context, addr sdk.AccAddress) types.AccountI
SetAccount(ctx sdk.Context, acc types.AccountI)

IterateAccounts(ctx sdk.Context, cb func(account types.AccountI) (stop bool))
}
2 changes: 1 addition & 1 deletion x/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock returns the begin blocker for the bank module.
func (am AppModule) BeginBlock(ctx sdk.Context, rbb abci.RequestBeginBlock) {
Expand Down
2 changes: 1 addition & 1 deletion x/crisis/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock performs a no-op.
func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }
hacheigriega marked this conversation as resolved.
Show resolved Hide resolved

// BeginBlock implements the Cosmos SDK BeginBlock module function.
func (am AppModule) BeginBlock(ctx sdk.Context, rbb abci.RequestBeginBlock) {
Expand Down
21 changes: 21 additions & 0 deletions x/gov/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v043 "github.com/cosmos/cosmos-sdk/x/gov/legacy/v043"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v043.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}
8 changes: 7 additions & 1 deletion x/gov/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
//govtypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

m := keeper.NewMigrator(am.keeper)
err := cfg.RegisterMigration(govtypes.ModuleName, 1, m.Migrate1to2)
if err != nil {
panic(err)
}
}

// InitGenesis performs genesis initialization for the governance module.
Expand All @@ -178,7 +184,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock implements the Cosmos SDK BeginBlock module function.
func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
2 changes: 1 addition & 1 deletion x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock processes module beginblock.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock returns the begin blocker for the slashing module.
func (am AppModule) BeginBlock(ctx sdk.Context, rbb abci.RequestBeginBlock) {
Expand Down
2 changes: 1 addition & 1 deletion x/staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (am AppModule) ConsensusVersion() uint64 { return am.ConsensusVersion() }

// BeginBlock implements the Cosmos SDK BeginBlock module function.
func (am AppModule) BeginBlock(ctx sdk.Context, rbb abci.RequestBeginBlock) {
Expand Down