0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-15 11:36:00 -05:00

[lib] add scheduler to node context

- also update test setup & access point in denial of service test
This commit is contained in:
Amiti Uttarwar 2020-02-12 11:08:28 -08:00
parent 930d837542
commit 7c8b6e5b52
6 changed files with 24 additions and 16 deletions

View file

@ -157,7 +157,6 @@ NODISCARD static bool CreatePidFile()
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle; static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
static boost::thread_group threadGroup; static boost::thread_group threadGroup;
static CScheduler scheduler;
void Interrupt(NodeContext& node) void Interrupt(NodeContext& node)
{ {
@ -295,6 +294,7 @@ void Shutdown(NodeContext& node)
globalVerifyHandle.reset(); globalVerifyHandle.reset();
ECC_Stop(); ECC_Stop();
if (node.mempool) node.mempool = nullptr; if (node.mempool) node.mempool = nullptr;
node.scheduler.reset();
LogPrintf("%s: done\n", __func__); LogPrintf("%s: done\n", __func__);
} }
@ -1265,16 +1265,19 @@ bool AppInitMain(NodeContext& node)
} }
} }
assert(!node.scheduler);
node.scheduler = MakeUnique<CScheduler>();
// Start the lightweight task scheduler thread // Start the lightweight task scheduler thread
CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, &scheduler); CScheduler::Function serviceLoop = [&node]{ node.scheduler->serviceQueue(); };
threadGroup.create_thread(std::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop)); threadGroup.create_thread(std::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));
// Gather some entropy once per minute. // Gather some entropy once per minute.
scheduler.scheduleEvery([]{ node.scheduler->scheduleEvery([]{
RandAddPeriodic(); RandAddPeriodic();
}, 60000); }, 60000);
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); GetMainSignals().RegisterBackgroundSignalScheduler(*node.scheduler);
// Create client interfaces for wallets that are supposed to be loaded // Create client interfaces for wallets that are supposed to be loaded
// according to -wallet and -disablewallet options. This only constructs // according to -wallet and -disablewallet options. This only constructs
@ -1324,7 +1327,7 @@ bool AppInitMain(NodeContext& node)
assert(!node.connman); assert(!node.connman);
node.connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()))); node.connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), scheduler)); node.peer_logic.reset(new PeerLogicValidation(node.connman.get(), node.banman.get(), *node.scheduler));
RegisterValidationInterface(node.peer_logic.get()); RegisterValidationInterface(node.peer_logic.get());
// sanitize comments per BIP-0014, format user agent and check total size // sanitize comments per BIP-0014, format user agent and check total size
@ -1816,7 +1819,7 @@ bool AppInitMain(NodeContext& node)
connOptions.m_specified_outgoing = connect; connOptions.m_specified_outgoing = connect;
} }
} }
if (!node.connman->Start(scheduler, connOptions)) { if (!node.connman->Start(*node.scheduler, connOptions)) {
return false; return false;
} }
@ -1845,11 +1848,11 @@ bool AppInitMain(NodeContext& node)
uiInterface.InitMessage(_("Done loading").translated); uiInterface.InitMessage(_("Done loading").translated);
for (const auto& client : node.chain_clients) { for (const auto& client : node.chain_clients) {
client->start(scheduler); client->start(*node.scheduler);
} }
BanMan* banman = node.banman.get(); BanMan* banman = node.banman.get();
scheduler.scheduleEvery([banman]{ node.scheduler->scheduleEvery([banman]{
banman->DumpBanlist(); banman->DumpBanlist();
}, DUMP_BANS_INTERVAL * 1000); }, DUMP_BANS_INTERVAL * 1000);

View file

@ -8,6 +8,7 @@
#include <interfaces/chain.h> #include <interfaces/chain.h>
#include <net.h> #include <net.h>
#include <net_processing.h> #include <net_processing.h>
#include <scheduler.h>
NodeContext::NodeContext() {} NodeContext::NodeContext() {}
NodeContext::~NodeContext() {} NodeContext::~NodeContext() {}

View file

