From 8aa21e119b0289e5b5b6fb02e275690a284073f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Thu, 23 Apr 2026 16:19:26 +0200 Subject: [PATCH] kernel, node: colocate dbcache bounds Keep the total database cache bounds with `kernel::CacheSizes` so node and Kernel callers validate against the same range. --- src/kernel/caches.h | 5 +++++ src/node/caches.cpp | 6 +----- src/node/caches.h | 2 -- src/test/caches_tests.cpp | 1 + 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/kernel/caches.h b/src/kernel/caches.h index ad65b165c23..6bc10c92355 100644 --- a/src/kernel/caches.h +++ b/src/kernel/caches.h @@ -9,7 +9,12 @@ #include #include +#include +//! Minimum total database cache (bytes) +static constexpr uint64_t MIN_DBCACHE_BYTES{4_MiB}; +//! Maximum total database cache on current architecture (bytes) +static constexpr uint64_t MAX_DBCACHE_BYTES{sizeof(void*) == 4 ? 1_GiB : std::numeric_limits::max()}; //! Suggested default amount of cache reserved for the kernel (bytes) static constexpr uint64_t DEFAULT_KERNEL_CACHE{450_MiB}; //! Default LevelDB write batch size diff --git a/src/node/caches.cpp b/src/node/caches.cpp index 37b674fa918..c25b3e9a9a7 100644 --- a/src/node/caches.cpp +++ b/src/node/caches.cpp @@ -18,7 +18,6 @@ #include #include -#include #include // Unlike for the UTXO database, for the txindex scenario the leveldb cache make @@ -29,8 +28,6 @@ static constexpr uint64_t MAX_TX_INDEX_CACHE{1_GiB}; static constexpr uint64_t MAX_FILTER_INDEX_CACHE{1_GiB}; //! Max memory allocated to tx spenderindex DB specific cache in bytes. static constexpr uint64_t MAX_TXOSPENDER_INDEX_CACHE{1_GiB}; -//! Maximum dbcache size on 32-bit systems. -static constexpr uint64_t MAX_32BIT_DBCACHE{1_GiB}; //! Larger default dbcache on 64-bit systems with enough RAM. static constexpr uint64_t HIGH_DEFAULT_DBCACHE{1_GiB}; //! Minimum detected RAM required for HIGH_DEFAULT_DBCACHE. @@ -52,8 +49,7 @@ uint64_t CalculateDbCacheBytes(const ArgsManager& args) if (auto db_cache{args.GetIntArg("-dbcache")}) { if (*db_cache < 0) db_cache = 0; const uint64_t db_cache_bytes{SaturatingLeftShift(*db_cache, 20)}; - constexpr uint64_t max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits::max()}; - return std::max(MIN_DBCACHE_BYTES, std::min(db_cache_bytes, max_db_cache)); + return std::max(MIN_DBCACHE_BYTES, std::min(db_cache_bytes, MAX_DBCACHE_BYTES)); } return GetDefaultDBCache(); } diff --git a/src/node/caches.h b/src/node/caches.h index 8bfd499b9ea..756dfa37874 100644 --- a/src/node/caches.h +++ b/src/node/caches.h @@ -14,8 +14,6 @@ class ArgsManager; -//! min. -dbcache (bytes) -static constexpr uint64_t MIN_DBCACHE_BYTES{4_MiB}; //! -dbcache default (bytes) static constexpr uint64_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE}; //! Reserved non-dbcache memory usage. diff --git a/src/test/caches_tests.cpp b/src/test/caches_tests.cpp index 8144aac00d5..15820a79746 100644 --- a/src/test/caches_tests.cpp +++ b/src/test/caches_tests.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or https://opensource.org/license/mit. +#include #include #include