2023-01-22 09:57:19 -08:00
|
|
|
// Copyright (c) 2023 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_TEST_UTIL_RANDOM_H
|
|
|
|
#define BITCOIN_TEST_UTIL_RANDOM_H
|
|
|
|
|
2023-01-20 11:05:09 -08:00
|
|
|
#include <consensus/amount.h>
|
2023-01-22 09:57:19 -08:00
|
|
|
#include <random.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2023-01-22 09:57:19 -08:00
|
|
|
/**
|
|
|
|
* This global and the helpers that use it are not thread-safe.
|
|
|
|
*
|
2024-05-21 10:29:16 +01:00
|
|
|
* If thread-safety is needed, a per-thread instance could be
|
|
|
|
* used in the multi-threaded test.
|
2023-01-22 09:57:19 -08:00
|
|
|
*/
|
|
|
|
extern FastRandomContext g_insecure_rand_ctx;
|
|
|
|
|
|
|
|
enum class SeedRand {
|
|
|
|
ZEROS, //!< Seed with a compile time constant of zeros
|
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.
2024-03-10 19:49:42 -04:00
|
|
|
SEED, //!< Use (and report) random seed from environment, or a (truly) random one.
|
2023-01-22 09:57:19 -08:00
|
|
|
};
|
|
|
|
|
2024-08-16 11:02:54 +02:00
|
|
|
/** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
|
|
|
|
void SeedRandomStateForTest(SeedRand seed);
|
2023-01-22 09:57:19 -08:00
|
|
|
|
2023-01-22 09:57:19 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-01-20 11:05:09 -08:00
|
|
|
static inline CAmount InsecureRandMoneyAmount()
|
|
|
|
{
|
|
|
|
return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
|
|
|
|
}
|
|
|
|
|
2023-01-22 09:57:19 -08:00
|
|
|
#endif // BITCOIN_TEST_UTIL_RANDOM_H
|