Skip to content

Commit

Permalink
feat: simulation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch committed Nov 22, 2024
1 parent 06ae845 commit 89950ec
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
8 changes: 7 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type App struct {
mm *module.Manager
// module configurator
configurator module.Configurator
// simulation manager
sm *module.SimulationManager
}

// New returns a reference to an initialized blockchain app
Expand Down Expand Up @@ -195,6 +197,10 @@ func New(
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(app.configurator)

/**** Simulations ****/
app.sm = module.NewSimulationManager(app.SetupSimulationModules(appCodec)...)
app.sm.RegisterStoreDecoders()

// initialize stores
app.MountKVStores(keepers.KVStoreKeys)
app.MountTransientStores(app.GetTransientStoreKey())
Expand Down Expand Up @@ -358,7 +364,7 @@ func RegisterSwaggerAPI(_ client.Context, rtr *mux.Router) {

// SimulationManager implements the SimulationApp interface
func (app *App) SimulationManager() *module.SimulationManager {
return nil
return app.sm
}

// GetTxConfig implements ibctesting.TestingApp
Expand Down
10 changes: 10 additions & 0 deletions app/keepers/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ func (a *AppKeepers) SetupModules(
}
}

func (a *AppKeepers) SetupSimulationModules(appCodec codec.Codec) []module.AppModuleSimulation {
return []module.AppModuleSimulation{
rollappmodule.NewAppModule(appCodec, a.RollappKeeper, a.AccountKeeper, a.BankKeeper),
sequencermodule.NewAppModule(appCodec, a.SequencerKeeper, a.AccountKeeper, a.BankKeeper),
eibcmodule.NewAppModule(appCodec, a.EIBCKeeper, a.AccountKeeper, a.BankKeeper),
dymnsmodule.NewAppModule(appCodec, a.DymNSKeeper),
//sponsorship.NewAppModule(a.SponsorshipKeeper),
}
}

// ModuleAccountAddrs returns all the app's module account addresses.
func (*AppKeepers) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
Expand Down
4 changes: 3 additions & 1 deletion ibctesting/genesis_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

"github.com/dymensionxyz/dymension/v3/testutil/sample"
irotypes "github.com/dymensionxyz/dymension/v3/x/iro/types"
"github.com/dymensionxyz/dymension/v3/x/rollapp/genesisbridge"
rollapptypes "github.com/dymensionxyz/dymension/v3/x/rollapp/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"

"github.com/dymensionxyz/dymension/v3/app/apptesting"
appparams "github.com/dymensionxyz/dymension/v3/app/params"
"github.com/stretchr/testify/suite"

"github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand Down
94 changes: 87 additions & 7 deletions simulation/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,97 @@ import (
"os"
"testing"

simapp "github.com/cosmos/cosmos-sdk/testutil/sims"
"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"
simulationtypes "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/dymensionxyz/dymension/v3/app"
"github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/testing/simapp"
"github.com/stretchr/testify/require"

"github.com/dymensionxyz/dymension/v3/app"
)

func init() {
simcli.GetSimulatorFlags()
}

const SimulationAppChainID = "dymension_100-1"

/*
To execute a completely pseudo-random simulation:
$ go test . \
-run=TestFullAppSimulation \
-Enabled=true \
-NumBlocks=100 \
-BlockSize=200 \
-Commit=true \
-Seed=99 \
-Period=5 \
-v -timeout 24h
*/

func TestFullAppSimulation(t *testing.T) {
config := simcli.NewConfigFromFlags()
config.ChainID = SimulationAppChainID

db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "leveldb-app-sim", "Simulation", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")

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

appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = app.DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

encoding := app.MakeEncodingConfig()

dymdApp := app.New(
logger,
db,
nil,
true,
map[int64]bool{},
app.DefaultNodeHome,
0,
encoding,
simapp.EmptyAppOptions{},
baseapp.SetChainID(SimulationAppChainID),
)
require.Equal(t, "dymension", dymdApp.Name())

// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
dymdApp.BaseApp,
simtestutil.AppStateFn(dymdApp.AppCodec(), dymdApp.SimulationManager(), app.NewDefaultGenesisState(dymdApp.AppCodec())),
simulationtypes.RandomAccounts,
simtestutil.SimulationOperations(dymdApp, dymdApp.AppCodec(), config),
dymdApp.ModuleAccountAddrs(),
config,
dymdApp.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = simtestutil.CheckExportSimulation(dymdApp, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)

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

// BenchmarkSimulation run the chain simulation
// Running using starport command:
// `starport chain simulate -v --numBlocks 200 --blockSize 50`
Expand All @@ -27,7 +106,7 @@ func BenchmarkSimulation(b *testing.B) {
config := simcli.NewConfigFromFlags()
config.ChainID = "simulation-app"

db, dir, logger, _, err := simapp.SetupSimulation(config, "goleveldb-app-sim", "Simulation", simcli.FlagEnabledValue, simcli.FlagEnabledValue)
db, dir, logger, _, err := simtestutil.SetupSimulation(config, "goleveldb-app-sim", "Simulation", simcli.FlagEnabledValue, simcli.FlagEnabledValue)
require.NoError(b, err, "simulation setup failed")

b.Cleanup(func() {
Expand All @@ -49,27 +128,28 @@ func BenchmarkSimulation(b *testing.B) {
0,
encoding,
simapp.EmptyAppOptions{},
baseapp.SetChainID(SimulationAppChainID),
)

// Run randomized simulations
_, simParams, simErr := simulation.SimulateFromSeed(
b,
os.Stdout,
dymdApp.BaseApp,
simapp.AppStateFn(dymdApp.AppCodec(), dymdApp.SimulationManager(), app.NewDefaultGenesisState(dymdApp.AppCodec())),
simtestutil.AppStateFn(dymdApp.AppCodec(), dymdApp.SimulationManager(), app.NewDefaultGenesisState(dymdApp.AppCodec())),
simulationtypes.RandomAccounts,
simapp.SimulationOperations(dymdApp, dymdApp.AppCodec(), config),
simtestutil.SimulationOperations(dymdApp, dymdApp.AppCodec(), config),
dymdApp.ModuleAccountAddrs(),
config,
dymdApp.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = simapp.CheckExportSimulation(dymdApp, config, simParams)
err = simtestutil.CheckExportSimulation(dymdApp, config, simParams)
require.NoError(b, err)
require.NoError(b, simErr)

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

0 comments on commit 89950ec

Please sign in to comment.