Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
JayT106 committed Oct 12, 2022
1 parent e8d5664 commit 17d2f06
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 91 deletions.
1 change: 0 additions & 1 deletion client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ const (
FlagTip = "tip"
FlagAux = "aux"
FlagOutput = tmcli.OutputFlag
FlagGenesisFilePath = "genesis-path"

// Tendermint logging flags
FlagLogLevel = "log_level"
Expand Down
7 changes: 6 additions & 1 deletion runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ type AppI interface {
LoadHeight(height int64) error

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

// Helper for the simulation framework.
SimulationManager() *module.SimulationManager
Expand Down
20 changes: 11 additions & 9 deletions server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
FlagForZeroHeight = "for-zero-height"
FlagJailAllowedAddrs = "jail-allowed-addrs"
FlagModulesToExport = "modules-to-export"
FlagSplitModules = "split-modules"
)

// ExportCmd dumps app state to JSON.
Expand All @@ -35,11 +36,6 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
homeDir, _ := cmd.Flags().GetString(flags.FlagHome)
config.SetRoot(homeDir)

genesisExportPath, err := cmd.Flags().GetString(flags.FlagGenesisFilePath)
if err != nil {
return err
}

if _, err := os.Stat(config.GenesisFile()); os.IsNotExist(err) {
return err
}
Expand Down Expand Up @@ -73,8 +69,9 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
forZeroHeight, _ := cmd.Flags().GetBool(FlagForZeroHeight)
jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(FlagJailAllowedAddrs)
modulesToExport, _ := cmd.Flags().GetStringSlice(FlagModulesToExport)
splitModules, _ := cmd.Flags().GetBool(FlagSplitModules)

exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport)
exported, err := appExporter(serverCtx.Logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, serverCtx.Viper, modulesToExport, splitModules)
if err != nil {
return fmt.Errorf("error exporting state: %v", err)
}
Expand Down Expand Up @@ -102,8 +99,13 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
},
}

if len(genesisExportPath) > 0 {
if err := doc.SaveAs(filepath.Join(genesisExportPath, "genesis", "genesis.json")); err != nil {
if splitModules {
wd, err := os.Getwd()
if err != nil {
return err
}

if err := doc.SaveAs(filepath.Join(wd, "genesis", "genesis.json")); err != nil {
return err
}
} else {
Expand All @@ -128,7 +130,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
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")
cmd.Flags().String(flags.FlagGenesisFilePath, "", "Export state to the designated file path")
cmd.Flags().Bool(FlagSplitModules, false, "Export module state and store it as separate file(s) in genesis/[module] folder")

return cmd
}
2 changes: 1 addition & 1 deletion server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,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, []string) (ExportedApp, error)
AppExporter func(log.Logger, dbm.DB, io.Writer, int64, bool, []string, AppOptions, []string, bool) (ExportedApp, error)
)
15 changes: 4 additions & 11 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,7 @@ type SimApp struct {
// simulation manager
sm *module.SimulationManager

// the root folder of the app config and data
homepath string

// the path for the genesis state exporting
exportpath string
appOpts servertypes.AppOptions
}

func init() {
Expand Down Expand Up @@ -249,11 +245,7 @@ func NewSimApp(
}

app.App = appBuilder.Build(logger, db, traceStore, baseAppOptions...)
app.homepath = cast.ToString(appOpts.Get(flags.FlagHome))
ep := cast.ToString(appOpts.Get(flags.FlagGenesisFilePath))
if len(ep) > 0 {
app.exportpath = filepath.Join(ep, "genesis")
}
app.appOpts = appOpts

// configure state listening capabilities using AppOptions
// we are doing nothing with the returned streamingServices and waitGroup in this case
Expand Down Expand Up @@ -336,7 +328,8 @@ func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.
}

if bytes.Equal(loadAppStateFromFolder, buf.Bytes()) {
app.App.ModuleManager.SetGenesisPath(filepath.Join(app.homepath, "config", "genesis"))
homepath := cast.ToString(app.appOpts.Get(flags.FlagHome))
app.App.ModuleManager.GenesisPath = filepath.Join(homepath, "config", "genesis")
}

app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
Expand Down
16 changes: 4 additions & 12 deletions simapp/app_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ type SimApp struct {
// module configurator
configurator module.Configurator

// the root folder of the app config and data
homepath string

// the path for the genesis state exporting
exportpath string
appOpts servertypes.AppOptions
}

