forked from CosmWasm/wasmd
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsim_test.go
292 lines (252 loc) · 10.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package app
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"github.com/Finschia/finschia-sdk/baseapp"
"github.com/Finschia/finschia-sdk/codec"
"github.com/Finschia/finschia-sdk/simapp"
"github.com/Finschia/finschia-sdk/store"
"github.com/Finschia/finschia-sdk/store/prefix"
sdk "github.com/Finschia/finschia-sdk/types"
"github.com/Finschia/finschia-sdk/types/kv"
"github.com/Finschia/finschia-sdk/types/module"
simtypes "github.com/Finschia/finschia-sdk/types/simulation"
authtypes "github.com/Finschia/finschia-sdk/x/auth/types"
authzkeeper "github.com/Finschia/finschia-sdk/x/authz/keeper"
banktypes "github.com/Finschia/finschia-sdk/x/bank/types"
capabilitytypes "github.com/Finschia/finschia-sdk/x/capability/types"
distrtypes "github.com/Finschia/finschia-sdk/x/distribution/types"
evidencetypes "github.com/Finschia/finschia-sdk/x/evidence/types"
"github.com/Finschia/finschia-sdk/x/feegrant"
govtypes "github.com/Finschia/finschia-sdk/x/gov/types"
minttypes "github.com/Finschia/finschia-sdk/x/mint/types"
paramstypes "github.com/Finschia/finschia-sdk/x/params/types"
"github.com/Finschia/finschia-sdk/x/simulation"
slashingtypes "github.com/Finschia/finschia-sdk/x/slashing/types"
stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
"github.com/Finschia/ostracon/libs/log"
ibctransfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
ibchost "github.com/cosmos/ibc-go/v4/modules/core/24-host"
"github.com/Finschia/wasmd/x/wasm"
wasmtypes "github.com/Finschia/wasmd/x/wasm/types"
)
// Get flags every time the simulator is run
func init() {
simapp.GetSimulatorFlags()
}
type StoreKeysPrefixes struct {
A sdk.StoreKey
B sdk.StoreKey
Prefixes [][]byte
}
// SetupSimulation wraps simapp.SetupSimulation in order to create any export directory if they do not exist yet
func SetupSimulation(dirPrefix, dbName string) (simtypes.Config, dbm.DB, string, log.Logger, bool, error) {
config, db, dir, logger, skip, err := simapp.SetupSimulation(dirPrefix, dbName)
if err != nil {
return simtypes.Config{}, nil, "", nil, false, err
}
paths := []string{config.ExportParamsPath, config.ExportStatePath, config.ExportStatsPath}
for _, path := range paths {
if len(path) == 0 {
continue
}
path = filepath.Dir(path)
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
panic(err)
}
}
}
return config, db, dir, logger, skip, err
}
// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the
// each's module store key and the prefix bytes of the KVPair's key.
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, kvAs, kvBs []kv.Pair) (log string) {
for i := 0; i < len(kvAs); i++ {
if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 {
// skip if the value doesn't have any bytes
continue
}
decoder, ok := sdr[storeName]
if ok {
log += decoder(kvAs[i], kvBs[i])
} else {
log += fmt.Sprintf("store A %q => %q\nstore B %q => %q\n", kvAs[i].Key, kvAs[i].Value, kvBs[i].Key, kvBs[i].Value)
}
}
return log
}
// fauxMerkleModeOpt returns a BaseApp option to use a dbStoreAdapter instead of
// an IAVLStore for faster simulation speed.
func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
bapp.SetFauxMerkleMode()
}
func TestAppImportExport(t *testing.T) {
config, db, dir, logger, skip, err := SetupSimulation("leveldb-app-sim", "Simulation")
if skip {
t.Skip("skipping application import/export simulation")
}
require.NoError(t, err, "simulation setup failed")
defer func() {
db.Close()
require.NoError(t, os.RemoveAll(dir))
}()
encConf := MakeEncodingConfig()
app := NewWasmApp(logger, db, nil, true, map[int64]bool{}, dir, simapp.FlagPeriodValue, encConf, wasm.EnableAllProposals, EmptyBaseAppOptions{}, nil, fauxMerkleModeOpt)
require.Equal(t, appName, app.Name())
// Run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
AppStateFn(app.AppCodec(), app.SimulationManager()),
simtypes.RandomAccounts,
simapp.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)
// export state and simParams before the simulation error is checked
err = simapp.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)
if config.Commit {
simapp.PrintStats(db)
}
t.Log("exporting genesis...")
exported, err := app.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err)
t.Log("importing genesis...")
_, newDB, newDir, _, _, err := SetupSimulation("leveldb-app-sim-2", "Simulation-2")
require.NoError(t, err, "simulation setup failed")
defer func() {
newDB.Close()
require.NoError(t, os.RemoveAll(newDir))
}()
newApp := NewWasmApp(logger, newDB, nil, true, map[int64]bool{}, newDir, simapp.FlagPeriodValue, encConf, wasm.EnableAllProposals, EmptyBaseAppOptions{}, nil, fauxMerkleModeOpt)
require.Equal(t, appName, newApp.Name())
var genesisState GenesisState
err = json.Unmarshal(exported.AppState, &genesisState)
require.NoError(t, err)
ctxA := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
ctxB := newApp.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
newApp.mm.InitGenesis(ctxB, app.AppCodec(), genesisState)
newApp.StoreConsensusParams(ctxB, exported.ConsensusParams)
t.Log("comparing stores...")
storeKeysPrefixes := []StoreKeysPrefixes{
{app.keys[authtypes.StoreKey], newApp.keys[authtypes.StoreKey], [][]byte{}},
{
app.keys[stakingtypes.StoreKey], newApp.keys[stakingtypes.StoreKey],
[][]byte{
stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey,
stakingtypes.HistoricalInfoKey,
},
},
{app.keys[slashingtypes.StoreKey], newApp.keys[slashingtypes.StoreKey], [][]byte{}},
{app.keys[minttypes.StoreKey], newApp.keys[minttypes.StoreKey], [][]byte{}},
{app.keys[distrtypes.StoreKey], newApp.keys[distrtypes.StoreKey], [][]byte{}},
{app.keys[banktypes.StoreKey], newApp.keys[banktypes.StoreKey], [][]byte{banktypes.BalancesPrefix}},
{app.keys[paramstypes.StoreKey], newApp.keys[paramstypes.StoreKey], [][]byte{}},
{app.keys[govtypes.StoreKey], newApp.keys[govtypes.StoreKey], [][]byte{}},
{app.keys[evidencetypes.StoreKey], newApp.keys[evidencetypes.StoreKey], [][]byte{}},
{app.keys[capabilitytypes.StoreKey], newApp.keys[capabilitytypes.StoreKey], [][]byte{}},
{app.keys[ibchost.StoreKey], newApp.keys[ibchost.StoreKey], [][]byte{}},
{app.keys[ibctransfertypes.StoreKey], newApp.keys[ibctransfertypes.StoreKey], [][]byte{}},
{app.keys[authzkeeper.StoreKey], newApp.keys[authzkeeper.StoreKey], [][]byte{}},
{app.keys[feegrant.StoreKey], newApp.keys[feegrant.StoreKey], [][]byte{}},
{app.keys[wasm.StoreKey], newApp.keys[wasm.StoreKey], [][]byte{}},
}
// delete persistent tx counter value
ctxA.KVStore(app.keys[wasm.StoreKey]).Delete(wasmtypes.TXCounterPrefix)
// reset contract code index in source DB for comparison with dest DB
dropContractHistory := func(s store.KVStore, keys ...[]byte) {
for _, key := range keys {
prefixStore := prefix.NewStore(s, key)
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
prefixStore.Delete(iter.Key())
}
iter.Close()
}
}
prefixes := [][]byte{wasmtypes.ContractCodeHistoryElementPrefix, wasmtypes.ContractByCodeIDAndCreatedSecondaryIndexPrefix}
dropContractHistory(ctxA.KVStore(app.keys[wasm.StoreKey]), prefixes...)
dropContractHistory(ctxB.KVStore(newApp.keys[wasm.StoreKey]), prefixes...)
normalizeContractInfo := func(ctx sdk.Context, app *WasmApp) {
var index uint64
app.WasmKeeper.IterateContractInfo(ctx, func(address sdk.AccAddress, info wasmtypes.ContractInfo) bool {
created := &wasmtypes.AbsoluteTxPosition{
BlockHeight: uint64(0),
TxIndex: index,
}
info.Created = created
store := ctx.KVStore(app.keys[wasm.StoreKey])
store.Set(wasmtypes.GetContractAddressKey(address), app.appCodec.MustMarshal(&info))
index++
return false
})
}
normalizeContractInfo(ctxA, app)
normalizeContractInfo(ctxB, newApp)
// diff both stores
for _, skp := range storeKeysPrefixes {
storeA := ctxA.KVStore(skp.A)
storeB := ctxB.KVStore(skp.B)
failedKVAs, failedKVBs := sdk.DiffKVStores(storeA, storeB, skp.Prefixes)
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")
t.Logf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
require.Len(t, failedKVAs, 0, GetSimulationLog(skp.A.Name(), app.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
}
}
func TestFullAppSimulation(t *testing.T) {
config, db, dir, logger, skip, err := SetupSimulation("leveldb-app-sim", "Simulation")
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")
defer func() {
db.Close()
require.NoError(t, os.RemoveAll(dir))
}()
encConf := MakeEncodingConfig()
app := NewWasmApp(logger, db, nil, true, map[int64]bool{}, t.TempDir(), simapp.FlagPeriodValue,
encConf, wasm.EnableAllProposals, simapp.EmptyAppOptions{}, nil, fauxMerkleModeOpt)
require.Equal(t, "WasmApp", app.Name())
// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
AppStateFn(app.appCodec, app.SimulationManager()),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simapp.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
app.AppCodec(),
)
// export state and simParams before the simulation error is checked
err = simapp.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)
if config.Commit {
simapp.PrintStats(db)
}
}
// 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) simtypes.AppStateFn {
// quick hack to setup app state genesis with our app modules
simapp.ModuleBasics = ModuleBasics
if simapp.FlagGenesisTimeValue == 0 { // always set to have a block time
simapp.FlagGenesisTimeValue = time.Now().Unix()
}
return simapp.AppStateFn(codec, manager)
}