mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-21 07:39:08 +01:00
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.
This commit is contained in:
@@ -13,21 +13,26 @@
|
||||
|
||||
FastRandomContext g_insecure_rand_ctx;
|
||||
|
||||
/** Return the unsigned from the environment var if available, otherwise 0 */
|
||||
static uint256 GetUintFromEnv(const std::string& env_name)
|
||||
{
|
||||
const char* num = std::getenv(env_name.c_str());
|
||||
if (!num) return {};
|
||||
return uint256S(num);
|
||||
}
|
||||
extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
|
||||
|
||||
void Seed(FastRandomContext& ctx)
|
||||
void SeedRandomForTest(SeedRand seedtype)
|
||||
{
|
||||
// Should be enough to get the seed once for the process
|
||||
static uint256 seed{};
|
||||
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
|
||||
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
|
||||
if (seed.IsNull()) seed = GetRandHash();
|
||||
|
||||
// Do this once, on the first call, regardless of seedtype, because once
|
||||
// MakeRandDeterministicDANGEROUS is called, the output of GetRandHash is
|
||||
// no longer truly random. It should be enough to get the seed once for the
|
||||
// process.
|
||||
static const uint256 ctx_seed = []() {
|
||||
// If RANDOM_CTX_SEED is set, use that as seed.
|
||||
const char* num = std::getenv(RANDOM_CTX_SEED.c_str());
|
||||
if (num) return uint256S(num);
|
||||
// Otherwise use a (truly) random value.
|
||||
return GetRandHash();
|
||||
}();
|
||||
|
||||
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
|
||||
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
|
||||
ctx = FastRandomContext(seed);
|
||||
MakeRandDeterministicDANGEROUS(seed);
|
||||
g_insecure_rand_ctx = FastRandomContext(GetRandHash());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user