From 84c2d2474f615875010422af74f03cef4b22b9ba Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Mon, 6 Jun 2022 08:17:48 -0500 Subject: [PATCH 1/2] refactor: Simplify SimulationManager setup (#12153) (cherry picked from commit 544afb65e2d008fe2509f5be955d72170078f2f1) # Conflicts: # simapp/app.go --- CHANGELOG.md | 1 + simapp/app.go | 7 +++++++ types/module/simulation.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14b304144d1e..10766264b438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Improvements * [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Mark the `TipDecorator` as beta, don't include it in simapp by default. +* [#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 2333aab6537a..7e77e9fd3f9e 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -416,6 +416,7 @@ func NewSimApp( // // NOTE: this is not required apps that don't use the simulator for fuzz testing // transactions +<<<<<<< HEAD app.sm = module.NewSimulationManager( auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts), bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper), @@ -432,6 +433,12 @@ func NewSimApp( groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), nftmodule.NewAppModule(appCodec, app.NFTKeeper, 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.ModuleManager.Modules, overrideModules) +>>>>>>> 544afb65e (refactor: Simplify SimulationManager setup (#12153)) app.sm.RegisterStoreDecoders() diff --git a/types/module/simulation.go b/types/module/simulation.go index 591e11e0846a..8cba9093452e 100644 --- a/types/module/simulation.go +++ b/types/module/simulation.go @@ -3,6 +3,7 @@ package module import ( "encoding/json" "math/rand" + "sort" "time" sdkmath "cosmossdk.io/math" @@ -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 { From c427432ec5ba275a55ce4cfdc62d0f1177fdc8b2 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 6 Jun 2022 19:30:56 +0200 Subject: [PATCH 2/2] fix conflict --- simapp/app.go | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/simapp/app.go b/simapp/app.go index 7e77e9fd3f9e..dc3b62ed452c 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -416,29 +416,10 @@ func NewSimApp( // // NOTE: this is not required apps that don't use the simulator for fuzz testing // transactions -<<<<<<< HEAD - 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, nil), - 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), - groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), - nftmodule.NewAppModule(appCodec, app.NFTKeeper, 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.ModuleManager.Modules, overrideModules) ->>>>>>> 544afb65e (refactor: Simplify SimulationManager setup (#12153)) + app.sm = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules) app.sm.RegisterStoreDecoders()