Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2023-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <test/util/random.h>7#include <random.h>8#include <uint256.h>9#include <util/check.h>10#include <util/log.h>12#include <cstdlib>13#include <iostream>15std::atomic<bool> g_seeded_g_prng_zero{false};17extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;19void SeedRandomStateForTest(SeedRand seedtype)20{21constexpr auto RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};23// Do this once, on the first call, regardless of seedtype, because once24// MakeRandDeterministicDANGEROUS is called, the output of GetRandHash is25// no longer truly random. It should be enough to get the seed once for the26// process.27static const auto g_ctx_seed = []() -> std::optional<uint256> {28if (EnableFuzzDeterminism()) return {};29// If RANDOM_CTX_SEED is set, use that as seed.30if (const char* num{std::getenv(RANDOM_CTX_SEED)}) {31if (auto num_parsed{uint256::FromUserHex(num)}) {32return *num_parsed;33} else {34std::cerr << RANDOM_CTX_SEED << " must consist of up to " << uint256::size() * 2 << " hex digits (\"0x\" prefix allowed), it was set to: '" << num << "'.\n";35std::abort();36}37}38// Otherwise use a (truly) random value.39return GetRandHash();40}();42g_seeded_g_prng_zero = seedtype == SeedRand::ZEROS;43if (EnableFuzzDeterminism()) {44Assert(g_seeded_g_prng_zero); // Only SeedRandomStateForTest(SeedRand::ZEROS) is allowed in fuzz tests45Assert(!g_used_g_prng); // The global PRNG must not have been used before SeedRandomStateForTest(SeedRand::ZEROS)46}47const uint256& seed{seedtype == SeedRand::FIXED_SEED ? g_ctx_seed.value() : uint256::ZERO};48LogInfo("Setting random seed for current tests to %s=%s\n", RANDOM_CTX_SEED, seed.GetHex());49MakeRandDeterministicDANGEROUS(seed);50}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.