diff --git a/x/mock/simulation/doc.go b/x/mock/simulation/doc.go index 2febd9e47..ff292bd38 100644 --- a/x/mock/simulation/doc.go +++ b/x/mock/simulation/doc.go @@ -2,12 +2,11 @@ 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 +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 +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, +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 @@ -15,12 +14,12 @@ 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 +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 +it for. Then run simulation.Simulate! The simulator will handle things like ensuring that validators periodically double signing, or go offline. */ package simulation diff --git a/x/mock/simulation/params.go b/x/mock/simulation/params.go index c46ab236b..8499e6c11 100644 --- a/x/mock/simulation/params.go +++ b/x/mock/simulation/params.go @@ -44,29 +44,6 @@ type Params struct { BlockSizeTransitionMatrix TransitionMatrix } -// getBlockSize returns a block size as determined from the transition matrix. -// It targets making average block size the provided parameter. The three -// states it moves between are: -// - "over stuffed" blocks with average size of 2 * avgblocksize, -// - normal sized blocks, hitting avgBlocksize on average, -// - and empty blocks, with no txs / only txs scheduled from the past. -func getBlockSize(r *rand.Rand, params Params, - lastBlockSizeState, avgBlockSize int) (state, blocksize int) { - - // TODO: Make default blocksize transition matrix actually make the average - // blocksize equal to avgBlockSize. - state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) - switch state { - case 0: - blocksize = r.Intn(avgBlockSize * 4) - case 1: - blocksize = r.Intn(avgBlockSize * 2) - default: - blocksize = 0 - } - return state, blocksize -} - // Return default simulation parameters func DefaultParams() Params { return Params{ diff --git a/x/mock/simulation/transition_matrix.go b/x/mock/simulation/transition_matrix.go index 97cd307e5..f7d713775 100644 --- a/x/mock/simulation/transition_matrix.go +++ b/x/mock/simulation/transition_matrix.go @@ -51,7 +51,7 @@ func (t TransitionMatrix) NextState(r *rand.Rand, i int) int { } // GetMemberOfInitialState takes an initial array of weights, of size n. -// It returns a weighted random number in [0,n]. +// It returns a weighted random number in [0,n). func GetMemberOfInitialState(r *rand.Rand, weights []int) int { n := len(weights) total := 0 diff --git a/x/mock/simulation/util.go b/x/mock/simulation/util.go index 02ef56d2e..f10364aaa 100644 --- a/x/mock/simulation/util.go +++ b/x/mock/simulation/util.go @@ -2,6 +2,7 @@ package simulation import ( "fmt" + "math/rand" "os" "strings" "testing" @@ -78,3 +79,26 @@ func logPrinter(testingmode bool, logs []*strings.Builder) func() { } } } + +// getBlockSize returns a block size as determined from the transition matrix. +// It targets making average block size the provided parameter. The three +// states it moves between are: +// - "over stuffed" blocks with average size of 2 * avgblocksize, +// - normal sized blocks, hitting avgBlocksize on average, +// - and empty blocks, with no txs / only txs scheduled from the past. +func getBlockSize(r *rand.Rand, params Params, + lastBlockSizeState, avgBlockSize int) (state, blocksize int) { + + // TODO: Make default blocksize transition matrix actually make the average + // blocksize equal to avgBlockSize. + state = params.BlockSizeTransitionMatrix.NextState(r, lastBlockSizeState) + switch state { + case 0: + blocksize = r.Intn(avgBlockSize * 4) + case 1: + blocksize = r.Intn(avgBlockSize * 2) + default: + blocksize = 0 + } + return state, blocksize +}