49
49
lean bool
50
50
commit bool
51
51
period int
52
+ onOperation bool // TODO Remove in favor of binary search for invariant violation
52
53
)
53
54
54
55
func init () {
@@ -61,15 +62,16 @@ func init() {
61
62
flag .BoolVar (& lean , "SimulationLean" , false , "lean simulation log output" )
62
63
flag .BoolVar (& commit , "SimulationCommit" , false , "have the simulation commit" )
63
64
flag .IntVar (& period , "SimulationPeriod" , 1 , "run slow invariants only once every period assertions" )
65
+ flag .BoolVar (& onOperation , "SimulateEveryOperation" , false , "run slow invariants every operation" )
64
66
}
65
67
66
68
// helper function for populating input for SimulateFromSeed
67
69
func getSimulateFromSeedInput (tb testing.TB , w io.Writer , app * GaiaApp ) (
68
70
testing.TB , io.Writer , * baseapp.BaseApp , simulation.AppStateFn , int64 ,
69
- simulation.WeightedOperations , sdk.Invariants , int , int , bool , bool ) {
71
+ simulation.WeightedOperations , sdk.Invariants , int , int , bool , bool , bool ) {
70
72
71
73
return tb , w , app .BaseApp , appStateFn , seed ,
72
- testAndRunTxs (app ), invariants (app ), numBlocks , blockSize , commit , lean
74
+ testAndRunTxs (app ), invariants (app ), numBlocks , blockSize , commit , lean , onOperation
73
75
}
74
76
75
77
func appStateFromGenesisFileFn (r * rand.Rand , accs []simulation.Account , genesisTimestamp time.Time ,
@@ -84,7 +86,7 @@ func appStateFromGenesisFileFn(r *rand.Rand, accs []simulation.Account, genesisT
84
86
cdc .MustUnmarshalJSON (bytes , & genesis )
85
87
var appState GenesisState
86
88
cdc .MustUnmarshalJSON (genesis .AppState , & appState )
87
- accounts := genaccounts .GetGenesisStateFromAppState (cdc , appState ). Accounts
89
+ accounts := genaccounts .GetGenesisStateFromAppState (cdc , appState )
88
90
89
91
var newAccs []simulation.Account
90
92
for _ , acc := range accounts {
@@ -165,8 +167,7 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest
165
167
genesisAccounts = append (genesisAccounts , gacc )
166
168
}
167
169
168
- genaccsGenesis := genaccounts .NewGenesisState (genesisAccounts )
169
- genesisState [genaccounts .ModuleName ] = cdc .MustMarshalJSON (genaccsGenesis )
170
+ genesisState [genaccounts .ModuleName ] = cdc .MustMarshalJSON (genesisAccounts )
170
171
171
172
authGenesis := auth .NewGenesisState (
172
173
nil ,
@@ -301,8 +302,8 @@ func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation {
301
302
{50 , distrsim .SimulateMsgWithdrawDelegatorReward (app .accountKeeper , app .distrKeeper )},
302
303
{50 , distrsim .SimulateMsgWithdrawValidatorCommission (app .accountKeeper , app .distrKeeper )},
303
304
{5 , govsim .SimulateSubmittingVotingAndSlashingForProposal (app .govKeeper , govsim .SimulateTextProposalContent )},
304
- {5 , govsim .SimulateSubmittingVotingAndSlashingForProposal (app .govKeeper , paramsim .SimulateParamChangeProposalContent )},
305
305
{5 , govsim .SimulateSubmittingVotingAndSlashingForProposal (app .govKeeper , distrsim .SimulateCommunityPoolSpendProposalContent (app .distrKeeper ))},
306
+ {5 , govsim .SimulateSubmittingVotingAndSlashingForProposal (app .govKeeper , paramsim .SimulateParamChangeProposalContent )},
306
307
{100 , govsim .SimulateMsgDeposit (app .govKeeper )},
307
308
{100 , stakingsim .SimulateMsgCreateValidator (app .accountKeeper , app .stakingKeeper )},
308
309
{5 , stakingsim .SimulateMsgEditValidator (app .stakingKeeper )},
@@ -323,13 +324,12 @@ func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
323
324
}
324
325
325
326
// Profile with:
326
- // /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/gaia/app -bench ^BenchmarkFullGaiaSimulation$ -SimulationCommit=true -cpuprofile cpu.out
327
- func BenchmarkFullGaiaSimulation (b * testing.B ) {
328
- // Setup Gaia application
327
+ // /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/cosmos-sdk/GaiaApp -bench ^BenchmarkFullAppSimulation$ -SimulationCommit=true -cpuprofile cpu.out
328
+ func BenchmarkFullAppSimulation (b * testing.B ) {
329
329
logger := log .NewNopLogger ()
330
330
331
331
var db dbm.DB
332
- dir , _ := ioutil .TempDir ("" , "goleveldb-gaia -sim" )
332
+ dir , _ := ioutil .TempDir ("" , "goleveldb-app -sim" )
333
333
db , _ = sdk .NewLevelDB ("Simulation" , dir )
334
334
defer func () {
335
335
db .Close ()
@@ -351,25 +351,28 @@ func BenchmarkFullGaiaSimulation(b *testing.B) {
351
351
}
352
352
}
353
353
354
- func TestFullGaiaSimulation (t * testing.T ) {
354
+ func TestFullAppSimulation (t * testing.T ) {
355
355
if ! enabled {
356
- t .Skip ("Skipping Gaia simulation" )
356
+ t .Skip ("Skipping application simulation" )
357
357
}
358
358
359
- // Setup Gaia application
360
359
var logger log.Logger
360
+
361
361
if verbose {
362
362
logger = log .TestingLogger ()
363
363
} else {
364
364
logger = log .NewNopLogger ()
365
365
}
366
+
366
367
var db dbm.DB
367
- dir , _ := ioutil .TempDir ("" , "goleveldb-gaia -sim" )
368
+ dir , _ := ioutil .TempDir ("" , "goleveldb-app -sim" )
368
369
db , _ = sdk .NewLevelDB ("Simulation" , dir )
370
+
369
371
defer func () {
370
372
db .Close ()
371
373
os .RemoveAll (dir )
372
374
}()
375
+
373
376
app := NewGaiaApp (logger , db , nil , true , 0 , fauxMerkleModeOpt )
374
377
require .Equal (t , "GaiaApp" , app .Name ())
375
378
@@ -382,28 +385,31 @@ func TestFullGaiaSimulation(t *testing.T) {
382
385
fmt .Println (db .Stats ()["leveldb.stats" ])
383
386
fmt .Println ("GoLevelDB cached block size" , db .Stats ()["leveldb.cachedblock" ])
384
387
}
388
+
385
389
require .Nil (t , err )
386
390
}
387
391
388
- func TestGaiaImportExport (t * testing.T ) {
392
+ func TestAppImportExport (t * testing.T ) {
389
393
if ! enabled {
390
- t .Skip ("Skipping Gaia import/export simulation" )
394
+ t .Skip ("Skipping application import/export simulation" )
391
395
}
392
396
393
- // Setup Gaia application
394
397
var logger log.Logger
395
398
if verbose {
396
399
logger = log .TestingLogger ()
397
400
} else {
398
401
logger = log .NewNopLogger ()
399
402
}
403
+
400
404
var db dbm.DB
401
- dir , _ := ioutil .TempDir ("" , "goleveldb-gaia -sim" )
405
+ dir , _ := ioutil .TempDir ("" , "goleveldb-app -sim" )
402
406
db , _ = sdk .NewLevelDB ("Simulation" , dir )
407
+
403
408
defer func () {
404
409
db .Close ()
405
410
os .RemoveAll (dir )
406
411
}()
412
+
407
413
app := NewGaiaApp (logger , db , nil , true , 0 , fauxMerkleModeOpt )
408
414
require .Equal (t , "GaiaApp" , app .Name ())
409
415
@@ -417,37 +423,43 @@ func TestGaiaImportExport(t *testing.T) {
417
423
fmt .Println (db .Stats ()["leveldb.stats" ])
418
424
fmt .Println ("GoLevelDB cached block size" , db .Stats ()["leveldb.cachedblock" ])
419
425
}
420
- require .Nil (t , err )
421
426
427
+ require .Nil (t , err )
422
428
fmt .Printf ("Exporting genesis...\n " )
423
429
424
430
appState , _ , err := app .ExportAppStateAndValidators (false , []string {})
425
431
require .NoError (t , err )
426
432
fmt .Printf ("Importing genesis...\n " )
427
433
428
- newDir , _ := ioutil .TempDir ("" , "goleveldb-gaia -sim-2" )
434
+ newDir , _ := ioutil .TempDir ("" , "goleveldb-app -sim-2" )
429
435
newDB , _ := sdk .NewLevelDB ("Simulation-2" , dir )
436
+
430
437
defer func () {
431
438
newDB .Close ()
432
439
os .RemoveAll (newDir )
433
440
}()
441
+
434
442
newApp := NewGaiaApp (log .NewNopLogger (), newDB , nil , true , 0 , fauxMerkleModeOpt )
435
443
require .Equal (t , "GaiaApp" , newApp .Name ())
444
+
436
445
var genesisState GenesisState
437
446
err = app .cdc .UnmarshalJSON (appState , & genesisState )
438
447
if err != nil {
439
448
panic (err )
440
449
}
450
+
441
451
ctxB := newApp .NewContext (true , abci.Header {})
442
452
newApp .mm .InitGenesis (ctxB , genesisState )
443
453
444
454
fmt .Printf ("Comparing stores...\n " )
445
455
ctxA := app .NewContext (true , abci.Header {})
456
+
446
457
type StoreKeysPrefixes struct {
447
458
A sdk.StoreKey
448
459
B sdk.StoreKey
449
460
Prefixes [][]byte
450
461
}
462
+
451
463
storeKeysPrefixes := []StoreKeysPrefixes {
452
464
{app .keyMain , newApp .keyMain , [][]byte {}},
453
465
{app .keyAccount , newApp .keyAccount , [][]byte {}},
@@ -460,6 +472,7 @@ func TestGaiaImportExport(t *testing.T) {
460
472
{app .keyParams , newApp .keyParams , [][]byte {}},
461
473
{app .keyGov , newApp .keyGov , [][]byte {}},
462
474
}
475
+
463
476
for _ , storeKeysPrefix := range storeKeysPrefixes {
464
477
storeKeyA := storeKeysPrefix .A
465
478
storeKeyB := storeKeysPrefix .B
@@ -476,24 +489,26 @@ func TestGaiaImportExport(t *testing.T) {
476
489
477
490
}
478
491
479
- func TestGaiaSimulationAfterImport (t * testing.T ) {
492
+ func TestAppSimulationAfterImport (t * testing.T ) {
480
493
if ! enabled {
481
- t .Skip ("Skipping Gaia simulation after import" )
494
+ t .Skip ("Skipping application simulation after import" )
482
495
}
483
496
484
- // Setup Gaia application
485
497
var logger log.Logger
486
498
if verbose {
487
499
logger = log .TestingLogger ()
488
500
} else {
489
501
logger = log .NewNopLogger ()
490
502
}
491
- dir , _ := ioutil .TempDir ("" , "goleveldb-gaia-sim" )
503
+
504
+ dir , _ := ioutil .TempDir ("" , "goleveldb-app-sim" )
492
505
db , _ := sdk .NewLevelDB ("Simulation" , dir )
506
+
493
507
defer func () {
494
508
db .Close ()
495
509
os .RemoveAll (dir )
496
510
}()
511
+
497
512
app := NewGaiaApp (logger , db , nil , true , 0 , fauxMerkleModeOpt )
498
513
require .Equal (t , "GaiaApp" , app .Name ())
499
514
@@ -507,6 +522,7 @@ func TestGaiaSimulationAfterImport(t *testing.T) {
507
522
fmt .Println (db .Stats ()["leveldb.stats" ])
508
523
fmt .Println ("GoLevelDB cached block size" , db .Stats ()["leveldb.cachedblock" ])
509
524
}
525
+
510
526
require .Nil (t , err )
511
527
512
528
if stopEarly {
@@ -524,12 +540,14 @@ func TestGaiaSimulationAfterImport(t *testing.T) {
524
540
525
541
fmt .Printf ("Importing genesis...\n " )
526
542
527
- newDir , _ := ioutil .TempDir ("" , "goleveldb-gaia -sim-2" )
543
+ newDir , _ := ioutil .TempDir ("" , "goleveldb-app -sim-2" )
528
544
newDB , _ := sdk .NewLevelDB ("Simulation-2" , dir )
545
+
529
546
defer func () {
530
547
newDB .Close ()
531
548
os .RemoveAll (newDir )
532
549
}()
550
+
533
551
newApp := NewGaiaApp (log .NewNopLogger (), newDB , nil , true , 0 , fauxMerkleModeOpt )
534
552
require .Equal (t , "GaiaApp" , newApp .Name ())
535
553
newApp .InitChain (abci.RequestInitChain {
@@ -539,14 +557,13 @@ func TestGaiaSimulationAfterImport(t *testing.T) {
539
557
// Run randomized simulation on imported app
540
558
_ , err = simulation .SimulateFromSeed (getSimulateFromSeedInput (t , os .Stdout , newApp ))
541
559
require .Nil (t , err )
542
-
543
560
}
544
561
545
562
// TODO: Make another test for the fuzzer itself, which just has noOp txs
546
- // and doesn't depend on gaia
563
+ // and doesn't depend on the application.
547
564
func TestAppStateDeterminism (t * testing.T ) {
548
565
if ! enabled {
549
- t .Skip ("Skipping Gaia simulation" )
566
+ t .Skip ("Skipping application simulation" )
550
567
}
551
568
552
569
numSeeds := 3
@@ -569,6 +586,7 @@ func TestAppStateDeterminism(t *testing.T) {
569
586
100 ,
570
587
true ,
571
588
false ,
589
+ false ,
572
590
)
573
591
appHash := app .LastCommitID ().Hash
574
592
appHashList [j ] = appHash
@@ -580,9 +598,8 @@ func TestAppStateDeterminism(t *testing.T) {
580
598
}
581
599
582
600
func BenchmarkInvariants (b * testing.B ) {
583
- // 1. Setup a simulated Gaia application
584
601
logger := log .NewNopLogger ()
585
- dir , _ := ioutil .TempDir ("" , "goleveldb-gaia -invariant-bench" )
602
+ dir , _ := ioutil .TempDir ("" , "goleveldb-app -invariant-bench" )
586
603
db , _ := sdk .NewLevelDB ("simulation" , dir )
587
604
588
605
defer func () {
@@ -595,7 +612,7 @@ func BenchmarkInvariants(b *testing.B) {
595
612
// 2. Run parameterized simulation (w/o invariants)
596
613
_ , err := simulation .SimulateFromSeed (
597
614
b , ioutil .Discard , app .BaseApp , appStateFn , seed , testAndRunTxs (app ),
598
- []sdk.Invariant {}, numBlocks , blockSize , commit , lean ,
615
+ []sdk.Invariant {}, numBlocks , blockSize , commit , lean , onOperation ,
599
616
)
600
617
if err != nil {
601
618
fmt .Println (err )
0 commit comments