1
+ //go:build sims
2
+
1
3
package app_test
2
4
3
5
// TODO: COsmos SDK fix for the simulator issue for custom keys
4
6
import (
5
7
"encoding/json"
6
8
"flag"
9
+ "fmt"
7
10
"io"
8
11
"math/rand"
9
12
"sync"
@@ -21,12 +24,17 @@ import (
21
24
abci "github.com/cometbft/cometbft/abci/types"
22
25
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
23
26
"github.com/cosmos/cosmos-sdk/baseapp"
27
+ "github.com/cosmos/cosmos-sdk/client/flags"
28
+ "github.com/cosmos/cosmos-sdk/runtime"
29
+ "github.com/cosmos/cosmos-sdk/server"
24
30
servertypes "github.com/cosmos/cosmos-sdk/server/types"
25
31
"github.com/cosmos/cosmos-sdk/simsx"
32
+ simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
26
33
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
27
34
"github.com/cosmos/cosmos-sdk/x/simulation"
28
35
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
29
36
"github.com/evmos/ethermint/app"
37
+ "github.com/evmos/ethermint/app/ante"
30
38
"github.com/evmos/ethermint/testutil"
31
39
"github.com/stretchr/testify/assert"
32
40
"github.com/stretchr/testify/require"
@@ -55,25 +63,24 @@ func setupStateFactory(app *app.EthermintApp) simsx.SimStateFactory {
55
63
return simsx.SimStateFactory {
56
64
Codec : app .AppCodec (),
57
65
AppStateFn : testutil .StateFn (app ),
58
- BlockedAddr : app .BlockedAddrs (),
66
+ BlockedAddr : app .BlockedAddresses (),
59
67
AccountSource : app .AuthKeeper ,
60
68
BalanceSource : app .BankKeeper ,
61
69
}
62
70
}
63
71
64
72
func TestFullAppSimulation (t * testing.T ) {
65
- return
66
73
config := simcli .NewConfigFromFlags ()
67
74
config .ChainID = SimAppChainID
68
75
config .BlockMaxGas = SimBlockMaxGas
69
- simsx .RunWithSeed (
76
+ simsx .RunWithSeedAndRandAcc (
70
77
t ,
71
78
config ,
72
- app . NewEthermintApp ,
79
+ NewSimApp ,
73
80
setupStateFactory ,
74
81
config .Seed ,
75
82
config .FuzzSeed ,
76
- simtypes .RandomAccounts ,
83
+ testutil .RandomAccounts ,
77
84
)
78
85
}
79
86
@@ -82,15 +89,54 @@ var (
82
89
exportWithValidatorSet []string
83
90
)
84
91
92
+ func NewSimApp (
93
+ logger log.Logger ,
94
+ db corestore.KVStoreWithBatch ,
95
+ traceStore io.Writer ,
96
+ loadLatest bool ,
97
+ appOpts servertypes.AppOptions ,
98
+ baseAppOptions ... func (* baseapp.BaseApp ),
99
+ ) * app.EthermintApp {
100
+ appOptions := make (simtestutil.AppOptionsMap , 0 )
101
+ appOptions [flags .FlagHome ] = app .DefaultNodeHome
102
+ appOptions [server .FlagInvCheckPeriod ] = simcli .FlagPeriodValue
103
+ app := app .NewEthermintApp (logger , db , nil , false , appOptions , baseAppOptions ... )
104
+ // disable feemarket on native tx
105
+ anteHandler , err := ante .NewAnteHandler (ante.HandlerOptions {
106
+ Environment : runtime .NewEnvironment (
107
+ nil ,
108
+ logger ,
109
+ ), // nil is set as the kvstoreservice to avoid module access
110
+ ConsensusKeeper : app .ConsensusParamsKeeper ,
111
+ AccountKeeper : app .AuthKeeper ,
112
+ AccountAbstractionKeeper : app .AccountsKeeper ,
113
+ BankKeeper : app .BankKeeper ,
114
+ SignModeHandler : app .TxConfig ().SignModeHandler (),
115
+ FeegrantKeeper : app .FeeGrantKeeper ,
116
+ SigGasConsumer : ante .DefaultSigVerificationGasConsumer ,
117
+ EvmKeeper : app .EvmKeeper ,
118
+ FeeMarketKeeper : app .FeeMarketKeeper ,
119
+ MaxTxGasWanted : 0 ,
120
+ })
121
+ if err != nil {
122
+ panic (err )
123
+ }
124
+ app .SetAnteHandler (anteHandler )
125
+ if err := app .LoadLatestVersion (); err != nil {
126
+ panic (err )
127
+ }
128
+ return app
129
+ }
130
+
85
131
func TestAppImportExport (t * testing.T ) {
86
132
return
87
133
config := simcli .NewConfigFromFlags ()
88
134
config .ChainID = SimAppChainID
89
135
config .BlockMaxGas = SimBlockMaxGas
90
- simsx .RunWithSeed (
136
+ simsx .RunWithSeedAndRandAcc (
91
137
t ,
92
138
config ,
93
- app . NewEthermintApp ,
139
+ NewSimApp ,
94
140
setupStateFactory ,
95
141
config .Seed ,
96
142
config .FuzzSeed ,
@@ -102,11 +148,14 @@ func TestAppImportExport(t *testing.T) {
102
148
require .NoError (t , err )
103
149
104
150
t .Log ("importing genesis...\n " )
105
- newTestInstance := simsx .NewSimulationAppInstance (t , ti .Cfg , app . NewEthermintApp )
151
+ newTestInstance := simsx .NewSimulationAppInstance (t , ti .Cfg , NewSimApp )
106
152
newApp := newTestInstance .App
107
153
var genesisState map [string ]json.RawMessage
108
154
require .NoError (t , json .Unmarshal (exported .AppState , & genesisState ))
109
- ctxB := newApp .NewContextLegacy (true , cmtproto.Header {Height : a .LastBlockHeight ()})
155
+ ctxB := newApp .NewContextLegacy (true , cmtproto.Header {
156
+ Height : a .LastBlockHeight (),
157
+ ChainID : config .ChainID ,
158
+ })
110
159
_ , err = newApp .ModuleManager .InitGenesis (ctxB , genesisState )
111
160
if simapp .IsEmptyValidatorSetErr (err ) {
112
161
t .Skip ("Skipping simulation as all validators have been unbonded" )
@@ -135,10 +184,10 @@ func TestAppSimulationAfterImport(t *testing.T) {
135
184
config := simcli .NewConfigFromFlags ()
136
185
config .ChainID = SimAppChainID
137
186
config .BlockMaxGas = SimBlockMaxGas
138
- simsx .RunWithSeed (
187
+ simsx .RunWithSeedAndRandAcc (
139
188
t ,
140
189
config ,
141
- app . NewEthermintApp ,
190
+ NewSimApp ,
142
191
setupStateFactory ,
143
192
config .Seed ,
144
193
config .FuzzSeed ,
@@ -158,7 +207,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
158
207
159
208
_ , err = a .InitChain (& abci.InitChainRequest {
160
209
AppStateBytes : exported .AppState ,
161
- ChainId : simsx . SimAppChainID ,
210
+ ChainId : config . ChainID ,
162
211
InitialHeight : exported .Height ,
163
212
Time : genesisTimestamp ,
164
213
})
@@ -170,18 +219,17 @@ func TestAppSimulationAfterImport(t *testing.T) {
170
219
// use accounts from initial run
171
220
return exported .AppState , accs , config .ChainID , genesisTimestamp
172
221
},
173
- BlockedAddr : a .BlockedAddrs (),
222
+ BlockedAddr : a .BlockedAddresses (),
174
223
AccountSource : a .AuthKeeper ,
175
224
BalanceSource : a .BankKeeper ,
176
225
}
177
226
}
178
227
ti .Cfg .InitialBlockHeight = int (exported .Height )
179
- simsx .RunWithSeed (t , ti .Cfg , app . NewEthermintApp , importGenesisStateFactory , ti .Cfg .Seed , ti .Cfg .FuzzSeed , testutil .RandomAccounts )
228
+ simsx .RunWithSeedAndRandAcc (t , ti .Cfg , NewSimApp , importGenesisStateFactory , ti .Cfg .Seed , ti .Cfg .FuzzSeed , testutil .RandomAccounts )
180
229
})
181
230
}
182
231
183
232
func TestAppStateDeterminism (t * testing.T ) {
184
- return
185
233
const numTimesToRunPerSeed = 3
186
234
var seeds []int64
187
235
if s := simcli .NewConfigFromFlags ().Seed ; s != simcli .DefaultSeedValue {
@@ -214,7 +262,7 @@ func TestAppStateDeterminism(t *testing.T) {
214
262
return others .Get (k )
215
263
})
216
264
}
217
- return app . NewEthermintApp (logger , db , nil , true , appOpts , append (baseAppOptions , interBlockCacheOpt ())... )
265
+ return NewSimApp (logger , db , nil , true , appOpts , append (baseAppOptions , interBlockCacheOpt ())... )
218
266
}
219
267
var mx sync.Mutex
220
268
appHashResults := make (map [int64 ][][]byte )
@@ -245,13 +293,23 @@ func TestAppStateDeterminism(t *testing.T) {
245
293
}
246
294
}
247
295
// run simulations
248
- simsx .RunWithSeeds (
249
- t ,
250
- interBlockCachingAppFactory ,
251
- setupStateFactory ,
252
- seeds ,
253
- []byte {},
254
- testutil .RandomAccounts ,
255
- captureAndCheckHash ,
256
- )
296
+ cfg := simcli .NewConfigFromFlags ()
297
+ cfg .ChainID = SimAppChainID
298
+ for i := range seeds {
299
+ seed := seeds [i ]
300
+ t .Run (fmt .Sprintf ("seed: %d" , seed ), func (t * testing.T ) {
301
+ t .Parallel ()
302
+ simsx .RunWithSeedAndRandAcc (
303
+ t ,
304
+ cfg ,
305
+ interBlockCachingAppFactory ,
306
+ setupStateFactory ,
307
+ seed ,
308
+ []byte {},
309
+ testutil .RandomAccounts ,
310
+ captureAndCheckHash ,
311
+ )
312
+ })
313
+ }
314
+
257
315
}
0 commit comments