@ -10,6 +10,7 @@
class BanMan; class BanMan;
class CConnman; class CConnman;
class CScheduler;
class CTxMemPool; class CTxMemPool;
class PeerLogicValidation; class PeerLogicValidation;
namespace interfaces { namespace interfaces {
@ -34,6 +35,7 @@ struct NodeContext {
std::unique_ptr<BanMan> banman; std::unique_ptr<BanMan> banman;
std::unique_ptr<interfaces::Chain> chain; std::unique_ptr<interfaces::Chain> chain;
std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients; std::vector<std::unique_ptr<interfaces::ChainClient>> chain_clients;
std::unique_ptr<CScheduler> scheduler;
//! Declare default constructor and destructor that are not inline, so code //! Declare default constructor and destructor that are not inline, so code
//! instantiating the NodeContext struct doesn't need to #include class //! instantiating the NodeContext struct doesn't need to #include class

View file

@ -78,7 +78,7 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
{ {
auto connman = MakeUnique<CConnman>(0x1337, 0x1337); auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), nullptr, scheduler); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), nullptr, *m_node.scheduler);
// Mock an outbound peer // Mock an outbound peer
CAddress addr1(ip(0xa0b0c001), NODE_NONE); CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@ -148,7 +148,7 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidat
BOOST_AUTO_TEST_CASE(stale_tip_peer_management) BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
{ {
auto connman = MakeUnique<CConnmanTest>(0x1337, 0x1337); auto connman = MakeUnique<CConnmanTest>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), nullptr, scheduler); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), nullptr, *m_node.scheduler);
const Consensus::Params& consensusParams = Params().GetConsensus(); const Consensus::Params& consensusParams = Params().GetConsensus();
constexpr int max_outbound_full_relay = 8; constexpr int max_outbound_full_relay = 8;
@ -221,7 +221,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
{ {
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
auto connman = MakeUnique<CConnman>(0x1337, 0x1337); auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), *m_node.scheduler);
banman->ClearBanned(); banman->ClearBanned();
CAddress addr1(ip(0xa0b0c001), NODE_NONE); CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@ -276,7 +276,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
{ {
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
auto connman = MakeUnique<CConnman>(0x1337, 0x1337); auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), *m_node.scheduler);
banman->ClearBanned(); banman->ClearBanned();
gArgs.ForceSetArg("-banscore", "111"); // because 11 is my favorite number gArgs.ForceSetArg("-banscore", "111"); // because 11 is my favorite number
@ -323,7 +323,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
{ {
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
auto connman = MakeUnique<CConnman>(0x1337, 0x1337); auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), *m_node.scheduler);
banman->ClearBanned(); banman->ClearBanned();
int64_t nStartTime = GetTime(); int64_t nStartTime = GetTime();

View file

@ -103,10 +103,12 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
g_rpc_node = &m_node; g_rpc_node = &m_node;
RegisterAllCoreRPCCommands(tableRPC); RegisterAllCoreRPCCommands(tableRPC);
m_node.scheduler = MakeUnique<CScheduler>();
// We have to run a scheduler thread to prevent ActivateBestChain // We have to run a scheduler thread to prevent ActivateBestChain
// from blocking due to queue overrun. // from blocking due to queue overrun.
threadGroup.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler)); threadGroup.create_thread([&]{ m_node.scheduler->serviceQueue(); });
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); GetMainSignals().RegisterBackgroundSignalScheduler(*g_rpc_node->scheduler);
pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pblocktree.reset(new CBlockTreeDB(1 << 20, true));
g_chainstate = MakeUnique<CChainState>(); g_chainstate = MakeUnique<CChainState>();
@ -147,6 +149,7 @@ TestingSetup::~TestingSetup()
m_node.connman.reset(); m_node.connman.reset();
m_node.banman.reset(); m_node.banman.reset();
m_node.mempool = nullptr; m_node.mempool = nullptr;
m_node.scheduler.reset();
UnloadBlockIndex(); UnloadBlockIndex();
g_chainstate.reset(); g_chainstate.reset();
pblocktree.reset(); pblocktree.reset();

View file

@ -85,7 +85,6 @@ private:
struct TestingSetup : public BasicTestingSetup { struct TestingSetup : public BasicTestingSetup {
NodeContext m_node; NodeContext m_node;
boost::thread_group threadGroup; boost::thread_group threadGroup;
CScheduler scheduler;
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN); explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~TestingSetup(); ~TestingSetup();