From 064e05ed3dd29a1f23e5730f622e874bed9ed9ef Mon Sep 17 00:00:00 2001 From: Tanmay Date: Wed, 23 Oct 2024 19:57:17 -0400 Subject: [PATCH] fix defer functions --- app/export.go | 7 ++++- docs/development/SIMULATION_TESTING.md | 10 +++---- tests/simulation/sim/sim_utils.go | 6 ++-- tests/simulation/sim_test.go | 40 +++++++++++++++++++------- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/app/export.go b/app/export.go index 037ad6f161..50287e204d 100644 --- a/app/export.go +++ b/app/export.go @@ -159,7 +159,12 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str counter := int16(0) for ; iter.Valid(); iter.Next() { - addr := sdk.ValAddress(iter.Key()[2:]) + key := iter.Key() + if len(key) <= 2 { + app.Logger().Error("unexpected key in staking store", "key", key) + continue + } + addr := sdk.ValAddress(key[2:]) validator, found := app.StakingKeeper.GetValidator(ctx, addr) if !found { panic("expected validator, not found") diff --git a/docs/development/SIMULATION_TESTING.md b/docs/development/SIMULATION_TESTING.md index 29697378be..1ff0ebd0af 100644 --- a/docs/development/SIMULATION_TESTING.md +++ b/docs/development/SIMULATION_TESTING.md @@ -23,7 +23,7 @@ make test-sim-full-app ### Import Export simulation test The import export simulation test runs a full application simulation and exports the application state at the end of the run. -This state is then then imported into a new simulation. +This state is then imported into a new simulation. At the end of the run we compare the keys for the application state for both the simulations to make sure they are the same. ```bash @@ -32,7 +32,7 @@ make test-sim-import-export ### Import and run simulation test This simulation test exports the application state at the end of the run and imports it into a new simulation. ```bash -make test-sim-import-export +make test-sim-after-import ``` ### Multi seed long test Multi seed long test runs a full application simulation with multiple seeds and multiple blocks.This runs the test for a longer duration compared to the multi seed short test @@ -40,7 +40,7 @@ Multi seed long test runs a full application simulation with multiple seeds and make test-sim-multi-seed-long ``` ### Multi seed short test -Multi seed short test runs a full application simulation with multiple seeds and multiple blocks. This runs the test for a longer duration compared to the multi seed long test +Multi seed short test runs a full application simulation with multiple seeds and multiple blocks. This runs the test for a shorter duration compared to the multi seed long test ```bash make test-sim-multi-seed-short ``` @@ -49,9 +49,9 @@ This test runs the import export simulation test for a longer duration.It uses t ```bash make test-sim-import-export-long ``` -### Import and run simulation test +### Import and run simulation test long This test runs the import and run simulation test for a longer duration. It uses the runsim tool to run the same test in parallel threads ```bash -make test-sim-after-import-longg +make test-sim-after-import-long ``` diff --git a/tests/simulation/sim/sim_utils.go b/tests/simulation/sim/sim_utils.go index a8d7ef7e61..e1ee19f307 100644 --- a/tests/simulation/sim/sim_utils.go +++ b/tests/simulation/sim/sim_utils.go @@ -77,10 +77,10 @@ func CheckExportSimulation(app runtime.AppI, config simtypes.Config, params simt if config.ExportStatePath != "" { exported, err := app.ExportAppStateAndValidators(false, nil, nil) if err != nil { - return err + return fmt.Errorf("failed to export app state: %w", err) } - if err := os.WriteFile(config.ExportStatePath, []byte(exported.AppState), 0o600); err != nil { + if err := os.WriteFile(config.ExportStatePath, exported.AppState, 0o600); err != nil { return err } } @@ -88,7 +88,7 @@ func CheckExportSimulation(app runtime.AppI, config simtypes.Config, params simt if config.ExportParamsPath != "" { paramsBz, err := json.MarshalIndent(params, "", " ") if err != nil { - return err + return fmt.Errorf("failed to write app state to %s: %w", config.ExportStatePath, err) } if err := os.WriteFile(config.ExportParamsPath, paramsBz, 0o600); err != nil { diff --git a/tests/simulation/sim_test.go b/tests/simulation/sim_test.go index 0a7d44aac6..0bf39938c4 100644 --- a/tests/simulation/sim_test.go +++ b/tests/simulation/sim_test.go @@ -197,8 +197,12 @@ func TestFullAppSimulation(t *testing.T) { require.NoError(t, err, "simulation setup failed") defer func() { - require.NoError(t, db.Close()) - require.NoError(t, os.RemoveAll(dir)) + if err := db.Close(); err != nil { + t.Errorf("Error closing new database: %v", err) + } + if err := os.RemoveAll(dir); err != nil { + t.Errorf("Error removing directory %s: %v", dir, err) + } }() appOptions := make(cosmossimutils.AppOptionsMap, 0) appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue @@ -256,8 +260,12 @@ func TestAppImportExport(t *testing.T) { require.NoError(t, err, "simulation setup failed") defer func() { - require.NoError(t, db.Close()) - require.NoError(t, os.RemoveAll(dir)) + if err := db.Close(); err != nil { + t.Errorf("Error closing new database: %v", err) + } + if err := os.RemoveAll(dir); err != nil { + t.Errorf("Error removing directory %s: %v", dir, err) + } }() appOptions := make(cosmossimutils.AppOptionsMap, 0) @@ -307,8 +315,12 @@ func TestAppImportExport(t *testing.T) { require.NoError(t, err, "simulation setup failed") defer func() { - require.NoError(t, newDB.Close()) - require.NoError(t, os.RemoveAll(newDir)) + if err := newDB.Close(); err != nil { + t.Errorf("Error closing new database: %v", err) + } + if err := os.RemoveAll(newDir); err != nil { + t.Errorf("Error removing directory %s: %v", newDir, err) + } }() newSimApp, err := simutils.NewSimApp( @@ -410,8 +422,12 @@ func TestAppSimulationAfterImport(t *testing.T) { require.NoError(t, err, "simulation setup failed") defer func() { - require.NoError(t, db.Close()) - require.NoError(t, os.RemoveAll(dir)) + if err := db.Close(); err != nil { + t.Errorf("Error closing new database: %v", err) + } + if err := os.RemoveAll(dir); err != nil { + t.Errorf("Error removing directory %s: %v", dir, err) + } }() appOptions := make(cosmossimutils.AppOptionsMap, 0) @@ -468,8 +484,12 @@ func TestAppSimulationAfterImport(t *testing.T) { require.NoError(t, err, "simulation setup failed") defer func() { - require.NoError(t, newDB.Close()) - require.NoError(t, os.RemoveAll(newDir)) + if err := newDB.Close(); err != nil { + t.Errorf("Error closing new database: %v", err) + } + if err := os.RemoveAll(newDir); err != nil { + t.Errorf("Error removing directory %s: %v", newDir, err) + } }() newSimApp, err := simutils.NewSimApp(