Skip to content

Commit

Permalink
move simulation tests to root directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 29, 2024
1 parent 5c682e9 commit 06c5d75
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
make-targets:
description: 'Comma separated list of make targets to run (e.g., test-sim-nondeterminism, test-sim-fullappsimulation)'
required: true
default: ''
default: 'test-sim-nondeterminism'

concurrency:
group: simulation-${{ github.head_ref || github.sha }}
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'
go-version: '1.22'

- name: Install dependencies
run: make runsim
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ start-upgrade-import-mainnet-test: zetanode-upgrade
###############################################################################

BINDIR ?= $(GOPATH)/bin
SIMAPP = ./tests/simulation
SIMAPP = ./simulation


# Run sim is a cosmos tool which helps us to run multiple simulations in parallel.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
109 changes: 55 additions & 54 deletions tests/simulation/sim_test.go → simulation/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
evmtypes "github.com/zeta-chain/ethermint/x/evm/types"
"github.com/zeta-chain/node/simulation/sim"

"github.com/cosmos/cosmos-sdk/store"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/node/app"
simutils "github.com/zeta-chain/node/tests/simulation/sim"

"github.com/cosmos/cosmos-sdk/store"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -41,7 +40,7 @@ import (
// AppChainID hardcoded chainID for simulation

func init() {
simutils.GetSimulatorFlags()
sim.GetSimulatorFlags()
}

type StoreKeysPrefixes struct {
Expand Down Expand Up @@ -70,11 +69,11 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
// The following test certifies that , for the same set of operations ( irrespective of what the operations are ) ,
// we would reach the same final state if the initial state is the same
func TestAppStateDeterminism(t *testing.T) {
if !simutils.FlagEnabledValue {
if !sim.FlagEnabledValue {
t.Skip("skipping application simulation")
}

config := simutils.NewConfigFromFlags()
config := sim.NewConfigFromFlags()

config.InitialBlockHeight = 1
config.ExportParamsPath = ""
Expand All @@ -95,7 +94,7 @@ func TestAppStateDeterminism(t *testing.T) {
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)

appOptions := make(cosmossimutils.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue
appOptions[server.FlagInvCheckPeriod] = sim.FlagPeriodValue

t.Log("Running tests for numSeeds: ", numSeeds, " numTimesToRunPerSeed: ", numTimesToRunPerSeed)

Expand All @@ -109,13 +108,13 @@ func TestAppStateDeterminism(t *testing.T) {
config,
SimDBBackend,
SimDBName,
simutils.FlagVerboseValue,
simutils.FlagEnabledValue,
sim.FlagVerboseValue,
sim.FlagEnabledValue,
)
require.NoError(t, err)
appOptions[flags.FlagHome] = dir

simApp, err := simutils.NewSimApp(
simApp, err := sim.NewSimApp(
logger,
db,
appOptions,
Expand All @@ -135,7 +134,7 @@ func TestAppStateDeterminism(t *testing.T) {
t,
os.Stdout,
simApp.BaseApp,
simutils.AppStateFn(
sim.AppStateFn(
simApp.AppCodec(),
simApp.SimulationManager(),
simApp.BasicManager().DefaultGenesis(simApp.AppCodec()),
Expand All @@ -148,14 +147,16 @@ func TestAppStateDeterminism(t *testing.T) {
)
require.NoError(t, err)

simutils.PrintStats(db)
sim.PrintStats(db)

appHash := simApp.LastCommitID().Hash
appHashList[j] = appHash

// Clean up resources
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
t.Cleanup(func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
})

if j != 0 {
require.Equal(
Expand All @@ -178,7 +179,7 @@ func TestAppStateDeterminism(t *testing.T) {
// At the end of the run it tries to export the genesis state to make sure the export works.
func TestFullAppSimulation(t *testing.T) {

config := simutils.NewConfigFromFlags()
config := sim.NewConfigFromFlags()

config.ChainID = SimAppChainID
config.BlockMaxGas = SimBlockMaxGas
Expand All @@ -188,35 +189,35 @@ func TestFullAppSimulation(t *testing.T) {
config,
SimDBBackend,
SimDBName,
simutils.FlagVerboseValue,
simutils.FlagEnabledValue,
sim.FlagVerboseValue,
sim.FlagEnabledValue,
)
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")

defer func() {
t.Cleanup(func() {
if err := db.Close(); err != nil {
require.NoError(t, err, "Error closing new database")
}
if err := os.RemoveAll(dir); err != nil {
require.NoError(t, err, "Error removing directory")
}
}()
})
appOptions := make(cosmossimutils.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue
appOptions[server.FlagInvCheckPeriod] = sim.FlagPeriodValue
appOptions[flags.FlagHome] = dir

simApp, err := simutils.NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
simApp, err := sim.NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
require.NoError(t, err)

blockedAddresses := simApp.ModuleAccountAddrs()
_, _, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
simApp.BaseApp,
simutils.AppStateFn(
sim.AppStateFn(
simApp.AppCodec(),
simApp.SimulationManager(),
simApp.BasicManager().DefaultGenesis(simApp.AppCodec()),
Expand All @@ -237,11 +238,11 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, err)
}

simutils.PrintStats(db)
sim.PrintStats(db)
}

func TestAppImportExport(t *testing.T) {
config := simutils.NewConfigFromFlags()
config := sim.NewConfigFromFlags()

config.ChainID = SimAppChainID
config.BlockMaxGas = SimBlockMaxGas
Expand All @@ -251,27 +252,27 @@ func TestAppImportExport(t *testing.T) {
config,
SimDBBackend,
SimDBName,
simutils.FlagVerboseValue,
simutils.FlagEnabledValue,
sim.FlagVerboseValue,
sim.FlagEnabledValue,
)
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")

defer func() {
t.Cleanup(func() {
if err := db.Close(); err != nil {
require.NoError(t, err, "Error closing new database")
}
if err := os.RemoveAll(dir); err != nil {
require.NoError(t, err, "Error removing directory")
}
}()
})

appOptions := make(cosmossimutils.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue
appOptions[server.FlagInvCheckPeriod] = sim.FlagPeriodValue
appOptions[flags.FlagHome] = dir
simApp, err := simutils.NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
simApp, err := sim.NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
require.NoError(t, err)

// Run randomized simulation
Expand All @@ -280,7 +281,7 @@ func TestAppImportExport(t *testing.T) {
t,
os.Stdout,
simApp.BaseApp,
simutils.AppStateFn(
sim.AppStateFn(
simApp.AppCodec(),
simApp.SimulationManager(),
simApp.BasicManager().DefaultGenesis(simApp.AppCodec()),
Expand All @@ -293,10 +294,10 @@ func TestAppImportExport(t *testing.T) {
)
require.NoError(t, simErr)

err = simutils.CheckExportSimulation(simApp, config, simParams)
err = sim.CheckExportSimulation(simApp, config, simParams)
require.NoError(t, err)

simutils.PrintStats(db)
sim.PrintStats(db)

t.Log("exporting genesis")
// export state and simParams
Expand All @@ -308,22 +309,22 @@ func TestAppImportExport(t *testing.T) {
config,
SimDBBackend+"_new",
SimDBName+"_new",
simutils.FlagVerboseValue,
simutils.FlagEnabledValue,
sim.FlagVerboseValue,
sim.FlagEnabledValue,
)

require.NoError(t, err, "simulation setup failed")

defer func() {
t.Cleanup(func() {
if err := newDB.Close(); err != nil {
require.NoError(t, err, "Error closing new database")
}
if err := os.RemoveAll(newDir); err != nil {
require.NoError(t, err, "Error removing directory")
}
}()
})

newSimApp, err := simutils.NewSimApp(
newSimApp, err := sim.NewSimApp(
logger,
newDB,
appOptions,
Expand Down Expand Up @@ -404,7 +405,7 @@ func TestAppImportExport(t *testing.T) {
}

func TestAppSimulationAfterImport(t *testing.T) {
config := simutils.NewConfigFromFlags()
config := sim.NewConfigFromFlags()

config.ChainID = SimAppChainID
config.BlockMaxGas = SimBlockMaxGas
Expand All @@ -414,27 +415,27 @@ func TestAppSimulationAfterImport(t *testing.T) {
config,
SimDBBackend,
SimDBName,
simutils.FlagVerboseValue,
simutils.FlagEnabledValue,
sim.FlagVerboseValue,
sim.FlagEnabledValue,
)
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")

defer func() {
t.Cleanup(func() {
if err := db.Close(); err != nil {
require.NoError(t, err, "Error closing new database")
}
if err := os.RemoveAll(dir); err != nil {
require.NoError(t, err, "Error removing directory")
}
}()
})

appOptions := make(cosmossimutils.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue
appOptions[server.FlagInvCheckPeriod] = sim.FlagPeriodValue
appOptions[flags.FlagHome] = dir
simApp, err := simutils.NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
simApp, err := sim.NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
require.NoError(t, err)

// Run randomized simulation
Expand All @@ -443,7 +444,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
t,
os.Stdout,
simApp.BaseApp,
simutils.AppStateFn(
sim.AppStateFn(
simApp.AppCodec(),
simApp.SimulationManager(),
simApp.BasicManager().DefaultGenesis(simApp.AppCodec()),
Expand All @@ -456,10 +457,10 @@ func TestAppSimulationAfterImport(t *testing.T) {
)
require.NoError(t, simErr)

err = simutils.CheckExportSimulation(simApp, config, simParams)
err = sim.CheckExportSimulation(simApp, config, simParams)
require.NoError(t, err)

simutils.PrintStats(db)
sim.PrintStats(db)

if stopEarly {
t.Log("can't export or import a zero-validator genesis, exiting test")
Expand All @@ -478,22 +479,22 @@ func TestAppSimulationAfterImport(t *testing.T) {
config,
SimDBBackend+"_new",
SimDBName+"_new",
simutils.FlagVerboseValue,
simutils.FlagEnabledValue,
sim.FlagVerboseValue,
sim.FlagEnabledValue,
)

require.NoError(t, err, "simulation setup failed")

defer func() {
t.Cleanup(func() {
if err := newDB.Close(); err != nil {
require.NoError(t, err, "Error closing new database")
}
if err := os.RemoveAll(newDir); err != nil {
require.NoError(t, err, "Error removing directory")
}
}()
})

newSimApp, err := simutils.NewSimApp(
newSimApp, err := sim.NewSimApp(
logger,
newDB,
appOptions,
Expand All @@ -511,7 +512,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
t,
os.Stdout,
newSimApp.BaseApp,
simutils.AppStateFn(
sim.AppStateFn(
simApp.AppCodec(),
simApp.SimulationManager(),
simApp.BasicManager().DefaultGenesis(simApp.AppCodec()),
Expand Down

0 comments on commit 06c5d75

Please sign in to comment.