cosmos-sdk/x/simulation/util.go

54 lines
1.3 KiB
Go
Raw Normal View History

2018-07-16 18:25:15 -07:00
package simulation
2018-07-16 18:15:50 -07:00
import (
"encoding/json"
2018-07-17 16:27:51 -07:00
"fmt"
2018-11-08 07:32:53 -08:00
"math/rand"
"testing"
2018-07-16 18:15:50 -07:00
)
2018-11-07 08:57:53 -08:00
func getTestingMode(tb testing.TB) (testingMode bool, t *testing.T, b *testing.B) {
testingMode = false
if _t, ok := tb.(*testing.T); ok {
t = _t
testingMode = true
} else {
b = tb.(*testing.B)
}
2019-03-14 11:13:15 -07:00
return testingMode, t, b
}
2018-11-08 07:32:53 -08:00
// 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.
2019-08-07 09:54:13 -07:00
func getBlockSize(r *rand.Rand, params Params, lastBlockSizeState, avgBlockSize int) (state, blockSize int) {
2018-11-08 07:32:53 -08:00
// TODO: Make default blocksize transition matrix actually make the average
// blocksize equal to avgBlockSize.
state = params.BlockSizeTransitionMatrix().NextState(r, lastBlockSizeState)
2019-08-07 09:54:13 -07:00
2018-11-08 07:32:53 -08:00
switch state {
case 0:
2019-08-07 09:54:13 -07:00
blockSize = r.Intn(avgBlockSize * 4)
2018-11-08 07:32:53 -08:00
case 1:
2019-08-07 09:54:13 -07:00
blockSize = r.Intn(avgBlockSize * 2)
2018-11-08 07:32:53 -08:00
default:
2019-08-07 09:54:13 -07:00
blockSize = 0
2018-11-08 07:32:53 -08:00
}
2019-08-07 09:54:13 -07:00
return state, blockSize
2018-11-08 07:32:53 -08:00
}
2018-11-09 07:31:56 -08:00
func mustMarshalJSONIndent(o interface{}) []byte {
bz, err := json.MarshalIndent(o, "", " ")
if err != nil {
panic(fmt.Sprintf("failed to JSON encode: %s", err))
}
return bz
}