|
7 | 7 | #include <logging.h>
|
8 | 8 | #include <random.h>
|
9 | 9 | #include <uint256.h>
|
| 10 | +#include <util/check.h> |
10 | 11 |
|
11 | 12 | #include <cstdlib>
|
12 |
| -#include <string> |
| 13 | +#include <iostream> |
13 | 14 |
|
14 | 15 | FastRandomContext g_insecure_rand_ctx;
|
15 | 16 |
|
16 | 17 | extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
|
17 | 18 |
|
18 | 19 | void SeedRandomForTest(SeedRand seedtype)
|
19 | 20 | {
|
20 |
| - static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"}; |
| 21 | + constexpr auto RANDOM_CTX_SEED{"RANDOM_CTX_SEED"}; |
21 | 22 |
|
22 | 23 | // Do this once, on the first call, regardless of seedtype, because once
|
23 | 24 | // MakeRandDeterministicDANGEROUS is called, the output of GetRandHash is
|
24 | 25 | // no longer truly random. It should be enough to get the seed once for the
|
25 | 26 | // process.
|
26 | 27 | static const uint256 ctx_seed = []() {
|
27 | 28 | // If RANDOM_CTX_SEED is set, use that as seed.
|
28 |
| - const char* num = std::getenv(RANDOM_CTX_SEED.c_str()); |
29 |
| - if (num) return uint256S(num); |
| 29 | + if (const char* num{std::getenv(RANDOM_CTX_SEED)}) { |
| 30 | + if (auto num_parsed{uint256::FromUserHex(num)}) { |
| 31 | + return *num_parsed; |
| 32 | + } else { |
| 33 | + std::cerr << RANDOM_CTX_SEED << " must consist of up to " << uint256::size() * 2 << " hex digits (\"0x\" prefix allowed), it was set to: '" << num << "'.\n"; |
| 34 | + std::abort(); |
| 35 | + } |
| 36 | + } |
30 | 37 | // Otherwise use a (truly) random value.
|
31 | 38 | return GetRandHash();
|
32 | 39 | }();
|
33 | 40 |
|
34 | 41 | const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
|
35 |
| - LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex()); |
| 42 | + LogInfo("Setting random seed for current tests to %s=%s\n", RANDOM_CTX_SEED, seed.GetHex()); |
36 | 43 | MakeRandDeterministicDANGEROUS(seed);
|
37 | 44 | g_insecure_rand_ctx.Reseed(GetRandHash());
|
38 | 45 | }
|
0 commit comments