2019-06-28 11:20:36 -07:00
|
|
|
package simapp
|
|
|
|
|
|
|
|
import (
|
2019-07-03 11:25:52 -07:00
|
|
|
"encoding/json"
|
2019-06-28 11:20:36 -07:00
|
|
|
"fmt"
|
2019-08-28 07:58:25 -07:00
|
|
|
"io/ioutil"
|
2019-06-28 11:20:36 -07:00
|
|
|
|
2020-01-16 13:46:51 -08:00
|
|
|
tmkv "github.com/tendermint/tendermint/libs/kv"
|
2019-12-17 10:28:52 -08:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
dbm "github.com/tendermint/tm-db"
|
2019-07-03 11:25:52 -07:00
|
|
|
|
2019-07-31 06:59:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-12-17 10:28:52 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp/helpers"
|
2019-06-28 11:20:36 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-12-05 01:29:54 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2019-07-03 11:25:52 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2019-06-28 11:20:36 -07:00
|
|
|
)
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// SetupSimulation creates the config, db (levelDB), temporary directory and logger for
|
|
|
|
// the simulation tests. If `FlagEnabledValue` is false it skips the current test.
|
|
|
|
// Returns error on an invalid db intantiation or temp dir creation.
|
|
|
|
func SetupSimulation(dirPrefix, dbName string) (simulation.Config, dbm.DB, string, log.Logger, bool, error) {
|
|
|
|
if !FlagEnabledValue {
|
|
|
|
return simulation.Config{}, nil, "", nil, true, nil
|
|
|
|
}
|
2019-07-03 11:25:52 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
config := NewConfigFromFlags()
|
|
|
|
config.ChainID = helpers.SimAppChainID
|
|
|
|
|
|
|
|
var logger log.Logger
|
|
|
|
if FlagVerboseValue {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2019-06-28 11:20:36 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
dir, err := ioutil.TempDir("", dirPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return simulation.Config{}, nil, "", nil, false, err
|
2019-07-03 11:25:52 -07:00
|
|
|
}
|
2019-12-17 10:28:52 -08:00
|
|
|
|
|
|
|
db, err := sdk.NewLevelDB(dbName, dir)
|
|
|
|
if err != nil {
|
|
|
|
return simulation.Config{}, nil, "", nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, db, dir, logger, false, nil
|
2019-07-03 11:25:52 -07:00
|
|
|
}
|
|
|
|
|
2019-12-05 01:29:54 -08:00
|
|
|
// SimulationOperations retrieves the simulation params from the provided file path
|
|
|
|
// and returns all the modules weighted operations
|
2019-12-17 10:28:52 -08:00
|
|
|
func SimulationOperations(app App, cdc *codec.Codec, config simulation.Config) []simulation.WeightedOperation {
|
2019-12-05 01:29:54 -08:00
|
|
|
simState := module.SimulationState{
|
|
|
|
AppParams: make(simulation.AppParams),
|
|
|
|
Cdc: cdc,
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ParamsFile != "" {
|
|
|
|
bz, err := ioutil.ReadFile(config.ParamsFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
app.Codec().MustUnmarshalJSON(bz, &simState.AppParams)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
simState.ParamChanges = app.SimulationManager().GenerateParamChanges(config.Seed)
|
|
|
|
simState.Contents = app.SimulationManager().GetProposalContents(simState)
|
|
|
|
return app.SimulationManager().WeightedOperations(simState)
|
2019-12-05 01:29:54 -08:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// CheckExportSimulation exports the app state and simulation parameters to JSON
|
|
|
|
// if the export paths are defined.
|
|
|
|
func CheckExportSimulation(
|
|
|
|
app App, config simulation.Config, params simulation.Params,
|
|
|
|
) error {
|
|
|
|
if config.ExportStatePath != "" {
|
|
|
|
fmt.Println("exporting app state...")
|
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-03 11:25:52 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
if err := ioutil.WriteFile(config.ExportStatePath, []byte(appState), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-03 11:25:52 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
if config.ExportParamsPath != "" {
|
|
|
|
fmt.Println("exporting simulation params...")
|
|
|
|
paramsBz, err := json.MarshalIndent(params, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-03 11:25:52 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
if err := ioutil.WriteFile(config.ExportParamsPath, paramsBz, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-03 11:25:52 -07:00
|
|
|
}
|
2019-12-17 10:28:52 -08:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-03 11:25:52 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// PrintStats prints the corresponding statistics from the app DB.
|
|
|
|
func PrintStats(db dbm.DB) {
|
|
|
|
fmt.Println("\nLevelDB Stats")
|
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("LevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
2019-07-03 11:25:52 -07:00
|
|
|
}
|
|
|
|
|
2019-07-03 09:12:43 -07:00
|
|
|
// GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the
|
2019-06-28 11:20:36 -07:00
|
|
|
// each's module store key and the prefix bytes of the KVPair's key.
|
2020-01-16 13:46:51 -08:00
|
|
|
func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec.Codec, kvAs, kvBs []tmkv.Pair) (log string) {
|
2019-08-13 15:16:03 -07:00
|
|
|
for i := 0; i < len(kvAs); i++ {
|
2019-07-19 07:05:37 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 {
|
2019-07-19 07:05:37 -07:00
|
|
|
// skip if the value doesn't have any bytes
|
|
|
|
continue
|
|
|
|
}
|
2019-06-28 11:20:36 -07:00
|
|
|
|
2019-08-13 15:16:03 -07:00
|
|
|
decoder, ok := sdr[storeName]
|
|
|
|
if ok {
|
|
|
|
log += decoder(cdc, kvAs[i], kvBs[i])
|
|
|
|
} else {
|
|
|
|
log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvAs[i].Key, kvAs[i].Value, kvBs[i].Key, kvBs[i].Value)
|
2019-07-19 07:05:37 -07:00
|
|
|
}
|
2019-06-28 11:20:36 -07:00
|
|
|
}
|
|
|
|
|
2019-07-19 07:05:37 -07:00
|
|
|
return
|
2019-06-28 11:20:36 -07:00
|
|
|
}
|