2018-07-16 18:25:15 -07:00
|
|
|
package simulation
|
2018-07-16 18:15:50 -07:00
|
|
|
|
|
|
|
import (
|
2019-06-08 13:55:47 -07:00
|
|
|
"encoding/json"
|
2018-07-17 16:27:51 -07:00
|
|
|
"fmt"
|
2018-11-08 07:32:53 -08:00
|
|
|
"math/rand"
|
2018-09-09 08:34:09 -07:00
|
|
|
"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
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2018-11-07 08:57:53 -08:00
|
|
|
if _t, ok := tb.(*testing.T); ok {
|
|
|
|
t = _t
|
|
|
|
testingMode = true
|
|
|
|
} else {
|
|
|
|
b = tb.(*testing.B)
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-03-14 11:13:15 -07:00
|
|
|
return testingMode, t, b
|
2018-09-09 08:34:09 -07:00
|
|
|
}
|
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.
|
2020-03-23 04:55:44 -07:00
|
|
|
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
|
|
|
|
2019-06-08 13:55:47 -07: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
|
|
|
|
}
|