aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2018-12-13 16:44:26 -0500
committerMarcoFalke <falke.marco@gmail.com>2018-12-13 16:44:36 -0500
commita5aea9654f3661254a90815a85067487647fbce9 (patch)
treec98d48a195cd410f774ebe3ec55c9c910b876911
parent9a4334443085970a42792db3528695585fe7053b (diff)
parentfaead93c6c7e1d27bd2a1b2a9b496fda838c2996 (diff)
downloadbitcoin-a5aea9654f3661254a90815a85067487647fbce9.tar.xz
Merge #14953: test: Make g_insecure_rand_ctx thread_local
faead93c6c test: Make g_insecure_rand_ctx thread_local (MarcoFalke) Pull request description: Some tests might spin up several threads and `FastRandomContext` is not thread safe. Fix that by giving each thread their own randomness context (as opposed to e.g. making `FastRandomContext` thread safe or add locks elsewhere). Also, add the `g_` prefix to it (according to developer notes), since I am touching it anyway. Tree-SHA512: c6b61375636dfbb2f8311efe8b47e9fe7c4f8bee9804871243f877545f3117cb6aa8556a2d9b1d1673e46e2e585b695a8ddd235b746b583c3eab962435efe2d1
-rw-r--r--src/test/denialofservice_tests.cpp2
-rw-r--r--src/test/test_bitcoin.cpp26
-rw-r--r--src/test/test_bitcoin.h16
3 files changed, 21 insertions, 23 deletions
diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp
index 5291dfdd4b..4fbf8ee0a8 100644
--- a/src/test/denialofservice_tests.cpp
+++ b/src/test/denialofservice_tests.cpp
@@ -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);
diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp
index 9b4076d956..bb8db9fa8d 100644
--- a/src/test/test_bitcoin.cpp
+++ b/src/test/test_bitcoin.cpp
@@ -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>
@@ -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)
{
@@ -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)
diff --git a/src/test/test_bitcoin.h b/src/test/test_bitcoin.h
index 124f862de8..71c0379eac 100644
--- a/src/test/test_bitcoin.h
+++ b/src/test/test_bitcoin.h
@@ -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};
@@ -67,7 +67,7 @@ struct CConnmanTest {
};
class PeerLogicValidation;
-struct TestingSetup: public BasicTestingSetup {
+struct TestingSetup : public BasicTestingSetup {
boost::thread_group threadGroup;
CConnman* connman;
CScheduler scheduler;