2018-07-17 14:06:30 -07:00
|
|
|
package app
|
2018-07-11 15:14:37 -07:00
|
|
|
|
|
|
|
import (
|
2018-07-17 15:04:10 -07:00
|
|
|
"encoding/json"
|
2018-07-18 23:40:46 -07:00
|
|
|
"flag"
|
2018-09-01 19:04:44 -07:00
|
|
|
"fmt"
|
2019-04-12 15:52:16 -07:00
|
|
|
"io"
|
2018-10-29 16:10:39 -07:00
|
|
|
"io/ioutil"
|
2018-07-17 15:04:10 -07:00
|
|
|
"math/rand"
|
2018-08-30 11:43:56 -07:00
|
|
|
"os"
|
2018-07-11 15:14:37 -07:00
|
|
|
"testing"
|
2018-11-04 20:44:43 -08:00
|
|
|
"time"
|
2018-07-11 15:14:37 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-12 13:01:43 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-01-29 14:53:42 -08:00
|
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
2018-07-12 13:01:43 -07:00
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-01-29 14:53:42 -08:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-07-12 13:01:43 -07:00
|
|
|
|
2018-11-27 00:14:22 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
2018-07-17 15:04:10 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-12-20 11:09:43 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/genaccounts"
|
2018-10-22 21:53:06 -07:00
|
|
|
authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation"
|
2019-01-28 19:06:48 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2018-07-18 00:05:48 -07:00
|
|
|
banksim "github.com/cosmos/cosmos-sdk/x/bank/simulation"
|
2018-10-05 17:32:06 -07:00
|
|
|
distr "github.com/cosmos/cosmos-sdk/x/distribution"
|
2018-10-23 11:33:39 -07:00
|
|
|
distrsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation"
|
2018-08-31 21:57:33 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov"
|
2018-08-16 08:36:15 -07:00
|
|
|
govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation"
|
2018-10-19 11:36:00 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mint"
|
2019-05-02 13:50:01 -07:00
|
|
|
paramsim "github.com/cosmos/cosmos-sdk/x/params/simulation"
|
2019-03-14 11:13:15 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2018-08-31 02:03:43 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
2018-08-16 08:36:15 -07:00
|
|
|
slashingsim "github.com/cosmos/cosmos-sdk/x/slashing/simulation"
|
2019-01-16 13:38:05 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingsim "github.com/cosmos/cosmos-sdk/x/staking/simulation"
|
2018-07-16 18:15:50 -07:00
|
|
|
)
|
|
|
|
|
2018-07-18 23:40:46 -07:00
|
|
|
var (
|
2019-01-29 14:53:42 -08:00
|
|
|
genesisFile string
|
|
|
|
seed int64
|
|
|
|
numBlocks int
|
|
|
|
blockSize int
|
|
|
|
enabled bool
|
|
|
|
verbose bool
|
2019-03-14 11:13:15 -07:00
|
|
|
lean bool
|
2019-01-29 14:53:42 -08:00
|
|
|
commit bool
|
|
|
|
period int
|
2018-07-11 15:14:37 -07:00
|
|
|
)
|
|
|
|
|
2018-07-18 23:40:46 -07:00
|
|
|
func init() {
|
2019-03-14 11:13:15 -07:00
|
|
|
flag.StringVar(&genesisFile, "SimulationGenesis", "", "custom simulation genesis file")
|
|
|
|
flag.Int64Var(&seed, "SimulationSeed", 42, "simulation random seed")
|
|
|
|
flag.IntVar(&numBlocks, "SimulationNumBlocks", 500, "number of blocks")
|
|
|
|
flag.IntVar(&blockSize, "SimulationBlockSize", 200, "operations per block")
|
|
|
|
flag.BoolVar(&enabled, "SimulationEnabled", false, "enable the simulation")
|
|
|
|
flag.BoolVar(&verbose, "SimulationVerbose", false, "verbose log output")
|
|
|
|
flag.BoolVar(&lean, "SimulationLean", false, "lean simulation log output")
|
|
|
|
flag.BoolVar(&commit, "SimulationCommit", false, "have the simulation commit")
|
|
|
|
flag.IntVar(&period, "SimulationPeriod", 1, "run slow invariants only once every period assertions")
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function for populating input for SimulateFromSeed
|
2019-04-12 15:52:16 -07:00
|
|
|
func getSimulateFromSeedInput(tb testing.TB, w io.Writer, app *GaiaApp) (
|
|
|
|
testing.TB, io.Writer, *baseapp.BaseApp, simulation.AppStateFn, int64,
|
2019-03-14 11:13:15 -07:00
|
|
|
simulation.WeightedOperations, sdk.Invariants, int, int, bool, bool) {
|
|
|
|
|
2019-04-12 15:52:16 -07:00
|
|
|
return tb, w, app.BaseApp, appStateFn, seed,
|
2019-03-14 11:13:15 -07:00
|
|
|
testAndRunTxs(app), invariants(app), numBlocks, blockSize, commit, lean
|
2018-07-18 23:40:46 -07:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
func appStateFromGenesisFileFn(r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time,
|
|
|
|
) (json.RawMessage, []simulation.Account, string) {
|
|
|
|
|
2019-01-29 14:53:42 -08:00
|
|
|
var genesis tmtypes.GenesisDoc
|
|
|
|
cdc := MakeCodec()
|
|
|
|
bytes, err := ioutil.ReadFile(genesisFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cdc.MustUnmarshalJSON(bytes, &genesis)
|
|
|
|
var appState GenesisState
|
|
|
|
cdc.MustUnmarshalJSON(genesis.AppState, &appState)
|
2019-05-16 08:25:32 -07:00
|
|
|
accounts := genaccounts.GetGenesisStateFromAppState(cdc, appState).Accounts
|
|
|
|
|
2019-01-29 14:53:42 -08:00
|
|
|
var newAccs []simulation.Account
|
2019-05-16 08:25:32 -07:00
|
|
|
for _, acc := range accounts {
|
2019-01-29 14:53:42 -08:00
|
|
|
// Pick a random private key, since we don't know the actual key
|
|
|
|
// This should be fine as it's only used for mock Tendermint validators
|
|
|
|
// and these keys are never actually used to sign by mock Tendermint.
|
|
|
|
privkeySeed := make([]byte, 15)
|
|
|
|
r.Read(privkeySeed)
|
|
|
|
privKey := secp256k1.GenPrivKeySecp256k1(privkeySeed)
|
|
|
|
newAccs = append(newAccs, simulation.Account{privKey, privKey.PubKey(), acc.Address})
|
|
|
|
}
|
2019-02-04 15:35:12 -08:00
|
|
|
return genesis.AppState, newAccs, genesis.ChainID
|
2019-01-29 14:53:42 -08:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// TODO refactor out random initialization code to the modules
|
|
|
|
func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time,
|
|
|
|
) (json.RawMessage, []simulation.Account, string) {
|
2019-01-29 14:53:42 -08:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var genesisAccounts []genaccounts.GenesisAccount
|
|
|
|
genesisState := NewDefaultGenesisState()
|
|
|
|
cdc := MakeCodec()
|
2018-07-17 11:33:53 -07:00
|
|
|
|
2019-02-06 16:15:01 -08:00
|
|
|
amount := int64(r.Intn(1e12))
|
2018-11-04 20:44:43 -08:00
|
|
|
numInitiallyBonded := int64(r.Intn(250))
|
|
|
|
numAccs := int64(len(accs))
|
|
|
|
if numInitiallyBonded > numAccs {
|
|
|
|
numInitiallyBonded = numAccs
|
|
|
|
}
|
2018-11-26 04:13:47 -08:00
|
|
|
fmt.Printf("Selected randomly generated parameters for simulated genesis:\n"+
|
2019-03-14 07:32:46 -07:00
|
|
|
"\t{amount of stake per account: %v, initially bonded validators: %v}\n",
|
2018-11-26 04:13:47 -08:00
|
|
|
amount, numInitiallyBonded)
|
2018-10-23 09:24:06 -07:00
|
|
|
|
2019-01-17 08:15:13 -08:00
|
|
|
// randomly generate some genesis accounts
|
|
|
|
for i, acc := range accs {
|
2019-02-13 15:01:50 -08:00
|
|
|
coins := sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(amount))}
|
2019-01-17 08:15:13 -08:00
|
|
|
bacc := auth.NewBaseAccountWithAddress(acc.Address)
|
|
|
|
bacc.SetCoins(coins)
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var gacc genaccounts.GenesisAccount
|
2019-01-17 08:15:13 -08:00
|
|
|
|
|
|
|
// Only consider making a vesting account once the initial bonded validator
|
|
|
|
// set is exhausted due to needing to track DelegatedVesting.
|
|
|
|
if int64(i) > numInitiallyBonded && r.Intn(100) < 50 {
|
|
|
|
var (
|
|
|
|
vacc auth.VestingAccount
|
2019-04-22 07:57:58 -07:00
|
|
|
endTime int64
|
2019-01-17 08:15:13 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
startTime := genesisTimestamp.Unix()
|
|
|
|
|
|
|
|
// Allow for some vesting accounts to vest very quickly while others very
|
|
|
|
// slowly.
|
|
|
|
if r.Intn(100) < 50 {
|
2019-04-22 07:57:58 -07:00
|
|
|
endTime = int64(simulation.RandIntBetween(r, int(startTime), int(startTime+(60*60*24*30))))
|
2019-01-17 08:15:13 -08:00
|
|
|
} else {
|
2019-04-22 07:57:58 -07:00
|
|
|
endTime = int64(simulation.RandIntBetween(r, int(startTime), int(startTime+(60*60*12))))
|
|
|
|
}
|
|
|
|
|
|
|
|
if startTime == endTime {
|
2019-05-16 08:25:32 -07:00
|
|
|
endTime++
|
2019-01-17 08:15:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Intn(100) < 50 {
|
2019-04-22 07:57:58 -07:00
|
|
|
vacc = auth.NewContinuousVestingAccount(&bacc, startTime, endTime)
|
2019-01-17 08:15:13 -08:00
|
|
|
} else {
|
2019-04-22 07:57:58 -07:00
|
|
|
vacc = auth.NewDelayedVestingAccount(&bacc, endTime)
|
2019-01-17 08:15:13 -08:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var err error
|
|
|
|
gacc, err = genaccounts.NewGenesisAccountI(vacc)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-17 08:15:13 -08:00
|
|
|
} else {
|
2019-05-16 08:25:32 -07:00
|
|
|
gacc = genaccounts.NewGenesisAccount(&bacc)
|
2019-01-17 08:15:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
genesisAccounts = append(genesisAccounts, gacc)
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
2018-10-05 17:32:06 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
genaccsGenesis := genaccounts.NewGenesisState(genesisAccounts)
|
|
|
|
genesisState[genaccounts.ModuleName] = cdc.MustMarshalJSON(genaccsGenesis)
|
|
|
|
|
2019-05-02 13:50:01 -07:00
|
|
|
authGenesis := auth.NewGenesisState(
|
|
|
|
nil,
|
2019-05-16 08:25:32 -07:00
|
|
|
auth.NewParams(
|
|
|
|
simulation.ModuleParamSimulator["MaxMemoCharacters"](r).(uint64),
|
|
|
|
simulation.ModuleParamSimulator["TxSigLimit"](r).(uint64),
|
|
|
|
simulation.ModuleParamSimulator["TxSizeCostPerByte"](r).(uint64),
|
|
|
|
simulation.ModuleParamSimulator["SigVerifyCostED25519"](r).(uint64),
|
|
|
|
simulation.ModuleParamSimulator["SigVerifyCostSecp256k1"](r).(uint64),
|
|
|
|
),
|
2019-05-02 13:50:01 -07:00
|
|
|
)
|
2018-12-20 11:09:43 -08:00
|
|
|
fmt.Printf("Selected randomly generated auth parameters:\n\t%+v\n", authGenesis)
|
2019-05-16 08:25:32 -07:00
|
|
|
genesisState[auth.ModuleName] = cdc.MustMarshalJSON(authGenesis)
|
2018-12-20 11:09:43 -08:00
|
|
|
|
2019-01-28 19:06:48 -08:00
|
|
|
bankGenesis := bank.NewGenesisState(r.Int63n(2) == 0)
|
2019-05-16 08:25:32 -07:00
|
|
|
genesisState[bank.ModuleName] = cdc.MustMarshalJSON(bankGenesis)
|
2019-01-28 19:06:48 -08:00
|
|
|
fmt.Printf("Selected randomly generated bank parameters:\n\t%+v\n", bankGenesis)
|
|
|
|
|
2018-11-04 20:44:43 -08:00
|
|
|
// Random genesis states
|
2019-05-02 13:50:01 -07:00
|
|
|
vp := simulation.ModuleParamSimulator["VotingParams/VotingPeriod"](r).(time.Duration)
|
|
|
|
govGenesis := gov.NewGenesisState(
|
|
|
|
uint64(r.Intn(100)),
|
2019-05-16 08:25:32 -07:00
|
|
|
gov.NewDepositParams(
|
|
|
|
simulation.ModuleParamSimulator["DepositParams/MinDeposit"](r).(sdk.Coins),
|
|
|
|
vp,
|
|
|
|
),
|
|
|
|
gov.NewVotingParams(vp),
|
|
|
|
gov.NewTallyParams(
|
|
|
|
simulation.ModuleParamSimulator["TallyParams/Quorum"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["TallyParams/Threshold"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["TallyParams/Veto"](r).(sdk.Dec),
|
|
|
|
),
|
2019-05-02 13:50:01 -07:00
|
|
|
)
|
2019-05-16 08:25:32 -07:00
|
|
|
genesisState[gov.ModuleName] = cdc.MustMarshalJSON(govGenesis)
|
2018-11-26 04:13:47 -08:00
|
|
|
fmt.Printf("Selected randomly generated governance parameters:\n\t%+v\n", govGenesis)
|
|
|
|
|
2019-05-02 13:50:01 -07:00
|
|
|
stakingGenesis := staking.NewGenesisState(
|
|
|
|
staking.InitialPool(),
|
2019-05-16 08:25:32 -07:00
|
|
|
staking.NewParams(
|
|
|
|
simulation.ModuleParamSimulator["UnbondingTime"](r).(time.Duration),
|
|
|
|
simulation.ModuleParamSimulator["MaxValidators"](r).(uint16),
|
|
|
|
7,
|
|
|
|
sdk.DefaultBondDenom,
|
|
|
|
),
|
2019-05-02 13:50:01 -07:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2019-01-11 12:08:01 -08:00
|
|
|
fmt.Printf("Selected randomly generated staking parameters:\n\t%+v\n", stakingGenesis)
|
2018-11-26 04:13:47 -08:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
slashingParams := slashing.NewParams(
|
|
|
|
stakingGenesis.Params.UnbondingTime,
|
|
|
|
simulation.ModuleParamSimulator["SignedBlocksWindow"](r).(int64),
|
|
|
|
simulation.ModuleParamSimulator["MinSignedPerWindow"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["DowntimeJailDuration"](r).(time.Duration),
|
|
|
|
simulation.ModuleParamSimulator["SlashFractionDoubleSign"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["SlashFractionDowntime"](r).(sdk.Dec),
|
|
|
|
)
|
|
|
|
slashingGenesis := slashing.NewGenesisState(slashingParams, nil, nil)
|
|
|
|
genesisState[slashing.ModuleName] = cdc.MustMarshalJSON(slashingGenesis)
|
2018-11-26 04:13:47 -08:00
|
|
|
fmt.Printf("Selected randomly generated slashing parameters:\n\t%+v\n", slashingGenesis)
|
|
|
|
|
2019-05-02 13:50:01 -07:00
|
|
|
mintGenesis := mint.NewGenesisState(
|
|
|
|
mint.InitialMinter(
|
2018-11-26 04:13:47 -08:00
|
|
|
sdk.NewDecWithPrec(int64(r.Intn(99)), 2)),
|
2019-05-02 13:50:01 -07:00
|
|
|
mint.NewParams(
|
2019-02-13 15:01:50 -08:00
|
|
|
sdk.DefaultBondDenom,
|
2019-05-02 13:50:01 -07:00
|
|
|
simulation.ModuleParamSimulator["InflationRateChange"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["InflationMax"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["InflationMin"](r).(sdk.Dec),
|
|
|
|
simulation.ModuleParamSimulator["GoalBonded"](r).(sdk.Dec),
|
|
|
|
uint64(60*60*8766/5),
|
|
|
|
),
|
|
|
|
)
|
2019-05-16 08:25:32 -07:00
|
|
|
genesisState[mint.ModuleName] = cdc.MustMarshalJSON(mintGenesis)
|
2018-11-26 04:13:47 -08:00
|
|
|
fmt.Printf("Selected randomly generated minting parameters:\n\t%+v\n", mintGenesis)
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
var validators []staking.Validator
|
|
|
|
var delegations []staking.Delegation
|
2018-10-05 17:32:06 -07:00
|
|
|
|
|
|
|
valAddrs := make([]sdk.ValAddress, numInitiallyBonded)
|
2018-08-16 08:36:15 -07:00
|
|
|
for i := 0; i < int(numInitiallyBonded); i++ {
|
2018-10-05 17:32:06 -07:00
|
|
|
valAddr := sdk.ValAddress(accs[i].Address)
|
|
|
|
valAddrs[i] = valAddr
|
|
|
|
|
2019-01-11 12:08:01 -08:00
|
|
|
validator := staking.NewValidator(valAddr, accs[i].PubKey, staking.Description{})
|
2019-01-02 12:29:47 -08:00
|
|
|
validator.Tokens = sdk.NewInt(amount)
|
2018-11-04 20:44:43 -08:00
|
|
|
validator.DelegatorShares = sdk.NewDec(amount)
|
2019-01-11 12:08:01 -08:00
|
|
|
delegation := staking.Delegation{accs[i].Address, valAddr, sdk.NewDec(amount)}
|
2018-08-16 08:36:15 -07:00
|
|
|
validators = append(validators, validator)
|
|
|
|
delegations = append(delegations, delegation)
|
|
|
|
}
|
2019-01-17 08:15:13 -08:00
|
|
|
|
2019-01-21 16:52:03 -08:00
|
|
|
stakingGenesis.Pool.NotBondedTokens = sdk.NewInt((amount * numAccs) + (numInitiallyBonded * amount))
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingGenesis.Validators = validators
|
2019-02-13 15:01:50 -08:00
|
|
|
stakingGenesis.Delegations = delegations
|
2019-05-16 08:25:32 -07:00
|
|
|
genesisState[staking.ModuleName] = cdc.MustMarshalJSON(stakingGenesis)
|
2018-10-05 17:32:06 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
// TODO make use NewGenesisState
|
2019-01-16 13:38:05 -08:00
|
|
|
distrGenesis := distr.GenesisState{
|
|
|
|
FeePool: distr.InitialFeePool(),
|
|
|
|
CommunityTax: sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2)),
|
|
|
|
BaseProposerReward: sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2)),
|
|
|
|
BonusProposerReward: sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(30)), 2)),
|
|
|
|
}
|
2019-05-16 08:25:32 -07:00
|
|
|
genesisState[distr.ModuleName] = cdc.MustMarshalJSON(distrGenesis)
|
2019-01-16 13:38:05 -08:00
|
|
|
fmt.Printf("Selected randomly generated distribution parameters:\n\t%+v\n", distrGenesis)
|
|
|
|
|
2018-07-17 11:33:53 -07:00
|
|
|
// Marshal genesis
|
2019-05-16 08:25:32 -07:00
|
|
|
appState, err := MakeCodec().MarshalJSON(genesisState)
|
2018-07-17 11:33:53 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-29 14:53:42 -08:00
|
|
|
return appState, accs, "simulation"
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
func appStateFn(r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time,
|
|
|
|
) (json.RawMessage, []simulation.Account, string) {
|
|
|
|
|
2019-01-29 14:53:42 -08:00
|
|
|
if genesisFile != "" {
|
|
|
|
return appStateFromGenesisFileFn(r, accs, genesisTimestamp)
|
|
|
|
}
|
|
|
|
return appStateRandomizedFn(r, accs, genesisTimestamp)
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
|
2018-09-12 00:16:52 -07:00
|
|
|
func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation {
|
|
|
|
return []simulation.WeightedOperation{
|
2018-10-23 08:54:58 -07:00
|
|
|
{5, authsim.SimulateDeductFee(app.accountKeeper, app.feeCollectionKeeper)},
|
2019-03-14 11:13:15 -07:00
|
|
|
{100, banksim.SimulateMsgSend(app.accountKeeper, app.bankKeeper)},
|
|
|
|
{10, banksim.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper)},
|
2018-10-23 11:33:39 -07:00
|
|
|
{50, distrsim.SimulateMsgSetWithdrawAddress(app.accountKeeper, app.distrKeeper)},
|
|
|
|
{50, distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper)},
|
2019-01-16 13:38:05 -08:00
|
|
|
{50, distrsim.SimulateMsgWithdrawValidatorCommission(app.accountKeeper, app.distrKeeper)},
|
2019-05-02 13:50:01 -07:00
|
|
|
{5, govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, govsim.SimulateTextProposalContent)},
|
|
|
|
{5, govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, paramsim.SimulateParamChangeProposalContent)},
|
2018-11-06 23:33:18 -08:00
|
|
|
{100, govsim.SimulateMsgDeposit(app.govKeeper)},
|
2019-01-11 12:08:01 -08:00
|
|
|
{100, stakingsim.SimulateMsgCreateValidator(app.accountKeeper, app.stakingKeeper)},
|
|
|
|
{5, stakingsim.SimulateMsgEditValidator(app.stakingKeeper)},
|
|
|
|
{100, stakingsim.SimulateMsgDelegate(app.accountKeeper, app.stakingKeeper)},
|
2019-01-17 09:53:22 -08:00
|
|
|
{100, stakingsim.SimulateMsgUndelegate(app.accountKeeper, app.stakingKeeper)},
|
2019-01-11 12:08:01 -08:00
|
|
|
{100, stakingsim.SimulateMsgBeginRedelegate(app.accountKeeper, app.stakingKeeper)},
|
2018-09-12 00:16:52 -07:00
|
|
|
{100, slashingsim.SimulateMsgUnjail(app.slashingKeeper)},
|
2018-08-17 07:19:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 15:01:50 -08:00
|
|
|
func invariants(app *GaiaApp) []sdk.Invariant {
|
2019-04-09 18:34:38 -07:00
|
|
|
return simulation.PeriodicInvariants(app.crisisKeeper.Invariants(), period, 0)
|
2018-08-17 07:19:33 -07:00
|
|
|
}
|
|
|
|
|
2018-11-27 00:14:22 -08:00
|
|
|
// Pass this in as an option to use a dbStoreAdapter instead of an IAVLStore for simulation speed.
|
|
|
|
func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
|
|
|
|
bapp.SetFauxMerkleMode()
|
|
|
|
}
|
|
|
|
|
2018-08-29 23:02:15 -07:00
|
|
|
// Profile with:
|
2018-09-01 19:04:44 -07:00
|
|
|
// /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/cosmos-sdk/cmd/gaia/app -bench ^BenchmarkFullGaiaSimulation$ -SimulationCommit=true -cpuprofile cpu.out
|
2018-08-29 23:02:15 -07:00
|
|
|
func BenchmarkFullGaiaSimulation(b *testing.B) {
|
|
|
|
// Setup Gaia application
|
2019-02-08 12:45:23 -08:00
|
|
|
logger := log.NewNopLogger()
|
|
|
|
|
2018-08-30 11:43:56 -07:00
|
|
|
var db dbm.DB
|
2018-10-29 16:10:39 -07:00
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ = sdk.NewLevelDB("Simulation", dir)
|
2018-09-01 12:32:24 -07:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-04-10 16:33:50 -07:00
|
|
|
app := NewGaiaApp(logger, db, nil, true, 0)
|
2018-08-29 23:02:15 -07:00
|
|
|
|
|
|
|
// Run randomized simulation
|
|
|
|
// TODO parameterize numbers, save for a later PR
|
2019-04-12 15:52:16 -07:00
|
|
|
_, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(b, os.Stdout, app))
|
2018-09-11 02:18:58 -07:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
2018-09-01 19:04:44 -07:00
|
|
|
if commit {
|
|
|
|
fmt.Println("GoLevelDB Stats")
|
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
|
|
|
}
|
2018-08-29 23:02:15 -07:00
|
|
|
}
|
|
|
|
|
2018-07-17 15:04:10 -07:00
|
|
|
func TestFullGaiaSimulation(t *testing.T) {
|
2018-07-18 23:40:46 -07:00
|
|
|
if !enabled {
|
2018-07-17 15:04:10 -07:00
|
|
|
t.Skip("Skipping Gaia simulation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Gaia application
|
2018-08-16 08:36:15 -07:00
|
|
|
var logger log.Logger
|
|
|
|
if verbose {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2018-10-29 16:10:39 -07:00
|
|
|
var db dbm.DB
|
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ = sdk.NewLevelDB("Simulation", dir)
|
2018-10-29 16:10:39 -07:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-04-10 16:33:50 -07:00
|
|
|
app := NewGaiaApp(logger, db, nil, true, 0, fauxMerkleModeOpt)
|
2018-07-17 15:04:10 -07:00
|
|
|
require.Equal(t, "GaiaApp", app.Name())
|
|
|
|
|
2018-07-16 18:15:50 -07:00
|
|
|
// Run randomized simulation
|
2019-04-12 15:52:16 -07:00
|
|
|
_, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app))
|
2018-09-01 19:04:44 -07:00
|
|
|
if commit {
|
2018-10-29 16:10:39 -07:00
|
|
|
// for memdb:
|
|
|
|
// fmt.Println("Database Size", db.Stats()["database.size"])
|
|
|
|
fmt.Println("GoLevelDB Stats")
|
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
2018-09-01 19:04:44 -07:00
|
|
|
}
|
2018-09-11 02:18:58 -07:00
|
|
|
require.Nil(t, err)
|
2018-07-11 15:14:37 -07:00
|
|
|
}
|
2018-08-16 08:36:15 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
func TestGaiaImportExport(t *testing.T) {
|
|
|
|
if !enabled {
|
|
|
|
t.Skip("Skipping Gaia import/export simulation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Gaia application
|
|
|
|
var logger log.Logger
|
|
|
|
if verbose {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
|
|
|
var db dbm.DB
|
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ = sdk.NewLevelDB("Simulation", dir)
|
2018-11-08 16:28:28 -08:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-04-10 16:33:50 -07:00
|
|
|
app := NewGaiaApp(logger, db, nil, true, 0, fauxMerkleModeOpt)
|
2018-11-08 16:28:28 -08:00
|
|
|
require.Equal(t, "GaiaApp", app.Name())
|
|
|
|
|
|
|
|
// Run randomized simulation
|
2019-04-12 15:52:16 -07:00
|
|
|
_, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app))
|
2019-03-14 11:13:15 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
if commit {
|
|
|
|
// for memdb:
|
|
|
|
// fmt.Println("Database Size", db.Stats()["database.size"])
|
|
|
|
fmt.Println("GoLevelDB Stats")
|
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
|
|
|
}
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
fmt.Printf("Exporting genesis...\n")
|
|
|
|
|
2019-02-04 08:42:48 -08:00
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, []string{})
|
2019-02-08 13:45:41 -08:00
|
|
|
require.NoError(t, err)
|
2018-11-08 16:28:28 -08:00
|
|
|
fmt.Printf("Importing genesis...\n")
|
|
|
|
|
|
|
|
newDir, _ := ioutil.TempDir("", "goleveldb-gaia-sim-2")
|
2019-03-04 12:31:55 -08:00
|
|
|
newDB, _ := sdk.NewLevelDB("Simulation-2", dir)
|
2018-11-08 16:28:28 -08:00
|
|
|
defer func() {
|
|
|
|
newDB.Close()
|
|
|
|
os.RemoveAll(newDir)
|
|
|
|
}()
|
2019-04-10 16:33:50 -07:00
|
|
|
newApp := NewGaiaApp(log.NewNopLogger(), newDB, nil, true, 0, fauxMerkleModeOpt)
|
2018-11-08 16:28:28 -08:00
|
|
|
require.Equal(t, "GaiaApp", newApp.Name())
|
2018-11-26 04:21:23 -08:00
|
|
|
var genesisState GenesisState
|
|
|
|
err = app.cdc.UnmarshalJSON(appState, &genesisState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
2018-11-26 04:21:23 -08:00
|
|
|
ctxB := newApp.NewContext(true, abci.Header{})
|
2019-05-16 08:25:32 -07:00
|
|
|
newApp.mm.InitGenesis(ctxB, genesisState)
|
2018-11-08 16:28:28 -08:00
|
|
|
|
|
|
|
fmt.Printf("Comparing stores...\n")
|
|
|
|
ctxA := app.NewContext(true, abci.Header{})
|
|
|
|
type StoreKeysPrefixes struct {
|
|
|
|
A sdk.StoreKey
|
|
|
|
B sdk.StoreKey
|
|
|
|
Prefixes [][]byte
|
|
|
|
}
|
|
|
|
storeKeysPrefixes := []StoreKeysPrefixes{
|
|
|
|
{app.keyMain, newApp.keyMain, [][]byte{}},
|
|
|
|
{app.keyAccount, newApp.keyAccount, [][]byte{}},
|
2019-03-14 11:13:15 -07:00
|
|
|
{app.keyStaking, newApp.keyStaking, [][]byte{staking.UnbondingQueueKey,
|
|
|
|
staking.RedelegationQueueKey, staking.ValidatorQueueKey}}, // ordering may change but it doesn't matter
|
2018-11-08 16:28:28 -08:00
|
|
|
{app.keySlashing, newApp.keySlashing, [][]byte{}},
|
|
|
|
{app.keyMint, newApp.keyMint, [][]byte{}},
|
|
|
|
{app.keyDistr, newApp.keyDistr, [][]byte{}},
|
|
|
|
{app.keyFeeCollection, newApp.keyFeeCollection, [][]byte{}},
|
|
|
|
{app.keyParams, newApp.keyParams, [][]byte{}},
|
|
|
|
{app.keyGov, newApp.keyGov, [][]byte{}},
|
|
|
|
}
|
|
|
|
for _, storeKeysPrefix := range storeKeysPrefixes {
|
|
|
|
storeKeyA := storeKeysPrefix.A
|
|
|
|
storeKeyB := storeKeysPrefix.B
|
|
|
|
prefixes := storeKeysPrefix.Prefixes
|
|
|
|
storeA := ctxA.KVStore(storeKeyA)
|
|
|
|
storeB := ctxB.KVStore(storeKeyB)
|
|
|
|
kvA, kvB, count, equal := sdk.DiffKVStores(storeA, storeB, prefixes)
|
|
|
|
fmt.Printf("Compared %d key/value pairs between %s and %s\n", count, storeKeyA, storeKeyB)
|
2019-01-24 13:01:32 -08:00
|
|
|
require.True(t, equal,
|
|
|
|
"unequal stores: %s / %s:\nstore A %X => %X\nstore B %X => %X",
|
|
|
|
storeKeyA, storeKeyB, kvA.Key, kvA.Value, kvB.Key, kvB.Value,
|
|
|
|
)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
func TestGaiaSimulationAfterImport(t *testing.T) {
|
|
|
|
if !enabled {
|
|
|
|
t.Skip("Skipping Gaia simulation after import")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Gaia application
|
|
|
|
var logger log.Logger
|
|
|
|
if verbose {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ := sdk.NewLevelDB("Simulation", dir)
|
2018-11-26 04:21:23 -08:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-04-10 16:33:50 -07:00
|
|
|
app := NewGaiaApp(logger, db, nil, true, 0, fauxMerkleModeOpt)
|
2018-11-26 04:21:23 -08:00
|
|
|
require.Equal(t, "GaiaApp", app.Name())
|
|
|
|
|
|
|
|
// Run randomized simulation
|
2019-04-12 15:52:16 -07:00
|
|
|
stopEarly, err := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app))
|
2019-03-14 11:13:15 -07:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
if commit {
|
|
|
|
// for memdb:
|
|
|
|
// fmt.Println("Database Size", db.Stats()["database.size"])
|
|
|
|
fmt.Println("GoLevelDB Stats")
|
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
|
|
|
}
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
if stopEarly {
|
|
|
|
// we can't export or import a zero-validator genesis
|
|
|
|
fmt.Printf("We can't export or import a zero-validator genesis, exiting test...\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Exporting genesis...\n")
|
|
|
|
|
2019-02-04 08:42:48 -08:00
|
|
|
appState, _, err := app.ExportAppStateAndValidators(true, []string{})
|
2018-11-26 04:21:23 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Importing genesis...\n")
|
|
|
|
|
|
|
|
newDir, _ := ioutil.TempDir("", "goleveldb-gaia-sim-2")
|
2019-03-04 12:31:55 -08:00
|
|
|
newDB, _ := sdk.NewLevelDB("Simulation-2", dir)
|
2018-11-26 04:21:23 -08:00
|
|
|
defer func() {
|
|
|
|
newDB.Close()
|
|
|
|
os.RemoveAll(newDir)
|
|
|
|
}()
|
2019-04-10 16:33:50 -07:00
|
|
|
newApp := NewGaiaApp(log.NewNopLogger(), newDB, nil, true, 0, fauxMerkleModeOpt)
|
2018-11-26 04:21:23 -08:00
|
|
|
require.Equal(t, "GaiaApp", newApp.Name())
|
|
|
|
newApp.InitChain(abci.RequestInitChain{
|
|
|
|
AppStateBytes: appState,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Run randomized simulation on imported app
|
2019-04-12 15:52:16 -07:00
|
|
|
_, err = simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, newApp))
|
2018-11-26 04:21:23 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-08-16 14:45:07 -07:00
|
|
|
// TODO: Make another test for the fuzzer itself, which just has noOp txs
|
|
|
|
// and doesn't depend on gaia
|
2018-08-16 08:36:15 -07:00
|
|
|
func TestAppStateDeterminism(t *testing.T) {
|
2018-08-17 07:19:33 -07:00
|
|
|
if !enabled {
|
|
|
|
t.Skip("Skipping Gaia simulation")
|
2018-08-16 08:36:15 -07:00
|
|
|
}
|
2018-08-17 07:19:33 -07:00
|
|
|
|
2018-09-03 19:13:08 -07:00
|
|
|
numSeeds := 3
|
2018-08-17 07:19:33 -07:00
|
|
|
numTimesToRunPerSeed := 5
|
|
|
|
appHashList := make([]json.RawMessage, numTimesToRunPerSeed)
|
|
|
|
|
|
|
|
for i := 0; i < numSeeds; i++ {
|
|
|
|
seed := rand.Int63()
|
|
|
|
for j := 0; j < numTimesToRunPerSeed; j++ {
|
|
|
|
logger := log.NewNopLogger()
|
|
|
|
db := dbm.NewMemDB()
|
2019-04-10 16:33:50 -07:00
|
|
|
app := NewGaiaApp(logger, db, nil, true, 0)
|
2018-08-17 07:19:33 -07:00
|
|
|
|
|
|
|
// Run randomized simulation
|
|
|
|
simulation.SimulateFromSeed(
|
2019-04-12 15:52:16 -07:00
|
|
|
t, os.Stdout, app.BaseApp, appStateFn, seed,
|
2018-08-17 07:19:33 -07:00
|
|
|
testAndRunTxs(app),
|
2019-02-13 15:01:50 -08:00
|
|
|
[]sdk.Invariant{},
|
2018-09-03 19:13:08 -07:00
|
|
|
50,
|
|
|
|
100,
|
2018-09-12 21:53:55 -07:00
|
|
|
true,
|
2019-03-14 11:13:15 -07:00
|
|
|
false,
|
2018-08-17 07:19:33 -07:00
|
|
|
)
|
|
|
|
appHash := app.LastCommitID().Hash
|
|
|
|
appHashList[j] = appHash
|
|
|
|
}
|
|
|
|
for k := 1; k < numTimesToRunPerSeed; k++ {
|
2018-08-27 14:27:00 -07:00
|
|
|
require.Equal(t, appHashList[0], appHashList[k], "appHash list: %v", appHashList)
|
2018-08-17 07:19:33 -07:00
|
|
|
}
|
2018-08-16 08:36:15 -07:00
|
|
|
}
|
|
|
|
}
|
2019-04-12 15:52:16 -07:00
|
|
|
|
|
|
|
func BenchmarkInvariants(b *testing.B) {
|
|
|
|
// 1. Setup a simulated Gaia application
|
|
|
|
logger := log.NewNopLogger()
|
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-gaia-invariant-bench")
|
|
|
|
db, _ := sdk.NewLevelDB("simulation", dir)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
2019-04-13 02:56:45 -07:00
|
|
|
app := NewGaiaApp(logger, db, nil, true, 0)
|
2019-04-12 15:52:16 -07:00
|
|
|
|
|
|
|
// 2. Run parameterized simulation (w/o invariants)
|
|
|
|
_, err := simulation.SimulateFromSeed(
|
|
|
|
b, ioutil.Discard, app.BaseApp, appStateFn, seed, testAndRunTxs(app),
|
|
|
|
[]sdk.Invariant{}, numBlocks, blockSize, commit, lean,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight() + 1})
|
|
|
|
|
|
|
|
// 3. Benchmark each invariant separately
|
|
|
|
//
|
|
|
|
// NOTE: We use the crisis keeper as it has all the invariants registered with
|
|
|
|
// their respective metadata which makes it useful for testing/benchmarking.
|
|
|
|
for _, cr := range app.crisisKeeper.Routes() {
|
|
|
|
b.Run(fmt.Sprintf("%s/%s", cr.ModuleName, cr.Route), func(b *testing.B) {
|
|
|
|
if err := cr.Invar(ctx); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|