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.h70
1 files changed, 16 insertions, 54 deletions
diff --git a/src/test/util/random.h b/src/test/util/random.h
index 18ab425e48..441150e666 100644
--- a/src/test/util/random.h
+++ b/src/test/util/random.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2023 The Bitcoin Core developers
+// Copyright (c) 2023-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -11,64 +11,26 @@
#include <cstdint>
-/**
- * This global and the helpers that use it are not thread-safe.
- *
- * If thread-safety is needed, 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 with a compile time constant of zeros.
+ */
+ ZEROS,
+ /**
+ * Seed with a fixed value that never changes over the lifetime of this
+ * process. The seed is read from the RANDOM_CTX_SEED environment variable
+ * if set, otherwise generated randomly once, saved, and reused.
+ */
+ FIXED_SEED,
};
-/** 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();
-}
-
-static inline uint256 InsecureRand256()
-{
- return g_insecure_rand_ctx.rand256();
-}
-
-static inline uint64_t InsecureRandBits(int bits)
-{
- return g_insecure_rand_ctx.randbits(bits);
-}
-
-static inline uint64_t InsecureRandRange(uint64_t range)
-{
- return g_insecure_rand_ctx.randrange(range);
-}
-
-static inline bool InsecureRandBool()
-{
- return g_insecure_rand_ctx.randbool();
-}
+/** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
+void SeedRandomStateForTest(SeedRand seed);
-static inline CAmount InsecureRandMoneyAmount()
+template <RandomNumberGenerator Rng>
+inline CAmount RandMoney(Rng&& rng)
{
- return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
+ return CAmount{rng.randrange(MAX_MONEY + 1)};
}
#endif // BITCOIN_TEST_UTIL_RANDOM_H