Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 2, 2024
1 parent b55f439 commit 913aa19
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 301 deletions.
7 changes: 5 additions & 2 deletions sims.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
BINDIR ?= $(GOPATH)/bin
SIMAPP = ./tests/simulation


# Run sim is a cosmos tool which helps us to run multiple simulations in parallel.
runsim: $(BINDIR)/runsim
$(BINDIR)/runsim:
@echo "Installing runsim..."
Expand All @@ -15,13 +17,14 @@ $(BINDIR)/runsim:
test-sim-nondeterminism:
@echo "Running non-determinism test..."
@go test -mod=readonly $(SIMAPP) -run TestAppStateDeterminism -Enabled=true \
-NumBlocks=10 -BlockSize=20 -Commit=true -Period=0 -v -timeout 24h
-NumBlocks=100 -BlockSize=200 -Commit=true -Period=0 -v -timeout 24h


test-sim-fullappsimulation:
@echo "Running TestFullAppSimulation."
@go test -mod=readonly $(SIMAPP) -run TestFullAppSimulation -Enabled=true \
-NumBlocks=10 -BlockSize=20 -Commit=true -Period=0 -v -timeout 24h
-NumBlocks=100 -BlockSize=200 -Commit=true -Period=0 -v -timeout 24h

test-sim-multi-seed-long: runsim
@echo "Running long multi-seed application simulation. This may take awhile!"
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(SIMAPP) -ExitOnFail 500 50 TestFullAppSimulation
Expand Down
5 changes: 2 additions & 3 deletions tests/simulation/sim/sim_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func AppStateFn(cdc codec.Codec, simManager *module.SimulationManager, genesisSt
notBondedTokens = notBondedTokens.Add(val.GetTokens())

Check warning on line 107 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L107

Added line #L107 was not covered by tests
}
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)

Check warning on line 109 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L109

Added line #L109 was not covered by tests

// edit bank state to make it have the not bonded pool tokens
bankStateBz, ok := rawState[banktypes.ModuleName]
// TODO(fdymylja/jonathan): should we panic in this case
if !ok {
panic("bank genesis state is missing")

Check warning on line 114 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L112-L114

Added lines #L112 - L114 were not covered by tests
}
Expand All @@ -134,6 +134,7 @@ func AppStateFn(cdc codec.Codec, simManager *module.SimulationManager, genesisSt
})

Check warning on line 134 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L130-L134

Added lines #L130 - L134 were not covered by tests
}

// Set the bond denom in the EVM genesis state
evmStateBz, ok := rawState[evmtypes.ModuleName]
if !ok {
panic("evm genesis state is missing")

Check warning on line 140 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L138-L140

Added lines #L138 - L140 were not covered by tests
Expand Down Expand Up @@ -207,8 +208,6 @@ func AppStateRandomizedFn(
GenTimestamp: genesisTimestamp,

Check warning on line 208 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L200-L208

Added lines #L200 - L208 were not covered by tests
}

fmt.Println("Generating genesis states...")

simManager.GenerateGenesisStates(simState)

Check warning on line 211 in tests/simulation/sim/sim_state.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_state.go#L211

Added line #L211 was not covered by tests

appState, err := json.Marshal(genesisState)
Expand Down
111 changes: 59 additions & 52 deletions tests/simulation/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ func init() {
const (
SimAppChainID = "simulation_777-1"
SimBlockMaxGas = 815000000
TestAppChainID = "zetachain_777-1"
SimDBBackend = "goleveldb"
SimDBName = "simulation"
)

// NewSimApp disable feemarket on native tx, otherwise the cosmos-sdk simulation tests will fail.
func NewSimApp(logger log.Logger, db dbm.DB, appOptions servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp)) (*zetaapp.App, error) {

encCdc := zetaapp.MakeEncodingConfig()
app := zetaapp.New(

// Set load latest version to false as we manually set it later.
zetaApp := zetaapp.New(
logger,
db,
nil,
Expand All @@ -58,29 +60,32 @@ func NewSimApp(logger log.Logger, db dbm.DB, appOptions servertypes.AppOptions,
appOptions,
baseAppOptions...,
)

// Set power reduction to 1 to make sure all bonded validators are added to the validator set
sdk.DefaultPowerReduction = sdk.OneInt()
// disable feemarket on native tx

// use zeta antehandler
options := ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
EvmKeeper: app.EvmKeeper,
FeeMarketKeeper: app.FeeMarketKeeper,
AccountKeeper: zetaApp.AccountKeeper,
BankKeeper: zetaApp.BankKeeper,
EvmKeeper: zetaApp.EvmKeeper,
FeeMarketKeeper: zetaApp.FeeMarketKeeper,
SignModeHandler: encCdc.TxConfig.SignModeHandler(),
SigGasConsumer: evmante.DefaultSigVerificationGasConsumer,
MaxTxGasWanted: 0,
ObserverKeeper: app.ObserverKeeper,
ObserverKeeper: zetaApp.ObserverKeeper,
}

anteHandler, err := ante.NewAnteHandler(options)
if err != nil {
panic(err)
}

app.SetAnteHandler(anteHandler)
if err := app.LoadLatestVersion(); err != nil {
zetaApp.SetAnteHandler(anteHandler)
if err := zetaApp.LoadLatestVersion(); err != nil {
return nil, err
}
return app, nil
return zetaApp, nil
}

// interBlockCacheOpt returns a BaseApp option function that sets the persistent
Expand All @@ -89,20 +94,24 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager())
}

// TODO: Make another test for the fuzzer itself, which just has noOp txs
// and doesn't depend on the application.
// TestAppStateDeterminism runs a full application simulation , and produces multiple blocks as per the config
// It checks the determinism of the application by comparing the apphash at the end of each run to other runs
// The following test certifies that , for the same set of operations ( irrespective of what the operations are ) ,
// we would reach the same final state
func TestAppStateDeterminism(t *testing.T) {
if !simutils.FlagEnabledValue {
t.Skip("skipping application simulation")
}

config := simutils.NewConfigFromFlags()

config.InitialBlockHeight = 1
config.ExportParamsPath = ""
config.OnOperation = false
config.AllInvariants = false
config.ChainID = SimAppChainID
config.DBBackend = "goleveldb"
config.DBBackend = SimDBBackend
config.BlockMaxGas = SimBlockMaxGas

numSeeds := 3
numTimesToRunPerSeed := 5
Expand All @@ -112,59 +121,51 @@ func TestAppStateDeterminism(t *testing.T) {
numSeeds = 1
}

// For the same seed, the app hash produced at the end of each run should be the same
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)

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

fmt.Println("Running tests for numSeeds: ", numSeeds, " numTimesToRunPerSeed: ", numTimesToRunPerSeed)

for i := 0; i < numSeeds; i++ {
if config.Seed == cosmossimcli.DefaultSeedValue {
config.Seed = rand.Int63()
}

fmt.Println("config.Seed: ", config.Seed)

//dbPrefix := fmt.Sprintf("%s-%d", SimDBBackend, i)
// For the same seed, the app hash produced at the end of each run should be the same
for j := 0; j < numTimesToRunPerSeed; j++ {
var logger log.Logger
if simutils.FlagVerboseValue {
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
} else {
logger = log.NewNopLogger()
}

db, dir, logger, skip, err := cosmossimutils.SetupSimulation(config, "level-db", "Simulation", simutils.FlagVerboseValue, simutils.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation")
}
db, dir, logger, _, err := cosmossimutils.SetupSimulation(config, SimDBBackend, SimDBName, simutils.FlagVerboseValue, simutils.FlagEnabledValue)
require.NoError(t, err)
appOptions[flags.FlagHome] = dir

app, err := NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
simApp, err := NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))

fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
)

