Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: sync with latest simulation changes + make it extensible #4463

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
- [#4317](https://github.com/ignite/cli/pull/4317) Remove xchisel dependency
- [#4361](https://github.com/ignite/cli/pull/4361) Remove unused `KeyPrefix` method
- [#4384](https://github.com/ignite/cli/pull/4384) Compare genesis params into chain genesis tests
- [#4463](https://github.com/ignite/cli/pull/4463) Run `chain simulation` with any simulation test case

### Fixes

Expand Down
10 changes: 5 additions & 5 deletions ignite/cmd/chain_simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ const (
flagSimappNumBlocks = "numBlocks"
flagSimappBlockSize = "blockSize"
flagSimappLean = "lean"
flagSimappPeriod = "period"
flagSimappGenesisTime = "genesisTime"
flagSimName = "simName"
)

// NewChainSimulate creates a new simulation command to run the blockchain simulation.
func NewChainSimulate() *cobra.Command {
c := &cobra.Command{
Use: "simulate",
Short: "Run simulation testing for the blockchain",
Long: "Run simulation testing for the blockchain. It sends many randomized-input messages of each module to a simulated node and checks if invariants break",
Long: "Run simulation testing for the blockchain. It sends many randomized-input messages of each module to a simulated node.",
Args: cobra.NoArgs,
RunE: chainSimulationHandler,
}
Expand All @@ -41,8 +41,8 @@ func NewChainSimulate() *cobra.Command {

func chainSimulationHandler(cmd *cobra.Command, _ []string) error {
var (
period, _ = cmd.Flags().GetUint(flagSimappPeriod)
genesisTime, _ = cmd.Flags().GetInt64(flagSimappGenesisTime)
simName, _ = cmd.Flags().GetString(flagSimName)
config = newConfigFromFlags(cmd)
appPath = flagGetPath(cmd)
)
Expand All @@ -62,7 +62,7 @@ func chainSimulationHandler(cmd *cobra.Command, _ []string) error {
}

return c.Simulate(cmd.Context(),
chain.SimappWithPeriod(period),
chain.SimappWithSimulationTestName(simName),
chain.SimappWithGenesisTime(genesisTime),
chain.SimappWithConfig(config),
)
Expand Down Expand Up @@ -114,6 +114,6 @@ func simappFlags(c *cobra.Command) {
c.Flags().Bool(flagSimappLean, false, "lean simulation log output")

// simulation flags
c.Flags().Uint(flagSimappPeriod, 0, "run slow invariants only once every period assertions")
c.Flags().String(flagSimName, "TestFullAppSimulation", "name of the simulation to run")
c.Flags().Int64(flagSimappGenesisTime, 0, "override genesis UNIX time instead of using a random UNIX time")
}
5 changes: 2 additions & 3 deletions ignite/pkg/chaincmd/runner/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
// Simulation run the chain simulation.
func (r Runner) Simulation(
ctx context.Context,
appPath string,
appPath, simName string,
enabled bool,
config simulation.Config,
period uint,
genesisTime int64,
) error {
return r.run(ctx, runOptions{stdout: os.Stdout},
chaincmd.SimulationCommand(
appPath,
simName,
chaincmd.SimappWithGenesis(config.GenesisFile),
chaincmd.SimappWithParams(config.ParamsFile),
chaincmd.SimappWithExportParamsPath(config.ExportParamsPath),
Expand All @@ -34,7 +34,6 @@ func (r Runner) Simulation(
chaincmd.SimappWithLean(config.Lean),
chaincmd.SimappWithCommit(config.Commit),
chaincmd.SimappWithEnable(enabled),
chaincmd.SimappWithPeriod(period),
chaincmd.SimappWithGenesisTime(genesisTime),
))
}
26 changes: 11 additions & 15 deletions ignite/pkg/chaincmd/simulate.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package chaincmd

import (
"fmt"
"path/filepath"
"strconv"

"github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step"
"github.com/ignite/cli/v29/ignite/pkg/gocmd"
"github.com/ignite/cli/v29/ignite/pkg/safeconverter"
)

const (
Expand All @@ -23,13 +23,11 @@ const (
optionSimappLean = "-Lean"
optionSimappCommit = "-Commit"
optionSimappEnabled = "-Enabled"
optionSimappPeriod = "-Period"
optionSimappGenesisTime = "-GenesisTime"

commandGoTest = "test"
optionGoBenchmem = "-benchmem"
optionGoSimappRun = "-run=^TestFullAppSimulation$"
optionGoSimsTags = "-tags='sims'"
commandGoTest = "test"
optionGoBenchmem = "-benchmem"
optionGoSimsTags = "-tags='sims'"
)

// SimappOption for the SimulateCommand.
Expand Down Expand Up @@ -157,13 +155,6 @@ func SimappWithEnable(enable bool) SimappOption {
}
}

// SimappWithPeriod provides period option for the simapp command.
func SimappWithPeriod(period uint) SimappOption {
return func(command []string) []string {
return append(command, optionSimappPeriod, strconv.Itoa(safeconverter.ToInt[uint](period)))
}
}

// SimappWithGenesisTime provides genesisTime option for the simapp command.
func SimappWithGenesisTime(genesisTime int64) SimappOption {
return func(command []string) []string {
Expand All @@ -172,11 +163,16 @@ func SimappWithGenesisTime(genesisTime int64) SimappOption {
}

// SimulationCommand returns the cli command for simapp tests.
func SimulationCommand(appPath string, options ...SimappOption) step.Option {
// simName must be a test defined within the application (defaults to TestFullAppSimulation).
func SimulationCommand(appPath string, simName string, options ...SimappOption) step.Option {
if simName == "" {
simName = "TestFullAppSimulation"
}

command := []string{
commandGoTest,
optionGoBenchmem,
optionGoSimappRun,
fmt.Sprintf("-run=^%s$", simName),
optionGoSimsTags,
filepath.Join(appPath, "app"),
}
Expand Down
25 changes: 12 additions & 13 deletions ignite/services/chain/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

type simappOptions struct {
enabled bool
config simulation.Config
period uint
genesisTime int64
simulationTestName string
enabled bool
config simulation.Config
genesisTime int64
}

func newSimappOptions() simappOptions {
Expand All @@ -19,21 +19,13 @@ func newSimappOptions() simappOptions {
Commit: true,
},
enabled: true,
period: 0,
genesisTime: 0,
}
}

// SimappOption provides options for the simapp command.
type SimappOption func(*simappOptions)

// SimappWithPeriod allows running slow invariants only once every period assertions.
func SimappWithPeriod(period uint) SimappOption {
return func(c *simappOptions) {
c.period = period
}
}

// SimappWithGenesisTime allows overriding genesis UNIX time instead of using a random UNIX time.
func SimappWithGenesisTime(genesisTime int64) SimappOption {
return func(c *simappOptions) {
Expand All @@ -48,6 +40,13 @@ func SimappWithConfig(config simulation.Config) SimappOption {
}
}

// SimappWithSimulationTestName allows to set the simulation test name.
func SimappWithSimulationTestName(name string) SimappOption {
return func(c *simappOptions) {
c.simulationTestName = name
}
}

func (c *Chain) Simulate(ctx context.Context, options ...SimappOption) error {
simappOptions := newSimappOptions()

Expand All @@ -62,9 +61,9 @@ func (c *Chain) Simulate(ctx context.Context, options ...SimappOption) error {
}
return commands.Simulation(ctx,
c.app.Path,
simappOptions.simulationTestName,
simappOptions.enabled,
simappOptions.config,
simappOptions.period,
simappOptions.genesisTime,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,6 @@ func appExport(
appOpts servertypes.AppOptions,
modulesToExport []string,
) (servertypes.ExportedApp, error) {
viperAppOpts, ok := appOpts.(*viper.Viper)
if !ok {
return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper")
}

// overwrite the FlagInvCheckPeriod
viperAppOpts.Set(server.FlagInvCheckPeriod, 1)
appOpts = viperAppOpts

var bApp *app.App
if height != -1 {
bApp = app.New(logger, db, traceStore, false, appOpts)
Expand Down
Loading