-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat(core): add migrations registering #19370
Changes from 8 commits
daf87aa
409a79b
72b2176
a4e7719
53dc459
3f40376
6153444
d973b87
ea80fb4
ac089cd
2e0b1d2
394a1e7
b001af0
e3b7e1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
package appmodule | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
import "context" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
type MigrationRegistrar interface { | ||||||||||||||||||||||||||||||||||||||||||||||||
// Register registers an in-place store migration for a module. The | ||||||||||||||||||||||||||||||||||||||||||||||||
// handler is a migration script to perform in-place migrations from version | ||||||||||||||||||||||||||||||||||||||||||||||||
// `fromVersion` to version `fromVersion+1`. | ||||||||||||||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||||||||||||||
// EACH TIME a module's ConsensusVersion increments, a new migration MUST | ||||||||||||||||||||||||||||||||||||||||||||||||
// be registered using this function. If a migration handler is missing for | ||||||||||||||||||||||||||||||||||||||||||||||||
// a particular function, the upgrade logic (see RunMigrations function) | ||||||||||||||||||||||||||||||||||||||||||||||||
// will panic. If the ConsensusVersion bump does not introduce any store | ||||||||||||||||||||||||||||||||||||||||||||||||
// changes, then a no-op function must be registered here. | ||||||||||||||||||||||||||||||||||||||||||||||||
Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+5
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The + // It is the caller's responsibility to ensure migrations are registered in a sequential manner without gaps. Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package module | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cosmos/gogoproto/grpc" | ||
|
@@ -20,6 +21,8 @@ import ( | |
// their services in the RegisterServices method. It is designed to eventually | ||
// support module object capabilities isolation as described in | ||
// https://github.com/cosmos/cosmos-sdk/issues/7093 | ||
// Deprecated: The Configurator is deprecated. | ||
// Preferably use core services for registering msg/query server and migrations. | ||
Comment on lines
+24
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deprecation of |
||
type Configurator interface { | ||
grpc.Server | ||
|
||
|
@@ -45,6 +48,10 @@ type Configurator interface { | |
// will panic. If the ConsensusVersion bump does not introduce any store | ||
// changes, then a no-op function must be registered here. | ||
RegisterMigration(moduleName string, fromVersion uint64, handler MigrationHandler) error | ||
|
||
// Register registers an in-place store migration for a module. | ||
// It permits to register modules migrations that have migrated to serverv2 but still be compatible with baseapp. | ||
Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error | ||
Comment on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
type configurator struct { | ||
|
@@ -119,6 +126,14 @@ func (c *configurator) RegisterMigration(moduleName string, fromVersion uint64, | |
return nil | ||
} | ||
|
||
// Register implements the Configurator.Register method | ||
// It permis to register modules migrations that have migrated to serverv2 but still be compatible with baseapp. | ||
julienrbrt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func (c *configurator) Register(moduleName string, fromVersion uint64, handler func(context.Context) error) error { | ||
return c.RegisterMigration(moduleName, fromVersion, func(sdkCtx sdk.Context) error { | ||
return handler(sdkCtx) | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
// runModuleMigrations runs all in-place store migrations for one given module from a | ||
// version to another version. | ||
func (c *configurator) runModuleMigrations(ctx sdk.Context, moduleName string, fromVersion, toVersion uint64) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"fmt" | ||
|
||
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
"google.golang.org/grpc" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing import documentation for |
||
|
||
"cosmossdk.io/core/address" | ||
"cosmossdk.io/core/appmodule" | ||
|
@@ -30,9 +31,10 @@ var ( | |
_ module.AppModuleBasic = AppModule{} | ||
_ module.AppModuleSimulation = AppModule{} | ||
_ module.HasGenesis = AppModule{} | ||
_ module.HasServices = AppModule{} | ||
|
||
_ appmodule.AppModule = AppModule{} | ||
_ appmodule.AppModule = AppModule{} | ||
_ appmodule.HasServices = AppModule{} | ||
_ appmodule.HasMigrations = AppModule{} | ||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interface assertions for |
||
) | ||
|
||
// AppModuleBasic defines the basic application module used by the auth module. | ||
|
@@ -98,27 +100,31 @@ func NewAppModule(cdc codec.Codec, accountKeeper keeper.AccountKeeper, randGenAc | |
} | ||
} | ||
|
||
// RegisterServices registers a GRPC query service to respond to the | ||
// module-specific GRPC queries. | ||
func (am AppModule) RegisterServices(cfg module.Configurator) { | ||
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.accountKeeper)) | ||
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.accountKeeper)) | ||
// RegisterServices registers module services. | ||
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting we could drop grpc here with an interface. Out of scope for this pr |
||
types.RegisterMsgServer(registrar, keeper.NewMsgServerImpl(am.accountKeeper)) | ||
types.RegisterQueryServer(registrar, keeper.NewQueryServer(am.accountKeeper)) | ||
|
||
m := keeper.NewMigrator(am.accountKeeper, cfg.QueryServer()) | ||
if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { | ||
panic(fmt.Sprintf("failed to migrate x/%s from version 1 to 2: %v", types.ModuleName, err)) | ||
} | ||
return nil | ||
} | ||
Comment on lines
+103
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if err := cfg.RegisterMigration(types.ModuleName, 2, m.Migrate2to3); err != nil { | ||
panic(fmt.Sprintf("failed to migrate x/%s from version 2 to 3: %v", types.ModuleName, err)) | ||
func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error { | ||
m := keeper.NewMigrator(am.accountKeeper) | ||
if err := mr.Register(types.ModuleName, 1, m.Migrate1to2); err != nil { | ||
return fmt.Errorf("failed to migrate x/%s from version 1 to 2: %w", types.ModuleName, err) | ||
} | ||
|
||
if err := cfg.RegisterMigration(types.ModuleName, 3, m.Migrate3to4); err != nil { | ||
panic(fmt.Sprintf("failed to migrate x/%s from version 3 to 4: %v", types.ModuleName, err)) | ||
if err := mr.Register(types.ModuleName, 2, m.Migrate2to3); err != nil { | ||
return fmt.Errorf("failed to migrate x/%s from version 2 to 3: %w", types.ModuleName, err) | ||
} | ||
if err := cfg.RegisterMigration(types.ModuleName, 4, m.Migrate4To5); err != nil { | ||
panic(fmt.Sprintf("failed to migrate x/%s from version 4 to 5", types.ModuleName)) | ||
if err := mr.Register(types.ModuleName, 3, m.Migrate3to4); err != nil { | ||
return fmt.Errorf("failed to migrate x/%s from version 3 to 4: %w", types.ModuleName, err) | ||
} | ||
if err := mr.Register(types.ModuleName, 4, m.Migrate4To5); err != nil { | ||
return fmt.Errorf("failed to migrate x/%s from version 4 to 5: %w", types.ModuleName, err) | ||
} | ||
|
||
return nil | ||
Comment on lines
+111
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
// InitGenesis performs genesis initialization for the auth module. It returns | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,9 +1,9 @@ | ||||||||
package keeper | ||||||||
|
||||||||
import ( | ||||||||
v2 "cosmossdk.io/x/authz/migrations/v2" | ||||||||
"context" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing import documentation. + // Package keeper provides functionality to manage and store authz grants. Committable suggestion
Suggested change
|
||||||||
|
||||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||||
v2 "cosmossdk.io/x/authz/migrations/v2" | ||||||||
) | ||||||||
|
||||||||
// Migrator is a struct for handling in-place store migrations. | ||||||||
|
@@ -17,6 +17,6 @@ func NewMigrator(keeper Keeper) Migrator { | |||||||
} | ||||||||
|
||||||||
// Migrate1to2 migrates from version 1 to 2. | ||||||||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||||||||
func (m Migrator) Migrate1to2(ctx context.Context) error { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect context usage for - func (m Migrator) Migrate1to2(ctx context.Context) error {
+ func (m Migrator) Migrate1to2(ctx sdk.Context) error { Committable suggestion
Suggested change
|
||||||||
return v2.MigrateStore(ctx, m.keeper.storeService, m.keeper.cdc) | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,8 +1,6 @@ | ||||||||
package keeper | ||||||||
|
||||||||
import ( | ||||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||||
) | ||||||||
import "context" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing import documentation. + // Package keeper provides functionality to manage and store bank transactions and balances. Committable suggestion
Suggested change
|
||||||||
|
||||||||
// Migrator is a struct for handling in-place store migrations. | ||||||||
type Migrator struct { | ||||||||
|
@@ -15,16 +13,16 @@ func NewMigrator(keeper BaseKeeper) Migrator { | |||||||
} | ||||||||
|
||||||||
// Migrate1to2 migrates from version 1 to 2. | ||||||||
func (m Migrator) Migrate1to2(ctx sdk.Context) error { | ||||||||
func (m Migrator) Migrate1to2(ctx context.Context) error { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect context usage for - func (m Migrator) Migrate1to2(ctx context.Context) error {
+ func (m Migrator) Migrate1to2(ctx sdk.Context) error { Committable suggestion
Suggested change
|
||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
// Migrate2to3 migrates x/bank storage from version 2 to 3. | ||||||||
func (m Migrator) Migrate2to3(ctx sdk.Context) error { | ||||||||
func (m Migrator) Migrate2to3(ctx context.Context) error { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect context usage for - func (m Migrator) Migrate2to3(ctx context.Context) error {
+ func (m Migrator) Migrate2to3(ctx sdk.Context) error { Committable suggestion
Suggested change
|
||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
// Migrate3to4 migrates x/bank storage from version 3 to 4. | ||||||||
func (m Migrator) Migrate3to4(ctx sdk.Context) error { | ||||||||
func (m Migrator) Migrate3to4(ctx context.Context) error { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect context usage for - func (m Migrator) Migrate3to4(ctx context.Context) error {
+ func (m Migrator) Migrate3to4(ctx sdk.Context) error { Committable suggestion
Suggested change
|
||||||||
return nil | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing package documentation.
+ // Package appmodule provides interfaces and structures for managing application modules.
Committable suggestion