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: add moduleStateCb to allow access moduleState in sim test #15903

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (simtestutil) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState.
* (x/consensus) [#15553](https://github.com/cosmos/cosmos-sdk/pull/15553) Migrate consensus module to use collections
* (x/auth) [#15867](https://github.com/cosmos/cosmos-sdk/pull/15867) Support better logging for signature verification failure.
* (simtestutil) [#15903](https://github.com/cosmos/cosmos-sdk/pull/15903) Add `AppStateFnWithExtendedCbs` with moduleStateCb callback function to allow access moduleState.

### State Machine Breaking

Expand Down
40 changes: 30 additions & 10 deletions testutil/sims/state_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"time"

"github.com/cosmos/gogoproto/proto"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -29,23 +31,34 @@ const (
)

// AppStateFn 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.
// It calls AppStateFnWithExtendedCb with nil rawStateCb.
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 calls AppStateFnWithExtendedCbs with nil moduleStateCb.
func AppStateFnWithExtendedCb(
cdc codec.JSONCodec,
simManager *module.SimulationManager,
genesisState map[string]json.RawMessage,
rawStateCb func(rawState map[string]json.RawMessage),
) simtypes.AppStateFn {
return AppStateFnWithExtendedCbs(cdc, simManager, genesisState, nil, rawStateCb)
}

// 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.
// cb is the callback function to extend rawState.
func AppStateFnWithExtendedCb(
// moduleStateCb is the callback function to access moduleState.
// rawStateCb is the callback function to extend rawState.
func AppStateFnWithExtendedCbs(
cdc codec.JSONCodec,
simManager *module.SimulationManager,
genesisState map[string]json.RawMessage,
cb func(rawState map[string]json.RawMessage),
moduleStateCb func(moduleName string, genesisState interface{}),
rawStateCb func(rawState map[string]json.RawMessage),
) simtypes.AppStateFn {
return func(
r *rand.Rand,
Expand Down Expand Up @@ -148,12 +161,19 @@ func AppStateFnWithExtendedCb(
}

// change appState back
rawState[stakingtypes.ModuleName] = cdc.MustMarshalJSON(stakingState)
rawState[banktypes.ModuleName] = cdc.MustMarshalJSON(bankState)
for name, state := range map[string]proto.Message{
stakingtypes.ModuleName: stakingState,
banktypes.ModuleName: bankState,
} {
if moduleStateCb != nil {
moduleStateCb(name, state)
}
rawState[name] = cdc.MustMarshalJSON(state)
}
Comment on lines +164 to +172

Check warning

Code scanning / CodeQL

Iteration over map

Iteration over map may be a possible source of non-determinism

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

// replace appstate
Expand Down
5 changes: 0 additions & 5 deletions types/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,6 @@ 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
Copy link
Member

@julienrbrt julienrbrt Apr 22, 2023

Choose a reason for hiding this comment

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

ACK because identical to AppStateFn and unused.
However maybe as Bez said it is worth a changelog.

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