From 112713d8af4f7607f77eb9a1f645824235092c82 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 10 Mar 2023 23:20:05 +0800 Subject: [PATCH 1/3] allow pass genesisState when call with extend --- CHANGELOG.md | 4 +++- simapp/state.go | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0d3d0831022..a0f0af93031a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,9 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog ## [Unreleased] -* (simapp) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState. + +### Improvements +* (simstestutil) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState and extra genesisState argument which is the genesis state of the app. ## [v0.46.11](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.11) - 2022-03-03 diff --git a/simapp/state.go b/simapp/state.go index 21e71470b24e..308e81f769d8 100644 --- a/simapp/state.go +++ b/simapp/state.go @@ -27,14 +27,21 @@ import ( // 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. func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simtypes.AppStateFn { - return AppStateFnWithExtendedCb(cdc, simManager, nil) + genesisState := NewDefaultGenesisState(cdc) + 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 genesis state of the app. // cb is the callback function to extend rawState. -func AppStateFnWithExtendedCb(cdc codec.JSONCodec, simManager *module.SimulationManager, cb func(rawState map[string]json.RawMessage)) simtypes.AppStateFn { +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, config simtypes.Config, ) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) { if FlagGenesisTimeValue == 0 { @@ -72,11 +79,11 @@ func AppStateFnWithExtendedCb(cdc codec.JSONCodec, simManager *module.Simulation if err != nil { panic(err) } - appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams) + appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) default: appParams := make(simtypes.AppParams) - appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams) + appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) } rawState := make(map[string]json.RawMessage) @@ -154,9 +161,9 @@ func AppStateFnWithExtendedCb(cdc codec.JSONCodec, simManager *module.Simulation func AppStateRandomizedFn( simManager *module.SimulationManager, r *rand.Rand, cdc codec.JSONCodec, accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams, + genesisState map[string]json.RawMessage, ) (json.RawMessage, []simtypes.Account) { numAccs := int64(len(accs)) - genesisState := NewDefaultGenesisState(cdc) // generate a random amount of initial stake coins and a random initial // number of bonded accounts From 7cdf2305c41260362a342f8e67d7c6c8508df80a Mon Sep 17 00:00:00 2001 From: mmsqe Date: Sat, 11 Mar 2023 00:33:25 +0800 Subject: [PATCH 2/3] add AppStateRandomizedFnWithState to keep interface no change for AppStateRandomizedFn --- CHANGELOG.md | 2 +- simapp/state.go | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0f0af93031a..96f0e0c451bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] ### Improvements -* (simstestutil) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState and extra genesisState argument which is the genesis state of the app. +* (simapp) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState and `AppStateRandomizedFnWithState` with extra genesisState argument which is the genesis state of the app. ## [v0.46.11](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.11) - 2022-03-03 diff --git a/simapp/state.go b/simapp/state.go index 308e81f769d8..a88b7e3ff8e9 100644 --- a/simapp/state.go +++ b/simapp/state.go @@ -79,11 +79,11 @@ func AppStateFnWithExtendedCb( if err != nil { panic(err) } - appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) + appState, simAccs = AppStateRandomizedFnWithState(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) default: appParams := make(simtypes.AppParams) - appState, simAccs = AppStateRandomizedFn(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) + appState, simAccs = AppStateRandomizedFnWithState(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) } rawState := make(map[string]json.RawMessage) @@ -161,6 +161,17 @@ func AppStateFnWithExtendedCb( func AppStateRandomizedFn( simManager *module.SimulationManager, r *rand.Rand, cdc codec.JSONCodec, accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams, +) (json.RawMessage, []simtypes.Account) { + genesisState := NewDefaultGenesisState(cdc) + return AppStateRandomizedFnWithState(simManager, r, cdc, accs, genesisTimestamp, appParams, genesisState) +} + +// AppStateRandomizedFnWithState creates calls each module's GenesisState generator function +// and creates the simulation params +// genesisState is the genesis state of the app. +func AppStateRandomizedFnWithState( + simManager *module.SimulationManager, r *rand.Rand, cdc codec.JSONCodec, + accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams, genesisState map[string]json.RawMessage, ) (json.RawMessage, []simtypes.Account) { numAccs := int64(len(accs)) From 9c89683d1906e624d9bcfe9095b79a97e98efd2a Mon Sep 17 00:00:00 2001 From: mmsqe Date: Sat, 11 Mar 2023 00:45:21 +0800 Subject: [PATCH 3/3] update comment --- simapp/state.go | 1 + 1 file changed, 1 insertion(+) diff --git a/simapp/state.go b/simapp/state.go index a88b7e3ff8e9..836891cda88d 100644 --- a/simapp/state.go +++ b/simapp/state.go @@ -169,6 +169,7 @@ func AppStateRandomizedFn( // AppStateRandomizedFnWithState creates calls each module's GenesisState generator function // and creates the simulation params // genesisState is the genesis state of the app. +// This function will not exist in v0.47, but be replaced by AppStateRandomizedFn with an extra genesisState argument. func AppStateRandomizedFnWithState( simManager *module.SimulationManager, r *rand.Rand, cdc codec.JSONCodec, accs []simtypes.Account, genesisTimestamp time.Time, appParams simtypes.AppParams,