@@ -4,38 +4,86 @@ import (
44 "os"
55 "testing"
66
7- abci "github.com/cometbft/cometbft/abci/types"
87 "github.com/cosmos/cosmos-sdk/baseapp"
9- "github.com/cosmos/cosmos-sdk/codec"
108 simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
11- sdk "github.com/cosmos/cosmos-sdk/types"
12- "github.com/cosmos/cosmos-sdk/types/module"
13- simulationtypes "github.com/cosmos/cosmos-sdk/types/simulation"
9+ simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
1410 "github.com/cosmos/cosmos-sdk/x/simulation"
1511 simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
12+
1613 "github.com/stretchr/testify/require"
1714
1815 "github.com/terra-money/alliance/app"
1916)
2017
21- // SimAppChainID hardcoded chainID for simulation
22- const SimAppChainID = "simulation-app"
18+ // Hardcoded chainID for simulation.
19+ const (
20+ simulationAppChainID = "simulation-app"
21+ simulationDirPrefix = "leveldb-app-sim"
22+ simulationDBName = "Simulation"
23+ )
2324
2425func init () {
2526 simcli .GetSimulatorFlags ()
2627}
2728
28- type SimApp interface {
29- app.App
30- GetBaseApp () * baseapp.BaseApp
31- AppCodec () codec.Codec
32- SimulationManager () * module.SimulationManager
33- ModuleAccountAddrs () map [string ]bool
34- Name () string
35- LegacyAmino () * codec.LegacyAmino
36- BeginBlocker (ctx sdk.Context , req abci.RequestBeginBlock ) abci.ResponseBeginBlock
37- EndBlocker (ctx sdk.Context , req abci.RequestEndBlock ) abci.ResponseEndBlock
38- InitChainer (ctx sdk.Context , req abci.RequestInitChain ) abci.ResponseInitChain
29+ // Running as a go test:
30+ //
31+ // go test -v -run=TestFullAppSimulation ./app -NumBlocks 200 -BlockSize 10 -Commit -Enabled -Period 1
32+ func TestFullAppSimulation (t * testing.T ) {
33+ config := simcli .NewConfigFromFlags ()
34+ config .ChainID = simulationAppChainID
35+
36+ if ! simcli .FlagEnabledValue {
37+ t .Skip ("skipping application simulation" )
38+ }
39+
40+ db , dir , logger , _ , err := simtestutil .SetupSimulation (
41+ config ,
42+ simulationDirPrefix ,
43+ simulationDBName ,
44+ simcli .FlagVerboseValue ,
45+ true , // Don't use this as it is confusing
46+ )
47+ require .NoError (t , err , "simulation setup failed" )
48+
49+ defer func () {
50+ require .NoError (t , db .Close ())
51+ require .NoError (t , os .RemoveAll (dir ))
52+ }()
53+
54+ app := app .New (logger ,
55+ db ,
56+ nil ,
57+ true ,
58+ map [int64 ]bool {},
59+ app .DefaultNodeHome ,
60+ simcli .FlagPeriodValue ,
61+ app .MakeTestEncodingConfig (),
62+ simtestutil.EmptyAppOptions {},
63+ baseapp .SetChainID (simulationAppChainID ),
64+ )
65+
66+ // run randomized simulation
67+ _ , simParams , simErr := simulation .SimulateFromSeed (
68+ t ,
69+ os .Stdout ,
70+ app .BaseApp ,
71+ simtestutil .AppStateFn (app .AppCodec (), app .SimulationManager (), app .DefaultGenesis ()),
72+ simtypes .RandomAccounts ,
73+ simtestutil .SimulationOperations (app , app .AppCodec (), config ),
74+ app .BankKeeper .GetBlockedAddresses (),
75+ config ,
76+ app .AppCodec (),
77+ )
78+
79+ // export state and simParams before the simulatino error is checked
80+ err = simtestutil .CheckExportSimulation (app , config , simParams )
81+ require .NoError (t , err )
82+ require .NoError (t , simErr )
83+
84+ if config .Commit {
85+ simtestutil .PrintStats (db )
86+ }
3987}
4088
4189// BenchmarkSimulation run the chain simulation
@@ -61,8 +109,7 @@ func BenchmarkSimulation(b *testing.B) {
61109
62110 encoding := app .MakeTestEncodingConfig ()
63111
64- simApp := app .New (
65- logger ,
112+ app := app .New (logger ,
66113 db ,
67114 nil ,
68115 true ,
@@ -77,17 +124,17 @@ func BenchmarkSimulation(b *testing.B) {
77124 _ , simParams , simErr := simulation .SimulateFromSeed (
78125 b ,
79126 os .Stdout ,
80- simApp .GetBaseApp (),
81- simtestutil .AppStateFn (simApp .AppCodec (), simApp .SimulationManager (), simApp .DefaultGenesis ()),
82- simulationtypes .RandomAccounts ,
83- simtestutil .SimulationOperations (simApp , simApp .AppCodec (), config ),
84- simApp .ModuleAccountAddrs (),
127+ app .GetBaseApp (),
128+ simtestutil .AppStateFn (app .AppCodec (), app .SimulationManager (), app .DefaultGenesis ()),
129+ simtypes .RandomAccounts ,
130+ simtestutil .SimulationOperations (app , app .AppCodec (), config ),
131+ app .ModuleAccountAddrs (),
85132 config ,
86- simApp .AppCodec (),
133+ app .AppCodec (),
87134 )
88135
89136 // export state and simParams before the simulation error is checked
90- err = simtestutil .CheckExportSimulation (simApp , config , simParams )
137+ err = simtestutil .CheckExportSimulation (app , config , simParams )
91138 require .NoError (b , err )
92139 require .NoError (b , simErr )
93140
0 commit comments