aboutsummaryrefslogtreecommitdiff
path: root/src/test/util/random.h
diff options
context:
space:
mode:
authorPieter Wuille <pieter@wuille.net>2024-03-10 19:49:42 -0400
committerPieter Wuille <pieter@wuille.net>2024-07-01 10:26:46 -0400
commit810cdf6b4e12a1fdace7998d75b4daf8b67d7028 (patch)
tree60e77d0360dc21f2271e946491987d308aeb767a /src/test/util/random.h
parent6cfdc5b104caf9952393f9dac2a36539d964077f (diff)
tests: overhaul deterministic test randomness
The existing code provides two randomness mechanisms for test purposes: - g_insecure_rand_ctx (with its wrappers InsecureRand*), which during tests is initialized using either zeros (SeedRand::ZEROS), or using environment-provided randomness (SeedRand::SEED). - g_mock_deterministic_tests, which controls some (but not all) of the normal randomness output if set, but then makes it extremely predictable (identical output repeatedly). Replace this with a single mechanism, which retains the SeedRand modes to control all randomness. There is a new internal deterministic PRNG inside the random module, which is used in GetRandBytes() when in test mode, and which is also used to initialize g_insecure_rand_ctx. This means that during tests, all random numbers are made deterministic. There is one exception, GetStrongRandBytes(), which even in test mode still uses the normal PRNG state. This probably opens the door to removing a lot of the ad-hoc "deterministic" mode functions littered through the codebase (by simply running relevant tests in SeedRand::ZEROS mode), but this isn't done yet.
Diffstat (limited to 'src/test/util/random.h')
-rw-r--r--src/test/util/random.h20
1 files changed, 3 insertions, 17 deletions
diff --git a/src/test/util/random.h b/src/test/util/random.h
index 18ab425e48..09a475f8b3 100644
--- a/src/test/util/random.h
+++ b/src/test/util/random.h
@@ -19,27 +19,13 @@
*/
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, //!< Use (and report) random seed from environment, or a (truly) random one.
};
-/** 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);
- }
-}
+/** Seed the RNG for testing. This affects all randomness, except GetStrongRandBytes(). */
+void SeedRandomForTest(SeedRand seed = SeedRand::SEED);
static inline uint32_t InsecureRand32()
{