-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsim_test.go
210 lines (175 loc) · 6.53 KB
/
sim_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package app_test
import (
"encoding/json"
"fmt"
"os"
"testing"
"time"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
terraapp "github.com/classic-terra/core/v3/app"
helpers "github.com/classic-terra/core/v3/app/testing"
dbm "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/libs/rand"
"github.com/stretchr/testify/require"
"cosmossdk.io/simapp"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
tax2gastypes "github.com/classic-terra/core/v3/x/tax2gas/types"
)
// SimAppChainID hardcoded chainID for simulation
const SimAppChainID = "simulation-app"
var emptyWasmOpts []wasmkeeper.Option
// interBlockCacheOpt returns a BaseApp option function that sets the persistent
// inter-block write-through cache.
func interBlockCacheOpt() func(*baseapp.BaseApp) {
return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager())
}
// fauxMerkleModeOpt is a BaseApp option function to enable faux Merkle Tree mode for faster sim speed
func fauxMerkleModeOpt() func(*baseapp.BaseApp) {
return func(app *baseapp.BaseApp) { app.SetFauxMerkleMode() }
}
func init() {
simcli.GetSimulatorFlags()
}
func setupSimulationApp(b *testing.B, msg string) (simtypes.Config, dbm.DB, simtestutil.AppOptionsMap, *terraapp.TerraApp) {
config := simcli.NewConfigFromFlags()
config.ChainID = SimAppChainID
db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "leveldb-app-sim", "Simulation", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
if skip {
b.Skip(msg)
}
require.NoError(b, err, "simulation setup failed")
b.Cleanup(func() {
require.NoError(b, db.Close())
require.NoError(b, os.RemoveAll(dir))
})
appOptions := make(simtestutil.AppOptionsMap, 0)
app := terraapp.NewTerraApp(
logger, db, nil, true, map[int64]bool{},
dir, terraapp.MakeEncodingConfig(),
appOptions, emptyWasmOpts, interBlockCacheOpt(), fauxMerkleModeOpt(), baseapp.SetChainID(SimAppChainID),
)
require.Equal(b, "WasmApp", app.Name())
return config, db, appOptions, app
}
// Profile with:
// /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/cosmos-sdk/GaiaApp -bench ^BenchmarkFullAppSimulation$ -Commit=true -cpuprofile cpu.out
func BenchmarkFullAppSimulation(b *testing.B) {
config, db, _, app := setupSimulationApp(b, "skipping application simulation")
// Run randomized simulation:w
_, simParams, simErr := simulation.SimulateFromSeed(
b,
os.Stdout,
app.BaseApp,
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)
// export state and simParams before the simulation error is checked
if err := simtestutil.CheckExportSimulation(app, config, simParams); err != nil {
b.Fatal(err)
}
if simErr != nil {
b.Fatal(simErr)
}
if config.Commit {
simtestutil.PrintStats(db)
}
}
// TODO: Make another test for the fuzzer itself, which just has noOp txs
// and doesn't depend on the application.
func TestAppStateDeterminism(t *testing.T) {
if !simcli.FlagEnabledValue {
t.Skip("skipping application simulation")
}
config := simcli.NewConfigFromFlags()
config.InitialBlockHeight = 1
config.ExportParamsPath = ""
config.OnOperation = false
config.AllInvariants = false
config.ChainID = helpers.SimAppChainID
numSeeds := 3
numTimesToRunPerSeed := 3
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)
for i := 0; i < numSeeds; i++ {
config.Seed = rand.Int63()
for j := 0; j < numTimesToRunPerSeed; j++ {
var logger log.Logger
if simcli.FlagVerboseValue {
logger = log.TestingLogger()
} else {
logger = log.NewNopLogger()
}
db := dbm.NewMemDB()
var emptyWasmOpts []wasmkeeper.Option
app := terraapp.NewTerraApp(
logger, db, nil, true, map[int64]bool{}, terraapp.DefaultNodeHome,
terraapp.MakeEncodingConfig(),
simtestutil.EmptyAppOptions{}, emptyWasmOpts, interBlockCacheOpt(), fauxMerkleModeOpt(),
)
appGenState := app.DefaultGenesis()
tax2gasGenState := tax2gastypes.GenesisState{}
err := app.AppCodec().UnmarshalJSON(appGenState[tax2gastypes.ModuleName], &tax2gasGenState)
require.NoError(t, err)
tax2gasGenState.Params = tax2gastypes.Params{
Enabled: true,
GasPrices: sdk.DecCoins{
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(0, 3)),
},
}
newGenState := tax2gasGenState
bz, err := app.AppCodec().MarshalJSON(&newGenState)
require.NoError(t, err)
appGenState[tax2gastypes.ModuleName] = bz
fmt.Printf(
"running non-determinism simulation; seed %d: %d/%d, attempt: %d/%d\n",
config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
)
_, _, err = simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
AppStateFn(app.AppCodec(), app.SimulationManager(), appGenState),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)
require.NoError(t, err)
if config.Commit {
simtestutil.PrintStats(db)
}
appHash := app.LastCommitID().Hash
appHashList[j] = appHash
if j != 0 {
require.Equal(
t, string(appHashList[0]), string(appHashList[j]),
"non-determinism in seed %d: %d/%d, attempt: %d/%d\n", config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
)
}
}
}
}
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
// It panics if the user provides files for both of them.
// If a file is not given for the genesis or the sim params, it creates a randomized one.
func AppStateFn(codec codec.Codec, manager *module.SimulationManager, genesisState map[string]json.RawMessage) simtypes.AppStateFn {
// quick hack to setup app state genesis with our app modules
simapp.ModuleBasics = terraapp.ModuleBasics
if simcli.FlagGenesisTimeValue == 0 { // always set to have a block time
simcli.FlagGenesisTimeValue = time.Now().Unix()
}
return simtestutil.AppStateFn(codec, manager, genesisState)
}