Skip to content

Commit

Permalink
Merge pull request #10 from 0xPolygon/raneet10/pos-1950
Browse files Browse the repository at this point in the history
Initialise app simulation tests
  • Loading branch information
Raneet10 authored Mar 15, 2024
2 parents 1c9589d + 02c35b1 commit 9e3df79
Show file tree
Hide file tree
Showing 4 changed files with 498 additions and 3 deletions.
8 changes: 8 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/x/auth"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
"github.com/cosmos/cosmos-sdk/x/bank"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/consensus"
Expand Down Expand Up @@ -366,6 +367,13 @@ func NewHeimdallApp(

testdata.RegisterQueryServer(app.GRPCQueryRouter(), testdata.QueryImpl{})

overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, nil),
}
app.simulationManager = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules)

app.simulationManager.RegisterStoreDecoders()

// initialize stores
app.MountKVStores(keys)
app.MountTransientStores(tkeys)
Expand Down
93 changes: 93 additions & 0 deletions app/sim_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package app

import (
"os"
"testing"

flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"

"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"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
)

var FlagEnableBenchStreamingValue bool

// Get flags every time the simulator is run
func init() {
flag.BoolVar(&FlagEnableBenchStreamingValue, "EnableStreaming", false, "Enable streaming service")
}

// TODO HV2: test this once https://github.com/0xPolygon/cosmos-sdk/pull/3 is merged
// see https://github.com/0xPolygon/heimdall-v2/pull/10#discussion_r1482779246

// Profile with:
// /usr/local/go/bin/go test -benchmem -run=^$ github.com/0xPolygon/heimdall-v2/app -bench ^BenchmarkFullAppSimulation$ -Commit=true -cpuprofile cpu.out
func BenchmarkFullAppSimulation(b *testing.B) {
b.ReportAllocs()

config := simcli.NewConfigFromFlags()
config.ChainID = HeimdallAppChainID

db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "goleveldb-app-heimdall", "Simulation", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
if err != nil {
b.Fatalf("simulation setup failed: %s", err.Error())
}

if skip {
b.Skip("skipping benchmark application simulation")
}

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

appOptions := viper.New()
if FlagEnableStreamingValue {
m := make(map[string]interface{})
m["streaming.abci.keys"] = []string{"*"}
m["streaming.abci.plugin"] = "abci_v1" //nolint:goconst
m["streaming.abci.stop-node-on-err"] = true
for key, value := range m {
appOptions.SetDefault(key, value)
}
}
appOptions.SetDefault(flags.FlagHome, DefaultNodeHome)
appOptions.SetDefault(server.FlagInvCheckPeriod, simcli.FlagPeriodValue)

app := NewHeimdallApp(logger, db, nil, true, appOptions, interBlockCacheOpt(), baseapp.SetChainID(HeimdallAppChainID))

// run randomized simulation

_, simParams, simErr := simulation.SimulateFromSeed(
b,
os.Stdout,
app.BaseApp,
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.BlockedModuleAccountAddrs(app.ModuleAccountAddrs()),
config,
app.AppCodec(),
)

// export state and simParams before the simulation error is checked
if err = simtestutil.CheckExportSimulation(app, config, simParams); err != nil {
b.Fatal(err)
}

if simErr != nil {
b.Fatal(simErr)
}

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

0 comments on commit 9e3df79

Please sign in to comment.