Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
util
random.cpp
Go to the documentation of this file.
1
// Copyright (c) 2023-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <
test/util/random.h
>
6
7
#include <
logging.h
>
8
#include <
random.h
>
9
#include <
uint256.h
>
10
#include <
util/check.h
>
11
12
#include <cstdlib>
13
#include <iostream>
14
15
std::atomic<bool>
g_seeded_g_prng_zero
{
false
};
16
17
extern
void
MakeRandDeterministicDANGEROUS
(
const
uint256
& seed)
noexcept
;
18
19
void
SeedRandomStateForTest
(
SeedRand
seedtype)
20
{
21
constexpr
auto
RANDOM_CTX_SEED{
"RANDOM_CTX_SEED"
};
22
23
// Do this once, on the first call, regardless of seedtype, because once
24
// MakeRandDeterministicDANGEROUS is called, the output of GetRandHash is
25
// no longer truly random. It should be enough to get the seed once for the
26
// process.
27
static
const
auto
g_ctx_seed = []() -> std::optional<uint256> {
28
if
(
EnableFuzzDeterminism
())
return
{};
29
// If RANDOM_CTX_SEED is set, use that as seed.
30
if
(
const
char
* num{std::getenv(RANDOM_CTX_SEED)}) {
31
if
(
auto
num_parsed{
uint256::FromUserHex
(num)}) {
32
return
*num_parsed;
33
}
else
{
34
std::cerr << RANDOM_CTX_SEED <<
" must consist of up to "
<<
uint256::size
() * 2 <<
" hex digits (\"0x\" prefix allowed), it was set to: '"
<< num <<
"'.\n"
;
35
std::abort();
36
}
37
}
38
// Otherwise use a (truly) random value.
39
return
GetRandHash
();
40
}();
41
42
g_seeded_g_prng_zero
= seedtype ==
SeedRand::ZEROS
;
43
if
(
EnableFuzzDeterminism
()) {
44
Assert
(
g_seeded_g_prng_zero
);
// Only SeedRandomStateForTest(SeedRand::ZEROS) is allowed in fuzz tests
45
Assert
(!
g_used_g_prng
);
// The global PRNG must not have been used before SeedRandomStateForTest(SeedRand::ZEROS)
46
}
47
const
uint256
& seed{seedtype ==
SeedRand::FIXED_SEED
? g_ctx_seed.value() :
uint256::ZERO
};
48
LogInfo
(
"Setting random seed for current tests to %s=%s\n"
, RANDOM_CTX_SEED, seed.
GetHex
());
49
MakeRandDeterministicDANGEROUS
(seed);
50
}
check.h
EnableFuzzDeterminism
bool EnableFuzzDeterminism()
Definition
check.h:35
Assert
#define Assert(val)
Identity function.
Definition
check.h:113
base_blob::GetHex
std::string GetHex() const
Definition
uint256.cpp:11
base_blob< 256 >::size
static constexpr unsigned int size()
Definition
uint256.h:106
uint256
256-bit opaque blob.
Definition
uint256.h:195
uint256::FromUserHex
static std::optional< uint256 > FromUserHex(std::string_view str)
Definition
uint256.h:198
uint256::ZERO
static const uint256 ZERO
Definition
uint256.h:203
LogInfo
#define LogInfo(...)
Definition
log.h:95
g_used_g_prng
std::atomic< bool > g_used_g_prng
Definition
random.cpp:599
GetRandHash
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition
random.h:463
logging.h
g_seeded_g_prng_zero
std::atomic< bool > g_seeded_g_prng_zero
Definition
random.cpp:15
MakeRandDeterministicDANGEROUS
void MakeRandDeterministicDANGEROUS(const uint256 &seed) noexcept
Internal function to set g_determinstic_rng.
Definition
random.cpp:595
SeedRandomStateForTest
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition
random.cpp:19
random.h
SeedRand
SeedRand
Definition
random.h:15
SeedRand::ZEROS
@ ZEROS
Seed with a compile time constant of zeros.
Definition
random.h:19
SeedRand::FIXED_SEED
@ FIXED_SEED
Seed with a fixed value that never changes over the lifetime of this process.
Definition
random.h:25
uint256.h
Generated on
for Bitcoin Core by
1.17.0