1
- package simapp
1
+ package sims
2
2
3
3
import (
4
4
"encoding/json"
@@ -8,12 +8,10 @@ import (
8
8
"os"
9
9
"time"
10
10
11
+ "cosmossdk.io/math"
11
12
cmtjson "github.com/cometbft/cometbft/libs/json"
12
13
cmttypes "github.com/cometbft/cometbft/types"
13
14
14
- "cosmossdk.io/math"
15
- simappparams "cosmossdk.io/simapp/params"
16
-
17
15
"github.com/cosmos/cosmos-sdk/codec"
18
16
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
19
17
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -25,11 +23,21 @@ import (
25
23
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
26
24
)
27
25
26
+ // Simulation parameter constants
27
+ const (
28
+ StakePerAccount = "stake_per_account"
29
+ InitiallyBondedValidators = "initially_bonded_validators"
30
+ )
31
+
28
32
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
29
33
// It panics if the user provides files for both of them.
30
34
// 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 ,
33
41
) (appState json.RawMessage , simAccs []simtypes.Account , chainID string , genesisTimestamp time.Time ) {
34
42
if simcli .FlagGenesisTimeValue == 0 {
35
43
genesisTimestamp = simtypes .RandTimestamp (r )
@@ -44,7 +52,10 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
44
52
45
53
case config .GenesisFile != "" :
46
54
// 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
+ }
48
59
49
60
if simcli .FlagGenesisTimeValue == 0 {
50
61
// 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
66
77
if err != nil {
67
78
panic (err )
68
79
}
69
- appState , simAccs = AppStateRandomizedFn (simManager , r , cdc , accs , genesisTimestamp , appParams )
80
+ appState , simAccs = AppStateRandomizedFn (simManager , r , cdc , accs , genesisTimestamp , appParams , genesisState )
70
81
71
82
default :
72
83
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 )
74
85
}
75
86
76
87
rawState := make (map [string ]json.RawMessage )
@@ -85,8 +96,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
85
96
}
86
97
87
98
stakingState := new (stakingtypes.GenesisState )
88
- err = cdc .UnmarshalJSON (stakingStateBz , stakingState )
89
- if err != nil {
99
+ if err = cdc .UnmarshalJSON (stakingStateBz , stakingState ); err != nil {
90
100
panic (err )
91
101
}
92
102
// compute not bonded balance
@@ -105,8 +115,7 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
105
115
panic ("bank genesis state is missing" )
106
116
}
107
117
bankState := new (banktypes.GenesisState )
108
- err = cdc .UnmarshalJSON (bankStateBz , bankState )
109
- if err != nil {
118
+ if err = cdc .UnmarshalJSON (bankStateBz , bankState ); err != nil {
110
119
panic (err )
111
120
}
112
121
@@ -141,24 +150,27 @@ func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager) simty
141
150
// AppStateRandomizedFn creates calls each module's GenesisState generator function
142
151
// and creates the simulation params
143
152
func 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 ,
146
160
) (json.RawMessage , []simtypes.Account ) {
147
161
numAccs := int64 (len (accs ))
148
- genesisState := ModuleBasics .DefaultGenesis (cdc )
149
-
150
162
// generate a random amount of initial stake coins and a random initial
151
163
// number of bonded accounts
152
164
var (
153
165
numInitiallyBonded int64
154
166
initialStake math.Int
155
167
)
156
168
appParams .GetOrGenerate (
157
- cdc , simappparams . StakePerAccount , & initialStake , r ,
169
+ cdc , StakePerAccount , & initialStake , r ,
158
170
func (r * rand.Rand ) { initialStake = math .NewInt (r .Int63n (1e12 )) },
159
171
)
160
172
appParams .GetOrGenerate (
161
- cdc , simappparams . InitiallyBondedValidators , & numInitiallyBonded , r ,
173
+ cdc , InitiallyBondedValidators , & numInitiallyBonded , r ,
162
174
func (r * rand.Rand ) { numInitiallyBonded = int64 (r .Intn (300 )) },
163
175
)
164
176
@@ -199,23 +211,21 @@ func AppStateRandomizedFn(
199
211
200
212
// AppStateFromGenesisFileFn util function to generate the genesis AppState
201
213
// 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 ) {
203
215
bytes , err := os .ReadFile (genesisFile )
204
216
if err != nil {
205
217
panic (err )
206
218
}
207
219
208
220
var genesis cmttypes.GenesisDoc
209
221
// 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
213
224
}
214
225
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
219
229
}
220
230
221
231
var authGenesis authtypes.GenesisState
@@ -235,15 +245,15 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str
235
245
236
246
privKey := secp256k1 .GenPrivKeyFromSecret (privkeySeed )
237
247
238
- a , ok := acc .GetCachedValue ().(authtypes .AccountI )
248
+ a , ok := acc .GetCachedValue ().(sdk .AccountI )
239
249
if ! ok {
240
- panic ("expected account" )
250
+ return genesis , nil , fmt . Errorf ("expected account" )
241
251
}
242
252
243
253
// create simulator accounts
244
254
simAcc := simtypes.Account {PrivKey : privKey , PubKey : privKey .PubKey (), Address : a .GetAddress ()}
245
255
newAccs [i ] = simAcc
246
256
}
247
257
248
- return genesis , newAccs
258
+ return genesis , newAccs , nil
249
259
}
0 commit comments