Skip to content

Commit 544afb6

Browse files
authored
refactor: Simplify SimulationManager setup (#12153)
1 parent fc02389 commit 544afb6

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
4444
### Improvements
4545

4646
* [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default.
47+
* [#12153](https://github.com/cosmos/cosmos-sdk/pull/12153) Add a new `NewSimulationManagerFromAppModules` constructor, to simplify simulation wiring.
4748

4849
### API Breaking Changes
4950

simapp/app.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -373,21 +373,10 @@ func NewSimApp(
373373
//
374374
// NOTE: this is not required apps that don't use the simulator for fuzz testing
375375
// transactions
376-
app.sm = module.NewSimulationManager(
377-
auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
378-
bank.NewAppModule(app.appCodec, app.BankKeeper, app.AccountKeeper),
379-
feegrantmodule.NewAppModule(app.appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
380-
gov.NewAppModule(app.appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
381-
mint.NewAppModule(app.appCodec, app.MintKeeper, app.AccountKeeper, nil),
382-
staking.NewAppModule(app.appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
383-
distr.NewAppModule(app.appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
384-
slashing.NewAppModule(app.appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
385-
params.NewAppModule(app.ParamsKeeper),
386-
evidence.NewAppModule(app.EvidenceKeeper),
387-
authzmodule.NewAppModule(app.appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
388-
groupmodule.NewAppModule(app.appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
389-
nftmodule.NewAppModule(app.appCodec, app.NFTKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
390-
)
376+
overrideModules := map[string]module.AppModuleSimulation{
377+
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
378+
}
379+
app.sm = module.NewSimulationManagerFromAppModules(app.ModuleManager.Modules, overrideModules)
391380

392381
app.sm.RegisterStoreDecoders()
393382

types/module/simulation.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package module
33
import (
44
"encoding/json"
55
"math/rand"
6+
"sort"
67
"time"
78

89
sdkmath "cosmossdk.io/math"
@@ -47,6 +48,38 @@ func NewSimulationManager(modules ...AppModuleSimulation) *SimulationManager {
4748
}
4849
}
4950

51+
// NewSimulationManagerFromAppModules creates a new SimulationManager object.
52+
//
53+
// First it sets any SimulationModule provided by overrideModules, and ignores any AppModule
54+
// with the same moduleName.
55+
// Then it attempts to cast every provided AppModule into an AppModuleSimulation.
56+
// If the cast succeeds, its included, otherwise it is excluded.
57+
func NewSimulationManagerFromAppModules(modules map[string]AppModule, overrideModules map[string]AppModuleSimulation) *SimulationManager {
58+
simModules := []AppModuleSimulation{}
59+
appModuleNamesSorted := make([]string, 0, len(modules))
60+
for moduleName := range modules {
61+
appModuleNamesSorted = append(appModuleNamesSorted, moduleName)
62+
}
63+
64+
sort.Strings(appModuleNamesSorted)
65+
66+
for _, moduleName := range appModuleNamesSorted {
67+
// for every module, see if we override it. If so, use override.
68+
// Else, if we can cast the app module into a simulation module add it.
69+
// otherwise no simulation module.
70+
if simModule, ok := overrideModules[moduleName]; ok {
71+
simModules = append(simModules, simModule)
72+
} else {
73+
appModule := modules[moduleName]
74+
if simModule, ok := appModule.(AppModuleSimulation); ok {
75+
simModules = append(simModules, simModule)
76+
}
77+
// cannot cast, so we continue
78+
}
79+
}
80+
return NewSimulationManager(simModules...)
81+
}
82+
5083
// GetProposalContents returns each module's proposal content generator function
5184
// with their default operation weight and key.
5285
func (sm *SimulationManager) GetProposalContents(simState SimulationState) []simulation.WeightedProposalContent {

0 commit comments

Comments
 (0)