Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2021-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <node/caches.h>7#include <common/args.h>8#include <common/system.h>9#include <index/txindex.h>10#include <index/txospenderindex.h>11#include <kernel/caches.h>12#include <node/interface_ui.h>13#include <tinyformat.h>14#include <util/byte_units.h>15#include <util/log.h>16#include <util/overflow.h>17#include <util/translation.h>19#include <algorithm>20#include <cstdint>21#include <string>23// Unlike for the UTXO database, for the txindex scenario the leveldb cache make24// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-22960199125//! Max memory allocated to tx index DB specific cache in bytes.26static constexpr uint64_t MAX_TX_INDEX_CACHE{1_GiB};27//! Max memory allocated to all block filter index caches combined in bytes.28static constexpr uint64_t MAX_FILTER_INDEX_CACHE{1_GiB};29//! Max memory allocated to tx spenderindex DB specific cache in bytes.30static constexpr uint64_t MAX_TXOSPENDER_INDEX_CACHE{1_GiB};31//! Larger default dbcache on 64-bit systems with enough RAM.32static constexpr uint64_t HIGH_DEFAULT_DBCACHE{1_GiB};33//! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE.34static constexpr uint64_t HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM{4_GiB};36namespace node {37uint64_t GetDefaultDBCache()38{39if constexpr (sizeof(void*) >= 8) {40if (TryGetTotalRam().value_or(0) >= HIGH_DEFAULT_DBCACHE_MIN_TOTAL_RAM) {41return HIGH_DEFAULT_DBCACHE;42}43}44return DEFAULT_KERNEL_CACHE;45}47uint64_t CalculateDbCacheBytes(const ArgsManager& args)48{49if (auto db_cache{args.GetIntArg("-dbcache")}) {50if (*db_cache < 0) db_cache = 0;51const uint64_t db_cache_bytes{SaturatingLeftShift<uint64_t>(*db_cache, 20)};52return std::max(MIN_DBCACHE_BYTES, std::min(db_cache_bytes, MAX_DBCACHE_BYTES));53}54return GetDefaultDBCache();55}57CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)58{59uint64_t total_cache{CalculateDbCacheBytes(args)};61// Allocate proportional to usage pattern benefit:62// - txindex (10%): serves getrawtransaction RPCs with mostly unique,63// non-repetitive lookups across the entire blockchain.64// - blockfilterindex (5%): serves BIP 157 light clients that repeatedly65// query recent blocks, benefiting from LevelDB cache, but the66// working set for a typical 2-week offline gap is ~200kiB, well within 5%67// of the total cache.68// - txospenderindex (5%): serves gettxspendingprevout RPCs with very69// specific, rarely repeated outpoint queries.70// - coinstatsindex: intentionally not included here, since usage pattern71// does not seem to suggest it would be necessary to cache.72IndexCacheSizes index_sizes;73index_sizes.tx_index = std::min(total_cache * 10 / 100, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE : 0);74index_sizes.txospender_index = std::min(total_cache * 5 / 100, args.GetBoolArg("-txospenderindex", DEFAULT_TXOSPENDERINDEX) ? MAX_TXOSPENDER_INDEX_CACHE : 0);75if (n_indexes > 0) {76uint64_t max_cache = std::min(total_cache * 5 / 100, MAX_FILTER_INDEX_CACHE);77index_sizes.filter_index = max_cache / n_indexes;78total_cache -= index_sizes.filter_index * n_indexes;79}80total_cache -= index_sizes.tx_index;81total_cache -= index_sizes.txospender_index;82return {index_sizes, kernel::CacheSizes{total_cache}};83}85void LogOversizedDbCache(const ArgsManager& args) noexcept86{87if (const auto total_ram{TryGetTotalRam()}) {88const uint64_t db_cache{CalculateDbCacheBytes(args)};89if (ShouldWarnOversizedDbCache(db_cache, *total_ram)) {90InitWarning(bilingual_str{tfm::format(_("A %zu MiB dbcache may be too large for a system memory of only %zu MiB."),91db_cache / 1_MiB, *total_ram / 1_MiB)});92}93}94}95} // namespace node