From d58f032828006d5dc1c80ac6b2caff041f8ee5ee Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 21 Apr 2023 09:23:21 +0800 Subject: [PATCH 1/5] add moduleStateCb to access module state --- testutil/sims/state_helpers.go | 40 +++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/testutil/sims/state_helpers.go b/testutil/sims/state_helpers.go index 7b97cb6160cf..3d5dcfc4d8bf 100644 --- a/testutil/sims/state_helpers.go +++ b/testutil/sims/state_helpers.go @@ -8,6 +8,8 @@ import ( "os" "time" + "github.com/gogo/protobuf/proto" + "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" @@ -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, @@ -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) + } // extend state from callback function - if cb != nil { - cb(rawState) + if rawStateCb != nil { + rawStateCb(rawState) } // replace appstate From ade55cd87a549832b3ee8bb846227a16cb53a068 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 21 Apr 2023 09:23:09 +0800 Subject: [PATCH 2/5] rm unused type --- types/simulation/types.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/types/simulation/types.go b/types/simulation/types.go index 56d496e03b0e..34d29d04e323 100644 --- a/types/simulation/types.go +++ b/types/simulation/types.go @@ -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 -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 From 4e002dacf153c15c79538e8c816809669a0ed44b Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 21 Apr 2023 09:26:21 +0800 Subject: [PATCH 3/5] add change doc --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 660866500e77..6da404818570 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (runtime) [#15547](https://github.com/cosmos/cosmos-sdk/pull/15547) Allow runtime to pass event core api service to modules * (telemetry) [#15657](https://github.com/cosmos/cosmos-sdk/pull/15657) Emit more data (go version, sdk version, upgrade height) in prom metrics * (modulemanager) [#15829](https://github.com/cosmos/cosmos-sdk/pull/15829) add new endblocker interface to handle valset updates +* (simtestutil) [#15903](https://github.com/cosmos/cosmos-sdk/pull/15903) Add `AppStateFnWithExtendedCbs` with moduleStateCb callback function to allow access moduleState. ### Improvements From faf5d37b02e0386e8538d76eeccb3a68eb30182e Mon Sep 17 00:00:00 2001 From: mmsqe Date: Sat, 22 Apr 2023 00:25:01 +0800 Subject: [PATCH 4/5] fix doc --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6da404818570..7fc52f5029f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (runtime) [#15547](https://github.com/cosmos/cosmos-sdk/pull/15547) Allow runtime to pass event core api service to modules * (telemetry) [#15657](https://github.com/cosmos/cosmos-sdk/pull/15657) Emit more data (go version, sdk version, upgrade height) in prom metrics * (modulemanager) [#15829](https://github.com/cosmos/cosmos-sdk/pull/15829) add new endblocker interface to handle valset updates -* (simtestutil) [#15903](https://github.com/cosmos/cosmos-sdk/pull/15903) Add `AppStateFnWithExtendedCbs` with moduleStateCb callback function to allow access moduleState. ### Improvements @@ -100,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 From 61778359a63dc7436e0e33fda529934fa2f8b664 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Sat, 22 Apr 2023 09:03:08 +0800 Subject: [PATCH 5/5] Update testutil/sims/state_helpers.go Co-authored-by: Julien Robert --- testutil/sims/state_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testutil/sims/state_helpers.go b/testutil/sims/state_helpers.go index 3d5dcfc4d8bf..73319ecaba48 100644 --- a/testutil/sims/state_helpers.go +++ b/testutil/sims/state_helpers.go @@ -8,7 +8,7 @@ import ( "os" "time" - "github.com/gogo/protobuf/proto" + "github.com/cosmos/gogoproto/proto" "cosmossdk.io/math"