Skip to content

Commit

Permalink
test(sim): add TestAppSimulationAfterImport
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Dec 28, 2023
1 parent 65198cb commit 2e6581f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 7 deletions.
19 changes: 12 additions & 7 deletions contrib/make/simulation.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ test-sim-import-export:
-NumBlocks=100 \
-Commit=true \
-Seed=99 \
-Period=0
-Period=5

.PHONY: test-sim-benchmark-invariants
test-sim-benchmark-invariants:
@echo "Running simulation invariant benchmarks..."
@go test -mod=readonly $(SIMAPP) -benchmem -bench=BenchmarkInvariants -run=^$ \
-Enabled=true -NumBlocks=1000 -BlockSize=200 \
-Period=1 -Commit=true -Seed=57 -v -timeout 24h
.PHONY: test-sim-after-import
test-sim-after-import:
@echo "Running application simulation-after-import. This may take several minutes..."
@go test -mod=readonly -v $(SIMAPP) \
-run TestAppSimulationAfterImport \
-Params=params.json \
-Enabled=true \
-NumBlocks=50 \
-Commit=true \
-Seed=99 \
-Period=5
89 changes: 89 additions & 0 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -298,3 +299,91 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(skp.A.Name(), oldApp.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
}
}

func TestAppSimulationAfterImport(t *testing.T) {
config := simcli.NewConfigFromFlags()
config.ChainID = SimAppChainID

db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "goleveldb-app-sim", "Simulation", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation after import")
}
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
}()

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

encoding := app.MakeEncodingConfig()
oldApp := app.NewNibiruApp(logger, db, nil, true, encoding, appOptions, baseapp.SetChainID(SimAppChainID))
require.Equal(t, "Nibiru", oldApp.Name())
appCodec := oldApp.AppCodec()

// Run randomized simulation
stopEarly, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
oldApp.BaseApp,
AppStateFn(appCodec, oldApp.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(oldApp, appCodec, config),
oldApp.ModuleAccountAddrs(),
config,
appCodec,
)

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

if config.Commit {
simtestutil.PrintStats(db)
}

if stopEarly {
fmt.Println("can't export or import a zero-validator genesis, exiting test...")
return
}

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

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

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

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

defer func() {
require.NoError(t, newDB.Close())
require.NoError(t, os.RemoveAll(newDir))
}()

newApp := app.NewNibiruApp(log.NewNopLogger(), newDB, nil, true, encoding, appOptions, baseapp.SetChainID(SimAppChainID))
require.Equal(t, "Nibiru", newApp.Name())

newApp.InitChain(abci.RequestInitChain{
ChainId: SimAppChainID,
AppStateBytes: exported.AppState,
})

_, _, err = simulation.SimulateFromSeed(
t,
os.Stdout,
newApp.BaseApp,
AppStateFn(appCodec, newApp.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(newApp, newApp.AppCodec(), config),
newApp.ModuleAccountAddrs(),
config,
oldApp.AppCodec(),
)
require.NoError(t, err)
}

0 comments on commit 2e6581f

Please sign in to comment.