2019-05-18 01:42:24 -07:00
|
|
|
package simapp
|
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"
|
2018-07-12 13:01:43 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
2019-08-02 06:20:39 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
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"
|
2019-07-31 06:59:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
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"
|
2019-07-31 06:59:16 -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"
|
2019-07-31 06:59:16 -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"
|
2019-07-31 06:59:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mint"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
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"
|
2019-07-31 06:59:16 -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"
|
2019-07-31 06:59:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2018-07-11 15:14:37 -07:00
|
|
|
)
|
|
|
|
|
2018-07-18 23:40:46 -07:00
|
|
|
func init() {
|
2019-07-19 09:59:04 -07:00
|
|
|
flag.StringVar(&genesisFile, "Genesis", "", "custom simulation genesis file; cannot be used with params file")
|
|
|
|
flag.StringVar(¶msFile, "Params", "", "custom simulation params file which overrides any random params; cannot be used with genesis")
|
|
|
|
flag.StringVar(&exportParamsPath, "ExportParamsPath", "", "custom file path to save the exported params JSON")
|
|
|
|
flag.IntVar(&exportParamsHeight, "ExportParamsHeight", 0, "height to which export the randomly generated params")
|
|
|
|
flag.StringVar(&exportStatePath, "ExportStatePath", "", "custom file path to save the exported app state JSON")
|
2019-07-26 13:09:15 -07:00
|
|
|
flag.StringVar(&exportStatsPath, "ExportStatsPath", "", "custom file path to save the exported simulation statistics JSON")
|
2019-07-19 09:59:04 -07:00
|
|
|
flag.Int64Var(&seed, "Seed", 42, "simulation random seed")
|
2019-08-02 08:28:50 -07:00
|
|
|
flag.IntVar(&initialBlockHeight, "InitialBlockHeight", 1, "initial block to start the simulation")
|
|
|
|
flag.IntVar(&numBlocks, "NumBlocks", 500, "number of new blocks to simulate from the initial block height")
|
2019-07-19 09:59:04 -07:00
|
|
|
flag.IntVar(&blockSize, "BlockSize", 200, "operations per block")
|
|
|
|
flag.BoolVar(&enabled, "Enabled", false, "enable the simulation")
|
|
|
|
flag.BoolVar(&verbose, "Verbose", false, "verbose log output")
|
|
|
|
flag.BoolVar(&lean, "Lean", false, "lean simulation log output")
|
|
|
|
flag.BoolVar(&commit, "Commit", false, "have the simulation commit")
|
|
|
|
flag.IntVar(&period, "Period", 1, "run slow invariants only once every period assertions")
|
2019-05-24 18:23:53 -07:00
|
|
|
flag.BoolVar(&onOperation, "SimulateEveryOperation", false, "run slow invariants every operation")
|
2019-07-11 03:56:43 -07:00
|
|
|
flag.BoolVar(&allInvariants, "PrintAllInvariants", false, "print all invariants if a broken invariant is found")
|
2019-07-19 14:46:06 -07:00
|
|
|
flag.Int64Var(&genesisTime, "GenesisTime", 0, "override genesis UNIX time instead of using a random UNIX time")
|
2019-03-14 11:13:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function for populating input for SimulateFromSeed
|
2019-08-02 08:28:50 -07:00
|
|
|
// TODO: clean up this function along with the simulation refactor
|
2019-05-18 01:42:24 -07:00
|
|
|
func getSimulateFromSeedInput(tb testing.TB, w io.Writer, app *SimApp) (
|
2019-04-12 15:52:16 -07:00
|
|
|
testing.TB, io.Writer, *baseapp.BaseApp, simulation.AppStateFn, int64,
|
2019-08-02 08:28:50 -07:00
|
|
|
simulation.WeightedOperations, sdk.Invariants, int, int, int, int, string,
|
2019-07-19 09:59:04 -07:00
|
|
|
bool, bool, bool, bool, bool, map[string]bool) {
|
|
|
|
|
|
|
|
exportParams := exportParamsPath != ""
|
2019-03-14 11:13:15 -07:00
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
return tb, w, app.BaseApp, appStateFn, seed,
|
2019-07-19 09:59:04 -07:00
|
|
|
testAndRunTxs(app), invariants(app),
|
2019-08-02 08:28:50 -07:00
|
|
|
initialBlockHeight, numBlocks, exportParamsHeight, blockSize,
|
2019-07-26 13:09:15 -07:00
|
|
|
exportStatsPath, exportParams, commit, lean, onOperation, allInvariants, app.ModuleAccountAddrs()
|
2018-07-18 23:40:46 -07:00
|
|
|
}
|
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
func appStateFn(
|
2019-07-19 14:46:06 -07:00
|
|
|
r *rand.Rand, accs []simulation.Account,
|
|
|
|
) (appState json.RawMessage, simAccs []simulation.Account, chainID string, genesisTimestamp time.Time) {
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-01-29 14:53:42 -08:00
|
|
|
cdc := MakeCodec()
|
2019-06-08 13:55:47 -07:00
|
|
|
|
2019-07-19 14:46:06 -07:00
|
|
|
if genesisTime == 0 {
|
|
|
|
genesisTimestamp = simulation.RandTimestamp(r)
|
|
|
|
} else {
|
|
|
|
genesisTimestamp = time.Unix(genesisTime, 0)
|
|
|
|
}
|
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
switch {
|
|
|
|
case paramsFile != "" && genesisFile != "":
|
|
|
|
panic("cannot provide both a genesis file and a params file")
|
2019-06-08 13:55:47 -07:00
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
case genesisFile != "":
|
|
|
|
appState, simAccs, chainID = AppStateFromGenesisFileFn(r, accs, genesisTimestamp)
|
2019-06-08 13:55:47 -07:00
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
case paramsFile != "":
|
|
|
|
appParams := make(simulation.AppParams)
|
|
|
|
bz, err := ioutil.ReadFile(paramsFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
cdc.MustUnmarshalJSON(bz, &appParams)
|
|
|
|
appState, simAccs, chainID = appStateRandomizedFn(r, accs, genesisTimestamp, appParams)
|
2019-06-08 13:55:47 -07:00
|
|
|
|
2019-07-03 11:25:52 -07:00
|
|
|
default:
|
|
|
|
appParams := make(simulation.AppParams)
|
|
|
|
appState, simAccs, chainID = appStateRandomizedFn(r, accs, genesisTimestamp, appParams)
|
2019-01-29 14:53:42 -08:00
|
|
|
}
|
2019-06-08 13:55:47 -07:00
|
|
|
|
2019-07-19 14:46:06 -07:00
|
|
|
return appState, simAccs, chainID, genesisTimestamp
|
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
|
2019-06-08 13:55:47 -07:00
|
|
|
func appStateRandomizedFn(
|
|
|
|
r *rand.Rand, accs []simulation.Account, genesisTimestamp time.Time, appParams simulation.AppParams,
|
2019-05-16 08:25:32 -07:00
|
|
|
) (json.RawMessage, []simulation.Account, string) {
|
2019-01-29 14:53:42 -08:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
cdc := MakeCodec()
|
2019-06-08 13:55:47 -07:00
|
|
|
genesisState := NewDefaultGenesisState()
|
|
|
|
|
|
|
|
var (
|
|
|
|
amount int64
|
|
|
|
numInitiallyBonded int64
|
|
|
|
)
|
|
|
|
|
|
|
|
appParams.GetOrGenerate(cdc, StakePerAccount, &amount, r,
|
|
|
|
func(r *rand.Rand) { amount = int64(r.Intn(1e12)) })
|
|
|
|
appParams.GetOrGenerate(cdc, InitiallyBondedValidators, &amount, r,
|
|
|
|
func(r *rand.Rand) { numInitiallyBonded = int64(r.Intn(250)) })
|
2018-07-17 11:33:53 -07:00
|
|
|
|
2018-11-04 20:44:43 -08:00
|
|
|
numAccs := int64(len(accs))
|
|
|
|
if numInitiallyBonded > numAccs {
|
|
|
|
numInitiallyBonded = numAccs
|
|
|
|
}
|
2019-06-08 13:55:47 -07:00
|
|
|
|
|
|
|
fmt.Printf(
|
|
|
|
`Selected randomly generated parameters for simulated genesis:
|
|
|
|
{
|
|
|
|
stake_per_account: "%v",
|
|
|
|
initially_bonded_validators: "%v"
|
|
|
|
}
|
|
|
|
`, amount, numInitiallyBonded,
|
|
|
|
)
|
|
|
|
|
2019-07-03 09:12:43 -07:00
|
|
|
GenGenesisAccounts(cdc, r, accs, genesisTimestamp, amount, numInitiallyBonded, genesisState)
|
|
|
|
GenAuthGenesisState(cdc, r, appParams, genesisState)
|
|
|
|
GenBankGenesisState(cdc, r, appParams, genesisState)
|
|
|
|
GenSupplyGenesisState(cdc, amount, numInitiallyBonded, int64(len(accs)), genesisState)
|
|
|
|
GenGovGenesisState(cdc, r, appParams, genesisState)
|
|
|
|
GenMintGenesisState(cdc, r, appParams, genesisState)
|
|
|
|
GenDistrGenesisState(cdc, r, appParams, genesisState)
|
|
|
|
stakingGen := GenStakingGenesisState(cdc, r, accs, amount, numAccs, numInitiallyBonded, appParams, genesisState)
|
|
|
|
GenSlashingGenesisState(cdc, r, stakingGen, appParams, genesisState)
|
2019-06-08 13:55:47 -07:00
|
|
|
|
|
|
|
appState, err := MakeCodec().MarshalJSON(genesisState)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return appState, accs, "simulation"
|
|
|
|
}
|
|
|
|
|
2019-07-19 09:59:04 -07:00
|
|
|
// TODO: add description
|
2019-06-08 13:55:47 -07:00
|
|
|
func testAndRunTxs(app *SimApp) []simulation.WeightedOperation {
|
|
|
|
cdc := MakeCodec()
|
|
|
|
ap := make(simulation.AppParams)
|
2019-05-16 08:25:32 -07:00
|
|
|
|
2019-06-08 13:55:47 -07:00
|
|
|
if paramsFile != "" {
|
|
|
|
bz, err := ioutil.ReadFile(paramsFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cdc.MustUnmarshalJSON(bz, &ap)
|
2019-01-29 14:53:42 -08:00
|
|
|
}
|
2018-07-17 15:04:10 -07:00
|
|
|
|
2018-09-12 00:16:52 -07:00
|
|
|
return []simulation.WeightedOperation{
|
2019-06-08 13:55:47 -07:00
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightDeductFee, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 5
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
2019-06-28 13:11:27 -07:00
|
|
|
authsim.SimulateDeductFee(app.accountKeeper, app.supplyKeeper),
|
2019-06-08 13:55:47 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgSend, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
2019-06-14 07:10:37 -07:00
|
|
|
bank.SimulateMsgSend(app.accountKeeper, app.bankKeeper),
|
2019-06-08 13:55:47 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightSingleInputMsgMultiSend, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 10
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
2019-06-14 07:10:37 -07:00
|
|
|
bank.SimulateSingleInputMsgMultiSend(app.accountKeeper, app.bankKeeper),
|
2019-06-08 13:55:47 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgSetWithdrawAddress, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 50
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
distrsim.SimulateMsgSetWithdrawAddress(app.accountKeeper, app.distrKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgWithdrawDelegationReward, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 50
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
distrsim.SimulateMsgWithdrawDelegatorReward(app.accountKeeper, app.distrKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgWithdrawValidatorCommission, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 50
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
distrsim.SimulateMsgWithdrawValidatorCommission(app.accountKeeper, app.distrKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightSubmitVotingSlashingTextProposal, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 5
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, govsim.SimulateTextProposalContent),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightSubmitVotingSlashingCommunitySpendProposal, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 5
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, distrsim.SimulateCommunityPoolSpendProposalContent(app.distrKeeper)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightSubmitVotingSlashingParamChangeProposal, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 5
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, paramsim.SimulateParamChangeProposalContent),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgDeposit, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
govsim.SimulateMsgDeposit(app.govKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgCreateValidator, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
stakingsim.SimulateMsgCreateValidator(app.accountKeeper, app.stakingKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgEditValidator, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 5
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
stakingsim.SimulateMsgEditValidator(app.stakingKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgDelegate, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
stakingsim.SimulateMsgDelegate(app.accountKeeper, app.stakingKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgUndelegate, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
stakingsim.SimulateMsgUndelegate(app.accountKeeper, app.stakingKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgBeginRedelegate, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
stakingsim.SimulateMsgBeginRedelegate(app.accountKeeper, app.stakingKeeper),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
func(_ *rand.Rand) int {
|
|
|
|
var v int
|
|
|
|
ap.GetOrGenerate(cdc, OpWeightMsgUnjail, &v, nil,
|
|
|
|
func(_ *rand.Rand) {
|
|
|
|
v = 100
|
|
|
|
})
|
|
|
|
return v
|
|
|
|
}(nil),
|
|
|
|
slashingsim.SimulateMsgUnjail(app.slashingKeeper),
|
|
|
|
},
|
2018-08-17 07:19:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
func invariants(app *SimApp) []sdk.Invariant {
|
2019-06-28 13:11:27 -07:00
|
|
|
// TODO: fix PeriodicInvariants, it doesn't seem to call individual invariants for a period of 1
|
|
|
|
// Ref: https://github.com/cosmos/cosmos-sdk/issues/4631
|
|
|
|
if period == 1 {
|
|
|
|
return app.crisisKeeper.Invariants()
|
|
|
|
}
|
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:
|
2019-07-19 09:59:04 -07:00
|
|
|
// /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/cosmos-sdk/simapp -bench ^BenchmarkFullAppSimulation$ -Commit=true -cpuprofile cpu.out
|
2019-05-18 01:42:24 -07:00
|
|
|
func BenchmarkFullAppSimulation(b *testing.B) {
|
2019-02-08 12:45:23 -08:00
|
|
|
logger := log.NewNopLogger()
|
|
|
|
|
2018-08-30 11:43:56 -07:00
|
|
|
var db dbm.DB
|
2019-05-18 01:42:24 -07:00
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-app-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-05-18 01:42:24 -07:00
|
|
|
app := NewSimApp(logger, db, nil, true, 0)
|
2018-08-29 23:02:15 -07:00
|
|
|
|
|
|
|
// Run randomized simulation
|
2019-07-19 09:59:04 -07:00
|
|
|
// TODO: parameterize numbers, save for a later PR
|
|
|
|
_, params, simErr := simulation.SimulateFromSeed(getSimulateFromSeedInput(b, os.Stdout, app))
|
|
|
|
|
|
|
|
// export state and params before the simulation error is checked
|
|
|
|
if exportStatePath != "" {
|
|
|
|
fmt.Println("Exporting app state...")
|
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(exportStatePath, []byte(appState), 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if exportParamsPath != "" {
|
|
|
|
fmt.Println("Exporting simulation params...")
|
|
|
|
paramsBz, err := json.MarshalIndent(params, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportParamsPath, paramsBz, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if simErr != nil {
|
|
|
|
fmt.Println(simErr)
|
|
|
|
b.FailNow()
|
2018-09-11 02:18:58 -07:00
|
|
|
}
|
2019-07-19 09:59:04 -07:00
|
|
|
|
2018-09-01 19:04:44 -07:00
|
|
|
if commit {
|
2019-07-26 13:09:15 -07:00
|
|
|
fmt.Println("\nGoLevelDB Stats")
|
2018-09-01 19:04:44 -07:00
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
|
|
|
}
|
2018-08-29 23:02:15 -07:00
|
|
|
}
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
func TestFullAppSimulation(t *testing.T) {
|
2018-07-18 23:40:46 -07:00
|
|
|
if !enabled {
|
2019-05-18 01:42:24 -07:00
|
|
|
t.Skip("Skipping application simulation")
|
2018-07-17 15:04:10 -07:00
|
|
|
}
|
|
|
|
|
2018-08-16 08:36:15 -07:00
|
|
|
var logger log.Logger
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-08-16 08:36:15 -07:00
|
|
|
if verbose {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-10-29 16:10:39 -07:00
|
|
|
var db dbm.DB
|
2019-05-18 01:42:24 -07:00
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-app-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ = sdk.NewLevelDB("Simulation", dir)
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-10-29 16:10:39 -07:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-05-18 01:42:24 -07:00
|
|
|
|
|
|
|
app := NewSimApp(logger, db, nil, true, 0, fauxMerkleModeOpt)
|
|
|
|
require.Equal(t, "SimApp", app.Name())
|
2018-07-17 15:04:10 -07:00
|
|
|
|
2018-07-16 18:15:50 -07:00
|
|
|
// Run randomized simulation
|
2019-07-19 09:59:04 -07:00
|
|
|
_, params, simErr := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app))
|
|
|
|
|
|
|
|
// export state and params before the simulation error is checked
|
|
|
|
if exportStatePath != "" {
|
|
|
|
fmt.Println("Exporting app state...")
|
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportStatePath, []byte(appState), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exportParamsPath != "" {
|
|
|
|
fmt.Println("Exporting simulation params...")
|
|
|
|
fmt.Println(params)
|
|
|
|
paramsBz, err := json.MarshalIndent(params, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportParamsPath, paramsBz, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, simErr)
|
|
|
|
|
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"])
|
2019-07-26 13:09:15 -07:00
|
|
|
fmt.Println("\nGoLevelDB Stats")
|
2018-10-29 16:10:39 -07:00
|
|
|
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-07-11 15:14:37 -07:00
|
|
|
}
|
2018-08-16 08:36:15 -07:00
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
func TestAppImportExport(t *testing.T) {
|
2018-11-08 16:28:28 -08:00
|
|
|
if !enabled {
|
2019-05-18 01:42:24 -07:00
|
|
|
t.Skip("Skipping application import/export simulation")
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var logger log.Logger
|
|
|
|
if verbose {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
var db dbm.DB
|
2019-05-18 01:42:24 -07:00
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-app-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ = sdk.NewLevelDB("Simulation", dir)
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-05-18 01:42:24 -07:00
|
|
|
|
|
|
|
app := NewSimApp(logger, db, nil, true, 0, fauxMerkleModeOpt)
|
|
|
|
require.Equal(t, "SimApp", app.Name())
|
2018-11-08 16:28:28 -08:00
|
|
|
|
|
|
|
// Run randomized simulation
|
2019-07-31 06:59:16 -07:00
|
|
|
_, simParams, simErr := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app))
|
2019-07-19 09:59:04 -07:00
|
|
|
|
2019-07-31 06:59:16 -07:00
|
|
|
// export state and simParams before the simulation error is checked
|
2019-07-19 09:59:04 -07:00
|
|
|
if exportStatePath != "" {
|
|
|
|
fmt.Println("Exporting app state...")
|
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportStatePath, []byte(appState), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exportParamsPath != "" {
|
|
|
|
fmt.Println("Exporting simulation params...")
|
2019-07-31 06:59:16 -07:00
|
|
|
simParamsBz, err := json.MarshalIndent(simParams, "", " ")
|
2019-07-19 09:59:04 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-31 06:59:16 -07:00
|
|
|
err = ioutil.WriteFile(exportParamsPath, simParamsBz, 0644)
|
2019-07-19 09:59:04 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, simErr)
|
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"])
|
2019-07-26 13:09:15 -07:00
|
|
|
fmt.Println("\nGoLevelDB Stats")
|
2018-11-08 16:28:28 -08:00
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
newDir, _ := ioutil.TempDir("", "goleveldb-app-sim-2")
|
2019-03-04 12:31:55 -08:00
|
|
|
newDB, _ := sdk.NewLevelDB("Simulation-2", dir)
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
defer func() {
|
|
|
|
newDB.Close()
|
2019-08-06 12:11:55 -07:00
|
|
|
_ = os.RemoveAll(newDir)
|
2018-11-08 16:28:28 -08:00
|
|
|
}()
|
2019-05-18 01:42:24 -07:00
|
|
|
|
|
|
|
newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, 0, fauxMerkleModeOpt)
|
|
|
|
require.Equal(t, "SimApp", 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
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2019-08-06 12:11:55 -07:00
|
|
|
ctxB := newApp.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
|
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")
|
2019-08-06 12:11:55 -07:00
|
|
|
ctxA := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
type StoreKeysPrefixes struct {
|
|
|
|
A sdk.StoreKey
|
|
|
|
B sdk.StoreKey
|
|
|
|
Prefixes [][]byte
|
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
storeKeysPrefixes := []StoreKeysPrefixes{
|
2019-07-31 06:59:16 -07:00
|
|
|
{app.keys[baseapp.MainStoreKey], newApp.keys[baseapp.MainStoreKey], [][]byte{}},
|
|
|
|
{app.keys[auth.StoreKey], newApp.keys[auth.StoreKey], [][]byte{}},
|
|
|
|
{app.keys[staking.StoreKey], newApp.keys[staking.StoreKey],
|
|
|
|
[][]byte{
|
|
|
|
staking.UnbondingQueueKey, staking.RedelegationQueueKey, staking.ValidatorQueueKey,
|
|
|
|
}}, // ordering may change but it doesn't matter
|
|
|
|
{app.keys[slashing.StoreKey], newApp.keys[slashing.StoreKey], [][]byte{}},
|
|
|
|
{app.keys[mint.StoreKey], newApp.keys[mint.StoreKey], [][]byte{}},
|
|
|
|
{app.keys[distr.StoreKey], newApp.keys[distr.StoreKey], [][]byte{}},
|
|
|
|
{app.keys[supply.StoreKey], newApp.keys[supply.StoreKey], [][]byte{}},
|
|
|
|
{app.keys[params.StoreKey], newApp.keys[params.StoreKey], [][]byte{}},
|
|
|
|
{app.keys[gov.StoreKey], newApp.keys[gov.StoreKey], [][]byte{}},
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, storeKeysPrefix := range storeKeysPrefixes {
|
|
|
|
storeKeyA := storeKeysPrefix.A
|
|
|
|
storeKeyB := storeKeysPrefix.B
|
|
|
|
prefixes := storeKeysPrefix.Prefixes
|
|
|
|
storeA := ctxA.KVStore(storeKeyA)
|
|
|
|
storeB := ctxB.KVStore(storeKeyB)
|
2019-07-19 07:05:37 -07:00
|
|
|
failedKVs := sdk.DiffKVStores(storeA, storeB, prefixes)
|
|
|
|
fmt.Printf("Compared %d key/value pairs between %s and %s\n", len(failedKVs)/2, storeKeyA, storeKeyB)
|
|
|
|
require.Len(t, failedKVs, 0, GetSimulationLog(storeKeyA.Name(), app.cdc, newApp.cdc, failedKVs))
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
func TestAppSimulationAfterImport(t *testing.T) {
|
2018-11-26 04:21:23 -08:00
|
|
|
if !enabled {
|
2019-05-18 01:42:24 -07:00
|
|
|
t.Skip("Skipping application simulation after import")
|
2018-11-26 04:21:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var logger log.Logger
|
|
|
|
if verbose {
|
|
|
|
logger = log.TestingLogger()
|
|
|
|
} else {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-app-sim")
|
2019-03-04 12:31:55 -08:00
|
|
|
db, _ := sdk.NewLevelDB("Simulation", dir)
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
2019-05-18 01:42:24 -07:00
|
|
|
|
|
|
|
app := NewSimApp(logger, db, nil, true, 0, fauxMerkleModeOpt)
|
|
|
|
require.Equal(t, "SimApp", app.Name())
|
2018-11-26 04:21:23 -08:00
|
|
|
|
|
|
|
// Run randomized simulation
|
2019-07-19 09:59:04 -07:00
|
|
|
stopEarly, params, simErr := simulation.SimulateFromSeed(getSimulateFromSeedInput(t, os.Stdout, app))
|
|
|
|
|
|
|
|
// export state and params before the simulation error is checked
|
|
|
|
if exportStatePath != "" {
|
|
|
|
fmt.Println("Exporting app state...")
|
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportStatePath, []byte(appState), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exportParamsPath != "" {
|
|
|
|
fmt.Println("Exporting simulation params...")
|
|
|
|
paramsBz, err := json.MarshalIndent(params, "", " ")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportParamsPath, paramsBz, 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, simErr)
|
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"])
|
2019-07-26 13:09:15 -07:00
|
|
|
fmt.Println("\nGoLevelDB Stats")
|
2018-11-26 04:21:23 -08:00
|
|
|
fmt.Println(db.Stats()["leveldb.stats"])
|
|
|
|
fmt.Println("GoLevelDB cached block size", db.Stats()["leveldb.cachedblock"])
|
|
|
|
}
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
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")
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
newDir, _ := ioutil.TempDir("", "goleveldb-app-sim-2")
|
2019-03-04 12:31:55 -08:00
|
|
|
newDB, _ := sdk.NewLevelDB("Simulation-2", dir)
|
2019-05-18 01:42:24 -07:00
|
|
|
|
2018-11-26 04:21:23 -08:00
|
|
|
defer func() {
|
|
|
|
newDB.Close()
|
|
|
|
os.RemoveAll(newDir)
|
|
|
|
}()
|
2019-05-18 01:42:24 -07:00
|
|
|
|
|
|
|
newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, 0, fauxMerkleModeOpt)
|
|
|
|
require.Equal(t, "SimApp", newApp.Name())
|
2018-11-26 04:21:23 -08:00
|
|
|
newApp.InitChain(abci.RequestInitChain{
|
|
|
|
AppStateBytes: appState,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Run randomized simulation on imported app
|
2019-07-19 09:59:04 -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
|
2019-05-18 01:42:24 -07:00
|
|
|
// and doesn't depend on the application.
|
2018-08-16 08:36:15 -07:00
|
|
|
func TestAppStateDeterminism(t *testing.T) {
|
2018-08-17 07:19:33 -07:00
|
|
|
if !enabled {
|
2019-05-18 01:42:24 -07:00
|
|
|
t.Skip("Skipping application 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-05-18 01:42:24 -07:00
|
|
|
app := NewSimApp(logger, db, nil, true, 0)
|
2018-08-17 07:19:33 -07:00
|
|
|
|
|
|
|
// Run randomized simulation
|
|
|
|
simulation.SimulateFromSeed(
|
2019-07-03 11:25:52 -07:00
|
|
|
t, os.Stdout, app.BaseApp, appStateFn, seed,
|
2019-07-19 09:59:04 -07:00
|
|
|
testAndRunTxs(app), []sdk.Invariant{},
|
2019-08-02 08:28:50 -07:00
|
|
|
1, 50, 100, 0, "",
|
2019-07-19 09:59:04 -07:00
|
|
|
false, true, false, false, false, app.ModuleAccountAddrs(),
|
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) {
|
|
|
|
logger := log.NewNopLogger()
|
2019-05-18 01:42:24 -07:00
|
|
|
dir, _ := ioutil.TempDir("", "goleveldb-app-invariant-bench")
|
2019-04-12 15:52:16 -07:00
|
|
|
db, _ := sdk.NewLevelDB("simulation", dir)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
app := NewSimApp(logger, db, nil, true, 0)
|
2019-07-19 09:59:04 -07:00
|
|
|
exportParams := exportParamsPath != ""
|
2019-04-12 15:52:16 -07:00
|
|
|
|
|
|
|
// 2. Run parameterized simulation (w/o invariants)
|
2019-07-19 09:59:04 -07:00
|
|
|
_, params, simErr := simulation.SimulateFromSeed(
|
2019-07-03 11:25:52 -07:00
|
|
|
b, ioutil.Discard, app.BaseApp, appStateFn, seed, testAndRunTxs(app),
|
2019-08-02 08:28:50 -07:00
|
|
|
[]sdk.Invariant{}, initialBlockHeight, numBlocks, exportParamsHeight, blockSize,
|
2019-07-26 13:09:15 -07:00
|
|
|
exportStatsPath, exportParams, commit, lean, onOperation, false, app.ModuleAccountAddrs(),
|
2019-04-12 15:52:16 -07:00
|
|
|
)
|
2019-07-19 09:59:04 -07:00
|
|
|
|
|
|
|
// export state and params before the simulation error is checked
|
|
|
|
if exportStatePath != "" {
|
|
|
|
fmt.Println("Exporting app state...")
|
|
|
|
appState, _, err := app.ExportAppStateAndValidators(false, nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(exportStatePath, []byte(appState), 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if exportParamsPath != "" {
|
|
|
|
fmt.Println("Exporting simulation params...")
|
|
|
|
paramsBz, err := json.MarshalIndent(params, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(exportParamsPath, paramsBz, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
b.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if simErr != nil {
|
|
|
|
fmt.Println(simErr)
|
2019-04-12 15:52:16 -07:00
|
|
|
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) {
|
2019-07-11 03:56:43 -07:00
|
|
|
if res, stop := cr.Invar(ctx); stop {
|
|
|
|
fmt.Printf("broken invariant at block %d of %d\n%s", ctx.BlockHeight()-1, numBlocks, res)
|
2019-04-12 15:52:16 -07:00
|
|
|
b.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|