mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-08-04 05:54:48 +02:00
36 lines
1.3 KiB
C++
36 lines
1.3 KiB
C++
// 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.
|
|
|
|
#include <test/util/random.h>
|
|
|
|
#include <logging.h>
|
|
#include <random.h>
|
|
#include <uint256.h>
|
|
|
|
#include <cstdlib>
|
|
#include <string>
|
|
|
|
extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
|
|
|
|
void SeedRandomStateForTest(SeedRand seedtype)
|
|
{
|
|
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
|
|
|
|
// 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());
|
|
MakeRandDeterministicDANGEROUS(seed);
|
|
}
|