Skip to content

Commit

Permalink
remove unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Oct 1, 2024
1 parent ac8fddf commit b55f439
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 90 deletions.
64 changes: 1 addition & 63 deletions tests/simulation/sim/sim_utils.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,14 @@
package sim

import (
"encoding/json"
"fmt"
"os"

dbm "github.com/cometbft/cometbft-db"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"

zetaapp "github.com/zeta-chain/node/app"
)

// SimulationOperations retrieves the simulation params from the provided file path
// and returns all the modules weighted operations
func SimulationOperations(app *zetaapp.App, cdc codec.JSONCodec, config simtypes.Config) []simtypes.WeightedOperation {
simState := module.SimulationState{
AppParams: make(simtypes.AppParams),
Cdc: cdc,
}

if config.ParamsFile != "" {
bz, err := os.ReadFile(config.ParamsFile)
if err != nil {
panic(err)
}

err = json.Unmarshal(bz, &simState.AppParams)
if err != nil {
panic(err)
}
}

simState.LegacyProposalContents = app.SimulationManager().GetProposalContents(simState) //nolint:staticcheck
simState.ProposalMsgs = app.SimulationManager().GetProposalMsgs(simState)
return app.SimulationManager().WeightedOperations(simState)
}

// CheckExportSimulation exports the app state and simulation parameters to JSON
// if the export paths are defined.
func CheckExportSimulation(app runtime.AppI, config simtypes.Config, params simtypes.Params) error {
if config.ExportStatePath != "" {
fmt.Println("exporting app state...")
exported, err := app.ExportAppStateAndValidators(false, nil, nil)
if err != nil {
return err
}

if err := os.WriteFile(config.ExportStatePath, []byte(exported.AppState), 0o600); err != nil {
return err
}
}

if config.ExportParamsPath != "" {
fmt.Println("exporting simulation params...")
paramsBz, err := json.MarshalIndent(params, "", " ")
if err != nil {
return err
}

if err := os.WriteFile(config.ExportParamsPath, paramsBz, 0o600); err != nil {
return err
}
}
return nil
}

// PrintStats prints the corresponding statistics from the app DB.
func PrintStats(db dbm.DB) {
fmt.Println("\nLevelDB Stats")
fmt.Println("\nDB Stats")
fmt.Println(db.Stats()["leveldb.stats"])
fmt.Println("LevelDB cached block size", db.Stats()["leveldb.cachedblock"])

Check warning on line 13 in tests/simulation/sim/sim_utils.go

View check run for this annotation

Codecov / codecov/patch

tests/simulation/sim/sim_utils.go#L10-L13

Added lines #L10 - L13 were not covered by tests
}
57 changes: 30 additions & 27 deletions tests/simulation/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +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"
simutils "github.com/zeta-chain/node/tests/simulation/sim"

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

Expand All @@ -24,16 +24,16 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
simulation2 "github.com/cosmos/cosmos-sdk/types/simulation"
cosmossimutils "github.com/cosmos/cosmos-sdk/testutil/sims"
cosmossim "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
cosmossimcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
)

// AppChainID hardcoded chainID for simulation

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

const (
Expand Down Expand Up @@ -92,46 +92,49 @@ 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 !sim2.FlagEnabledValue {
if !simutils.FlagEnabledValue {
t.Skip("skipping application simulation")
}

config := sim2.NewConfigFromFlags()
config := simutils.NewConfigFromFlags()
config.InitialBlockHeight = 1
config.ExportParamsPath = ""
config.OnOperation = false
config.AllInvariants = false
config.ChainID = SimAppChainID
config.DBBackend = "goleveldb"

numSeeds := 3
numTimesToRunPerSeed := 5

// We will be overriding the random seed and just run a single simulation on the provided seed value
if config.Seed != simcli.DefaultSeedValue {
if config.Seed != cosmossimcli.DefaultSeedValue {
numSeeds = 1
}

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

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

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

for j := 0; j < numTimesToRunPerSeed; j++ {
var logger log.Logger
if sim2.FlagVerboseValue {
if simutils.FlagVerboseValue {
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
} else {
logger = log.NewNopLogger()
}

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

Expand All @@ -148,17 +151,17 @@ func TestAppStateDeterminism(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
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),
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),
blockedAddresses,
config,
app.AppCodec(),
)
require.NoError(t, err)

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

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

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

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

app, err := NewSimApp(logger, db, appOptions, interBlockCacheOpt(), baseapp.SetChainID(SimAppChainID))
require.NoError(t, err)
Expand All @@ -202,9 +205,9 @@ func TestFullAppSimulation(t *testing.T) {
t,
os.Stdout,
app.BaseApp,
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),
simutils.AppStateFn(app.AppCodec(), app.SimulationManager(), app.ModuleBasics.DefaultGenesis(app.AppCodec())),
cosmossim.RandomAccounts,
cosmossimutils.SimulationOperations(app, app.AppCodec(), config),
blockedAddresses,
config,
app.AppCodec(),
Expand All @@ -216,6 +219,6 @@ func TestFullAppSimulation(t *testing.T) {
require.NoError(t, err)

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

0 comments on commit b55f439

Please sign in to comment.