Documentation ¶
Overview ¶
Package simulation implements a simulation framework for any state machine built on the SDK which utilizes auth.
It is primarily intended for fuzz testing the integration of modules. It will test that the provided operations are interoperable, and that the desired invariants hold. It can additionally be used to detect what the performance benchmarks in the system are, by using benchmarking mode and cpu / mem profiling. If it detects a failure, it provides the entire log of what was ran.
The simulator takes as input: a random seed, the set of operations to run, the invariants to test, and additional parameters to configure how long to run, and misc. parameters that affect simulation speed.
It is intended that every module provides a list of Operations which will randomly create and run a message / tx in a manner that is interesting to fuzz, and verify that the state transition was executed as expected. Each module should additionally provide methods to assert that the desired invariants hold.
Then to perform a randomized simulation, select the set of desired operations, the weightings for each, the invariants you want to test, and how long to run it for. Then run simulation.Simulate! The simulator will handle things like ensuring that validators periodically double signing, or go offline.
Index ¶
- func DeriveRand(r *rand.Rand) *rand.Rand
- func GetMemberOfInitialState(r *rand.Rand, weights []int) int
- func PeriodicInvariant(invariant sdk.Invariant, period int, offset int) sdk.Invariant
- func RandStringOfLength(r *rand.Rand, n int) string
- func RandTimestamp(r *rand.Rand) time.Time
- func RandomAmount(r *rand.Rand, max sdk.Int) sdk.Int
- func RandomDecAmount(r *rand.Rand, max sdk.Dec) sdk.Dec
- func RandomRequestBeginBlock(r *rand.Rand, params Params, validators mockValidators, pastTimes []time.Time, ...) abci.RequestBeginBlock
- func RandomSetGenesis(r *rand.Rand, app *mock.App, accs []Account, denoms []string)
- func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn AppStateFn, ...) (bool, error)
- func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, appStateFn AppStateFn, seed int64, ...) (stopEarly bool, simError error)
- type Account
- type AppStateFn
- type FutureOperation
- type Operation
- type OperationQueue
- type Params
- type TransitionMatrix
- type WeightedOperation
- type WeightedOperations
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeriveRand ¶
Derive a new rand deterministically from a rand. Unlike rand.New(rand.NewSource(seed)), the result is "more random" depending on the source and state of r. NOTE: not crypto safe.
func GetMemberOfInitialState ¶
GetMemberOfInitialState takes an initial array of weights, of size n. It returns a weighted random number in [0,n).
func PeriodicInvariant ¶
PeriodicInvariant returns an Invariant function closure that asserts a given invariant if the mock application's last block modulo the given period is congruent to the given offset.
NOTE this function is intended to be used manually used while running computationally heavy simulations. TODO reference this function in the codebase probably through use of a switch
func RandStringOfLength ¶
shamelessly copied from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-golang#31832326 Generate a random string of a particular length
func RandTimestamp ¶
RandTimestamp generates a random timestamp
func RandomAmount ¶
Generate a random amount Note: The range of RandomAmount includes max, and is, in fact, biased to return max as well as 0.
func RandomDecAmount ¶
RandomDecAmount generates a random decimal amount Note: The range of RandomDecAmount includes max, and is, in fact, biased to return max as well as 0.
func RandomRequestBeginBlock ¶
func RandomRequestBeginBlock(r *rand.Rand, params Params, validators mockValidators, pastTimes []time.Time, pastVoteInfos [][]abci.VoteInfo, event func(string), header abci.Header) abci.RequestBeginBlock
RandomRequestBeginBlock generates a list of signing validators according to the provided list of validators, signing fraction, and evidence fraction
func RandomSetGenesis ¶
RandomSetGenesis wraps mock.RandomSetGenesis, but using simulation accounts
func Simulate ¶
func Simulate(t *testing.T, app *baseapp.BaseApp, appStateFn AppStateFn, ops WeightedOperations, invariants sdk.Invariants, numBlocks int, blockSize int, commit bool) (bool, error)
Simulate tests application by sending random messages.
func SimulateFromSeed ¶
func SimulateFromSeed(tb testing.TB, app *baseapp.BaseApp, appStateFn AppStateFn, seed int64, ops WeightedOperations, invariants sdk.Invariants, numBlocks int, blockSize int, commit bool) (stopEarly bool, simError error)
SimulateFromSeed tests an application by running the provided operations, testing the provided invariants, but using the provided seed. TODO split this monster function up
Types ¶
type Account ¶
Account contains a privkey, pubkey, address tuple eventually more useful data can be placed in here. (e.g. number of coins)
func RandomAcc ¶
RandomAcc pick a random account from an array
func RandomAccounts ¶
RandomAccounts generates n random accounts
type AppStateFn ¶
type AppStateFn func(r *rand.Rand, accs []Account, genesisTimestamp time.Time) (appState json.RawMessage, accounts []Account, chainId string)
AppStateFn returns the app state json bytes, the genesis accounts, and the chain identifier
type FutureOperation ¶
FutureOperation is an operation which will be ran at the beginning of the provided BlockHeight. If both a BlockHeight and BlockTime are specified, it will use the BlockHeight. In the (likely) event that multiple operations are queued at the same block height, they will execute in a FIFO pattern.
type Operation ¶
type Operation func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accounts []Account, event func(string)) ( action string, futureOps []FutureOperation, err error)
Operation runs a state machine transition, and ensures the transition happened as expected. The operation could be running and testing a fuzzed transaction, or doing the same for a message.
For ease of debugging, an operation returns a descriptive message "action", which details what this fuzzed state machine transition actually did.
Operations can optionally provide a list of "FutureOperations" to run later These will be ran at the beginning of the corresponding block.
type Params ¶
type Params struct { PastEvidenceFraction float64 NumKeys int EvidenceFraction float64 InitialLivenessWeightings []int LivenessTransitionMatrix TransitionMatrix BlockSizeTransitionMatrix TransitionMatrix }
Simulation parameters
type TransitionMatrix ¶
type TransitionMatrix struct {
// contains filtered or unexported fields
}
TransitionMatrix is _almost_ a left stochastic matrix. It is technically not one due to not normalizing the column values. In the future, if we want to find the steady state distribution, it will be quite easy to normalize these values to get a stochastic matrix. Floats aren't currently used as the default due to non-determinism across architectures
func CreateTransitionMatrix ¶
func CreateTransitionMatrix(weights [][]int) (TransitionMatrix, error)
CreateTransitionMatrix creates a transition matrix from the provided weights. TODO: Provide example usage
type WeightedOperation ¶
WeightedOperation is an operation with associated weight. This is used to bias the selection operation within the simulator.
type WeightedOperations ¶
type WeightedOperations []WeightedOperation
WeightedOperations is the group of all weighted operations to simulate.