From 07b14c4d2d0aed5d8a8ec1feffe8f7cc8a047b64 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 7 Jun 2022 13:40:39 +0200 Subject: [PATCH] refactor: Simplify SimulationManager setup (backport #12153) (#12159) * refactor: Simplify SimulationManager setup (#12153) (cherry picked from commit 544afb65e2d008fe2509f5be955d72170078f2f1) # Conflicts: # CHANGELOG.md # simapp/app.go * fix conflict * fix conflict Co-authored-by: Dev Ojha Co-authored-by: Julien Robert --- CHANGELOG.md | 1 + simapp/app.go | 18 ++++-------------- types/module/simulation.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1264cada19a..4a61874b43a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/feegrant) [\#11813](https://github.com/cosmos/cosmos-sdk/pull/11813) Fix pagination total count in `AllowancesByGranter` query. * (errors) [\#12002](https://github.com/cosmos/cosmos-sdk/pull/12002) Removed 'redacted' error message from defaultErrEncoder. * (ante) [#12017](https://github.com/cosmos/cosmos-sdk/pull/12017) Index ante events for failed tx (backport #12013). +* [#12153](https://github.com/cosmos/cosmos-sdk/pull/12153) Add a new `NewSimulationManagerFromAppModules` constructor, to simplify simulation wiring. ### Bug Fixes diff --git a/simapp/app.go b/simapp/app.go index d30301c14c8d..783774aa04c5 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -373,20 +373,10 @@ func NewSimApp( // // NOTE: this is not required apps that don't use the simulator for fuzz testing // transactions - app.sm = module.NewSimulationManager( - auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts), - bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), - capability.NewAppModule(appCodec, *app.CapabilityKeeper), - feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), - gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper), - mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), - staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper), - distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), - slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper), - params.NewAppModule(app.ParamsKeeper), - evidence.NewAppModule(app.EvidenceKeeper), - authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), - ) + overrideModules := map[string]module.AppModuleSimulation{ + authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts), + } + app.sm = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules) app.sm.RegisterStoreDecoders() diff --git a/types/module/simulation.go b/types/module/simulation.go index 252cf268f837..7edc90cde3c5 100644 --- a/types/module/simulation.go +++ b/types/module/simulation.go @@ -4,6 +4,7 @@ import ( "encoding/json" "math/rand" + "sort" "time" "github.com/cosmos/cosmos-sdk/codec" @@ -47,6 +48,38 @@ func NewSimulationManager(modules ...AppModuleSimulation) *SimulationManager { } } +// NewSimulationManagerFromAppModules creates a new SimulationManager object. +// +// First it sets any SimulationModule provided by overrideModules, and ignores any AppModule +// with the same moduleName. +// Then it attempts to cast every provided AppModule into an AppModuleSimulation. +// If the cast succeeds, its included, otherwise it is excluded. +func NewSimulationManagerFromAppModules(modules map[string]AppModule, overrideModules map[string]AppModuleSimulation) *SimulationManager { + simModules := []AppModuleSimulation{} + appModuleNamesSorted := make([]string, 0, len(modules)) + for moduleName := range modules { + appModuleNamesSorted = append(appModuleNamesSorted, moduleName) + } + + sort.Strings(appModuleNamesSorted) + + for _, moduleName := range appModuleNamesSorted { + // for every module, see if we override it. If so, use override. + // Else, if we can cast the app module into a simulation module add it. + // otherwise no simulation module. + if simModule, ok := overrideModules[moduleName]; ok { + simModules = append(simModules, simModule) + } else { + appModule := modules[moduleName] + if simModule, ok := appModule.(AppModuleSimulation); ok { + simModules = append(simModules, simModule) + } + // cannot cast, so we continue + } + } + return NewSimulationManager(simModules...) +} + // GetProposalContents returns each module's proposal content generator function // with their default operation weight and key. func (sm *SimulationManager) GetProposalContents(simState SimulationState) []simulation.WeightedProposalContent {