blockedAddresses := app.ModuleAccountAddrs()
blockedAddresses := simApp.ModuleAccountAddrs()

_, _, err = simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
simutils.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
cosmossim.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
cosmossimutils.SimulationOperations(app, app.AppCodec(), config),
simApp.BaseApp,
simutils.AppStateFn(simApp.AppCodec(), simApp.SimulationManager(), simApp.ModuleBasics.DefaultGenesis(simApp.AppCodec())),
cosmossim.RandomAccounts,
cosmossimutils.SimulationOperations(simApp, simApp.AppCodec(), config),
blockedAddresses,
config,
app.AppCodec(),
simApp.AppCodec(),
)
require.NoError(t, err)

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

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

if j != 0 {
Expand All @@ -177,14 +178,17 @@ func TestAppStateDeterminism(t *testing.T) {
}
}

// TestFullAppSimulation runs a full app simulation with the provided configuration.
// 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.ChainID = SimAppChainID
config.BlockMaxGas = SimBlockMaxGas
config.DBBackend = "goleveldb"
//config.ExportStatePath = "/Users/tanmay/.zetacored/simulation_state_export.json"
config.DBBackend = SimDBBackend

db, dir, logger, skip, err := cosmossimutils.SetupSimulation(config, "level-db", "Simulation", simutils.FlagVerboseValue, simutils.FlagEnabledValue)
db, dir, logger, skip, err := cosmossimutils.SetupSimulation(config, SimDBBackend, SimDBName, simutils.FlagVerboseValue, simutils.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation")
}
Expand All @@ -196,29 +200,32 @@ func TestFullAppSimulation(t *testing.T) {
}()
appOptions := make(cosmossimutils.AppOptionsMap, 0)
appOptions[server.FlagInvCheckPeriod] = simutils.FlagPeriodValue
appOptions[flags.FlagHome] = dir

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

blockedAddresses := app.ModuleAccountAddrs()
blockedAddresses := simApp.ModuleAccountAddrs()
_, _, simerr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
simutils.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
simApp.BaseApp,
simutils.AppStateFn(simApp.AppCodec(), simApp.SimulationManager(), simApp.ModuleBasics.DefaultGenesis(simApp.AppCodec())),
cosmossim.RandomAccounts,
cosmossimutils.SimulationOperations(app, app.AppCodec(), config),
cosmossimutils.SimulationOperations(simApp, simApp.AppCodec(), config),
blockedAddresses,
config,
app.AppCodec(),
simApp.AppCodec(),
)
require.NoError(t, simerr)

// check export works as expected
_, err = app.ExportAppStateAndValidators(false, nil, nil)
exported, err := simApp.ExportAppStateAndValidators(false, nil, nil)
require.NoError(t, err)

if config.Commit {
cosmossimutils.PrintStats(db)
if config.ExportStatePath != "" {
err := os.WriteFile(config.ExportStatePath, exported.AppState, 0o600)
require.NoError(t, err)
}

cosmossimutils.PrintStats(db)
}
25 changes: 0 additions & 25 deletions testutil/simapp/default_constants.go

This file was deleted.

Loading

0 comments on commit 913aa19

Please sign in to comment.