Skip to content

Commit

Permalink
refactor: Simplify SimulationManager setup (backport cosmos#12153) (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored and JeancarloBarrios committed Sep 28, 2024
1 parent bd5a1fc commit 0bfeadb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
11 changes: 2 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ It is strongly recommended to upgrade to these releases as well.

### Improvements

* (deps) Migrate to [CometBFT](https://github.com/cometbft/cometbft). Follow the instructions in the [release notes](./RELEASE_NOTES.md).
* (deps) [#15127](https://github.com/cosmos/cosmos-sdk/pull/15127) Bump btcd.
* (store) [#14410](https://github.com/cosmos/cosmos-sdk/pull/14410) `rootmulti.Store.loadVersion` has validation to check if all the module stores' height is correct, it will error if any module store has incorrect height.

## [v0.45.14](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.14) - 2023-02-16

### Features

* (cli) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Add the `tendermint key-migrate` to perform Tendermint v0.35 DB key migration.
* [#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

Expand Down
32 changes: 32 additions & 0 deletions types/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ func NewSimulationManagerFromAppModules(modules map[string]AppModule, overrideMo
return NewSimulationManager(simModules...)
}

// 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 {
Expand Down

0 comments on commit 0bfeadb

Please sign in to comment.