Skip to content

Commit

Permalink
feat!: specify module to export (cosmos#13437)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored and Cashmaney committed Nov 29, 2022
1 parent ae915c4 commit c94a4d7
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* [#13437](https://github.com/cosmos/cosmos-sdk/pull/13437) Add new flag `--modules-to-export` in `simd export` command to export only selected modules.
* (grpc) [#13485](https://github.com/cosmos/cosmos-sdk/pull/13485) Implement a new gRPC query, `/cosmos/base/node/v1beta1/config`, which provides operator configuration. Applications that wish to expose operator minimum gas prices via gRPC should have their application implement the `ApplicationQueryService` interface (see `SimApp#RegisterNodeService` as an example).
* [#13557](https://github.com/cosmos/cosmos-sdk/pull/#13557) - Add `GenSignedMockTx`. This can be used as workaround for #12437 revertion. `v0.46+` contains as well a `GenSignedMockTx` that behaves the same way.
* (x/auth) [#13612](https://github.com/cosmos/cosmos-sdk/pull/13612) Add `Query/ModuleAccountByName` endpoint for accessing the module account info by module name.
Expand Down
5 changes: 4 additions & 1 deletion server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
FlagHeight = "height"
FlagForZeroHeight = "for-zero-height"
FlagJailAllowedAddrs = "jail-allowed-addrs"
FlagModulesToExport = "modules-to-export"
)

// ExportCmd dumps app state to JSON.
Expand Down Expand Up @@ -67,8 +68,9 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
height, _ := cmd.Flags().GetInt64(FlagHeight)
forZeroHeight, _ := cmd.Flags().GetBool(FlagForZeroHeight)
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs)
modulesToExport, _ := cmd.Flags().GetStringSlice(FlagModulesToExport)

exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper)
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport)
if err != nil {
return fmt.Errorf("error exporting state: %v", err)
}
Expand Down Expand Up @@ -121,6 +123,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
cmd.Flags().Int64(FlagHeight, -1, "Export state from a particular height (-1 means latest height)")
cmd.Flags().Bool(FlagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)")
cmd.Flags().StringSlice(FlagJailAllowedAddrs, []string{}, "Comma-separated list of operator addresses of jailed validators to unjail")
cmd.Flags().StringSlice(FlagModulesToExport, []string{}, "Comma-separated list of modules to export. If empty, will export all modules")

return cmd
}
4 changes: 2 additions & 2 deletions server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *t
app.Commit()

