aboutsummaryrefslogtreecommitdiff
path: root/src/test/util/random.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/util/random.h')
-rw-r--r--src/test/util/random.h32
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();