Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/processor_tests.cpp
// Copyright (c) 2018-2020 The Bitcoin developers | // Copyright (c) 2018-2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/processor.h> | #include <avalanche/processor.h> | ||||
#include <avalanche/delegationbuilder.h> | |||||
#include <avalanche/peermanager.h> | #include <avalanche/peermanager.h> | ||||
#include <avalanche/proofbuilder.h> | #include <avalanche/proofbuilder.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <net_processing.h> // For PeerLogicValidation | #include <net_processing.h> // For PeerLogicValidation | ||||
#include <util/time.h> | #include <util/time.h> | ||||
// D6970 moved LookupBlockIndex from chain.h to validation.h TODO: remove this | // D6970 moved LookupBlockIndex from chain.h to validation.h TODO: remove this | ||||
// when LookupBlockIndex is refactored out of validation | // when LookupBlockIndex is refactored out of validation | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | Proof GetProof() { | ||||
const CTransaction &coinbase = *m_coinbase_txns[current_coinbase]; | const CTransaction &coinbase = *m_coinbase_txns[current_coinbase]; | ||||
ProofBuilder pb(0, 0, masterpriv.GetPubKey()); | ProofBuilder pb(0, 0, masterpriv.GetPubKey()); | ||||
BOOST_CHECK(pb.addUTXO(COutPoint(coinbase.GetId(), 0), | BOOST_CHECK(pb.addUTXO(COutPoint(coinbase.GetId(), 0), | ||||
coinbase.vout[0].nValue, current_coinbase + 1, | coinbase.vout[0].nValue, current_coinbase + 1, | ||||
true, coinbaseKey)); | true, coinbaseKey)); | ||||
return pb.build(); | return pb.build(); | ||||
} | } | ||||
bool addNode(NodeId nodeid) { | |||||
Proof proof = GetProof(); | |||||
return m_processor->addNode(nodeid, proof, | |||||
DelegationBuilder(proof).build()); | |||||
} | |||||
std::array<CNode *, 8> ConnectNodes() { | std::array<CNode *, 8> ConnectNodes() { | ||||
PeerManager &pm = getPeerManager(); | PeerManager &pm = getPeerManager(); | ||||
Proof proof = GetProof(); | Proof proof = GetProof(); | ||||
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, masterpriv.GetPubKey())); | 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() { | ||||
▲ Show 20 Lines • Show All 415 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(poll_and_response) { | ||||
// There is no node to query. | // There is no node to query. | ||||
BOOST_CHECK_EQUAL(getSuitableNodeToQuery(), NO_NODE); | BOOST_CHECK_EQUAL(getSuitableNodeToQuery(), NO_NODE); | ||||
// Create a node that supports avalanche and one that doesn't. | // Create a node that supports avalanche and one that doesn't. | ||||
ConnectNode(NODE_NONE); | ConnectNode(NODE_NONE); | ||||
auto avanode = ConnectNode(NODE_AVALANCHE); | auto avanode = ConnectNode(NODE_AVALANCHE); | ||||
NodeId avanodeid = avanode->GetId(); | NodeId avanodeid = avanode->GetId(); | ||||
BOOST_CHECK(m_processor->addNode(avanodeid, GetProof(), CPubKey())); | BOOST_CHECK(addNode(avanodeid)); | ||||
// It returns the avalanche peer. | // It returns the avalanche peer. | ||||
BOOST_CHECK_EQUAL(getSuitableNodeToQuery(), avanodeid); | BOOST_CHECK_EQUAL(getSuitableNodeToQuery(), avanodeid); | ||||
// Register a block and check it is added to the list of elements to poll. | // Register a block and check it is added to the list of elements to poll. | ||||
BOOST_CHECK(m_processor->addBlockToReconcile(pindex)); | BOOST_CHECK(m_processor->addBlockToReconcile(pindex)); | ||||
auto invs = getInvsForNextPoll(); | auto invs = getInvsForNextPoll(); | ||||
BOOST_CHECK_EQUAL(invs.size(), 1); | BOOST_CHECK_EQUAL(invs.size(), 1); | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(poll_inflight_timeout, *boost::unit_test::timeout(60)) { | ||||
} | } | ||||
// Add the block | // Add the block | ||||
BOOST_CHECK(m_processor->addBlockToReconcile(pindex)); | BOOST_CHECK(m_processor->addBlockToReconcile(pindex)); | ||||
// Create a node that supports avalanche. | // Create a node that supports avalanche. | ||||
auto avanode = ConnectNode(NODE_AVALANCHE); | auto avanode = ConnectNode(NODE_AVALANCHE); | ||||
NodeId avanodeid = avanode->GetId(); | NodeId avanodeid = avanode->GetId(); | ||||
BOOST_CHECK(m_processor->addNode(avanodeid, GetProof(), CPubKey())); | BOOST_CHECK(addNode(avanodeid)); | ||||
// Expire requests after some time. | // Expire requests after some time. | ||||
auto queryTimeDuration = std::chrono::milliseconds(10); | auto queryTimeDuration = std::chrono::milliseconds(10); | ||||
m_processor->setQueryTimeoutDuration(queryTimeDuration); | m_processor->setQueryTimeoutDuration(queryTimeDuration); | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
Response resp = {getRound(), 0, {Vote(0, blockHash)}}; | Response resp = {getRound(), 0, {Vote(0, blockHash)}}; | ||||
auto start = std::chrono::steady_clock::now(); | auto start = std::chrono::steady_clock::now(); | ||||
Show All 23 Lines | for (int i = 0; i < 10; i++) { | ||||
!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. | ||||
PeerManager &pm = getPeerManager(); | PeerManager &pm = getPeerManager(); | ||||
Proof proof = GetProof(); | Proof proof = GetProof(); | ||||
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, CPubKey())); | 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); | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(event_loop) { | ||||
BOOST_CHECK(!m_processor->startEventLoop(s)); | BOOST_CHECK(!m_processor->startEventLoop(s)); | ||||
// Start the scheduler thread. | // Start the scheduler thread. | ||||
std::thread schedulerThread(std::bind(&CScheduler::serviceQueue, &s)); | std::thread schedulerThread(std::bind(&CScheduler::serviceQueue, &s)); | ||||
// Create a node that supports avalanche. | // Create a node that supports avalanche. | ||||
auto avanode = ConnectNode(NODE_AVALANCHE); | auto avanode = ConnectNode(NODE_AVALANCHE); | ||||
NodeId nodeid = avanode->GetId(); | NodeId nodeid = avanode->GetId(); | ||||
BOOST_CHECK(m_processor->addNode(nodeid, GetProof(), CPubKey())); | BOOST_CHECK(addNode(nodeid)); | ||||
// There is no query in flight at the moment. | // There is no query in flight at the moment. | ||||
BOOST_CHECK_EQUAL(getSuitableNodeToQuery(), nodeid); | BOOST_CHECK_EQUAL(getSuitableNodeToQuery(), nodeid); | ||||
// Add a new block. Check it is added to the polls. | // Add a new block. Check it is added to the polls. | ||||
uint64_t queryRound = getRound(); | uint64_t queryRound = getRound(); | ||||
BOOST_CHECK(m_processor->addBlockToReconcile(pindex)); | BOOST_CHECK(m_processor->addBlockToReconcile(pindex)); | ||||
▲ Show 20 Lines • Show All 71 Lines • Show Last 20 Lines |