perf: Replace runsim with Go stdlib testing (#24045)
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
co-authored by
Alex | Interchain Labs
parent
b7c35f2568
commit
bc57ce3ba7
@@ -12,6 +12,7 @@ import (
|
||||
"cosmossdk.io/log"
|
||||
storetypes "cosmossdk.io/store/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/runtime"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -23,7 +24,7 @@ import (
|
||||
|
||||
// SetupSimulation creates the config, db (levelDB), temporary directory and logger for the simulation tests.
|
||||
// If `skip` is false it skips the current test. `skip` should be set using the `FlagEnabledValue` flag.
|
||||
// Returns error on an invalid db intantiation or temp dir creation.
|
||||
// Returns error on an invalid db instantiation or temp dir creation.
|
||||
func SetupSimulation(config simtypes.Config, dirPrefix, dbName string, verbose, skip bool) (dbm.DB, string, log.Logger, bool, error) {
|
||||
if !skip {
|
||||
return nil, "", nil, true, nil
|
||||
@@ -31,7 +32,7 @@ func SetupSimulation(config simtypes.Config, dirPrefix, dbName string, verbose,
|
||||
|
||||
var logger log.Logger
|
||||
if verbose {
|
||||
logger = log.NewLogger(os.Stdout) // TODO(mr): enable selection of log destination.
|
||||
logger = log.NewLogger(os.Stdout)
|
||||
} else {
|
||||
logger = log.NewNopLogger()
|
||||
}
|
||||
@@ -51,11 +52,18 @@ func SetupSimulation(config simtypes.Config, dirPrefix, dbName string, verbose,
|
||||
|
||||
// SimulationOperations retrieves the simulation params from the provided file path
|
||||
// and returns all the modules weighted operations
|
||||
// Deprecated: use BuildSimulationOperations with TxConfig
|
||||
func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes.Config) []simtypes.WeightedOperation {
|
||||
return BuildSimulationOperations(app, cdc, config, moduletestutil.MakeTestTxConfig())
|
||||
}
|
||||
|
||||
// BuildSimulationOperations retrieves the simulation params from the provided file path
|
||||
// and returns all the modules weighted operations
|
||||
func BuildSimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes.Config, txConfig client.TxConfig) []simtypes.WeightedOperation {
|
||||
simState := module.SimulationState{
|
||||
AppParams: make(simtypes.AppParams),
|
||||
Cdc: cdc,
|
||||
TxConfig: moduletestutil.MakeTestTxConfig(),
|
||||
TxConfig: txConfig,
|
||||
BondDenom: sdk.DefaultBondDenom,
|
||||
}
|
||||
|
||||
@@ -71,8 +79,7 @@ func SimulationOperations(app runtime.AppI, cdc codec.JSONCodec, config simtypes
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:staticcheck // used for legacy testing
|
||||
simState.LegacyProposalContents = app.SimulationManager().GetProposalContents(simState)
|
||||
simState.LegacyProposalContents = app.SimulationManager().GetProposalContents(simState) //nolint:staticcheck // we're testing the old way here
|
||||
simState.ProposalMsgs = app.SimulationManager().GetProposalMsgs(simState)
|
||||
return app.SimulationManager().WeightedOperations(simState)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/module"
|
||||
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
||||
@@ -35,7 +36,11 @@ const (
|
||||
|
||||
// AppStateFn returns the initial application state using a genesis or the simulation parameters.
|
||||
// It calls AppStateFnWithExtendedCb with nil rawStateCb.
|
||||
func AppStateFn(cdc codec.JSONCodec, simManager *module.SimulationManager, genesisState map[string]json.RawMessage) simtypes.AppStateFn {
|
||||
func AppStateFn(
|
||||
cdc codec.JSONCodec,
|
||||
simManager *module.SimulationManager,
|
||||
genesisState map[string]json.RawMessage,
|
||||
) simtypes.AppStateFn {
|
||||
return AppStateFnWithExtendedCb(cdc, simManager, genesisState, nil)
|
||||
}
|
||||
|
||||
@@ -68,13 +73,9 @@ func AppStateFnWithExtendedCbs(
|
||||
accs []simtypes.Account,
|
||||
config simtypes.Config,
|
||||
) (appState json.RawMessage, simAccs []simtypes.Account, chainID string, genesisTimestamp time.Time) {
|
||||
if simcli.FlagGenesisTimeValue == 0 {
|
||||
genesisTimestamp = simtypes.RandTimestamp(r)
|
||||
} else {
|
||||
genesisTimestamp = time.Unix(simcli.FlagGenesisTimeValue, 0)
|
||||
}
|
||||
|
||||
genesisTimestamp = time.Unix(config.GenesisTime, 0)
|
||||
chainID = config.ChainID
|
||||
|
||||
switch {
|
||||
case config.ParamsFile != "" && config.GenesisFile != "":
|
||||
panic("cannot provide both a genesis file and a params file")
|
||||
@@ -138,8 +139,7 @@ func AppStateFnWithExtendedCbs(
|
||||
}
|
||||
notBondedCoins := sdk.NewCoin(stakingState.Params.BondDenom, notBondedTokens)
|
||||
// edit bank state to make it have the not bonded pool tokens
|
||||
bankStateBz, ok := rawState[banktypes.ModuleName]
|
||||
// TODO(fdymylja/jonathan): should we panic in this case
|
||||
bankStateBz, ok := rawState[testutil.BankModuleName]
|
||||
if !ok {
|
||||
panic("bank genesis state is missing")
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func AppStateFnWithExtendedCbs(
|
||||
// change appState back
|
||||
for name, state := range map[string]proto.Message{
|
||||
stakingtypes.ModuleName: stakingState,
|
||||
banktypes.ModuleName: bankState,
|
||||
testutil.BankModuleName: bankState,
|
||||
} {
|
||||
if moduleStateCb != nil {
|
||||
moduleStateCb(name, state)
|
||||
@@ -219,15 +219,6 @@ func AppStateRandomizedFn(
|
||||
numInitiallyBonded = numAccs
|
||||
}
|
||||
|
||||
fmt.Printf(
|
||||
`Selected randomly generated parameters for simulated genesis:
|
||||
{
|
||||
stake_per_account: "%d",
|
||||
initially_bonded_validators: "%d"
|
||||
}
|
||||
`, initialStake.Uint64(), numInitiallyBonded,
|
||||
)
|
||||
|
||||
simState := &module.SimulationState{
|
||||
AppParams: appParams,
|
||||
Cdc: cdc,
|
||||
@@ -273,8 +264,8 @@ func AppStateFromGenesisFileFn(r io.Reader, cdc codec.JSONCodec, genesisFile str
|
||||
}
|
||||
|
||||
var authGenesis authtypes.GenesisState
|
||||
if appState[authtypes.ModuleName] != nil {
|
||||
cdc.MustUnmarshalJSON(appState[authtypes.ModuleName], &authGenesis)
|
||||
if appState[testutil.AuthModuleName] != nil {
|
||||
cdc.MustUnmarshalJSON(appState[testutil.AuthModuleName], &authGenesis)
|
||||
}
|
||||
|
||||
newAccs := make([]simtypes.Account, len(authGenesis.Accounts))
|
||||
|
||||
Reference in New Issue
Block a user