github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/simapp/utils.go (about)

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