Skip to content

Commit

Permalink
[init] Initialize and start TxIndex in init code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Posen committed Apr 25, 2018
1 parent f90c3a6 commit 8181db8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/index/txindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
constexpr int64_t SYNC_LOG_INTERVAL = 30; // seconds
constexpr int64_t SYNC_LOCATOR_WRITE_INTERVAL = 30; // seconds

std::unique_ptr<TxIndex> g_txindex;

template<typename... Args>
static void FatalError(const char* fmt, const Args&... args)
{
Expand Down
3 changes: 3 additions & 0 deletions src/index/txindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ class TxIndex final : public CValidationInterface
void Stop();
};

/// The global transaction index, used in GetTransaction. May be null.
extern std::unique_ptr<TxIndex> g_txindex;

#endif // BITCOIN_INDEX_TXINDEX_H
37 changes: 27 additions & 10 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <fs.h>
#include <httpserver.h>
#include <httprpc.h>
#include <index/txindex.h>
#include <key.h>
#include <validation.h>
#include <miner.h>
Expand Down Expand Up @@ -182,6 +183,9 @@ void Interrupt()
InterruptMapPort();
if (g_connman)
g_connman->Interrupt();
if (g_txindex) {
g_txindex->Interrupt();
}
}

void Shutdown()
Expand Down Expand Up @@ -212,6 +216,9 @@ void Shutdown()
if (g_connman) g_connman->Stop();
peerLogic.reset();
g_connman.reset();
if (g_txindex) {
g_txindex.reset();
}

StopTorControl();

Expand Down Expand Up @@ -1414,16 +1421,20 @@ bool AppInitMain()
int64_t nTotalCache = (gArgs.GetArg("-dbcache", nDefaultDbCache) << 20);
nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
int64_t nBlockTreeDBCache = nTotalCache / 8;
nBlockTreeDBCache = std::min(nBlockTreeDBCache, (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxBlockDBAndTxIndexCache : nMaxBlockDBCache) << 20);
int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
nTotalCache -= nBlockTreeDBCache;
int64_t nTxIndexCache = std::min(nTotalCache / 8, gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
nTotalCache -= nTxIndexCache;
int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
nTotalCache -= nCoinDBCache;
nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
LogPrintf("Cache configuration:\n");
LogPrintf("* Using %.1fMiB for block index database\n", 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", nCoinDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for in-memory UTXO set (plus up to %.1fMiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));

Expand Down Expand Up @@ -1457,9 +1468,8 @@ bool AppInitMain()

if (fRequestShutdown) break;

// LoadBlockIndex will load fTxIndex from the db, or set it if
// we're reindexing. It will also load fHavePruned if we've
// ever removed a block file from disk.
// LoadBlockIndex will load fHavePruned if we've ever removed a
// block file from disk.
// Note that it also sets fReindex based on the disk flag!
// From here on out fReindex and fReset mean something different!
if (!LoadBlockIndex(chainparams)) {
Expand Down Expand Up @@ -1608,10 +1618,17 @@ bool AppInitMain()
::feeEstimator.Read(est_filein);
fFeeEstimatesInitialized = true;

// ********************************************************* Step 8: load wallet
// ********************************************************* Step 8: start indexers
if (gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
auto txindex_db = MakeUnique<TxIndexDB>(nTxIndexCache, false, fReindex);
g_txindex = MakeUnique<TxIndex>(std::move(txindex_db));
g_txindex->Start();
}

// ********************************************************* Step 9: load wallet
if (!g_wallet_init_interface.Open()) return false;

// ********************************************************* Step 9: data directory maintenance
// ********************************************************* Step 10: data directory maintenance

// if pruning, unset the service bit and perform the initial blockstore prune
// after any wallet rescanning has taken place.
Expand All @@ -1633,7 +1650,7 @@ bool AppInitMain()
nLocalServices = ServiceFlags(nLocalServices | NODE_WITNESS);
}

// ********************************************************* Step 10: import blocks
// ********************************************************* Step 11: import blocks

if (!CheckDiskSpace() && !CheckDiskSpace(0, true))
return false;
Expand Down Expand Up @@ -1672,7 +1689,7 @@ bool AppInitMain()
return false;
}

// ********************************************************* Step 11: start node
// ********************************************************* Step 12: start node

int chain_active_height;

Expand Down Expand Up @@ -1750,7 +1767,7 @@ bool AppInitMain()
return false;
}

// ********************************************************* Step 12: finished
// ********************************************************* Step 13: finished

SetRPCWarmupFinished();
uiInterface.InitMessage(_("Done loading"));
Expand Down
2 changes: 1 addition & 1 deletion src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static const int64_t nMaxBlockDBCache = 2;
//! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
static const int64_t nMaxTxIndexCache = 1024;
//! Max memory allocated to coin DB specific cache (MiB)
static const int64_t nMaxCoinsDBCache = 8;

Expand Down

0 comments on commit 8181db8

Please sign in to comment.