Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testutil/sims): add back AppStateFnWithExtendedCbs #23236

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ func AppStateFn(
addressCodec, validatorCodec address.Codec,
modules []module.AppModuleSimulation,
genesisState map[string]json.RawMessage,
) simtypes.AppStateFn {
return AppStateFnWithExtendedCbs(cdc, addressCodec, validatorCodec, modules, genesisState, nil, nil)
}

// AppStateFnWithExtendedCbs 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.
// moduleStateCb is the callback function to access moduleState.
// postRawStateCb is the callback function to extend rawState.
func AppStateFnWithExtendedCbs(
cdc codec.JSONCodec,
addressCodec, validatorCodec address.Codec,
modules []module.AppModuleSimulation,
genesisState map[string]json.RawMessage,
moduleStateCb func(moduleName string, genesisState interface{}),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't both moduleStateCb and rawStateCb all for the same functionality? What is the purpose of including both of them?

The semantics of this function become confused because it is unclear to callers that first the module state callback is executed and then second the raw state callback is executed, meaning things could be overwritten

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is use case of accessing specific module genesisState first, then mutate the raw state accordingly. May be we can do a better naming of the post callback to prevent confusion.

postRawStateCb func(rawState map[string]json.RawMessage),
) simtypes.AppStateFn {
return func(
r *rand.Rand,
Expand Down Expand Up @@ -143,9 +160,17 @@ func AppStateFn(
stakingtypes.ModuleName: stakingState,
testutil.BankModuleName: bankState,
} {
if moduleStateCb != nil {
moduleStateCb(name, state)
}
rawState[name] = cdc.MustMarshalJSON(state)
}

// extend state from callback function
if postRawStateCb != nil {
postRawStateCb(rawState)
}

// replace appstate
appState, err = json.Marshal(rawState)
if err != nil {
Expand Down
Loading