Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/processor_tests.cpp
Show All 29 Lines | struct AvalancheTest { | ||||
static std::vector<CInv> getInvsForNextPoll(Processor &p) { | static std::vector<CInv> getInvsForNextPoll(Processor &p) { | ||||
return p.getInvsForNextPoll(false); | return p.getInvsForNextPoll(false); | ||||
} | } | ||||
static NodeId getSuitableNodeToQuery(Processor &p) { | static NodeId getSuitableNodeToQuery(Processor &p) { | ||||
return p.getSuitableNodeToQuery(); | return p.getSuitableNodeToQuery(); | ||||
} | } | ||||
static avalanche::PeerManager &getPeerManager(Processor &p) { | template <typename F> static auto withPeerManager(Processor &p, F fun) { | ||||
LOCK(p.cs_peerManager); | LOCK(p.cs_peerManager); | ||||
return *p.peerManager; | return fun(*p.peerManager); | ||||
} | } | ||||
static uint64_t getRound(const Processor &p) { return p.round; } | static uint64_t getRound(const Processor &p) { return p.round; } | ||||
}; | }; | ||||
} // namespace | } // namespace | ||||
} // namespace avalanche | } // namespace avalanche | ||||
namespace { | namespace { | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | struct AvalancheTestingSetup : public TestChain100Setup { | ||||
} | } | ||||
bool addNode(NodeId nodeid) { | bool addNode(NodeId nodeid) { | ||||
auto proof = GetProof(); | auto proof = GetProof(); | ||||
return m_processor->addNode(nodeid, proof, | return m_processor->addNode(nodeid, proof, | ||||
DelegationBuilder(*proof).build()); | DelegationBuilder(*proof).build()); | ||||
} | } | ||||
template <typename F> auto withPeerManager(F fun) { | |||||
return AvalancheTest::withPeerManager(*m_processor, fun); | |||||
} | |||||
std::array<CNode *, 8> ConnectNodes() { | std::array<CNode *, 8> ConnectNodes() { | ||||
avalanche::PeerManager &pm = getPeerManager(); | return withPeerManager([&](avalanche::PeerManager &pm) { | ||||
auto proof = GetProof(); | auto proof = GetProof(); | ||||
Delegation dg = DelegationBuilder(*proof).build(); | Delegation dg = DelegationBuilder(*proof).build(); | ||||
std::array<CNode *, 8> nodes; | std::array<CNode *, 8> nodes; | ||||
for (CNode *&n : nodes) { | for (CNode *&n : nodes) { | ||||
n = ConnectNode(NODE_AVALANCHE); | n = ConnectNode(NODE_AVALANCHE); | ||||
BOOST_CHECK(pm.addNode(n->GetId(), proof, dg)); | BOOST_CHECK(pm.addNode(n->GetId(), proof, dg)); | ||||
} | } | ||||
return nodes; | return nodes; | ||||
}); | |||||
} | } | ||||
void runEventLoop() { AvalancheTest::runEventLoop(*m_processor); } | void runEventLoop() { AvalancheTest::runEventLoop(*m_processor); } | ||||
NodeId getSuitableNodeToQuery() { | NodeId getSuitableNodeToQuery() { | ||||
return AvalancheTest::getSuitableNodeToQuery(*m_processor); | return AvalancheTest::getSuitableNodeToQuery(*m_processor); | ||||
} | } | ||||
std::vector<CInv> getInvsForNextPoll() { | std::vector<CInv> getInvsForNextPoll() { | ||||
return AvalancheTest::getInvsForNextPoll(*m_processor); | return AvalancheTest::getInvsForNextPoll(*m_processor); | ||||
} | } | ||||
avalanche::PeerManager &getPeerManager() { | |||||
return AvalancheTest::getPeerManager(*m_processor); | |||||
} | |||||
uint64_t getRound() const { return AvalancheTest::getRound(*m_processor); } | uint64_t getRound() const { return AvalancheTest::getRound(*m_processor); } | ||||
}; | }; | ||||
} // namespace | } // namespace | ||||
BOOST_FIXTURE_TEST_SUITE(processor_tests, AvalancheTestingSetup) | BOOST_FIXTURE_TEST_SUITE(processor_tests, AvalancheTestingSetup) | ||||
#define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | #define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | ||||
vr.registerVote(NO_NODE, vote); \ | vr.registerVote(NO_NODE, vote); \ | ||||
▲ Show 20 Lines • Show All 566 Lines • ▼ Show 20 Lines | for (int i = 0; i < 10; i++) { | ||||
runEventLoop(); | runEventLoop(); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
!m_processor->registerVotes(avanodeid, next(resp), updates)); | !m_processor->registerVotes(avanodeid, next(resp), updates)); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poll_inflight_count) { | BOOST_AUTO_TEST_CASE(poll_inflight_count) { | ||||
// Create enough nodes so that we run into the inflight request limit. | // Create enough nodes so that we run into the inflight request limit. | ||||
avalanche::PeerManager &pm = getPeerManager(); | withPeerManager([&](avalanche::PeerManager &pm) { | ||||
auto proof = GetProof(); | auto proof = GetProof(); | ||||
Delegation dg = DelegationBuilder(*proof).build(); | Delegation dg = DelegationBuilder(*proof).build(); | ||||
std::array<CNode *, AVALANCHE_MAX_INFLIGHT_POLL + 1> nodes; | std::array<CNode *, AVALANCHE_MAX_INFLIGHT_POLL + 1> nodes; | ||||
for (auto &n : nodes) { | for (auto &n : nodes) { | ||||
n = ConnectNode(NODE_AVALANCHE); | n = ConnectNode(NODE_AVALANCHE); | ||||
BOOST_CHECK(pm.addNode(n->GetId(), proof, dg)); | BOOST_CHECK(pm.addNode(n->GetId(), proof, dg)); | ||||
} | } | ||||
}); | |||||
// Add a block to poll | // Add a block to poll | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const BlockHash blockHash = block.GetHash(); | const BlockHash blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex; | const CBlockIndex *pindex; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
pindex = LookupBlockIndex(blockHash); | pindex = LookupBlockIndex(blockHash); | ||||
▲ Show 20 Lines • Show All 248 Lines • Show Last 20 Lines |