Skip to content

Commit

Permalink
feat!: use versioned keys for dynamically adding and remove commit st…
Browse files Browse the repository at this point in the history
…ores (#3320)

As was discovered with the apphash mismatch in
#3167, by adding both
v1 and v2 stores in the constructor we actually end up with a different
app hash to `v1.x` as now there are a bunch of empy but initialized
stores. What's needed is a mechanism that can start with only the v1
stores and then during the upgrade process add or remove the store for
v2 as necessary.

The main problem with this is that because we are using a branch of
state we can't actually modify the stores until we write the new state
to disk. Hence we can no longer perform the migration in `EndBlock` but
in `Commit` whereby we first write state, then create the new stores,
then perform the module migrations (like call `InitGenesis`) and then
write it again finally returning the app hash.

This PR depends on celestiaorg/cosmos-sdk#387
which introduces the upgrade hooks during `Commit`. This PR itself adds
the two hooks for updating the stores and performing the migrations.

---------

Co-authored-by: Rootul Patel <[email protected]>
  • Loading branch information
cmwaters and rootulp authored Apr 17, 2024
1 parent 8226370 commit 9db2f8b
Show file tree
Hide file tree
Showing 17 changed files with 612 additions and 442 deletions.
462 changes: 143 additions & 319 deletions app/app.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ func TestNew(t *testing.T) {
logger := log.NewNopLogger()
db := tmdb.NewMemDB()
traceStore := &NoopWriter{}
loadLatest := true
invCheckPeriod := uint(1)
encodingConfig := encoding.MakeConfig(app.ModuleEncodingRegisters...)
upgradeHeight := int64(0)
appOptions := NoopAppOptions{}

got := app.New(logger, db, traceStore, loadLatest, invCheckPeriod, encodingConfig, upgradeHeight, appOptions)
got := app.New(logger, db, traceStore, invCheckPeriod, encodingConfig, upgradeHeight, appOptions)

t.Run("initializes ICAHostKeeper", func(t *testing.T) {
assert.NotNil(t, got.ICAHostKeeper)
Expand Down
31 changes: 28 additions & 3 deletions app/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@ func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []
// MigrationHandler is the migration function that each module registers.
type MigrationHandler func(sdk.Context) error

// VersionMap is a map of moduleName -> version
type VersionMap map[string]uint64

// RunMigrations performs in-place store migrations for all modules. This
// function MUST be called when the state machine changes appVersion
func (m Manager) RunMigrations(ctx sdk.Context, cfg sdkmodule.Configurator, fromVersion, toVersion uint64) error {
Expand Down Expand Up @@ -335,6 +332,22 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.Respo
}
}

// GetVersionMap gets consensus version from all modules
func (m *Manager) GetVersionMap(version uint64) sdkmodule.VersionMap {
vermap := make(sdkmodule.VersionMap)
if version > m.lastVersion || version < m.firstVersion {
return vermap
}

for _, v := range m.versionedModules[version] {
version := v.ConsensusVersion()
name := v.Name()
vermap[name] = version
}

return vermap
}

// ModuleNames returns list of all module names, without any particular order.
func (m *Manager) ModuleNames(version uint64) []string {
modules, ok := m.versionedModules[version]
Expand All @@ -351,6 +364,7 @@ func (m *Manager) ModuleNames(version uint64) []string {
return ms
}

// SupportedVersions returns all the supported versions for the module manager
func (m *Manager) SupportedVersions() []uint64 {
output := make([]uint64, 0, m.lastVersion-m.firstVersion+1)
for version := m.firstVersion; version <= m.lastVersion; version++ {
Expand Down Expand Up @@ -385,6 +399,17 @@ func (m *Manager) checkUpgradeSchedule() error {
return nil
}

// assertMatchingModules performs a sanity check that the basic module manager
// contains all the same modules present in the module manager
func (m *Manager) AssertMatchingModules(basicModuleManager sdkmodule.BasicManager) error {
for _, module := range m.allModules {
if _, exists := basicModuleManager[module.Name()]; !exists {
return fmt.Errorf("module %s not found in basic module manager", module.Name())
}
}
return nil
}

// 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
Expand Down
Loading

0 comments on commit 9db2f8b

Please sign in to comment.