Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
// Copyright (c) 2010-2020 The Bitcoin developers | // Copyright (c) 2010-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.h> | #include <avalanche.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <net_processing.h> // For PeerLogicValidation | #include <net_processing.h> // For PeerLogicValidation | ||||
#include <util/time.h> | |||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
struct AvalancheTest { | struct AvalancheTest { | ||||
static void runEventLoop(AvalancheProcessor &p) { p.runEventLoop(); } | static void runEventLoop(AvalancheProcessor &p) { p.runEventLoop(); } | ||||
▲ Show 20 Lines • Show All 881 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(event_loop) { | ||||
// 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); | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
for (int i = 0; i < 60 * 1000; i++) { | for (int i = 0; i < 60 * 1000; i++) { | ||||
// Technically, this is a race condition, but this should do just fine | // Technically, this is a race condition, but this should do just fine | ||||
// as we wait up to 1 minute for an event that should take 10ms. | // as we wait up to 1 minute for an event that should take 10ms. | ||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); | UninterruptibleSleep(std::chrono::milliseconds(1)); | ||||
if (AvalancheTest::getRound(p) != queryRound) { | if (AvalancheTest::getRound(p) != queryRound) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// Check that we effectively got a request and not timed out. | // Check that we effectively got a request and not timed out. | ||||
BOOST_CHECK(AvalancheTest::getRound(p) > queryRound); | BOOST_CHECK(AvalancheTest::getRound(p) > queryRound); | ||||
// Respond and check the cooldown time is respected. | // Respond and check the cooldown time is respected. | ||||
uint64_t responseRound = AvalancheTest::getRound(p); | uint64_t responseRound = AvalancheTest::getRound(p); | ||||
auto queryTime = | auto queryTime = | ||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(100); | std::chrono::steady_clock::now() + std::chrono::milliseconds(100); | ||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
p.registerVotes(nodeid, {queryRound, 100, {AvalancheVote(0, blockHash)}}, | p.registerVotes(nodeid, {queryRound, 100, {AvalancheVote(0, blockHash)}}, | ||||
updates); | updates); | ||||
for (int i = 0; i < 10000; i++) { | for (int i = 0; i < 10000; i++) { | ||||
// We make sure that we do not get a request before queryTime. | // We make sure that we do not get a request before queryTime. | ||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); | UninterruptibleSleep(std::chrono::milliseconds(1)); | ||||
if (AvalancheTest::getRound(p) != responseRound) { | if (AvalancheTest::getRound(p) != responseRound) { | ||||
BOOST_CHECK(std::chrono::steady_clock::now() > queryTime); | BOOST_CHECK(std::chrono::steady_clock::now() > queryTime); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// But we eventually get one. | // But we eventually get one. | ||||
BOOST_CHECK(AvalancheTest::getRound(p) > responseRound); | BOOST_CHECK(AvalancheTest::getRound(p) > responseRound); | ||||
Show All 39 Lines |