Changeset View
Changeset View
Standalone View
Standalone View
src/init.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2018 The Bitcoin Core developers | // Copyright (c) 2009-2018 The Bitcoin Core 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. | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include <config/bitcoin-config.h> | #include <config/bitcoin-config.h> | ||||
#endif | #endif | ||||
#include <init.h> | #include <init.h> | ||||
#include <addrman.h> | #include <addrman.h> | ||||
#include <amount.h> | #include <amount.h> | ||||
#include <avalanche.h> | |||||
#include <banman.h> | #include <banman.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <checkpoints.h> | #include <checkpoints.h> | ||||
#include <compat/sanity.h> | #include <compat/sanity.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <flatfile.h> | #include <flatfile.h> | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | |||||
void Interrupt() { | void Interrupt() { | ||||
InterruptHTTPServer(); | InterruptHTTPServer(); | ||||
InterruptHTTPRPC(); | InterruptHTTPRPC(); | ||||
InterruptRPC(); | InterruptRPC(); | ||||
InterruptREST(); | InterruptREST(); | ||||
InterruptTorControl(); | InterruptTorControl(); | ||||
InterruptMapPort(); | InterruptMapPort(); | ||||
if (g_avalanche) { | |||||
// Avalanche needs to be stopped before we interrupt the thread group as | |||||
// the scheduler will stop working then. | |||||
g_avalanche->stopEventLoop(); | |||||
} | |||||
if (g_connman) { | if (g_connman) { | ||||
g_connman->Interrupt(); | g_connman->Interrupt(); | ||||
} | } | ||||
if (g_txindex) { | if (g_txindex) { | ||||
g_txindex->Interrupt(); | g_txindex->Interrupt(); | ||||
} | } | ||||
} | } | ||||
Show All 16 Lines | void Shutdown(InitInterfaces &interfaces) { | ||||
StopREST(); | StopREST(); | ||||
StopRPC(); | StopRPC(); | ||||
StopHTTPServer(); | StopHTTPServer(); | ||||
for (const auto &client : interfaces.chain_clients) { | for (const auto &client : interfaces.chain_clients) { | ||||
client->flush(); | client->flush(); | ||||
} | } | ||||
StopMapPort(); | StopMapPort(); | ||||
// Because avalanche and the network depend on each other, it is important | |||||
// to shut them down in this order: | |||||
// 1. Stop avalanche event loop. | |||||
// 2. Shutdown network processing. | |||||
// 3. Destroy AvalancheProcessor. | |||||
// 4. Destroy CConnman | |||||
if (g_avalanche) { | |||||
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 (peerLogic) { | ||||
UnregisterValidationInterface(peerLogic.get()); | UnregisterValidationInterface(peerLogic.get()); | ||||
} | } | ||||
if (g_connman) { | if (g_connman) { | ||||
g_connman->Stop(); | g_connman->Stop(); | ||||
} | } | ||||
if (g_txindex) { | if (g_txindex) { | ||||
g_txindex->Stop(); | g_txindex->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(); | peerLogic.reset(); | ||||
// Destroy various global instances | |||||
g_avalanche.reset(); | |||||
g_connman.reset(); | g_connman.reset(); | ||||
g_banman.reset(); | g_banman.reset(); | ||||
g_txindex.reset(); | g_txindex.reset(); | ||||
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,730 Lines • ▼ Show 20 Lines | #endif | ||||
g_banman = std::make_unique<BanMan>( | g_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(!g_connman); | ||||
g_connman = std::make_unique<CConnman>( | g_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.reset(new PeerLogicValidation( | peerLogic = std::make_unique<PeerLogicValidation>( | ||||
g_connman.get(), g_banman.get(), scheduler, | g_connman.get(), g_banman.get(), scheduler, | ||||
gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61))); | gArgs.GetBoolArg("-enablebip61", DEFAULT_ENABLE_BIP61)); | ||||
RegisterValidationInterface(peerLogic.get()); | RegisterValidationInterface(peerLogic.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."), cmt)); | _("User Agent comment (%s) contains unsafe characters."), cmt)); | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | #endif | ||||
uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME; | uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME; | ||||
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. | |||||
g_avalanche = std::make_unique<AvalancheProcessor>(g_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); | ||||
// total cache cannot be less than nMinDbCache | // total cache cannot be less than nMinDbCache | ||||
▲ Show 20 Lines • Show All 399 Lines • ▼ Show 20 Lines | #endif | ||||
scheduler.scheduleEvery( | scheduler.scheduleEvery( | ||||
[] { | [] { | ||||
g_banman->DumpBanlist(); | g_banman->DumpBanlist(); | ||||
return true; | return true; | ||||
}, | }, | ||||
DUMP_BANS_INTERVAL * 1000); | DUMP_BANS_INTERVAL * 1000); | ||||
// Start Avalanche's event loop. | |||||
g_avalanche->startEventLoop(scheduler); | |||||
return true; | return true; | ||||
} | } |