Skip to content

Commit

Permalink
feat: add extend cb to avoid unmarshal appState twice for sim test (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Mar 9, 2023
1 parent 818f6a0 commit 665a2ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (cli) [#14953](https://github.com/cosmos/cosmos-sdk/pull/14953) Enable profiling block replay during abci handshake with `--cpu-profile`.
* (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.
* (store) [#14189](https://github.com/cosmos/cosmos-sdk/pull/14189) Add config `iavl-lazy-loading` to enable lazy loading of iavl store, to improve start up time of archive nodes, add method `SetLazyLoading` to `CommitMultiStore` interface.
* (simapp) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState.

### API Breaking Changes

Expand Down
19 changes: 19 additions & 0 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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,
Expand Down Expand Up @@ -138,6 +152,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 {
Expand Down
5 changes: 5 additions & 0 deletions types/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,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

Expand Down

0 comments on commit 665a2ab

Please sign in to comment.