cmd := server.ExportCmd(
func(_ log.Logger, _ dbm.DB, _ io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string, appOptons types.AppOptions) (types.ExportedApp, error) {
func(_ log.Logger, _ dbm.DB, _ io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string, appOptons types.AppOptions, modulesToExport []string) (types.ExportedApp, error) {
encCfg := simapp.MakeTestEncodingConfig()

var simApp *simapp.SimApp
Expand All @@ -162,7 +162,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *t
simApp = simapp.NewSimApp(logger, db, nil, true, map[int64]bool{}, "", 0, encCfg, appOptons)
}

return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs)
return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
}, tempDir)

ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ type (

// AppExporter is a function that dumps all app state to
// JSON-serializable structure and returns the current validator set.
AppExporter func(log.Logger, dbm.DB, io.Writer, int64, bool, []string, AppOptions) (ExportedApp, error)
AppExporter func(log.Logger, dbm.DB, io.Writer, int64, bool, []string, AppOptions, []string) (ExportedApp, error)
)
2 changes: 1 addition & 1 deletion simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, EmptyAppOptions{})
_, err = app2.ExportAppStateAndValidators(false, []string{})
_, err = app2.ExportAppStateAndValidators(false, []string{}, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

Expand Down
6 changes: 2 additions & 4 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (

// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *SimApp) ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
) (servertypes.ExportedApp, error) {
func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []string) (servertypes.ExportedApp, error) {
// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})

Expand All @@ -29,7 +27,7 @@ func (app *SimApp) ExportAppStateAndValidators(
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.mm.ExportGenesis(ctx, app.appCodec)
genState := app.mm.ExportGenesis(ctx, app.appCodec, modulesToExport)
appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return servertypes.ExportedApp{}, err
Expand Down
4 changes: 2 additions & 2 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestAppImportExport(t *testing.T) {

fmt.Printf("exporting genesis...\n")

exported, err := app.ExportAppStateAndValidators(false, []string{})
exported, err := app.ExportAppStateAndValidators(false, []string{}, []string{})
require.NoError(t, err)

fmt.Printf("importing genesis...\n")
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestAppSimulationAfterImport(t *testing.T) {

fmt.Printf("exporting genesis...\n")

exported, err := app.ExportAppStateAndValidators(true, []string{})
exported, err := app.ExportAppStateAndValidators(true, []string{}, []string{})
require.NoError(t, err)

fmt.Printf("importing genesis...\n")
Expand Down
3 changes: 2 additions & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a
func (a appCreator) appExport(
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string,
appOpts servertypes.AppOptions,
modulesToExport []string,
) (servertypes.ExportedApp, error) {
var simApp *simapp.SimApp
homePath, ok := appOpts.Get(flags.FlagHome).(string)
Expand All @@ -303,5 +304,5 @@ func (a appCreator) appExport(
simApp = simapp.NewSimApp(logger, db, traceStore, true, map[int64]bool{}, homePath, uint(1), a.encCfg, appOpts)
}

return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs)
return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
}
4 changes: 1 addition & 3 deletions simapp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ type App interface {
LoadHeight(height int64) error

// Exports the state of the application for a genesis file.
ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
) (types.ExportedApp, error)
ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []string) (types.ExportedApp, error)

// All the registered module account addreses.
ModuleAccountAddrs() map[string]bool
Expand Down
2 changes: 1 addition & 1 deletion simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func CheckExportSimulation(
) error {
if config.ExportStatePath != "" {
fmt.Println("exporting app state...")
exported, err := app.ExportAppStateAndValidators(false, nil)
exported, err := app.ExportAppStateAndValidators(false, nil, nil)
if err != nil {
return err
}
Expand Down
32 changes: 30 additions & 2 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,27 @@ func (m *Manager) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, genesisData
}

// ExportGenesis performs export genesis functionality for modules
func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) map[string]json.RawMessage {
func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec, modulesToExport []string) map[string]json.RawMessage {
genesisData := make(map[string]json.RawMessage)
for _, moduleName := range m.OrderExportGenesis {
if len(modulesToExport) == 0 {

for _, moduleName := range m.OrderExportGenesis {
if moduleName == "interchainaccounts" {
genesisData[moduleName] = m.Modules[moduleName].DefaultGenesis(cdc)
} else {
genesisData[moduleName] = m.Modules[moduleName].ExportGenesis(ctx, cdc)
}
}

return genesisData
}

// verify modules exists in app, so that we don't panic in the middle of an export
if err := m.checkModulesExists(modulesToExport); err != nil {
panic(err)
}

for _, moduleName := range modulesToExport {
if moduleName == "interchainaccounts" {
genesisData[moduleName] = m.Modules[moduleName].DefaultGenesis(cdc)
} else {
Expand All @@ -356,6 +373,17 @@ func (m *Manager) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) map[string
return genesisData
}

// checkModulesExists verifies that all modules in the list exist in the app
func (m *Manager) checkModulesExists(moduleName []string) error {
for _, name := range moduleName {
if _, ok := m.Modules[name]; !ok {
return fmt.Errorf("module %s does not exist", name)
}
}

return nil
}

// assertNoForgottenModules checks that we didn't forget any modules in the
// SetOrder* functions.
func (m *Manager) assertNoForgottenModules(setOrderFnName string, moduleNames []string) {
Expand Down
6 changes: 5 additions & 1 deletion types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ func TestManager_ExportGenesis(t *testing.T) {
"module1": json.RawMessage(`{"key1": "value1"}`),
"module2": json.RawMessage(`{"key2": "value2"}`),
}
require.Equal(t, want, mm.ExportGenesis(ctx, cdc))
require.Equal(t, want, mm.ExportGenesis(ctx, cdc, []string{}))

require.Panics(t, func() {
mm.ExportGenesis(ctx, cdc, []string{"module1", "modulefoo"})
})
}

func TestManager_BeginBlock(t *testing.T) {
Expand Down

0 comments on commit c94a4d7

Please sign in to comment.