mirror of https://github.com/bitcoin/bitcoin
Merge bitcoin/bitcoin#27425: test: move remaining rand code from util/setup_common to util/random
pull/28035/head1cd45d4e08
test: move random.h include header from setup_common.h to cpp (Jon Atack)1b246fdd14
test: move remaining random test util code from setup_common to random (jonatack) Pull request description: and drop the `util/random` dependency on `util/setup_common`. This improves code separation and allows `util/setup_common` to call `util/random` functions without creating a circular dependency, thereby addressing https://github.com/bitcoin/bitcoin/pull/26940#issuecomment-1497266140 by glozow (thanks!) ACKs for top commit: MarcoFalke: lgtm ACK1cd45d4e08
🌂 Tree-SHA512: 6ce63d9103ba9b04eebbd8ad02fe9aa79e356296533404034a1ae88e9b7ca0bc9a5c51fd754b71cf4e7b55b18bcd4d5474b2d588edee3851e3b3ce0e4d309a93
commit
24d5cf9314
@ -0,0 +1,33 @@
|
||||
// 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>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void Seed(FastRandomContext& ctx)
|
||||
{
|
||||
// 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();
|
||||
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
|
||||
ctx = FastRandomContext(seed);
|
||||
}
|
Loading…
Reference in new issue