forked from cosmos/interchain-security
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_test.go
99 lines (79 loc) · 3.13 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
package app_test
import (
"fmt"
"math/rand"
"os"
"testing"
spew "github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"
"cosmossdk.io/store"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
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"
providerapp "github.com/cosmos/interchain-security/v6/app/provider"
)
func init() {
simcli.GetSimulatorFlags()
}
// 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 returns a BaseApp option to use a dbStoreAdapter instead of
// an IAVLStore for faster simulation speed.
func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
bapp.SetFauxMerkleMode()
}
func TestFullAppSimulation(t *testing.T) {
config := simcli.NewConfigFromFlags()
config.ChainID = "provi"
// if no seed is provided (aka we use the default seed), override this by choosing an actually random seed
if config.Seed == simcli.DefaultSeedValue {
fmt.Printf("Default seed value %v detected, using random seed value\n", simcli.DefaultSeedValue)
config.Seed = int64(rand.Uint32())
}
fmt.Println("========================================")
fmt.Println("Running with the configuration:")
fmt.Println(spew.Sdump(config))
fmt.Println("========================================")
db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "leveldb-app-sim", "Simulation", simcli.FlagVerboseValue, simcli.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")
defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
}()
appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = providerapp.DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue
app := providerapp.New(logger, db, nil, true, appOptions, fauxMerkleModeOpt, interBlockCacheOpt(), baseapp.SetChainID("provi"))
require.Equal(t, "interchain-security-p", app.Name())
encoding := providerapp.MakeTestEncodingConfig()
genesisState := providerapp.NewDefaultGenesisState(encoding.Codec)
// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
simtestutil.AppStateFn(encoding.Codec, app.SimulationManager(), genesisState),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
providerapp.ComputeBankBlockedAddrs(app),
config,
app.AppCodec(),
)
// export state and simParams before the simulation error is checked
err = simtestutil.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)
if config.Commit {
simtestutil.PrintStats(db)
}
}