1- package simapp
1+ package sims
22
33import (
44 "encoding/json"
@@ -8,12 +8,10 @@ import (
88 "os"
99 "time"
1010
11+ "cosmossdk.io/math"
1112 cmtjson "github.com/cometbft/cometbft/libs/json"
1213 cmttypes "github.com/cometbft/cometbft/types"
1314
14- "cosmossdk.io/math"
15- simappparams "cosmossdk.io/simapp/params"
16-
1715 "github.com/cosmos/cosmos-sdk/codec"
1816 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
1917 sdk "github.com/cosmos/cosmos-sdk/types"
@@ -25,11 +23,21 @@ import (
2523 stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
2624)
2725
26+ // Simulation parameter constants
27+ const (
28+ StakePerAccount = "stake_per_account"
29+ InitiallyBondedValidators = "initially_bonded_validators"
30+ )
31+
2832// AppStateFn returns the initial application state using a genesis or the simulation parameters.
2933// It panics if the user provides files for both of them.
3034// If a file is not given for the genesis or the sim params, it creates a randomized one.
31- func AppStateFn (cdc codec.JSONCodec , simManager * module.SimulationManager ) simtypes.AppStateFn {
32- return func (r * rand.Rand , accs []simtypes.Account , config simtypes.Config ,
35+ // genesisState is the default genesis state of the whole app.
36+ func AppStateFn (cdc codec.JSONCodec , simManager * module.SimulationManager , genesisState map [string ]json.RawMessage ) simtypes.AppStateFn {
37+ return func (
38+ r * rand.Rand ,
39+ accs []simtypes.Account ,
40+ config simtypes.Config ,
3341 ) (appState json.RawMessage , simAccs []simtypes.Account , chainID string , genesisTimestamp time.Time ) {
3442 if simcli .FlagGenesisTimeValue == 0 {
3543 genesisTimestamp = simtypes .RandTimestamp (r )
@@ -44,7 +52,10 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
4452
4553 case config .GenesisFile != "" :
4654 // override the default chain-id from simapp to set it later to the config
47- genesisDoc , accounts := AppStateFromGenesisFileFn (r , cdc , config .GenesisFile )
55+ genesisDoc , accounts , err := AppStateFromGenesisFileFn (r , cdc , config .GenesisFile )
56+ if err != nil {
57+ panic (err )
58+ }
4859
4960 if simcli .FlagGenesisTimeValue == 0 {
5061 // use genesis timestamp if no custom timestamp is provided (i.e no random timestamp)
@@ -66,11 +77,11 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
6677 if err != nil {
6778 panic (err )
6879 }
69- appState , simAccs = AppStateRandomizedFn (simManager , r , cdc , accs , genesisTimestamp , appParams )
80+ appState , simAccs = AppStateRandomizedFn (simManager , r , cdc , accs , genesisTimestamp , appParams , genesisState )
7081
7182 default :
7283 appParams := make (simtypes.AppParams )
73- appState , simAccs = AppStateRandomizedFn (simManager , r , cdc , accs , genesisTimestamp , appParams )
84+ appState , simAccs = AppStateRandomizedFn (simManager , r , cdc , accs , genesisTimestamp , appParams , genesisState )
7485 }
7586
7687 rawState := make (map [string ]json.RawMessage )
@@ -85,8 +96,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
8596 }
8697
8798 stakingState := new (stakingtypes.GenesisState )
88- err = cdc .UnmarshalJSON (stakingStateBz , stakingState )
89- if err != nil {
99+ if err = cdc .UnmarshalJSON (stakingStateBz , stakingState ); err != nil {
90100 panic (err )
91101 }
92102 // compute not bonded balance
@@ -105,8 +115,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
105115 panic ("bank genesis state is missing" )
106116 }
107117 bankState := new (banktypes.GenesisState )
108- err = cdc .UnmarshalJSON (bankStateBz , bankState )
109- if err != nil {
118+ if err = cdc .UnmarshalJSON (bankStateBz , bankState ); err != nil {
110119 panic (err )
111120 }
112121
@@ -141,24 +150,27 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
141150// AppStateRandomizedFn creates calls each module's GenesisState generator function
142151// and creates the simulation params
143152func AppStateRandomizedFn (
144- simManager * module.SimulationManager , r * rand.Rand , cdc codec.JSONCodec ,
145- accs []simtypes.Account , genesisTimestamp time.Time , appParams simtypes.AppParams ,
153+ simManager * module.SimulationManager ,
154+ r * rand.Rand ,
155+ cdc codec.JSONCodec ,
156+ accs []simtypes.Account ,
157+ genesisTimestamp time.Time ,
158+ appParams simtypes.AppParams ,
159+ genesisState map [string ]json.RawMessage ,
146160) (json.RawMessage , []simtypes.Account ) {
147161 numAccs := int64 (len (accs ))
148- genesisState := ModuleBasics .DefaultGenesis (cdc )
149-
150162 // generate a random amount of initial stake coins and a random initial
151163 // number of bonded accounts
152164 var (
153165 numInitiallyBonded int64
154166 initialStake math.Int
155167 )
156168 appParams .GetOrGenerate (
157- cdc , simappparams . StakePerAccount , & initialStake , r ,
169+ cdc , StakePerAccount , & initialStake , r ,
158170 func (r * rand.Rand ) { initialStake = math .NewInt (r .Int63n (1e12 )) },
159171 )
160172 appParams .GetOrGenerate (
161- cdc , simappparams . InitiallyBondedValidators , & numInitiallyBonded , r ,
173+ cdc , InitiallyBondedValidators , & numInitiallyBonded , r ,
162174 func (r * rand.Rand ) { numInitiallyBonded = int64 (r .Intn (300 )) },
163175 )
164176
@@ -199,23 +211,21 @@ func AppStateRandomizedFn(
199211
200212// AppStateFromGenesisFileFn util function to generate the genesis AppState
201213// from a genesis.json file.
202- func AppStateFromGenesisFileFn (r io.Reader , cdc codec.JSONCodec , genesisFile string ) (cmttypes.GenesisDoc , []simtypes.Account ) {
214+ func AppStateFromGenesisFileFn (r io.Reader , cdc codec.JSONCodec , genesisFile string ) (cmttypes.GenesisDoc , []simtypes.Account , error ) {
203215 bytes , err := os .ReadFile (genesisFile )
204216 if err != nil {
205217 panic (err )
206218 }
207219
208220 var genesis cmttypes.GenesisDoc
209221 // NOTE: CometBFT uses a custom JSON decoder for GenesisDoc
210- err = cmtjson .Unmarshal (bytes , & genesis )
211- if err != nil {
212- panic (err )
222+ if err = cmtjson .Unmarshal (bytes , & genesis ); err != nil {
223+ return genesis , nil , err
213224 }
214225
215- var appState GenesisState
216- err = json .Unmarshal (genesis .AppState , & appState )
217- if err != nil {
218- panic (err )
226+ var appState map [string ]json.RawMessage
227+ if err = json .Unmarshal (genesis .AppState , & appState ); err != nil {
228+ return genesis , nil , err
219229 }
220230
221231 var authGenesis authtypes.GenesisState
@@ -235,15 +245,15 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str
235245
236246 privKey := secp256k1 .GenPrivKeyFromSecret (privkeySeed )
237247
238- a , ok := acc .GetCachedValue ().(authtypes .AccountI )
248+ a , ok := acc .GetCachedValue ().(sdk .AccountI )
239249 if ! ok {
240- panic ("expected account" )
250+ return genesis , nil , fmt . Errorf ("expected account" )
241251 }
242252
243253 // create simulator accounts
244254 simAcc := simtypes.Account {PrivKey : privKey , PubKey : privKey .PubKey (), Address : a .GetAddress ()}
245255 newAccs [i ] = simAcc
246256 }
247257
248- return genesis , newAccs
258+ return genesis , newAccs , nil
249259}
0 commit comments