Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
Show All 19 Lines | struct AvalancheTest { | ||||
static NodeId getSuitableNodeToQuery(AvalancheProcessor &p) { | static NodeId getSuitableNodeToQuery(AvalancheProcessor &p) { | ||||
return p.getSuitableNodeToQuery(); | return p.getSuitableNodeToQuery(); | ||||
} | } | ||||
static uint64_t getRound(const AvalancheProcessor &p) { return p.round; } | static uint64_t getRound(const AvalancheProcessor &p) { return p.round; } | ||||
}; | }; | ||||
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(); | |||||
} | |||||
}; | |||||
BOOST_FIXTURE_TEST_SUITE(avalanche_tests, TestChain100Setup) | BOOST_FIXTURE_TEST_SUITE(avalanche_tests, TestChain100Setup) | ||||
#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); \ | ||||
BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | ||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | ||||
BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | ||||
▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
CService ip(uint32_t i) { | CService ip(uint32_t i) { | ||||
struct in_addr s; | struct in_addr s; | ||||
s.s_addr = i; | s.s_addr = i; | ||||
return CService(CNetAddr(s), Params().GetDefaultPort()); | return CService(CNetAddr(s), Params().GetDefaultPort()); | ||||
} | } | ||||
std::unique_ptr<CNode> ConnectNode(const Config &config, ServiceFlags nServices, | CNode *ConnectNode(const Config &config, ServiceFlags nServices, | ||||
PeerLogicValidation &peerLogic) { | PeerLogicValidation &peerLogic, CConnmanTest *connman) { | ||||
static NodeId id = 0; | static NodeId id = 0; | ||||
CAddress addr(ip(GetRandInt(0xffffffff)), NODE_NONE); | CAddress addr(ip(GetRandInt(0xffffffff)), NODE_NONE); | ||||
std::unique_ptr<CNode> nodeptr(new CNode(id++, ServiceFlags(NODE_NETWORK), | auto node = new CNode(id++, ServiceFlags(NODE_NETWORK), 0, INVALID_SOCKET, | ||||
0, INVALID_SOCKET, addr, 0, 0, | addr, 0, 0, CAddress(), "", | ||||
CAddress(), "", | /*fInboundIn=*/false); | ||||
/*fInboundIn=*/false)); | node->SetSendVersion(PROTOCOL_VERSION); | ||||
CNode &node = *nodeptr; | node->nServices = nServices; | ||||
node.SetSendVersion(PROTOCOL_VERSION); | peerLogic.InitializeNode(config, node); | ||||
node.nServices = nServices; | node->nVersion = 1; | ||||
peerLogic.InitializeNode(config, &node); | node->fSuccessfullyConnected = true; | ||||
node.nVersion = 1; | |||||
node.fSuccessfullyConnected = true; | connman->AddNode(*node); | ||||
return node; | |||||
CConnmanTest::AddNode(node); | } | ||||
return nodeptr; | |||||
} | std::array<CNode *, 8> ConnectNodes(const Config &config, AvalancheProcessor &p, | ||||
ServiceFlags nServices, | |||||
std::array<std::unique_ptr<CNode>, 8> | PeerLogicValidation &peerLogic, | ||||
ConnectNodes(const Config &config, AvalancheProcessor &p, | CConnmanTest *connman) { | ||||
ServiceFlags nServices, PeerLogicValidation &peerLogic) { | std::array<CNode *, 8> nodes; | ||||
std::array<std::unique_ptr<CNode>, 8> nodes; | for (CNode *&n : nodes) { | ||||
for (auto &n : nodes) { | n = ConnectNode(config, nServices, peerLogic, connman); | ||||
n = ConnectNode(config, nServices, peerLogic); | |||||
BOOST_CHECK(p.addPeer(n->GetId(), 0)); | BOOST_CHECK(p.addPeer(n->GetId(), 0)); | ||||
} | } | ||||
return nodes; | return nodes; | ||||
} | } | ||||
static AvalancheResponse next(AvalancheResponse &r) { | static AvalancheResponse next(AvalancheResponse &r) { | ||||
auto copy = r; | auto copy = r; | ||||
r = {r.getRound() + 1, r.getCooldown(), r.GetVotes()}; | r = {r.getRound() + 1, r.getCooldown(), r.GetVotes()}; | ||||
return copy; | return copy; | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(block_register) { | BOOST_AUTO_TEST_CASE(block_register) { | ||||
AvalancheProcessor p(g_connman.get()); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
const Config &config = GetConfig(); | |||||
// Create nodes that supports avalanche. | // Create nodes that supports avalanche. | ||||
auto avanodes = ConnectNodes(config, p, NODE_AVALANCHE, *peerLogic); | auto avanodes = | ||||
ConnectNodes(config, p, NODE_AVALANCHE, *peerLogic, connman.get()); | |||||
// Querying for random block returns false. | // Querying for random block returns false. | ||||
BOOST_CHECK(!p.isAccepted(pindex)); | BOOST_CHECK(!p.isAccepted(pindex)); | ||||
// Add a new block. Check it is added to the polls. | // Add a new block. Check it is added to the polls. | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
auto invs = AvalancheTest::getInvsForNextPoll(p); | auto invs = AvalancheTest::getInvsForNextPoll(p); | ||||
BOOST_CHECK_EQUAL(invs.size(), 1); | BOOST_CHECK_EQUAL(invs.size(), 1); | ||||
▲ Show 20 Lines • Show All 128 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(block_register) { | ||||
invs = AvalancheTest::getInvsForNextPoll(p); | invs = AvalancheTest::getInvsForNextPoll(p); | ||||
BOOST_CHECK_EQUAL(invs.size(), 0); | BOOST_CHECK_EQUAL(invs.size(), 0); | ||||
// Adding the block twice does nothing. | // Adding the block twice does nothing. | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
BOOST_CHECK(!p.addBlockToReconcile(pindex)); | BOOST_CHECK(!p.addBlockToReconcile(pindex)); | ||||
BOOST_CHECK(p.isAccepted(pindex)); | BOOST_CHECK(p.isAccepted(pindex)); | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(multi_block_register) { | BOOST_AUTO_TEST_CASE(multi_block_register) { | ||||
AvalancheProcessor p(g_connman.get()); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
CBlockIndex indexA, indexB; | CBlockIndex indexA, indexB; | ||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
const Config &config = GetConfig(); | |||||
// Create several nodes that support avalanche. | // Create several nodes that support avalanche. | ||||
auto avanodes = ConnectNodes(config, p, NODE_AVALANCHE, *peerLogic); | auto avanodes = | ||||
ConnectNodes(config, p, NODE_AVALANCHE, *peerLogic, connman.get()); | |||||
// Make sure the block has a hash. | // Make sure the block has a hash. | ||||
CBlock blockA = CreateAndProcessBlock({}, CScript()); | CBlock blockA = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHashA = blockA.GetHash(); | const uint256 blockHashA = blockA.GetHash(); | ||||
const CBlockIndex *pindexA = mapBlockIndex[blockHashA]; | const CBlockIndex *pindexA = mapBlockIndex[blockHashA]; | ||||
CBlock blockB = CreateAndProcessBlock({}, CScript()); | CBlock blockB = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHashB = blockB.GetHash(); | const uint256 blockHashB = blockB.GetHash(); | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(multi_block_register) { | ||||
BOOST_CHECK_EQUAL(updates[0].getStatus(), | BOOST_CHECK_EQUAL(updates[0].getStatus(), | ||||
AvalancheBlockUpdate::Status::Finalized); | AvalancheBlockUpdate::Status::Finalized); | ||||
updates = {}; | updates = {}; | ||||
// There is nothing left to vote on. | // There is nothing left to vote on. | ||||
invs = AvalancheTest::getInvsForNextPoll(p); | invs = AvalancheTest::getInvsForNextPoll(p); | ||||
BOOST_CHECK_EQUAL(invs.size(), 0); | BOOST_CHECK_EQUAL(invs.size(), 0); | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poll_and_response) { | BOOST_AUTO_TEST_CASE(poll_and_response) { | ||||
AvalancheProcessor p(g_connman.get()); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
const Config &config = GetConfig(); | |||||
// There is no node to query. | // There is no node to query. | ||||
BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), NO_NODE); | BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), NO_NODE); | ||||
// Create a node that supports avalanche and one that doesn't. | // Create a node that supports avalanche and one that doesn't. | ||||
auto oldnode = ConnectNode(config, NODE_NONE, *peerLogic); | ConnectNode(config, NODE_NONE, *peerLogic, connman.get()); | ||||
auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic); | auto avanode = | ||||
ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); | |||||
NodeId avanodeid = avanode->GetId(); | NodeId avanodeid = avanode->GetId(); | ||||
BOOST_CHECK(p.addPeer(avanodeid, 0)); | BOOST_CHECK(p.addPeer(avanodeid, 0)); | ||||
// It returns the avalanche peer. | // It returns the avalanche peer. | ||||
BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); | BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), 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(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
▲ Show 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(poll_and_response) { | ||||
// When a block is marked invalid, stop polling. | // When a block is marked invalid, stop polling. | ||||
pindex2->nStatus = pindex2->nStatus.withFailed(); | pindex2->nStatus = pindex2->nStatus.withFailed(); | ||||
resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(0, blockHash)}}; | resp = {AvalancheTest::getRound(p), 0, {AvalancheVote(0, blockHash)}}; | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(avanodeid, resp, updates)); | BOOST_CHECK(p.registerVotes(avanodeid, resp, updates)); | ||||
BOOST_CHECK_EQUAL(updates.size(), 0); | BOOST_CHECK_EQUAL(updates.size(), 0); | ||||
BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); | BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poll_inflight_timeout, *boost::unit_test::timeout(60)) { | BOOST_AUTO_TEST_CASE(poll_inflight_timeout, *boost::unit_test::timeout(60)) { | ||||
AvalancheProcessor p(g_connman.get()); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
// Add the block | // Add the block | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
// Create a node that supports avalanche. | // Create a node that supports avalanche. | ||||
const Config &config = GetConfig(); | auto avanode = | ||||
auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic); | ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); | ||||
NodeId avanodeid = avanode->GetId(); | NodeId avanodeid = avanode->GetId(); | ||||
BOOST_CHECK(p.addPeer(avanodeid, 0)); | BOOST_CHECK(p.addPeer(avanodeid, 0)); | ||||
// Expire requests after some time. | // Expire requests after some time. | ||||
auto queryTimeDuration = std::chrono::milliseconds(10); | auto queryTimeDuration = std::chrono::milliseconds(10); | ||||
p.setQueryTimeoutDuration(queryTimeDuration); | p.setQueryTimeoutDuration(queryTimeDuration); | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
AvalancheResponse resp = { | AvalancheResponse resp = { | ||||
Show All 20 Lines | for (int i = 0; i < 10; i++) { | ||||
// Now try again but wait for expiration. | // Now try again but wait for expiration. | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
std::this_thread::sleep_for(queryTimeDuration); | std::this_thread::sleep_for(queryTimeDuration); | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(!p.registerVotes(avanodeid, next(resp), updates)); | BOOST_CHECK(!p.registerVotes(avanodeid, next(resp), updates)); | ||||
} | } | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poll_inflight_count) { | BOOST_AUTO_TEST_CASE(poll_inflight_count) { | ||||
AvalancheProcessor p(g_connman.get()); | |||||
const Config &config = GetConfig(); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
// Create enough nodes so that we run into the inflight request limit. | // Create enough nodes so that we run into the inflight request limit. | ||||
std::array<std::unique_ptr<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(config, NODE_AVALANCHE, *peerLogic); | n = ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); | ||||
BOOST_CHECK(p.addPeer(n->GetId(), 0)); | BOOST_CHECK(p.addPeer(n->GetId(), 0)); | ||||
} | } | ||||
// Add a block to poll | // Add a block to poll | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
Show All 27 Lines | BOOST_AUTO_TEST_CASE(poll_inflight_count) { | ||||
BOOST_CHECK(p.registerVotes(it->first, resp, updates)); | BOOST_CHECK(p.registerVotes(it->first, resp, updates)); | ||||
node_round_map.erase(it); | node_round_map.erase(it); | ||||
invs = AvalancheTest::getInvsForNextPoll(p); | invs = AvalancheTest::getInvsForNextPoll(p); | ||||
BOOST_CHECK_EQUAL(invs.size(), 1); | BOOST_CHECK_EQUAL(invs.size(), 1); | ||||
BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | BOOST_CHECK_EQUAL(invs[0].type, MSG_BLOCK); | ||||
BOOST_CHECK(invs[0].hash == blockHash); | BOOST_CHECK(invs[0].hash == blockHash); | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(quorum_diversity) { | BOOST_AUTO_TEST_CASE(quorum_diversity) { | ||||
AvalancheProcessor p(g_connman.get()); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
const Config &config = GetConfig(); | |||||
// Create nodes that supports avalanche. | // Create nodes that supports avalanche. | ||||
auto avanodes = ConnectNodes(config, p, NODE_AVALANCHE, *peerLogic); | auto avanodes = | ||||
ConnectNodes(config, p, NODE_AVALANCHE, *peerLogic, connman.get()); | |||||
// Querying for random block returns false. | // Querying for random block returns false. | ||||
BOOST_CHECK(!p.isAccepted(pindex)); | BOOST_CHECK(!p.isAccepted(pindex)); | ||||
// Add a new block. Check it is added to the polls. | // Add a new block. Check it is added to the polls. | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
// Do one valid round of voting. | // Do one valid round of voting. | ||||
Show All 36 Lines | for (auto &pair : node_round_map) { | ||||
nodeid, {r, 0, {AvalancheVote(0, blockHash)}}, updates)); | nodeid, {r, 0, {AvalancheVote(0, blockHash)}}, updates)); | ||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), confidence); | BOOST_CHECK_EQUAL(p.getConfidence(pindex), confidence); | ||||
} | } | ||||
BOOST_CHECK(p.registerVotes( | BOOST_CHECK(p.registerVotes( | ||||
firstNodeId, {round, 0, {AvalancheVote(0, blockHash)}}, updates)); | firstNodeId, {round, 0, {AvalancheVote(0, blockHash)}}, updates)); | ||||
BOOST_CHECK_EQUAL(p.getConfidence(pindex), confidence + 1); | BOOST_CHECK_EQUAL(p.getConfidence(pindex), confidence + 1); | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(event_loop) { | BOOST_AUTO_TEST_CASE(event_loop) { | ||||
AvalancheProcessor p(g_connman.get()); | const Config &config = GetConfig(); | ||||
auto connman = std::make_unique<CConnmanTest>(config, 0x1337, 0x1337); | |||||
auto peerLogic = | |||||
std::make_unique<PeerLogicValidation>(connman.get(), scheduler, false); | |||||
AvalancheProcessor p(connman.get()); | |||||
CScheduler s; | CScheduler s; | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
// Starting the event loop. | // Starting the event loop. | ||||
BOOST_CHECK(p.startEventLoop(s)); | BOOST_CHECK(p.startEventLoop(s)); | ||||
// There is one task planned in the next hour (our event loop). | // There is one task planned in the next hour (our event loop). | ||||
boost::chrono::system_clock::time_point start, stop; | boost::chrono::system_clock::time_point start, stop; | ||||
BOOST_CHECK_EQUAL(s.getQueueInfo(start, stop), 1); | BOOST_CHECK_EQUAL(s.getQueueInfo(start, stop), 1); | ||||
// Starting twice doesn't start it twice. | // Starting twice doesn't start it twice. | ||||
BOOST_CHECK(!p.startEventLoop(s)); | BOOST_CHECK(!p.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 and a block to query. | |||||
const Config &config = GetConfig(); | |||||
// Create a node that supports avalanche. | // Create a node that supports avalanche. | ||||
auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic); | auto avanode = | ||||
ConnectNode(config, NODE_AVALANCHE, *peerLogic, connman.get()); | |||||
NodeId nodeid = avanode->GetId(); | NodeId nodeid = avanode->GetId(); | ||||
BOOST_CHECK(p.addPeer(nodeid, 0)); | BOOST_CHECK(p.addPeer(nodeid, 0)); | ||||
// There is no query in flight at the moment. | // There is no query in flight at the moment. | ||||
BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), nodeid); | BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), 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 = AvalancheTest::getRound(p); | uint64_t queryRound = AvalancheTest::getRound(p); | ||||
Show All 39 Lines | BOOST_AUTO_TEST_CASE(event_loop) { | ||||
// Can't stop the event loop twice. | // Can't stop the event loop twice. | ||||
BOOST_CHECK(!p.stopEventLoop()); | BOOST_CHECK(!p.stopEventLoop()); | ||||
// Wait for the scheduler to stop. | // Wait for the scheduler to stop. | ||||
s.stop(true); | s.stop(true); | ||||
schedulerThread.join(); | schedulerThread.join(); | ||||
CConnmanTest::ClearNodes(); | connman->ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(destructor) { | BOOST_AUTO_TEST_CASE(destructor) { | ||||
CScheduler s; | CScheduler s; | ||||
boost::chrono::system_clock::time_point start, stop; | boost::chrono::system_clock::time_point start, stop; | ||||
// Start the scheduler thread. | // Start the scheduler thread. | ||||
std::thread schedulerThread(std::bind(&CScheduler::serviceQueue, &s)); | std::thread schedulerThread(std::bind(&CScheduler::serviceQueue, &s)); | ||||
Show All 16 Lines |