Skip to content

Commit 136bd79

Browse files
theunidongcarl
authored andcommitted
tests: remove member connman/peerLogic in TestingSetup
1 parent 7cc2b9f commit 136bd79

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

src/test/denialofservice_tests.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@
2020

2121
#include <boost/test/unit_test.hpp>
2222

23+
struct CConnmanTest : public CConnman {
24+
using CConnman::CConnman;
25+
void AddNode(CNode& node)
26+
{
27+
LOCK(cs_vNodes);
28+
vNodes.push_back(&node);
29+
}
30+
void ClearNodes()
31+
{
32+
LOCK(cs_vNodes);
33+
for (CNode* node : vNodes) {
34+
delete node;
35+
}
36+
vNodes.clear();
37+
}
38+
};
39+
2340
// Tests these internal-to-net_processing.cpp methods:
2441
extern bool AddOrphanTx(const CTransactionRef& tx, NodeId peer);
2542
extern void EraseOrphansFor(NodeId peer);
@@ -57,6 +74,8 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
5774
// work.
5875
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
5976
{
77+
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
78+
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
6079

6180
// Mock an outbound peer
6281
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@@ -109,7 +128,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
109128
peerLogic->FinalizeNode(dummyNode1.GetId(), dummy);
110129
}
111130

112-
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
131+
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman)
113132
{
114133
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
115134
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
@@ -120,11 +139,14 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidat
120139
node.nVersion = 1;
121140
node.fSuccessfullyConnected = true;
122141

123-
CConnmanTest::AddNode(node);
142+
connman->AddNode(node);
124143
}
125144

126145
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
127146
{
147+
auto connman = MakeUnique<CConnmanTest>(0x1337, 0x1337);
148+
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
149+
128150
const Consensus::Params& consensusParams = Params().GetConsensus();
129151
constexpr int nMaxOutbound = 8;
130152
CConnman::Options options;
@@ -137,7 +159,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
137159

138160
// Mock some outbound peers
139161
for (int i=0; i<nMaxOutbound; ++i) {
140-
AddRandomOutboundPeer(vNodes, *peerLogic);
162+
AddRandomOutboundPeer(vNodes, *peerLogic, connman.get());
141163
}
142164

143165
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams);
@@ -162,7 +184,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
162184
// If we add one more peer, something should get marked for eviction
163185
// on the next check (since we're mocking the time to be in the future, the
164186
// required time connected check should be satisfied).
165-
AddRandomOutboundPeer(vNodes, *peerLogic);
187+
AddRandomOutboundPeer(vNodes, *peerLogic, connman.get());
166188

167189
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams);
168190
for (int i=0; i<nMaxOutbound; ++i) {
@@ -189,11 +211,13 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
189211
peerLogic->FinalizeNode(node->GetId(), dummy);
190212
}
191213

192-
CConnmanTest::ClearNodes();
214+
connman->ClearNodes();
193215
}
194216

195217
BOOST_AUTO_TEST_CASE(DoS_banning)
196218
{
219+
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
220+
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
197221

198222
connman->ClearBanned();
199223
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@@ -246,6 +270,8 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
246270

247271
BOOST_AUTO_TEST_CASE(DoS_banscore)
248272
{
273+
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
274+
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
249275

250276
connman->ClearBanned();
251277
gArgs.ForceSetArg("-banscore", "111"); // because 11 is my favorite number
@@ -290,6 +316,8 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
290316

291317
BOOST_AUTO_TEST_CASE(DoS_bantime)
292318
{
319+
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
320+
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
293321

294322
connman->ClearBanned();
295323
int64_t nStartTime = GetTime();

src/test/test_bitcoin.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
2424

2525
FastRandomContext g_insecure_rand_ctx;
2626

27-
void CConnmanTest::AddNode(CNode& node)
28-
{
29-
LOCK(g_connman->cs_vNodes);
30-
g_connman->vNodes.push_back(&node);
31-
}
32-
33-
void CConnmanTest::ClearNodes()
34-
{
35-
LOCK(g_connman->cs_vNodes);
36-
for (const CNode* node : g_connman->vNodes) {
37-
delete node;
38-
}
39-
g_connman->vNodes.clear();
40-
}
41-
4227
std::ostream& operator<<(std::ostream& os, const uint256& num)
4328
{
4429
os << num.ToString();
@@ -109,8 +94,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
10994
for (int i=0; i < nScriptCheckThreads-1; i++)
11095
threadGroup.create_thread(&ThreadScriptCheck);
11196
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
112-
connman = g_connman.get();
113-
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
11497
}
11598

11699
TestingSetup::~TestingSetup()
@@ -120,7 +103,6 @@ TestingSetup::~TestingSetup()
120103
GetMainSignals().FlushBackgroundCallbacks();
121104
GetMainSignals().UnregisterBackgroundSignalScheduler();
122105
g_connman.reset();
123-
peerLogic.reset();
124106
UnloadBlockIndex();
125107
pcoinsTip.reset();
126108
pcoinsdbview.reset();

src/test/test_bitcoin.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,11 @@ struct BasicTestingSetup {
6868
*/
6969
class CConnman;
7070
class CNode;
71-
struct CConnmanTest {
72-
static void AddNode(CNode& node);
73-
static void ClearNodes();
74-
};
7571

7672
class PeerLogicValidation;
7773
struct TestingSetup : public BasicTestingSetup {
7874
boost::thread_group threadGroup;
79-
CConnman* connman;
8075
CScheduler scheduler;
81-
std::unique_ptr<PeerLogicValidation> peerLogic;
8276

8377
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
8478
~TestingSetup();

0 commit comments

Comments
 (0)