Skip to content

Commit

Permalink
[1760]: Set the init-version-map in the upgrade keeper during app.New…
Browse files Browse the repository at this point in the history
…. The upgrade keeper uses it during InitGenesis to write the version map to state. We also write the version map to state during InitChainer. But we don't call InitChainer during the import-export sim test, so the second app was missing all of those entries.
  • Loading branch information
SpicyLemon committed May 31, 2024
1 parent d0b5422 commit 65f4768
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
9 changes: 5 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ func New(

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
var skipGenesisInvariants = cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))
skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))

// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
Expand Down Expand Up @@ -791,6 +791,10 @@ func New(
ibctm.AppModule{},
)

// Set the upgrade-keeper's version map that it uses during init-genesis.
// When using depinject, this is done by means of the PopulateVersionMap function.
app.UpgradeKeeper.SetInitVersionMap(app.mm.GetVersionMap())

// BasicModuleManager defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration and genesis verification.
// By default it is composed of all the module from the module manager.
Expand Down Expand Up @@ -1163,9 +1167,6 @@ func (app *App) InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
if err := app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap()); err != nil {
panic(err)
}
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}

Expand Down
43 changes: 19 additions & 24 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,6 @@ func setupSimulation(dirPrefix string, dbName string) (simtypes.Config, dbm.DB,
return config, db, dir, logger, skip, err
}

// logIfEmptyValidatorSetErr writes some stuff to the logs and returns true if the provided obj is from an
// error about the validator set being empty. If the provided obj is not that, this is a no-op and returns false.
func logIfEmptyValidatorSetErr(logger log.Logger, obj interface{}) bool {
if obj == nil {
return false
}
errStr := fmt.Sprintf("%v", obj)
if strings.Contains(errStr, "validator set is empty after InitGenesis") {
logger.Info("Skipping simulation as all validators have been unbonded",
"err", errStr, "stacktrace", string(debug.Stack()))
return true
}
return false
}

// fauxMerkleModeOpt returns a BaseApp option to use a dbStoreAdapter instead of
// an IAVLStore for faster simulation speed.
func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
Expand Down Expand Up @@ -262,6 +247,16 @@ func TestSimple(t *testing.T) {
func TestAppImportExport(t *testing.T) {
// uncomment to run in ide without flags.
//simcli.FlagEnabledValue = true
//tempDir, err := os.MkdirTemp("", "sim-log-*")
//require.NoError(t, err, "MkdirTemp")
//t.Logf("tempDir: %s", tempDir)
//simcli.FlagNumBlocksValue = 30
//simcli.FlagVerboseValue = true
//simcli.FlagCommitValue = true
//simcli.FlagSeedValue = 2
//simcli.FlagPeriodValue = 3
//simcli.FlagExportParamsPathValue = filepath.Join(tempDir, fmt.Sprintf("sim_params-%d.json", simcli.FlagSeedValue))
//simcli.FlagExportStatePathValue = filepath.Join(tempDir, fmt.Sprintf("sim_state-%d.json", simcli.FlagSeedValue))

config, db, dir, logger, skip, err := setupSimulation("leveldb-app-sim", "Simulation")
if skip {
Expand Down Expand Up @@ -300,8 +295,8 @@ func TestAppImportExport(t *testing.T) {

// export state and simParams before the simulation error is checked
err = simtestutil.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)
require.NoError(t, err, "CheckExportSimulation")
require.NoError(t, simErr, "SimulateFromSeedProv")

printStats(config, db)

Expand All @@ -313,7 +308,7 @@ func TestAppImportExport(t *testing.T) {
fmt.Printf("importing genesis...\n")

newDB, newDir, newLogger, _, err := simtestutil.SetupSimulation(config, "leveldb-app-sim-2", "Simulation-2", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
require.NoError(t, err, "simulation setup failed")
require.NoError(t, err, "simulation setup 2 failed")

defer func() {
require.NoError(t, newDB.Close())
Expand All @@ -326,15 +321,15 @@ func TestAppImportExport(t *testing.T) {
err = json.Unmarshal(exported.AppState, &genesisState)
require.NoError(t, err)

defer func() {
logIfEmptyValidatorSetErr(logger, recover())
}()

ctxA := app.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight(), Time: lastBlockTime})
ctxB := newApp.NewContextLegacy(true, cmtproto.Header{Height: app.LastBlockHeight(), Time: lastBlockTime})
_, err = newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState)
if logIfEmptyValidatorSetErr(logger, err) {
return
if err != nil {
if strings.Contains(err.Error(), "validator set is empty after InitGenesis") {
logger.Info("Skipping simulation as all validators have been unbonded")
logger.Info("err", err, "stacktrace", string(debug.Stack()))
return
}
}
require.NoError(t, err, "InitGenesis")

Expand Down

0 comments on commit 65f4768

Please sign in to comment.