Changeset View
Changeset View
Standalone View
Standalone View
src/init.cpp
Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | |||||
static const bool DEFAULT_PROXYRANDOMIZE = true; | static const bool DEFAULT_PROXYRANDOMIZE = true; | ||||
static const bool DEFAULT_REST_ENABLE = false; | static const bool DEFAULT_REST_ENABLE = false; | ||||
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false; | static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false; | ||||
// Dump addresses to banlist.dat every 15 minutes (900s) | // Dump addresses to banlist.dat every 15 minutes (900s) | ||||
static constexpr int DUMP_BANS_INTERVAL = 60 * 15; | static constexpr int DUMP_BANS_INTERVAL = 60 * 15; | ||||
std::unique_ptr<CConnman> g_connman; | |||||
std::unique_ptr<PeerLogicValidation> peerLogic; | |||||
std::unique_ptr<BanMan> g_banman; | |||||
#ifdef WIN32 | #ifdef WIN32 | ||||
// Win32 LevelDB doesn't use filedescriptors, and the ones used for accessing | // Win32 LevelDB doesn't use filedescriptors, and the ones used for accessing | ||||
// block files don't count towards the fd_set size limit anyway. | // block files don't count towards the fd_set size limit anyway. | ||||
#define MIN_CORE_FILEDESCRIPTORS 0 | #define MIN_CORE_FILEDESCRIPTORS 0 | ||||
#else | #else | ||||
#define MIN_CORE_FILEDESCRIPTORS 150 | #define MIN_CORE_FILEDESCRIPTORS 150 | ||||
#endif | #endif | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | void Interrupt(NodeContext &node) { | ||||
InterruptREST(); | InterruptREST(); | ||||
InterruptTorControl(); | InterruptTorControl(); | ||||
InterruptMapPort(); | InterruptMapPort(); | ||||
if (g_avalanche) { | if (g_avalanche) { | ||||
// Avalanche needs to be stopped before we interrupt the thread group as | // Avalanche needs to be stopped before we interrupt the thread group as | ||||
// the scheduler will stop working then. | // the scheduler will stop working then. | ||||
g_avalanche->stopEventLoop(); | g_avalanche->stopEventLoop(); | ||||
} | } | ||||
if (g_connman) { | if (node.connman) { | ||||
g_connman->Interrupt(); | node.connman->Interrupt(); | ||||
} | } | ||||
if (g_txindex) { | if (g_txindex) { | ||||
g_txindex->Interrupt(); | g_txindex->Interrupt(); | ||||
} | } | ||||
ForEachBlockFilterIndex([](BlockFilterIndex &index) { index.Interrupt(); }); | ForEachBlockFilterIndex([](BlockFilterIndex &index) { index.Interrupt(); }); | ||||
} | } | ||||
void Shutdown(NodeContext &node) { | void Shutdown(NodeContext &node) { | ||||
Show All 27 Lines | void Shutdown(NodeContext &node) { | ||||
// 3. Destroy AvalancheProcessor. | // 3. Destroy AvalancheProcessor. | ||||
// 4. Destroy CConnman | // 4. Destroy CConnman | ||||
if (g_avalanche) { | if (g_avalanche) { | ||||
g_avalanche->stopEventLoop(); | g_avalanche->stopEventLoop(); | ||||
} | } | ||||
// Because these depend on each-other, we make sure that neither can be | // Because these depend on each-other, we make sure that neither can be | ||||
// using the other before destroying them. | // using the other before destroying them. | ||||
if (peerLogic) { | if (node.peer_logic) { | ||||
UnregisterValidationInterface(peerLogic.get()); | UnregisterValidationInterface(node.peer_logic.get()); | ||||
} | } | ||||
if (g_connman) { | if (node.connman) { | ||||
g_connman->Stop(); | node.connman->Stop(); | ||||
} | } | ||||
if (g_txindex) { | if (g_txindex) { | ||||
g_txindex->Stop(); | g_txindex->Stop(); | ||||
} | } | ||||
ForEachBlockFilterIndex([](BlockFilterIndex &index) { index.Stop(); }); | ForEachBlockFilterIndex([](BlockFilterIndex &index) { index.Stop(); }); | ||||
StopTorControl(); | StopTorControl(); | ||||
// After everything has been shut down, but before things get flushed, stop | // After everything has been shut down, but before things get flushed, stop | ||||
// the CScheduler/checkqueue threadGroup | // the CScheduler/checkqueue threadGroup | ||||
threadGroup.interrupt_all(); | threadGroup.interrupt_all(); | ||||
threadGroup.join_all(); | threadGroup.join_all(); | ||||
// After the threads that potentially access these pointers have been | // After the threads that potentially access these pointers have been | ||||
// stopped, destruct and reset all to nullptr. | // stopped, destruct and reset all to nullptr. | ||||
peerLogic.reset(); | node.peer_logic.reset(); | ||||
// Destroy various global instances | // Destroy various global instances | ||||
g_avalanche.reset(); | g_avalanche.reset(); | ||||
g_connman.reset(); | node.connman.reset(); | ||||
g_banman.reset(); | node.banman.reset(); | ||||
g_txindex.reset(); | g_txindex.reset(); | ||||
DestroyAllBlockFilterIndexes(); | DestroyAllBlockFilterIndexes(); | ||||
if (::g_mempool.IsLoaded() && | if (::g_mempool.IsLoaded() && | ||||
gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) { | gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) { | ||||
DumpMempool(::g_mempool); | DumpMempool(::g_mempool); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,891 Lines • ▼ Show 20 Lines | #endif | ||||
// Step 6: network initialization | // Step 6: network initialization | ||||
// Note that we absolutely cannot open any actual connections | // Note that we absolutely cannot open any actual connections | ||||
// until the very end ("start node") as the UTXO/block state | // until the very end ("start node") as the UTXO/block state | ||||
// is not yet setup and may end up being set up twice if we | // is not yet setup and may end up being set up twice if we | ||||
// need to reindex later. | // need to reindex later. | ||||
assert(!g_banman); | assert(!node.banman); | ||||
g_banman = std::make_unique<BanMan>( | node.banman = std::make_unique<BanMan>( | ||||
GetDataDir() / "banlist.dat", config.GetChainParams(), &uiInterface, | GetDataDir() / "banlist.dat", config.GetChainParams(), &uiInterface, | ||||
gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME)); | gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME)); | ||||
assert(!g_connman); | assert(!node.connman); | ||||
g_connman = std::make_unique<CConnman>( | node.connman = std::make_unique<CConnman>( | ||||
config, GetRand(std::numeric_limits<uint64_t>::max()), | config, GetRand(std::numeric_limits<uint64_t>::max()), | ||||
GetRand(std::numeric_limits<uint64_t>::max())); | GetRand(std::numeric_limits<uint64_t>::max())); | ||||
peerLogic = std::make_unique<PeerLogicValidation>( | node.peer_logic = std::make_unique<PeerLogicValidation>( | ||||
g_connman.get(), g_banman.get(), scheduler, | node.connman.get(), node.banman.get(), scheduler, | ||||
gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)); | gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)); | ||||
RegisterValidationInterface(peerLogic.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 | ||||
std::vector<std::string> uacomments; | std::vector<std::string> uacomments; | ||||
for (const std::string &cmt : gArgs.GetArgs("-uacomment")) { | for (const std::string &cmt : gArgs.GetArgs("-uacomment")) { | ||||
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT)) { | if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT)) { | ||||
return InitError(strprintf( | return InitError(strprintf( | ||||
_("User Agent comment (%s) contains unsafe characters.") | _("User Agent comment (%s) contains unsafe characters.") | ||||
.translated, | .translated, | ||||
▲ Show 20 Lines • Show All 120 Lines • ▼ Show 20 Lines | #endif | ||||
if (gArgs.IsArgSet("-maxuploadtarget")) { | if (gArgs.IsArgSet("-maxuploadtarget")) { | ||||
nMaxOutboundLimit = | nMaxOutboundLimit = | ||||
gArgs.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET) * 1024 * | gArgs.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET) * 1024 * | ||||
1024; | 1024; | ||||
} | } | ||||
// Step 6.5 (I guess ?): Initialize Avalanche. | // Step 6.5 (I guess ?): Initialize Avalanche. | ||||
g_avalanche = std::make_unique<AvalancheProcessor>(g_connman.get()); | g_avalanche = std::make_unique<AvalancheProcessor>(node.connman.get()); | ||||
// Step 7: load block chain | // Step 7: load block chain | ||||
fReindex = gArgs.GetBoolArg("-reindex", false); | fReindex = gArgs.GetBoolArg("-reindex", false); | ||||
bool fReindexChainState = gArgs.GetBoolArg("-reindex-chainstate", false); | bool fReindexChainState = gArgs.GetBoolArg("-reindex-chainstate", false); | ||||
// cache size calculations | // cache size calculations | ||||
int64_t nTotalCache = (gArgs.GetArg("-dbcache", nDefaultDbCache) << 20); | int64_t nTotalCache = (gArgs.GetArg("-dbcache", nDefaultDbCache) << 20); | ||||
▲ Show 20 Lines • Show All 358 Lines • ▼ Show 20 Lines | #endif | ||||
connOptions.nLocalServices = nLocalServices; | connOptions.nLocalServices = nLocalServices; | ||||
connOptions.nMaxConnections = nMaxConnections; | connOptions.nMaxConnections = nMaxConnections; | ||||
connOptions.nMaxOutbound = | connOptions.nMaxOutbound = | ||||
std::min(MAX_OUTBOUND_CONNECTIONS, connOptions.nMaxConnections); | std::min(MAX_OUTBOUND_CONNECTIONS, connOptions.nMaxConnections); | ||||
connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS; | connOptions.nMaxAddnode = MAX_ADDNODE_CONNECTIONS; | ||||
connOptions.nMaxFeeler = 1; | connOptions.nMaxFeeler = 1; | ||||
connOptions.nBestHeight = chain_active_height; | connOptions.nBestHeight = chain_active_height; | ||||
connOptions.uiInterface = &uiInterface; | connOptions.uiInterface = &uiInterface; | ||||
connOptions.m_banman = g_banman.get(); | connOptions.m_banman = node.banman.get(); | ||||
connOptions.m_msgproc = peerLogic.get(); | connOptions.m_msgproc = node.peer_logic.get(); | ||||
connOptions.nSendBufferMaxSize = | connOptions.nSendBufferMaxSize = | ||||
1000 * gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER); | 1000 * gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER); | ||||
connOptions.nReceiveFloodSize = | connOptions.nReceiveFloodSize = | ||||
1000 * gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); | 1000 * gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); | ||||
connOptions.m_added_nodes = gArgs.GetArgs("-addnode"); | connOptions.m_added_nodes = gArgs.GetArgs("-addnode"); | ||||
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe; | connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe; | ||||
connOptions.nMaxOutboundLimit = nMaxOutboundLimit; | connOptions.nMaxOutboundLimit = nMaxOutboundLimit; | ||||
Show All 30 Lines | #endif | ||||
// Initiate outbound connections unless connect=0 | // Initiate outbound connections unless connect=0 | ||||
connOptions.m_use_addrman_outgoing = !gArgs.IsArgSet("-connect"); | connOptions.m_use_addrman_outgoing = !gArgs.IsArgSet("-connect"); | ||||
if (!connOptions.m_use_addrman_outgoing) { | if (!connOptions.m_use_addrman_outgoing) { | ||||
const auto connect = gArgs.GetArgs("-connect"); | const auto connect = gArgs.GetArgs("-connect"); | ||||
if (connect.size() != 1 || connect[0] != "0") { | if (connect.size() != 1 || connect[0] != "0") { | ||||
connOptions.m_specified_outgoing = connect; | connOptions.m_specified_outgoing = connect; | ||||
} | } | ||||
} | } | ||||
if (!g_connman->Start(scheduler, connOptions)) { | if (!node.connman->Start(scheduler, connOptions)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Step 13: finished | // Step 13: finished | ||||
SetRPCWarmupFinished(); | SetRPCWarmupFinished(); | ||||
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(scheduler); | ||||
} | } | ||||
BanMan *banman = g_banman.get(); | BanMan *banman = node.banman.get(); | ||||
scheduler.scheduleEvery( | scheduler.scheduleEvery( | ||||
[banman] { | [banman] { | ||||
banman->DumpBanlist(); | banman->DumpBanlist(); | ||||
return true; | return true; | ||||
}, | }, | ||||
DUMP_BANS_INTERVAL * 1000); | DUMP_BANS_INTERVAL * 1000); | ||||
// Start Avalanche's event loop. | // Start Avalanche's event loop. | ||||
g_avalanche->startEventLoop(scheduler); | g_avalanche->startEventLoop(scheduler); | ||||
return true; | return true; | ||||
} | } |