github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/simapp/utils.go (about) 1 package simapp 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 8 tmkv "github.com/fibonacci-chain/fbc/libs/tendermint/libs/kv" 9 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 10 dbm "github.com/fibonacci-chain/fbc/libs/tm-db" 11 12 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/simapp/helpers" 14 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 15 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module" 16 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation" 17 ) 18 19 // SetupSimulation creates the config, db (levelDB), temporary directory and logger for 20 // the simulation tests. If `FlagEnabledValue` is false it skips the current test. 21 // Returns error on an invalid db intantiation or temp dir creation. 22 func SetupSimulation(dirPrefix, dbName string) (simulation.Config, dbm.DB, string, log.Logger, bool, error) { 23 if !FlagEnabledValue { 24 return simulation.Config{}, nil, "", nil, true, nil 25 } 26 27 config := NewConfigFromFlags() 28 config.ChainID = helpers.SimAppChainID 29 30 var logger log.Logger 31 if FlagVerboseValue { 32 logger = log.TestingLogger() 33 } else { 34 logger = log.NewNopLogger() 35 } 36 37 dir, err := ioutil.TempDir("", dirPrefix) 38 if err != nil { 39 return simulation.Config{}, nil, "", nil, false, err 40 } 41 42 db, err := sdk.NewDB(dbName, dir) 43 if err != nil { 44 return simulation.Config{}, nil, "", nil, false, err 45 } 46 47 return config, db, dir, logger, false, nil 48 } 49 50 // SimulationOperations retrieves the simulation params from the provided file path 51 // and returns all the modules weighted operations 52 func SimulationOperations(app App, cdc *codec.Codec, config simulation.Config) []simulation.WeightedOperation { 53 simState := module.SimulationState{ 54 AppParams: make(simulation.AppParams), 55 Cdc: cdc, 56 } 57 58 if config.ParamsFile != "" { 59 bz, err := ioutil.ReadFile(config.ParamsFile) 60 if err != nil { 61 panic(err) 62 } 63 64 app.Codec().MustUnmarshalJSON(bz, &simState.AppParams) 65 } 66 67 simState.ParamChanges = app.SimulationManager().GenerateParamChanges(config.Seed) 68 simState.Contents = app.SimulationManager().GetProposalContents(simState) 69 return app.SimulationManager().WeightedOperations(simState) 70 } 71 72 // CheckExportSimulation exports the app state and simulation parameters to JSON 73 // if the export paths are defined. 74 func CheckExportSimulation( 75 app App, config simulation.Config, params simulation.Params, 76 ) error { 77 if config.ExportStatePath != "" { 78 fmt.Println("exporting app state...") 79 appState, _, err := app.ExportAppStateAndValidators(false, nil) 80 if err != nil { 81 return err 82 } 83 84 if err := ioutil.WriteFile(config.ExportStatePath, []byte(appState), 0600); err != nil { 85 return err 86 } 87 } 88 89 if config.ExportParamsPath != "" { 90 fmt.Println("exporting simulation params...") 91 paramsBz, err := json.MarshalIndent(params, "", " ") 92 if err != nil { 93 return err 94 } 95 96 if err := ioutil.WriteFile(config.ExportParamsPath, paramsBz, 0600); err != nil { 97 return err 98 } 99 } 100 return nil 101 } 102 103 // PrintStats prints the corresponding statistics from the app DB. 104 func PrintStats(db dbm.DB) { 105 fmt.Println("\nLevelDB Stats") 106 fmt.Println(db.Stats()["leveldb.stats"]) 107 fmt.Println("LevelDB cached block size", db.Stats()["leveldb.cachedblock"]) 108 } 109 110 // GetSimulationLog unmarshals the KVPair's Value to the corresponding type based on the 111 // each's module store key and the prefix bytes of the KVPair's key. 112 func GetSimulationLog(storeName string, sdr sdk.StoreDecoderRegistry, cdc *codec.Codec, kvAs, kvBs []tmkv.Pair) (log string) { 113 for i := 0; i < len(kvAs); i++ { 114 115 if len(kvAs[i].Value) == 0 && len(kvBs[i].Value) == 0 { 116 // skip if the value doesn't have any bytes 117 continue 118 } 119 120 decoder, ok := sdr[storeName] 121 if ok { 122 log += decoder(cdc, kvAs[i], kvBs[i]) 123 } else { 124 log += fmt.Sprintf("store A %X => %X\nstore B %X => %X\n", kvAs[i].Key, kvAs[i].Value, kvBs[i].Key, kvBs[i].Value) 125 } 126 } 127 128 return 129 }