func init() {
Expand Down Expand Up @@ -263,12 +259,7 @@ func NewSimApp(
keys: keys,
tkeys: tkeys,
memKeys: memKeys,
}

app.homepath = cast.ToString(appOpts.Get(flags.FlagHome))
ep := cast.ToString(appOpts.Get(flags.FlagGenesisFilePath))
if len(ep) > 0 {
app.exportpath = filepath.Join(ep, "genesis")
appOpts: appOpts,
}

app.ParamsKeeper = initParamsKeeper(appCodec, legacyAmino, keys[paramstypes.StoreKey], tkeys[paramstypes.TStoreKey])
Expand Down Expand Up @@ -571,7 +562,8 @@ func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.

var genesisState GenesisState
if bytes.Equal(loadAppStateFromFolder, buf.Bytes()) {
app.ModuleManager.SetGenesisPath(filepath.Join(app.homepath, "config", "genesis"))
homepath := cast.ToString(app.appOpts.Get(flags.FlagHome))
app.ModuleManager.GenesisPath = filepath.Join(homepath, "config", "genesis")
} else {
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion simapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {
logger2 := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(logger2, db, nil, true, simtestutil.NewAppOptionsWithFlagHome(DefaultNodeHome))
_, err := app2.ExportAppStateAndValidators(false, []string{}, []string{})
_, err := app2.ExportAppStateAndValidators(false, []string{}, []string{}, false)
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

Expand Down
18 changes: 14 additions & 4 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"os"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

Expand All @@ -16,7 +17,12 @@ import (

// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAddrs []string, modulesToExport []string) (servertypes.ExportedApp, error) {
func (app *SimApp) ExportAppStateAndValidators(
forZeroHeight bool,
jailAllowedAddrs []string,
modulesToExport []string,
splitModules bool,
) (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 @@ -28,14 +34,18 @@ func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAd
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

app.ModuleManager.SetGenesisPath(app.exportpath)
genState, err := app.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
wd, err := os.Getwd()
if err != nil {
return servertypes.ExportedApp{}, err
}
app.ModuleManager.GenesisPath = wd
genState, err := app.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport, splitModules)
if err != nil {
return servertypes.ExportedApp{}, err
}

var appState []byte
if app.exportpath != "" {
if splitModules {
jsonObj := make(map[string]json.RawMessage)
jsonObj["module_genesis_state"] = []byte("true")
appState, err = json.MarshalIndent(jsonObj, "", " ")
Expand Down
15 changes: 7 additions & 8 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, "SimApp", app.Name())

// Run randomized simulation and export the genesis state
exported := randomSimulation(t, app, config, db)
exported := randomSimulation(t, app, config, db, false)

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

Expand Down Expand Up @@ -218,7 +218,7 @@ func TestAppSimulationAfterImport(t *testing.T) {

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

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

fmt.Printf("importing genesis...\n")
Expand Down Expand Up @@ -341,15 +341,14 @@ func TestAppImportExportWithAppStatePath(t *testing.T) {
tmp := t.TempDir()

appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = DefaultNodeHome
appOptions[flags.FlagHome] = tmp
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue
appOptions[flags.FlagGenesisFilePath] = tmp

app := NewSimApp(logger, db, nil, true, appOptions, fauxMerkleModeOpt)
require.Equal(t, "SimApp", app.Name())

// Run randomized simulation and export the genesis state
exported := randomSimulation(t, app, config, db)
exported := randomSimulation(t, app, config, db, true)

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

Expand All @@ -375,11 +374,11 @@ func TestAppImportExportWithAppStatePath(t *testing.T) {
}
}()

newApp.ModuleManager.SetGenesisPath(tmp)
newApp.ModuleManager.GenesisPath = tmp
checkStoresAfterInitGenesis(t, app, newApp, exported.ConsensusParams, nil)
}

func randomSimulation(t *testing.T, app *SimApp, config simtypes.Config, db dbm.DB) servertypes.ExportedApp {
func randomSimulation(t *testing.T, app *SimApp, config simtypes.Config, db dbm.DB, splitModule bool) servertypes.ExportedApp {
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
Expand All @@ -403,7 +402,7 @@ func randomSimulation(t *testing.T, app *SimApp, config simtypes.Config, db dbm.

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

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

return exported
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 @@ -319,6 +319,7 @@ func appExport(
jailAllowedAddrs []string,
appOpts servertypes.AppOptions,
modulesToExport []string,
splitModules bool,
) (servertypes.ExportedApp, error) {
var simApp *simapp.SimApp

Expand Down Expand Up @@ -348,5 +349,5 @@ func appExport(
simApp = simapp.NewSimApp(logger, db, traceStore, true, appOpts)
}

return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport, splitModules)
}
2 changes: 1 addition & 1 deletion tests/e2e/server/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func setupApp(t *testing.T, tempDir string) (*simapp.SimApp, context.Context, *t
simApp = simapp.NewSimApp(logger, db, nil, true, appOptions)
}

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

ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion testutil/sims/simulation_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes
func CheckExportSimulation(app runtime.AppI, config simtypes.Config, params simtypes.Params) error {
if config.ExportStatePath != "" {
fmt.Println("exporting app state...")
exported, err := app.ExportAppStateAndValidators(false, nil, nil)
exported, err := app.ExportAppStateAndValidators(false, nil, nil, false)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 17d2f06

Please sign in to comment.