Skip to content

Commit a073e64

Browse files
fanquakevijaydasmp
authored andcommitted
Merge bitcoin#27425: test: move remaining rand code from util/setup_common to util/random
1cd45d4 test: move random.h include header from setup_common.h to cpp (Jon Atack) 1b246fd 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 bitcoin#26940 (comment) by glozow (thanks!) ACKs for top commit: MarcoFalke: lgtm ACK 1cd45d4 🌂 Tree-SHA512: 6ce63d9103ba9b04eebbd8ad02fe9aa79e356296533404034a1ae88e9b7ca0bc9a5c51fd754b71cf4e7b55b18bcd4d5474b2d588edee3851e3b3ce0e4d309a93
1 parent 3e92f72 commit a073e64

File tree

7 files changed

+39
-52
lines changed

7 files changed

+39
-52
lines changed

src/Makefile.test_util.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ libtest_util_a_SOURCES = \
3131
test/util/logging.cpp \
3232
test/util/mining.cpp \
3333
test/util/net.cpp \
34+
test/util/random.cpp \
3435
test/util/script.cpp \
3536
test/util/setup_common.cpp \
3637
test/util/str.cpp \

src/test/denialofservice_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <script/signingprovider.h>
1414
#include <script/standard.h>
1515
#include <test/util/net.h>
16+
#include <test/util/random.h>
1617
#include <test/util/setup_common.h>
1718
#include <timedata.h>
1819
#include <util/string.h>

src/test/random_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <random.h>
66

7+
#include <test/util/random.h>
78
#include <test/util/setup_common.h>
89
#include <util/time.h>
910

src/test/util/random.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2023 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+
11+
#include <cstdlib>
12+
#include <string>
13+
14+
FastRandomContext g_insecure_rand_ctx;
15+
16+
/** Return the unsigned from the environment var if available, otherwise 0 */
17+
static uint256 GetUintFromEnv(const std::string& env_name)
18+
{
19+
const char* num = std::getenv(env_name.c_str());
20+
if (!num) return {};
21+
return uint256S(num);
22+
}
23+
24+
void Seed(FastRandomContext& ctx)
25+
{
26+
// Should be enough to get the seed once for the process
27+
static uint256 seed{};
28+
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
29+
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
30+
if (seed.IsNull()) seed = GetRandHash();
31+
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
32+
ctx = FastRandomContext(seed);
33+
}

src/test/util/setup_common.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <node/miner.h>
2424
#include <policy/fees.h>
2525
#include <pow.h>
26+
#include <random.h>
2627
#include <rpc/blockchain.h>
2728
#include <rpc/register.h>
2829
#include <rpc/server.h>
@@ -73,29 +74,9 @@
7374
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
7475
UrlDecodeFn* const URL_DECODE = nullptr;
7576

76-
FastRandomContext g_insecure_rand_ctx;
7777
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
7878
static FastRandomContext g_insecure_rand_ctx_temp_path;
7979

80-
/** Return the unsigned from the environment var if available, otherwise 0 */
81-
static uint256 GetUintFromEnv(const std::string& env_name)
82-
{
83-
const char* num = std::getenv(env_name.c_str());
84-
if (!num) return {};
85-
return uint256S(num);
86-
}
87-
88-
void Seed(FastRandomContext& ctx)
89-
{
90-
// Should be enough to get the seed once for the process
91-
static uint256 seed{};
92-
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
93-
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
94-
if (seed.IsNull()) seed = GetRandHash();
95-
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
96-
ctx = FastRandomContext(seed);
97-
}
98-
9980
std::ostream& operator<<(std::ostream& os, const uint256& num)
10081
{
10182
os << num.ToString();

src/test/util/setup_common.h

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <node/caches.h>
1414
#include <node/context.h> // IWYU pragma: export
1515
#include <pubkey.h>
16-
#include <random.h>
1716
#include <txmempool.h>
1817
#include <util/check.h>
1918
#include <util/string.h>
@@ -28,6 +27,7 @@ class CChainParams;
2827
namespace Consensus {
2928
struct Params;
3029
};
30+
class FastRandomContext;
3131

3232
/** This is connected to the logger. Can be used to redirect logs to any other log */
3333
extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
@@ -44,37 +44,6 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
4444
}
4545
} // namespace std
4646

47-
/**
48-
* This global and the helpers that use it are not thread-safe.
49-
*
50-
* If thread-safety is needed, the global could be made thread_local (given
51-
* that thread_local is supported on all architectures we support) or a
52-
* per-thread instance could be used in the multi-threaded test.
53-
*/
54-
extern FastRandomContext g_insecure_rand_ctx;
55-
56-
/**
57-
* Flag to make GetRand in random.h return the same number
58-
*/
59-
extern bool g_mock_deterministic_tests;
60-
61-
enum class SeedRand {
62-
ZEROS, //!< Seed with a compile time constant of zeros
63-
SEED, //!< Call the Seed() helper
64-
};
65-
66-
/** Seed the given random ctx or use the seed passed in via an environment var */
67-
void Seed(FastRandomContext& ctx);
68-
69-
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
70-
{
71-
if (seed == SeedRand::ZEROS) {
72-
g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
73-
} else {
74-
Seed(g_insecure_rand_ctx);
75-
}
76-
}
77-
7847
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
7948
static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
8049
static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }

src/wallet/test/wallet_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <rpc/rawtransaction_util.h>
2222
#include <rpc/server.h>
2323
#include <test/util/logging.h>
24+
#include <test/util/random.h>
2425
#include <test/util/setup_common.h>
2526
#include <util/translation.h>
2627
#include <policy/settings.h>

0 commit comments

Comments
 (0)