Skip to content

Commit

Permalink
tests: remove member connman/peerLogic in TestingSetup
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni authored and dongcarl committed Jan 16, 2019
1 parent 7cc2b9f commit 136bd79
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
38 changes: 33 additions & 5 deletions src/test/denialofservice_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@

#include <boost/test/unit_test.hpp>

struct CConnmanTest : public CConnman {
using CConnman::CConnman;
void AddNode(CNode& node)
{
LOCK(cs_vNodes);
vNodes.push_back(&node);
}
void ClearNodes()
{
LOCK(cs_vNodes);
for (CNode* node : vNodes) {
delete node;
}
vNodes.clear();
}
};

// Tests these internal-to-net_processing.cpp methods:
extern bool AddOrphanTx(const CTransactionRef& tx, NodeId peer);
extern void EraseOrphansFor(NodeId peer);
Expand Down Expand Up @@ -57,6 +74,8 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
// work.
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
{
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);

// Mock an outbound peer
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
Expand Down Expand Up @@ -109,7 +128,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
peerLogic->FinalizeNode(dummyNode1.GetId(), dummy);
}

static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic)
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman)
{
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));
Expand All @@ -120,11 +139,14 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidat
node.nVersion = 1;
node.fSuccessfullyConnected = true;

CConnmanTest::AddNode(node);
connman->AddNode(node);
}

BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
{
auto connman = MakeUnique<CConnmanTest>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);

const Consensus::Params& consensusParams = Params().GetConsensus();
constexpr int nMaxOutbound = 8;
CConnman::Options options;
Expand All @@ -137,7 +159,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)

// Mock some outbound peers
for (int i=0; i<nMaxOutbound; ++i) {
AddRandomOutboundPeer(vNodes, *peerLogic);
AddRandomOutboundPeer(vNodes, *peerLogic, connman.get());
}

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

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

CConnmanTest::ClearNodes();
connman->ClearNodes();
}

BOOST_AUTO_TEST_CASE(DoS_banning)
{
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);

connman->ClearBanned();
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
Expand Down Expand Up @@ -246,6 +270,8 @@ BOOST_AUTO_TEST_CASE(DoS_banning)

BOOST_AUTO_TEST_CASE(DoS_banscore)
{
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);

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

BOOST_AUTO_TEST_CASE(DoS_bantime)
{
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);

connman->ClearBanned();
int64_t nStartTime = GetTime();
Expand Down
18 changes: 0 additions & 18 deletions src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;

FastRandomContext g_insecure_rand_ctx;

void CConnmanTest::AddNode(CNode& node)
{
LOCK(g_connman->cs_vNodes);
g_connman->vNodes.push_back(&node);
}

void CConnmanTest::ClearNodes()
{
LOCK(g_connman->cs_vNodes);
for (const CNode* node : g_connman->vNodes) {
delete node;
}
g_connman->vNodes.clear();
}

std::ostream& operator<<(std::ostream& os, const uint256& num)
{
os << num.ToString();
Expand Down Expand Up @@ -109,8 +94,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck);
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
connman = g_connman.get();
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
}

TestingSetup::~TestingSetup()
Expand All @@ -120,7 +103,6 @@ TestingSetup::~TestingSetup()
GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler();
g_connman.reset();
peerLogic.reset();
UnloadBlockIndex();
pcoinsTip.reset();
pcoinsdbview.reset();
Expand Down
6 changes: 0 additions & 6 deletions src/test/test_bitcoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,11 @@ struct BasicTestingSetup {
*/
class CConnman;
class CNode;
struct CConnmanTest {
static void AddNode(CNode& node);
static void ClearNodes();
};

class PeerLogicValidation;
struct TestingSetup : public BasicTestingSetup {
boost::thread_group threadGroup;
CConnman* connman;
CScheduler scheduler;
std::unique_ptr<PeerLogicValidation> peerLogic;

explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~TestingSetup();
Expand Down

0 comments on commit 136bd79

Please sign in to comment.