From 8564c5079f7f8a11bcae7cc16db29caea5feac69 Mon Sep 17 00:00:00 2001 From: Sam Kleinman Date: Wed, 5 Jan 2022 12:17:23 -0500 Subject: [PATCH] e2e: use more simple strings for generated transactions (#7513) --- libs/rand/random.go | 11 ++++++++--- test/e2e/generator/generate.go | 2 +- test/e2e/pkg/manifest.go | 2 +- test/e2e/pkg/testnet.go | 2 +- test/e2e/runner/load.go | 10 +++------- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/libs/rand/random.go b/libs/rand/random.go index c6bfa04b2..6b486a7fd 100644 --- a/libs/rand/random.go +++ b/libs/rand/random.go @@ -10,15 +10,20 @@ const ( // Str constructs a random alphanumeric string of given length // from math/rand's global default Source. -func Str(length int) string { +func Str(length int) string { return buildString(length, mrand.Int63) } + +// StrFromSource produces a random string of a specified length from +// the specified random source. +func StrFromSource(r *mrand.Rand, length int) string { return buildString(length, r.Int63) } + +func buildString(length int, picker func() int64) string { if length <= 0 { return "" } chars := make([]byte, 0, length) for { - // nolint:gosec // G404: Use of weak random number generator - val := mrand.Int63() + val := picker() for i := 0; i < 10; i++ { v := int(val & 0x3f) // rightmost 6 bits if v >= 62 { // only 62 characters in strChars diff --git a/test/e2e/generator/generate.go b/test/e2e/generator/generate.go index d4b581928..90c19e6ff 100644 --- a/test/e2e/generator/generate.go +++ b/test/e2e/generator/generate.go @@ -113,7 +113,7 @@ func generateTestnet(r *rand.Rand, opt map[string]interface{}) (e2e.Manifest, er KeyType: keyType.Choose(r).(string), Evidence: evidence.Choose(r).(int), QueueType: opt["queueType"].(string), - TxSize: int64(txSize.Choose(r).(int)), + TxSize: txSize.Choose(r).(int), } var numSeeds, numValidators, numFulls, numLightClients int diff --git a/test/e2e/pkg/manifest.go b/test/e2e/pkg/manifest.go index b57b9ac4a..895e62939 100644 --- a/test/e2e/pkg/manifest.go +++ b/test/e2e/pkg/manifest.go @@ -64,7 +64,7 @@ type Manifest struct { QueueType string `toml:"queue_type"` // Number of bytes per tx. Default is 1kb (1024) - TxSize int64 + TxSize int // ABCIProtocol specifies the protocol used to communicate with the ABCI // application: "unix", "tcp", "grpc", or "builtin". Defaults to builtin. diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index 3fd9e77a2..f4b75c71a 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -70,7 +70,7 @@ type Testnet struct { KeyType string Evidence int LogLevel string - TxSize int64 + TxSize int ABCIProtocol string } diff --git a/test/e2e/runner/load.go b/test/e2e/runner/load.go index 703fd43b0..674972d54 100644 --- a/test/e2e/runner/load.go +++ b/test/e2e/runner/load.go @@ -7,6 +7,7 @@ import ( "math/rand" "time" + tmrand "github.com/tendermint/tendermint/libs/rand" rpchttp "github.com/tendermint/tendermint/rpc/client/http" e2e "github.com/tendermint/tendermint/test/e2e/pkg" "github.com/tendermint/tendermint/types" @@ -85,7 +86,7 @@ func Load(ctx context.Context, r *rand.Rand, testnet *e2e.Testnet) error { // generation is primarily the result of backpressure from the // broadcast transaction, though there is still some timer-based // limiting. -func loadGenerate(ctx context.Context, r *rand.Rand, chTx chan<- types.Tx, txSize int64, networkSize int) { +func loadGenerate(ctx context.Context, r *rand.Rand, chTx chan<- types.Tx, txSize int, networkSize int) { timer := time.NewTimer(0) defer timer.Stop() defer close(chTx) @@ -101,12 +102,7 @@ func loadGenerate(ctx context.Context, r *rand.Rand, chTx chan<- types.Tx, txSiz // space, while reduce the size of the data in the app. id := r.Int63n(100) - bz := make([]byte, txSize) - _, err := r.Read(bz) - if err != nil { - panic(fmt.Errorf("failed to read random bytes: %w", err)) - } - tx := types.Tx(fmt.Sprintf("load-%X=%x", id, bz)) + tx := types.Tx(fmt.Sprintf("load-%X=%s", id, tmrand.StrFromSource(r, txSize))) select { case <-ctx.Done():