diff --git a/CHANGELOG.md b/CHANGELOG.md index 7559c61ffda5..193dea1b302b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#14406](https://github.com/cosmos/cosmos-sdk/issues/14406) Migrate usage of types/store.go to store/types/.. * (x/staking) [#14590](https://github.com/cosmos/cosmos-sdk/pull/14590) Return undelegate amount in MsgUndelegateResponse * (baseapp) [#15023](https://github.com/cosmos/cosmos-sdk/pull/15023) & [#15213](https://github.com/cosmos/cosmos-sdk/pull/15213) Add `MessageRouter` interface to baseapp and pass it to authz, gov and groups instead of concrete type. +* (simapp) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState. ### State Machine Breaking diff --git a/testutil/sims/state_helpers.go b/testutil/sims/state_helpers.go index 7143ebfbb017..7b97cb6160cf 100644 --- a/testutil/sims/state_helpers.go +++ b/testutil/sims/state_helpers.go @@ -33,6 +33,20 @@ const ( // If a file is not given for the genesis or the sim params, it creates a randomized one. // genesisState is the default genesis state of the whole app. func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager, genesisState map[string]json.RawMessage) simtypes.AppStateFn { + return AppStateFnWithExtendedCb(cdc, simManager, genesisState, nil) +} + +// AppStateFnWithExtendedCb returns the initial application state using a genesis or the simulation parameters. +// It panics if the user provides files for both of them. +// If a file is not given for the genesis or the sim params, it creates a randomized one. +// genesisState is the default genesis state of the whole app. +// cb is the callback function to extend rawState. +func AppStateFnWithExtendedCb( + cdc codec.JSONCodec, + simManager *module.SimulationManager, + genesisState map[string]json.RawMessage, + cb func(rawState map[string]json.RawMessage), +) simtypes.AppStateFn { return func( r *rand.Rand, accs []simtypes.Account, @@ -137,6 +151,11 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager, genes rawState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingState) rawState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState) + // extend state from callback function + if cb != nil { + cb(rawState) + } + // replace appstate appState, err = json.Marshal(rawState) if err != nil { diff --git a/types/simulation/types.go b/types/simulation/types.go index 34d29d04e323..56d496e03b0e 100644 --- a/types/simulation/types.go +++ b/types/simulation/types.go @@ -177,6 +177,11 @@ type AppStateFn func(r *rand.Rand, accs []Account, config Config) ( appState json.RawMessage, accounts []Account, chainId string, genesisTimestamp time.Time, ) +// AppStateFnWithExtendedCb returns the app state json bytes and the genesis accounts +type AppStateFnWithExtendedCb func(r *rand.Rand, accs []Account, config Config) ( + appState json.RawMessage, accounts []Account, chainId string, genesisTimestamp time.Time, +) + // RandomAccountFn returns a slice of n random simulation accounts type RandomAccountFn func(r *rand.Rand, n int) []Account