Changeset View
Changeset View
Standalone View
Standalone View
src/init.cpp
Show All 15 Lines | |||||
#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 <diskblockpos.h> | #include <diskblockpos.h> | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <httprpc.h> | #include <httprpc.h> | ||||
#include <httpserver.h> | #include <httpserver.h> | ||||
#include <index/txindex.h> | |||||
#include <key.h> | #include <key.h> | ||||
#include <miner.h> | #include <miner.h> | ||||
#include <net.h> | #include <net.h> | ||||
#include <net_processing.h> | #include <net_processing.h> | ||||
#include <netbase.h> | #include <netbase.h> | ||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <rpc/blockchain.h> | #include <rpc/blockchain.h> | ||||
#include <rpc/register.h> | #include <rpc/register.h> | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | void Interrupt() { | ||||
InterruptHTTPRPC(); | InterruptHTTPRPC(); | ||||
InterruptRPC(); | InterruptRPC(); | ||||
InterruptREST(); | InterruptREST(); | ||||
InterruptTorControl(); | InterruptTorControl(); | ||||
InterruptMapPort(); | InterruptMapPort(); | ||||
if (g_connman) { | if (g_connman) { | ||||
g_connman->Interrupt(); | g_connman->Interrupt(); | ||||
} | } | ||||
if (g_txindex) { | |||||
g_txindex->Interrupt(); | |||||
} | |||||
} | } | ||||
void Shutdown() { | void Shutdown() { | ||||
LogPrintf("%s: In progress...\n", __func__); | LogPrintf("%s: In progress...\n", __func__); | ||||
static CCriticalSection cs_Shutdown; | static CCriticalSection cs_Shutdown; | ||||
TRY_LOCK(cs_Shutdown, lockShutdown); | TRY_LOCK(cs_Shutdown, lockShutdown); | ||||
if (!lockShutdown) { | if (!lockShutdown) { | ||||
return; | return; | ||||
Show All 18 Lines | void Shutdown() { | ||||
if (peerLogic) { | if (peerLogic) { | ||||
UnregisterValidationInterface(peerLogic.get()); | UnregisterValidationInterface(peerLogic.get()); | ||||
} | } | ||||
if (g_connman) { | if (g_connman) { | ||||
g_connman->Stop(); | g_connman->Stop(); | ||||
} | } | ||||
peerLogic.reset(); | peerLogic.reset(); | ||||
g_connman.reset(); | g_connman.reset(); | ||||
if (g_txindex) { | |||||
g_txindex.reset(); | |||||
} | |||||
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(); | ||||
▲ Show 20 Lines • Show All 1,817 Lines • ▼ Show 20 Lines | #endif | ||||
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 | ||||
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); | nTotalCache = std::max(nTotalCache, nMinDbCache << 20); | ||||
// total cache cannot be greater than nMaxDbcache | // total cache cannot be greater than nMaxDbcache | ||||
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); | nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); | ||||
int64_t nBlockTreeDBCache = nTotalCache / 8; | int64_t nBlockTreeDBCache = | ||||
nBlockTreeDBCache = std::min(nBlockTreeDBCache, | std::min(nTotalCache / 8, nMaxBlockDBCache << 20); | ||||
(gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX) | |||||
? nMaxBlockDBAndTxIndexCache | |||||
: nMaxBlockDBCache) | |||||
<< 20); | |||||
nTotalCache -= nBlockTreeDBCache; | nTotalCache -= nBlockTreeDBCache; | ||||
int64_t nTxIndexCache = | |||||
std::min(nTotalCache / 8, gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX) | |||||
? nMaxTxIndexCache << 20 | |||||
: 0); | |||||
nTotalCache -= nTxIndexCache; | |||||
// use 25%-50% of the remainder for disk cache | // use 25%-50% of the remainder for disk cache | ||||
int64_t nCoinDBCache = | int64_t nCoinDBCache = | ||||
std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); | std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); | ||||
// cap total coins db cache | // cap total coins db cache | ||||
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); | nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); | ||||
nTotalCache -= nCoinDBCache; | nTotalCache -= nCoinDBCache; | ||||
// the rest goes to in-memory cache | // the rest goes to in-memory cache | ||||
nCoinCacheUsage = nTotalCache; | nCoinCacheUsage = nTotalCache; | ||||
int64_t nMempoolSizeMax = | int64_t nMempoolSizeMax = | ||||
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; | gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; | ||||
LogPrintf("Cache configuration:\n"); | LogPrintf("Cache configuration:\n"); | ||||
LogPrintf("* Using %.1fMiB for block index database\n", | LogPrintf("* Using %.1fMiB for block index database\n", | ||||
nBlockTreeDBCache * (1.0 / 1024 / 1024)); | nBlockTreeDBCache * (1.0 / 1024 / 1024)); | ||||
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) { | |||||
LogPrintf("* Using %.1fMiB for transaction index database\n", | |||||
nTxIndexCache * (1.0 / 1024 / 1024)); | |||||
} | |||||
LogPrintf("* Using %.1fMiB for chain state database\n", | LogPrintf("* Using %.1fMiB for chain state database\n", | ||||
nCoinDBCache * (1.0 / 1024 / 1024)); | nCoinDBCache * (1.0 / 1024 / 1024)); | ||||
LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of " | LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of " | ||||
"unused mempool space)\n", | "unused mempool space)\n", | ||||
nCoinCacheUsage * (1.0 / 1024 / 1024), | nCoinCacheUsage * (1.0 / 1024 / 1024), | ||||
nMempoolSizeMax * (1.0 / 1024 / 1024)); | nMempoolSizeMax * (1.0 / 1024 / 1024)); | ||||
int64_t nStart = 0; | int64_t nStart = 0; | ||||
Show All 24 Lines | while (!fLoaded && !fRequestShutdown) { | ||||
CleanupBlockRevFiles(); | CleanupBlockRevFiles(); | ||||
} | } | ||||
} | } | ||||
if (fRequestShutdown) { | if (fRequestShutdown) { | ||||
break; | break; | ||||
} | } | ||||
// LoadBlockIndex will load fTxIndex from the db, or set it if | // LoadBlockIndex will load fHavePruned if we've ever removed a | ||||
// we're reindexing. It will also load fHavePruned if we've | // block file from disk. | ||||
// ever removed a block file from disk. | |||||
// Note that it also sets fReindex based on the disk flag! | // Note that it also sets fReindex based on the disk flag! | ||||
// From here on out fReindex and fReset mean something | // From here on out fReindex and fReset mean something | ||||
// different! | // different! | ||||
if (!LoadBlockIndex(config)) { | if (!LoadBlockIndex(config)) { | ||||
strLoadError = _("Error loading block database"); | strLoadError = _("Error loading block database"); | ||||
break; | break; | ||||
} | } | ||||
// If the loaded chain has a wrong genesis, bail out immediately | // If the loaded chain has a wrong genesis, bail out immediately | ||||
// (we're likely using a testnet datadir, or the other way | // (we're likely using a testnet datadir, or the other way | ||||
// around). | // around). | ||||
if (!mapBlockIndex.empty() && | if (!mapBlockIndex.empty() && | ||||
!LookupBlockIndex( | !LookupBlockIndex( | ||||
chainparams.GetConsensus().hashGenesisBlock)) { | chainparams.GetConsensus().hashGenesisBlock)) { | ||||
return InitError(_("Incorrect or no genesis block found. " | return InitError(_("Incorrect or no genesis block found. " | ||||
"Wrong datadir for network?")); | "Wrong datadir for network?")); | ||||
} | } | ||||
// Check for changed -txindex state | |||||
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) { | |||||
strLoadError = _("You need to rebuild the database using " | |||||
"-reindex-chainstate to change -txindex"); | |||||
break; | |||||
} | |||||
// Check for changed -prune state. What we are concerned about | // Check for changed -prune state. What we are concerned about | ||||
// is a user who has pruned blocks in the past, but is now | // is a user who has pruned blocks in the past, but is now | ||||
// trying to run unpruned. | // trying to run unpruned. | ||||
if (fHavePruned && !fPruneMode) { | if (fHavePruned && !fPruneMode) { | ||||
strLoadError = | strLoadError = | ||||
_("You need to rebuild the database using -reindex to " | _("You need to rebuild the database using -reindex to " | ||||
"go back to unpruned mode. This will redownload the " | "go back to unpruned mode. This will redownload the " | ||||
"entire blockchain"); | "entire blockchain"); | ||||
▲ Show 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | #endif | ||||
} | } | ||||
LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart); | LogPrintf(" block index %15dms\n", GetTimeMillis() - nStart); | ||||
// Encoded addresses using cashaddr instead of base58 | // Encoded addresses using cashaddr instead of base58 | ||||
// Activates by default on Jan, 14 | // Activates by default on Jan, 14 | ||||
config.SetCashAddrEncoding( | config.SetCashAddrEncoding( | ||||
gArgs.GetBoolArg("-usecashaddr", GetAdjustedTime() > 1515900000)); | gArgs.GetBoolArg("-usecashaddr", GetAdjustedTime() > 1515900000)); | ||||
// Step 8: load wallet | // Step 8: load indexers | ||||
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) { | |||||
auto txindex_db = | |||||
std::make_unique<TxIndexDB>(nTxIndexCache, false, fReindex); | |||||
g_txindex = std::make_unique<TxIndex>(std::move(txindex_db)); | |||||
g_txindex->Start(); | |||||
} | |||||
// Step 9: load wallet | |||||
if (!g_wallet_init_interface.Open(chainparams)) { | if (!g_wallet_init_interface.Open(chainparams)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Step 9: data directory maintenance | // Step 10: data directory maintenance | ||||
// if pruning, unset the service bit and perform the initial blockstore | // if pruning, unset the service bit and perform the initial blockstore | ||||
// prune after any wallet rescanning has taken place. | // prune after any wallet rescanning has taken place. | ||||
if (fPruneMode) { | if (fPruneMode) { | ||||
LogPrintf("Unsetting NODE_NETWORK on prune mode\n"); | LogPrintf("Unsetting NODE_NETWORK on prune mode\n"); | ||||
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK); | nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK); | ||||
if (!fReindex) { | if (!fReindex) { | ||||
uiInterface.InitMessage(_("Pruning blockstore...")); | uiInterface.InitMessage(_("Pruning blockstore...")); | ||||
PruneAndFlush(); | PruneAndFlush(); | ||||
} | } | ||||
} | } | ||||
// Step 10: import blocks | // Step 11: import blocks | ||||
if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ false)) { | if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ false)) { | ||||
InitError( | InitError( | ||||
strprintf(_("Error: Disk space is low for %s"), GetDataDir())); | strprintf(_("Error: Disk space is low for %s"), GetDataDir())); | ||||
return false; | return false; | ||||
} | } | ||||
if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ true)) { | if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ true)) { | ||||
InitError( | InitError( | ||||
strprintf(_("Error: Disk space is low for %s"), GetBlocksDir())); | strprintf(_("Error: Disk space is low for %s"), GetBlocksDir())); | ||||
Show All 32 Lines | // Wait for genesis block to be processed | ||||
} | } | ||||
uiInterface.NotifyBlockTip.disconnect(BlockNotifyGenesisWait); | uiInterface.NotifyBlockTip.disconnect(BlockNotifyGenesisWait); | ||||
} | } | ||||
if (ShutdownRequested()) { | if (ShutdownRequested()) { | ||||
return false; | return false; | ||||
} | } | ||||
// Step 11: start node | // Step 12: start node | ||||
int chain_active_height; | int chain_active_height; | ||||
//// debug print | //// debug print | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size()); | LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size()); | ||||
chain_active_height = chainActive.Height(); | chain_active_height = chainActive.Height(); | ||||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | if (!connOptions.m_use_addrman_outgoing) { | ||||
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 (!connman.Start(scheduler, connOptions)) { | if (!connman.Start(scheduler, connOptions)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Step 12: finished | // Step 13: finished | ||||
SetRPCWarmupFinished(); | SetRPCWarmupFinished(); | ||||
uiInterface.InitMessage(_("Done loading")); | uiInterface.InitMessage(_("Done loading")); | ||||
g_wallet_init_interface.Start(scheduler); | g_wallet_init_interface.Start(scheduler); | ||||
return true; | return true; | ||||
} | } |