Skip to content

Commit 96722c1

Browse files
authored
refactor!: sync with latest simulation changes + make it extensible (#4463)
* refactor: sync with latest simulation changes + make it extensible * cl * fix
1 parent 5fdbb28 commit 96722c1

File tree

6 files changed

+31
-45
lines changed

6 files changed

+31
-45
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- [#4317](https://github.com/ignite/cli/pull/4317) Remove xchisel dependency
4848
- [#4361](https://github.com/ignite/cli/pull/4361) Remove unused `KeyPrefix` method
4949
- [#4384](https://github.com/ignite/cli/pull/4384) Compare genesis params into chain genesis tests
50+
- [#4463](https://github.com/ignite/cli/pull/4463) Run `chain simulation` with any simulation test case
5051

5152
### Fixes
5253

ignite/cmd/chain_simulate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ const (
2222
flagSimappNumBlocks = "numBlocks"
2323
flagSimappBlockSize = "blockSize"
2424
flagSimappLean = "lean"
25-
flagSimappPeriod = "period"
2625
flagSimappGenesisTime = "genesisTime"
26+
flagSimName = "simName"
2727
)
2828

2929
// NewChainSimulate creates a new simulation command to run the blockchain simulation.
3030
func NewChainSimulate() *cobra.Command {
3131
c := &cobra.Command{
3232
Use: "simulate",
3333
Short: "Run simulation testing for the blockchain",
34-
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",
34+
Long: "Run simulation testing for the blockchain. It sends many randomized-input messages of each module to a simulated node.",
3535
Args: cobra.NoArgs,
3636
RunE: chainSimulationHandler,
3737
}
@@ -41,8 +41,8 @@ func NewChainSimulate() *cobra.Command {
4141

4242
func chainSimulationHandler(cmd *cobra.Command, _ []string) error {
4343
var (
44-
period, _ = cmd.Flags().GetUint(flagSimappPeriod)
4544
genesisTime, _ = cmd.Flags().GetInt64(flagSimappGenesisTime)
45+
simName, _ = cmd.Flags().GetString(flagSimName)
4646
config = newConfigFromFlags(cmd)
4747
appPath = flagGetPath(cmd)
4848
)
@@ -62,7 +62,7 @@ func chainSimulationHandler(cmd *cobra.Command, _ []string) error {
6262
}
6363

6464
return c.Simulate(cmd.Context(),
65-
chain.SimappWithPeriod(period),
65+
chain.SimappWithSimulationTestName(simName),
6666
chain.SimappWithGenesisTime(genesisTime),
6767
chain.SimappWithConfig(config),
6868
)
@@ -114,6 +114,6 @@ func simappFlags(c *cobra.Command) {
114114
c.Flags().Bool(flagSimappLean, false, "lean simulation log output")
115115

116116
// simulation flags
117-
c.Flags().Uint(flagSimappPeriod, 0, "run slow invariants only once every period assertions")
117+
c.Flags().String(flagSimName, "TestFullAppSimulation", "name of the simulation to run")
118118
c.Flags().Int64(flagSimappGenesisTime, 0, "override genesis UNIX time instead of using a random UNIX time")
119119
}

ignite/pkg/chaincmd/runner/simulate.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212
// Simulation run the chain simulation.
1313
func (r Runner) Simulation(
1414
ctx context.Context,
15-
appPath string,
15+
appPath, simName string,
1616
enabled bool,
1717
config simulation.Config,
18-
period uint,
1918
genesisTime int64,
2019
) error {
2120
return r.run(ctx, runOptions{stdout: os.Stdout},
2221
chaincmd.SimulationCommand(
2322
appPath,
23+
simName,
2424
chaincmd.SimappWithGenesis(config.GenesisFile),
2525
chaincmd.SimappWithParams(config.ParamsFile),
2626
chaincmd.SimappWithExportParamsPath(config.ExportParamsPath),
@@ -34,7 +34,6 @@ func (r Runner) Simulation(
3434
chaincmd.SimappWithLean(config.Lean),
3535
chaincmd.SimappWithCommit(config.Commit),
3636
chaincmd.SimappWithEnable(enabled),
37-
chaincmd.SimappWithPeriod(period),
3837
chaincmd.SimappWithGenesisTime(genesisTime),
3938
))
4039
}

ignite/pkg/chaincmd/simulate.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package chaincmd
22

33
import (
4+
"fmt"
45
"path/filepath"
56
"strconv"
67

78
"github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step"
89
"github.com/ignite/cli/v29/ignite/pkg/gocmd"
9-
"github.com/ignite/cli/v29/ignite/pkg/safeconverter"
1010
)
1111

1212
const (
@@ -23,13 +23,11 @@ const (
2323
optionSimappLean = "-Lean"
2424
optionSimappCommit = "-Commit"
2525
optionSimappEnabled = "-Enabled"
26-
optionSimappPeriod = "-Period"
2726
optionSimappGenesisTime = "-GenesisTime"
2827

29-
commandGoTest = "test"
30-
optionGoBenchmem = "-benchmem"
31-
optionGoSimappRun = "-run=^TestFullAppSimulation$"
32-
optionGoSimsTags = "-tags='sims'"
28+
commandGoTest = "test"
29+
optionGoBenchmem = "-benchmem"
30+
optionGoSimsTags = "-tags='sims'"
3331
)
3432

3533
// SimappOption for the SimulateCommand.
@@ -157,13 +155,6 @@ func SimappWithEnable(enable bool) SimappOption {
157155
}
158156
}
159157

160-
// SimappWithPeriod provides period option for the simapp command.
161-
func SimappWithPeriod(period uint) SimappOption {
162-
return func(command []string) []string {
163-
return append(command, optionSimappPeriod, strconv.Itoa(safeconverter.ToInt[uint](period)))
164-
}
165-
}
166-
167158
// SimappWithGenesisTime provides genesisTime option for the simapp command.
168159
func SimappWithGenesisTime(genesisTime int64) SimappOption {
169160
return func(command []string) []string {
@@ -172,11 +163,16 @@ func SimappWithGenesisTime(genesisTime int64) SimappOption {
172163
}
173164

174165
// SimulationCommand returns the cli command for simapp tests.
175-
func SimulationCommand(appPath string, options ...SimappOption) step.Option {
166+
// simName must be a test defined within the application (defaults to TestFullAppSimulation).
167+
func SimulationCommand(appPath string, simName string, options ...SimappOption) step.Option {
168+
if simName == "" {
169+
simName = "TestFullAppSimulation"
170+
}
171+
176172
command := []string{
177173
commandGoTest,
178174
optionGoBenchmem,
179-
optionGoSimappRun,
175+
fmt.Sprintf("-run=^%s$", simName),
180176
optionGoSimsTags,
181177
filepath.Join(appPath, "app"),
182178
}

ignite/services/chain/simulate.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
)
88

99
type simappOptions struct {
10-
enabled bool
11-
config simulation.Config
12-
period uint
13-
genesisTime int64
10+
simulationTestName string
11+
enabled bool
12+
config simulation.Config
13+
genesisTime int64
1414
}
1515

1616
func newSimappOptions() simappOptions {
@@ -19,21 +19,13 @@ func newSimappOptions() simappOptions {
1919
Commit: true,
2020
},
2121
enabled: true,
22-
period: 0,
2322
genesisTime: 0,
2423
}
2524
}
2625

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

30-
// SimappWithPeriod allows running slow invariants only once every period assertions.
31-
func SimappWithPeriod(period uint) SimappOption {
32-
return func(c *simappOptions) {
33-
c.period = period
34-
}
35-
}
36-
3729
// SimappWithGenesisTime allows overriding genesis UNIX time instead of using a random UNIX time.
3830
func SimappWithGenesisTime(genesisTime int64) SimappOption {
3931
return func(c *simappOptions) {
@@ -48,6 +40,13 @@ func SimappWithConfig(config simulation.Config) SimappOption {
4840
}
4941
}
5042

43+
// SimappWithSimulationTestName allows to set the simulation test name.
44+
func SimappWithSimulationTestName(name string) SimappOption {
45+
return func(c *simappOptions) {
46+
c.simulationTestName = name
47+
}
48+
}
49+
5150
func (c *Chain) Simulate(ctx context.Context, options ...SimappOption) error {
5251
simappOptions := newSimappOptions()
5352

@@ -62,9 +61,9 @@ func (c *Chain) Simulate(ctx context.Context, options ...SimappOption) error {
6261
}
6362
return commands.Simulation(ctx,
6463
c.app.Path,
64+
simappOptions.simulationTestName,
6565
simappOptions.enabled,
6666
simappOptions.config,
67-
simappOptions.period,
6867
simappOptions.genesisTime,
6968
)
7069
}

ignite/templates/app/files/cmd/{{binaryNamePrefix}}d/cmd/commands.go.plush

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,6 @@ func appExport(
137137
appOpts servertypes.AppOptions,
138138
modulesToExport []string,
139139
) (servertypes.ExportedApp, error) {
140-
viperAppOpts, ok := appOpts.(*viper.Viper)
141-
if !ok {
142-
return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper")
143-
}
144-
145-
// overwrite the FlagInvCheckPeriod
146-
viperAppOpts.Set(server.FlagInvCheckPeriod, 1)
147-
appOpts = viperAppOpts
148-
149140
var bApp *app.App
150141
if height != -1 {
151142
bApp = app.New(logger, db, traceStore, false, appOpts)

0 commit comments

Comments
 (0)