diff options
author | jonatack <jon@atack.com> | 2023-01-22 09:57:19 -0800 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2023-06-14 08:28:33 -0600 |
commit | 1b246fdd145a95f5da479159f5e8eaf5a76bdc3a (patch) | |
tree | a7799cb90d8d0c5ffa96040ed95a97e82eff399a /src/test/util/random.h | |
parent | 681ecac5c2d462920cd32636eec15599a9bcf424 (diff) | |
download | bitcoin-1b246fdd145a95f5da479159f5e8eaf5a76bdc3a.tar.xz |
test: move remaining random test util code from setup_common to random
and drop the util/random dependency on util/setup_common.
This improves code separation and avoids creating a circular dependency if
setup_common needs to call the util/random functions.
Diffstat (limited to 'src/test/util/random.h')
-rw-r--r-- | src/test/util/random.h | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/test/util/random.h b/src/test/util/random.h index 7997e8a346..c910bd6a3a 100644 --- a/src/test/util/random.h +++ b/src/test/util/random.h @@ -7,11 +7,41 @@ #include <consensus/amount.h> #include <random.h> -#include <test/util/setup_common.h> #include <uint256.h> #include <cstdint> +/** + * This global and the helpers that use it are not thread-safe. + * + * If thread-safety is needed, the global could be made thread_local (given + * that thread_local is supported on all architectures we support) or a + * per-thread instance could be used in the multi-threaded test. + */ +extern FastRandomContext g_insecure_rand_ctx; + +/** + * Flag to make GetRand in random.h return the same number + */ +extern bool g_mock_deterministic_tests; + +enum class SeedRand { + ZEROS, //!< Seed with a compile time constant of zeros + SEED, //!< Call the Seed() helper +}; + +/** Seed the given random ctx or use the seed passed in via an environment var */ +void Seed(FastRandomContext& ctx); + +static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED) +{ + if (seed == SeedRand::ZEROS) { + g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true); + } else { + Seed(g_insecure_rand_ctx); + } +} + static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); |