Skip to content

Commit

Permalink
move simulation tests to a separate directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 1, 2024
1 parent b774b1e commit ac8fddf
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 59 deletions.
44 changes: 2 additions & 42 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/auth"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
authzmodule "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -64,7 +62,7 @@ func simulationModules(
_ bool,
) []module.AppModuleSimulation {
return []module.AppModuleSimulation{
auth.NewAppModule(appCodec, app.AccountKeeper, RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper, app.GetSubspace(banktypes.ModuleName)),
//gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
Expand All @@ -84,41 +82,3 @@ func simulationModules(
groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
}
}

// RandomGenesisAccounts defines the default RandomGenesisAccountsFn used on the SDK.
// It creates a slice of BaseAccount, ContinuousVestingAccount and DelayedVestingAccount.
func RandomGenesisAccounts(simState *module.SimulationState) authtypes.GenesisAccounts {
genesisAccs := make(authtypes.GenesisAccounts, len(simState.Accounts))
for i, acc := range simState.Accounts {
bacc := authtypes.NewBaseAccountWithAddress(acc.Address)

// Only consider making a vesting account once the initial bonded validator
// set is exhausted due to needing to track DelegatedVesting.
if !(int64(i) > simState.NumBonded && simState.Rand.Intn(100) < 50) {
genesisAccs[i] = bacc
continue
}

initialVesting := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, simState.Rand.Int63n(simState.InitialStake.Int64())))
var endTime int64

startTime := simState.GenTimestamp.Unix()

// Allow for some vesting accounts to vest very quickly while others very slowly.
if simState.Rand.Intn(100) < 50 {
endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*24*30))))
} else {
endTime = int64(simulation.RandIntBetween(simState.Rand, int(startTime)+1, int(startTime+(60*60*12))))
}

bva := vestingtypes.NewBaseVestingAccount(bacc, initialVesting, endTime)

if simState.Rand.Intn(100) < 50 {
genesisAccs[i] = vestingtypes.NewContinuousVestingAccountRaw(bva, startTime)
} else {
genesisAccs[i] = vestingtypes.NewDelayedVestingAccountRaw(bva)
}
}

return genesisAccs
}
2 changes: 1 addition & 1 deletion sims.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
### Simulations

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

runsim: $(BINDIR)/runsim
$(BINDIR)/runsim:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 13 additions & 16 deletions app/sim_test.go → tests/simulation/sim_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app_test
package simulation_test

import (
"encoding/json"
Expand All @@ -15,6 +15,7 @@ import (
evmante "github.com/zeta-chain/ethermint/app/ante"
zetaapp "github.com/zeta-chain/node/app"
"github.com/zeta-chain/node/app/ante"
sim2 "github.com/zeta-chain/node/tests/simulation/sim"

dbm "github.com/cometbft/cometbft-db"

Expand All @@ -27,16 +28,12 @@ import (
simulation2 "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"

// "github.com/cosmos/gaia/v11/app/helpers"
// "github.com/cosmos/gaia/v11/app/params"
"github.com/zeta-chain/node/app/sim"
)

// AppChainID hardcoded chainID for simulation

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

const (
Expand Down Expand Up @@ -95,11 +92,11 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
// TODO: Make another test for the fuzzer itself, which just has noOp txs
// and doesn't depend on the application.
func TestAppStateDeterminism(t *testing.T) {
if !sim.FlagEnabledValue {
if !sim2.FlagEnabledValue {
t.Skip("skipping application simulation")
}

config := sim.NewConfigFromFlags()
config := sim2.NewConfigFromFlags()
config.InitialBlockHeight = 1
config.ExportParamsPath = ""
config.OnOperation = false
Expand All @@ -116,7 +113,7 @@ func TestAppStateDeterminism(t *testing.T) {

appHashList := make([]json.RawMessage, numTimesToRunPerSeed)
appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = sim.FlagPeriodValue
appOptions[server.FlagInvCheckPeriod] = sim2.FlagPeriodValue

for i := 0; i < numSeeds; i++ {
if config.Seed == simcli.DefaultSeedValue {
Expand All @@ -127,7 +124,7 @@ func TestAppStateDeterminism(t *testing.T) {

for j := 0; j < numTimesToRunPerSeed; j++ {
var logger log.Logger
if sim.FlagVerboseValue {
if sim2.FlagVerboseValue {
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
} else {
logger = log.NewNopLogger()
Expand All @@ -151,7 +148,7 @@ func TestAppStateDeterminism(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
sim.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
sim2.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
simulation2.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
blockedAddresses,
Expand All @@ -161,7 +158,7 @@ func TestAppStateDeterminism(t *testing.T) {
require.NoError(t, err)

if config.Commit {
sim.PrintStats(db)
sim2.PrintStats(db)
}

appHash := app.LastCommitID().Hash
Expand All @@ -178,13 +175,13 @@ func TestAppStateDeterminism(t *testing.T) {
}

func TestFullAppSimulation(t *testing.T) {
config := sim.NewConfigFromFlags()
config := sim2.NewConfigFromFlags()
config.ChainID = SimAppChainID
config.BlockMaxGas = SimBlockMaxGas
config.DBBackend = "memdb"
//config.ExportStatePath = "/Users/tanmay/.zetacored/simulation_state_export.json"

db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "mem-db", "Simulation", sim.FlagVerboseValue, sim.FlagEnabledValue)
db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "mem-db", "Simulation", sim2.FlagVerboseValue, sim2.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation")
}
Expand All @@ -195,7 +192,7 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, os.RemoveAll(dir))
}()
appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = sim.FlagPeriodValue
appOptions[server.FlagInvCheckPeriod] = sim2.FlagPeriodValue

app, err := NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
require.NoError(t, err)
Expand All @@ -205,7 +202,7 @@ func TestFullAppSimulation(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
sim.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
sim2.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
simulation2.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
blockedAddresses,
Expand Down

0 comments on commit ac8fddf

Please sign in to comment.