34
34
#include <net_processing.h>
35
35
#include <netbase.h>
36
36
#include <node/blockstorage.h>
37
+ #include <node/caches.h> // for CalculateCacheSizes
37
38
#include <node/chainstate.h> // for LoadChainstate
38
39
#include <node/context.h>
39
40
#include <node/miner.h>
@@ -1381,36 +1382,20 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1381
1382
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
1382
1383
1383
1384
// cache size calculations
1384
- int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
1385
- nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
1386
- nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
1387
- int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
1388
- nTotalCache -= nBlockTreeDBCache;
1389
- int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
1390
- nTotalCache -= nTxIndexCache;
1391
- int64_t filter_index_cache = 0;
1392
- if (!g_enabled_filter_types.empty()) {
1393
- size_t n_indexes = g_enabled_filter_types.size();
1394
- int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
1395
- filter_index_cache = max_cache / n_indexes;
1396
- nTotalCache -= filter_index_cache * n_indexes;
1397
- }
1398
- int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
1399
- nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
1400
- nTotalCache -= nCoinDBCache;
1401
- int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
1385
+ CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
1386
+
1402
1387
int64_t nMempoolSizeMax = args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
1403
1388
LogPrintf("Cache configuration:\n");
1404
- LogPrintf("* Using %.1f MiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
1389
+ LogPrintf("* Using %.1f MiB for block index database\n", cache_sizes.block_tree_db * (1.0 / 1024 / 1024));
1405
1390
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
1406
- LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
1391
+ LogPrintf("* Using %.1f MiB for transaction index database\n", cache_sizes.tx_index * (1.0 / 1024 / 1024));
1407
1392
}
1408
1393
for (BlockFilterType filter_type : g_enabled_filter_types) {
1409
1394
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
1410
- filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
1395
+ cache_sizes.filter_index * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
1411
1396
}
1412
- LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
1413
- LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
1397
+ LogPrintf("* Using %.1f MiB for chain state database\n", cache_sizes.coins_db * (1.0 / 1024 / 1024));
1398
+ LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
1414
1399
1415
1400
bool fLoaded = false;
1416
1401
while (!fLoaded && !ShutdownRequested()) {
@@ -1427,9 +1412,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1427
1412
fPruneMode,
1428
1413
chainparams.GetConsensus(),
1429
1414
fReindexChainState,
1430
- nBlockTreeDBCache ,
1431
- nCoinDBCache ,
1432
- nCoinCacheUsage ,
1415
+ cache_sizes.block_tree_db ,
1416
+ cache_sizes.coins_db ,
1417
+ cache_sizes.coins ,
1433
1418
ShutdownRequested,
1434
1419
[]() {
1435
1420
uiInterface.ThreadSafeMessageBox(
@@ -1548,14 +1533,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
1548
1533
return InitError(*error);
1549
1534
}
1550
1535
1551
- g_txindex = std::make_unique<TxIndex>(nTxIndexCache , false, fReindex);
1536
+ g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index , false, fReindex);
1552
1537
if (!g_txindex->Start(chainman.ActiveChainstate())) {
1553
1538
return false;
1554
1539
}
1555
1540
}
1556
1541
1557
1542
for (const auto& filter_type : g_enabled_filter_types) {
1558
- InitBlockFilterIndex(filter_type, filter_index_cache , false, fReindex);
1543
+ InitBlockFilterIndex(filter_type, cache_sizes.filter_index , false, fReindex);
1559
1544
if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
1560
1545
return false;
1561
1546
}
0 commit comments