feat: support in-place migration ordering (#10614)
## Description Closes: #10604 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
@@ -160,10 +160,10 @@ var (
|
||||
// Examples: not DB domain error, file writing etc...
|
||||
ErrIO = Register(RootCodespace, 39, "Internal IO error")
|
||||
|
||||
// ErrAppConfig defines an error occurred if min-gas-prices field in BaseConfig is empty.
|
||||
ErrAppConfig = Register(RootCodespace, 40, "error in app.toml")
|
||||
|
||||
// ErrPanic is only set when we recover from a panic, so we know to
|
||||
// redact potentially sensitive system info
|
||||
ErrPanic = errorsmod.ErrPanic
|
||||
|
||||
// ErrAppConfig defines an error occurred if min-gas-prices field in BaseConfig is empty.
|
||||
ErrAppConfig = Register(RootCodespace, 40, "error in app.toml")
|
||||
)
|
||||
|
||||
+66
-24
@@ -230,6 +230,7 @@ type Manager struct {
|
||||
OrderExportGenesis []string
|
||||
OrderBeginBlockers []string
|
||||
OrderEndBlockers []string
|
||||
OrderMigrations []string
|
||||
}
|
||||
|
||||
// NewManager creates a new Manager object
|
||||
@@ -253,28 +254,35 @@ func NewManager(modules ...AppModule) *Manager {
|
||||
|
||||
// SetOrderInitGenesis sets the order of init genesis calls
|
||||
func (m *Manager) SetOrderInitGenesis(moduleNames ...string) {
|
||||
m.checkForgottenModules("SetOrderInitGenesis", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderInitGenesis", moduleNames)
|
||||
m.OrderInitGenesis = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderExportGenesis sets the order of export genesis calls
|
||||
func (m *Manager) SetOrderExportGenesis(moduleNames ...string) {
|
||||
m.checkForgottenModules("SetOrderExportGenesis", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderExportGenesis", moduleNames)
|
||||
m.OrderExportGenesis = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderBeginBlockers sets the order of set begin-blocker calls
|
||||
func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
|
||||
m.checkForgottenModules("SetOrderBeginBlockers", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderBeginBlockers", moduleNames)
|
||||
m.OrderBeginBlockers = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderEndBlockers sets the order of set end-blocker calls
|
||||
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
|
||||
m.checkForgottenModules("SetOrderEndBlockers", moduleNames)
|
||||
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames)
|
||||
m.OrderEndBlockers = moduleNames
|
||||
}
|
||||
|
||||
// SetOrderMigrations sets the order of migrations to be run. If not set
|
||||
// then migrations will be run with an order defined in `DefaultMigrationsOrder`.
|
||||
func (m *Manager) SetOrderMigrations(moduleNames ...string) {
|
||||
m.assertNoForgottenModules("SetOrderMigrations", moduleNames)
|
||||
m.OrderMigrations = moduleNames
|
||||
}
|
||||
|
||||
// RegisterInvariants registers all module invariants
|
||||
func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) {
|
||||
for _, module := range m.Modules {
|
||||
@@ -345,24 +353,29 @@ func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) map[string
|
||||
return genesisData
|
||||
}
|
||||
|
||||
// checkForgottenModules checks that we didn't forget any modules in the
|
||||
// assertNoForgottenModules checks that we didn't forget any modules in the
|
||||
// SetOrder* functions.
|
||||
func (m *Manager) checkForgottenModules(setOrderFnName string, moduleNames []string) {
|
||||
setOrderMap := map[string]struct{}{}
|
||||
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string) {
|
||||
ms := make(map[string]bool)
|
||||
for _, m := range moduleNames {
|
||||
setOrderMap[m] = struct{}{}
|
||||
ms[m] = true
|
||||
}
|
||||
|
||||
if len(setOrderMap) != len(m.Modules) {
|
||||
panic(fmt.Sprintf("got %d modules in the module manager, but %d modules in %s", len(m.Modules), len(setOrderMap), setOrderFnName))
|
||||
var missing []string
|
||||
for m := range m.Modules {
|
||||
if !ms[m] {
|
||||
missing = append(missing, m)
|
||||
}
|
||||
}
|
||||
if len(missing) != 0 {
|
||||
panic(fmt.Sprintf(
|
||||
"%s: all modules must be defined when setting SetOrderMigrations, missing: %v", setOrderFnName, missing))
|
||||
}
|
||||
}
|
||||
|
||||
// MigrationHandler is the migration function that each module registers.
|
||||
type MigrationHandler func(sdk.Context) error
|
||||
|
||||
// VersionMap is a map of moduleName -> version, where version denotes the
|
||||
// version from which we should perform the migration for each module.
|
||||
// VersionMap is a map of moduleName -> version
|
||||
type VersionMap map[string]uint64
|
||||
|
||||
// RunMigrations performs in-place store migrations for all modules. This
|
||||
@@ -389,6 +402,9 @@ type VersionMap map[string]uint64
|
||||
// `InitGenesis` on that module.
|
||||
// - return the `updatedVM` to be persisted in the x/upgrade's store.
|
||||
//
|
||||
// Migrations are run in an order defined by `Manager.OrderMigrations` or (if not set) defined by
|
||||
// `DefaultMigrationsOrder` function.
|
||||
//
|
||||
// As an app developer, if you wish to skip running InitGenesis for your new
|
||||
// module "foo", you need to manually pass a `fromVM` argument to this function
|
||||
// foo's module version set to its latest ConsensusVersion. That way, the diff
|
||||
@@ -415,19 +431,13 @@ func (m Manager) RunMigrations(ctx sdk.Context, cfg Configurator, fromVM Version
|
||||
if !ok {
|
||||
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "expected %T, got %T", configurator{}, cfg)
|
||||
}
|
||||
|
||||
updatedVM := make(VersionMap)
|
||||
// for deterministic iteration order
|
||||
// (as some migrations depend on other modules
|
||||
// and the order of executing migrations matters)
|
||||
// TODO: make the order user-configurable?
|
||||
sortedModNames := make([]string, 0, len(m.Modules))
|
||||
for key := range m.Modules {
|
||||
sortedModNames = append(sortedModNames, key)
|
||||
var modules = m.OrderMigrations
|
||||
if modules == nil {
|
||||
modules = DefaultMigrationsOrder(m.ModuleNames())
|
||||
}
|
||||
sort.Strings(sortedModNames)
|
||||
|
||||
for _, moduleName := range sortedModNames {
|
||||
updatedVM := VersionMap{}
|
||||
for _, moduleName := range modules {
|
||||
module := m.Modules[moduleName]
|
||||
fromVersion, exists := fromVM[moduleName]
|
||||
toVersion := module.ConsensusVersion()
|
||||
@@ -514,3 +524,35 @@ func (m *Manager) GetVersionMap() VersionMap {
|
||||
|
||||
return vermap
|
||||
}
|
||||
|
||||
// ModuleNames returns list of all module names, without any particular order.
|
||||
func (m *Manager) ModuleNames() []string {
|
||||
ms := make([]string, len(m.Modules))
|
||||
i := 0
|
||||
for m := range m.Modules {
|
||||
ms[i] = m
|
||||
i++
|
||||
}
|
||||
return ms
|
||||
}
|
||||
|
||||
// DefaultMigrationsOrder returns a default migrations order: ascending alphabetical by module name,
|
||||
// except x/auth which will run last, see:
|
||||
// https://github.com/cosmos/cosmos-sdk/issues/10591
|
||||
func DefaultMigrationsOrder(modules []string) []string {
|
||||
const authName = "auth"
|
||||
out := make([]string, 0, len(modules))
|
||||
hasAuth := false
|
||||
for _, m := range modules {
|
||||
if m == authName {
|
||||
hasAuth = true
|
||||
} else {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
if hasAuth {
|
||||
out = append(out, authName)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
func TestModuleIntSuite(t *testing.T) {
|
||||
suite.Run(t, new(TestSuite))
|
||||
}
|
||||
|
||||
type TestSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func (s TestSuite) TestAssertNoForgottenModules() {
|
||||
m := Manager{
|
||||
Modules: map[string]AppModule{"a": nil, "b": nil},
|
||||
}
|
||||
tcs := []struct {
|
||||
name string
|
||||
positive bool
|
||||
modules []string
|
||||
}{
|
||||
{"same modules", true, []string{"a", "b"}},
|
||||
{"more modules", true, []string{"a", "b", "c"}},
|
||||
}
|
||||
|
||||
for _, tc := range tcs {
|
||||
if tc.positive {
|
||||
m.assertNoForgottenModules("x", tc.modules)
|
||||
} else {
|
||||
s.Panics(func() { m.assertNoForgottenModules("x", tc.modules) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s TestSuite) TestModuleNames() {
|
||||
m := Manager{
|
||||
Modules: map[string]AppModule{"a": nil, "b": nil},
|
||||
}
|
||||
ms := m.ModuleNames()
|
||||
sort.Strings(ms)
|
||||
s.Require().Equal([]string{"a", "b"}, ms)
|
||||
}
|
||||
|
||||
func (s TestSuite) TestDefaultMigrationsOrder() {
|
||||
require := s.Require()
|
||||
require.Equal(
|
||||
[]string{"auth2", "d", "z", "auth"},
|
||||
DefaultMigrationsOrder([]string{"d", "auth", "auth2", "z"}), "alphabetical, but auth should be last")
|
||||
require.Equal(
|
||||
[]string{"auth2", "d", "z"},
|
||||
DefaultMigrationsOrder([]string{"d", "auth2", "z"}), "alphabetical")
|
||||
}
|
||||
Reference in New Issue
Block a user