Skip to content

Commit 24d5cf9

Browse files
committed
Merge bitcoin/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/bitcoin#26940 (comment) by glozow (thanks!) ACKs for top commit: MarcoFalke: lgtm ACK 1cd45d4 🌂 Tree-SHA512: 6ce63d9103ba9b04eebbd8ad02fe9aa79e356296533404034a1ae88e9b7ca0bc9a5c51fd754b71cf4e7b55b18bcd4d5474b2d588edee3851e3b3ce0e4d309a93
2 parents c6a338b + 1cd45d4 commit 24d5cf9

9 files changed

+72
-53
lines changed

src/Makefile.test_util.include

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ libtest_util_a_SOURCES = \
4040
test/util/logging.cpp \
4141
test/util/mining.cpp \
4242
test/util/net.cpp \
43+
test/util/random.cpp \
4344
test/util/script.cpp \
4445
test/util/setup_common.cpp \
4546
test/util/str.cpp \

src/test/denialofservice_tests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <script/standard.h>
1616
#include <serialize.h>
1717
#include <test/util/net.h>
18+
#include <test/util/random.h>
1819
#include <test/util/setup_common.h>
1920
#include <timedata.h>
2021
#include <util/string.h>

src/test/miniscript_tests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <vector>
88

9+
#include <test/util/random.h>
910
#include <test/util/setup_common.h>
1011
#include <boost/test/unit_test.hpp>
1112

src/test/random_tests.cpp

+1
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

+33
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/random.h

+31-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,41 @@
77

88
#include <consensus/amount.h>
99
#include <random.h>
10-
#include <test/util/setup_common.h>
1110
#include <uint256.h>
1211

1312
#include <cstdint>
1413

14+
/**
15+
* This global and the helpers that use it are not thread-safe.
16+
*
17+
* If thread-safety is needed, the global could be made thread_local (given
18+
* that thread_local is supported on all architectures we support) or a
19+
* per-thread instance could be used in the multi-threaded test.
20+
*/
21+
extern FastRandomContext g_insecure_rand_ctx;
22+
23+
/**
24+
* Flag to make GetRand in random.h return the same number
25+
*/
26+
extern bool g_mock_deterministic_tests;
27+
28+
enum class SeedRand {
29+
ZEROS, //!< Seed with a compile time constant of zeros
30+
SEED, //!< Call the Seed() helper
31+
};
32+
33+
/** Seed the given random ctx or use the seed passed in via an environment var */
34+
void Seed(FastRandomContext& ctx);
35+
36+
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
37+
{
38+
if (seed == SeedRand::ZEROS) {
39+
g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
40+
} else {
41+
Seed(g_insecure_rand_ctx);
42+
}
43+
}
44+
1545
static inline uint32_t InsecureRand32()
1646
{
1747
return g_insecure_rand_ctx.rand32();

src/test/util/setup_common.cpp

+2-20
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <policy/fees.h>
3333
#include <policy/fees_args.h>
3434
#include <pow.h>
35+
#include <random.h>
3536
#include <rpc/blockchain.h>
3637
#include <rpc/register.h>
3738
#include <rpc/server.h>
@@ -40,6 +41,7 @@
4041
#include <shutdown.h>
4142
#include <streams.h>
4243
#include <test/util/net.h>
44+
#include <test/util/random.h>
4345
#include <test/util/txmempool.h>
4446
#include <timedata.h>
4547
#include <txdb.h>
@@ -73,29 +75,9 @@ using node::VerifyLoadedChainstate;
7375
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
7476
UrlDecodeFn* const URL_DECODE = nullptr;
7577

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

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-
9981
std::ostream& operator<<(std::ostream& os, const uint256& num)
10082
{
10183
os << num.ToString();

src/test/util/setup_common.h

+1-32
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <node/context.h> // IWYU pragma: export
1212
#include <primitives/transaction.h>
1313
#include <pubkey.h>
14-
#include <random.h>
1514
#include <stdexcept>
1615
#include <util/chaintype.h>
1716
#include <util/check.h>
@@ -25,6 +24,7 @@
2524

2625
class CFeeRate;
2726
class Chainstate;
27+
class FastRandomContext;
2828

2929
/** This is connected to the logger. Can be used to redirect logs to any other log */
3030
extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
@@ -41,37 +41,6 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
4141
}
4242
} // namespace std
4343

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

7746
/** Basic testing setup.

src/wallet/test/wallet_tests.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <policy/policy.h>
1616
#include <rpc/server.h>
1717
#include <test/util/logging.h>
18+
#include <test/util/random.h>
1819
#include <test/util/setup_common.h>
1920
#include <util/translation.h>
2021
#include <validation.h>

0 commit comments

Comments
 (0)