Skip to content

Commit faead93

Browse files
author
MarcoFalke
committed
test: Make g_insecure_rand_ctx thread_local
1 parent 9a43344 commit faead93

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/test/denialofservice_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
111111

112112
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
113113
{
114-
CAddress addr(ip(insecure_rand_ctx.randbits(32)), NODE_NONE);
114+
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
115115
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
116116
CNode &node = *vNodes.back();
117117
node.SetSendVersion(PROTOCOL_VERSION);

src/test/test_bitcoin.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <crypto/sha256.h>
1212
#include <miner.h>
1313
#include <net_processing.h>
14+
#include <noui.h>
1415
#include <pow.h>
1516
#include <rpc/register.h>
1617
#include <rpc/server.h>
@@ -36,10 +37,7 @@ void CConnmanTest::ClearNodes()
3637
g_connman->vNodes.clear();
3738
}
3839

39-
FastRandomContext insecure_rand_ctx;
40-
41-
extern bool fPrintToConsole;
42-
extern void noui_connect();
40+
thread_local FastRandomContext g_insecure_rand_ctx;
4341

4442
std::ostream& operator<<(std::ostream& os, const uint256& num)
4543
{
@@ -117,16 +115,16 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
117115

118116
TestingSetup::~TestingSetup()
119117
{
120-
threadGroup.interrupt_all();
121-
threadGroup.join_all();
122-
GetMainSignals().FlushBackgroundCallbacks();
123-
GetMainSignals().UnregisterBackgroundSignalScheduler();
124-
g_connman.reset();
125-
peerLogic.reset();
126-
UnloadBlockIndex();
127-
pcoinsTip.reset();
128-
pcoinsdbview.reset();
129-
pblocktree.reset();
118+
threadGroup.interrupt_all();
119+
threadGroup.join_all();
120+
GetMainSignals().FlushBackgroundCallbacks();
121+
GetMainSignals().UnregisterBackgroundSignalScheduler();
122+
g_connman.reset();
123+
peerLogic.reset();
124+
UnloadBlockIndex();
125+
pcoinsTip.reset();
126+
pcoinsdbview.reset();
127+
pblocktree.reset();
130128
}
131129

132130
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)

src/test/test_bitcoin.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
2626
return stream << static_cast<typename std::underlying_type<T>::type>(e);
2727
}
2828

29-
extern FastRandomContext insecure_rand_ctx;
29+
thread_local extern FastRandomContext g_insecure_rand_ctx;
3030

3131
static inline void SeedInsecureRand(bool deterministic = false)
3232
{
33-
insecure_rand_ctx = FastRandomContext(deterministic);
33+
g_insecure_rand_ctx = FastRandomContext(deterministic);
3434
}
3535

36-
static inline uint32_t InsecureRand32() { return insecure_rand_ctx.rand32(); }
37-
static inline uint256 InsecureRand256() { return insecure_rand_ctx.rand256(); }
38-
static inline uint64_t InsecureRandBits(int bits) { return insecure_rand_ctx.randbits(bits); }
39-
static inline uint64_t InsecureRandRange(uint64_t range) { return insecure_rand_ctx.randrange(range); }
40-
static inline bool InsecureRandBool() { return insecure_rand_ctx.randbool(); }
36+
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
37+
static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
38+
static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
39+
static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
40+
static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
4141

4242
static constexpr CAmount CENT{1000000};
4343

@@ -67,7 +67,7 @@ struct CConnmanTest {
6767
};
6868

6969
class PeerLogicValidation;
70-
struct TestingSetup: public BasicTestingSetup {
70+
struct TestingSetup : public BasicTestingSetup {
7171
boost::thread_group threadGroup;
7272
CConnman* connman;
7373
CScheduler scheduler;

0 commit comments

Comments
 (0)