Skip to content

Commit

Permalink
test: Make g_insecure_rand_ctx thread_local
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Dec 13, 2018
1 parent 9a43344 commit faead93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/test/denialofservice_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)

static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
{
CAddress addr(ip(insecure_rand_ctx.randbits(32)), NODE_NONE);
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
CNode &node = *vNodes.back();
node.SetSendVersion(PROTOCOL_VERSION);
Expand Down
26 changes: 12 additions & 14 deletions src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <crypto/sha256.h>
#include <miner.h>
#include <net_processing.h>
#include <noui.h>
#include <pow.h>
#include <rpc/register.h>
#include <rpc/server.h>
Expand All @@ -36,10 +37,7 @@ void CConnmanTest::ClearNodes()
g_connman->vNodes.clear();
}

FastRandomContext insecure_rand_ctx;

extern bool fPrintToConsole;
extern void noui_connect();
thread_local FastRandomContext g_insecure_rand_ctx;

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

TestingSetup::~TestingSetup()
{
threadGroup.interrupt_all();
threadGroup.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
g_connman.reset();
peerLogic.reset();
UnloadBlockIndex();
pcoinsTip.reset();
pcoinsdbview.reset();
pblocktree.reset();
threadGroup.interrupt_all();
threadGroup.join_all();
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
g_connman.reset();
peerLogic.reset();
UnloadBlockIndex();
pcoinsTip.reset();
pcoinsdbview.reset();
pblocktree.reset();
}

TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
Expand Down
16 changes: 8 additions & 8 deletions src/test/test_bitcoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
return stream << static_cast<typename std::underlying_type<T>::type>(e);
}

extern FastRandomContext insecure_rand_ctx;
thread_local extern FastRandomContext g_insecure_rand_ctx;

static inline void SeedInsecureRand(bool deterministic = false)
{
insecure_rand_ctx = FastRandomContext(deterministic);
g_insecure_rand_ctx = FastRandomContext(deterministic);
}

static inline uint32_t InsecureRand32() { return insecure_rand_ctx.rand32(); }
static inline uint256 InsecureRand256() { return insecure_rand_ctx.rand256(); }
static inline uint64_t InsecureRandBits(int bits) { return insecure_rand_ctx.randbits(bits); }
static inline uint64_t InsecureRandRange(uint64_t range) { return insecure_rand_ctx.randrange(range); }
static inline bool InsecureRandBool() { return insecure_rand_ctx.randbool(); }
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }

static constexpr CAmount CENT{1000000};

Expand Down Expand Up @@ -67,7 +67,7 @@ struct CConnmanTest {
};

class PeerLogicValidation;
struct TestingSetup: public BasicTestingSetup {
struct TestingSetup : public BasicTestingSetup {
boost::thread_group threadGroup;
CConnman* connman;
CScheduler scheduler;
Expand Down

0 comments on commit faead93

Please sign in to comment.