kernel, node: colocate dbcache bounds
What changed, and why it matters
This commit is a small code cleanup: it moves two constants that define the minimum and maximum size of the database cache (dbcache) into a shared kernel header so that both the node and kernel code use the same limits. There is no change to the actual values or behavior, and no security issue is present.
No security action needed; this is a benign refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change relocates MIN_DBCACHE_BYTES and the architecture-dependent maximum dbcache bound (previously a local constexpr in node/caches.cpp) into kernel/caches.h as static constexpr values. node/caches.cpp and node/caches.h are updated to use the shared definitions, and the test file now includes the kernel header. The logic and numeric limits remain identical: minimum 4 MiB, maximum 1 GiB on 32-bit and uint64_t::max() on 64-bit.
Changed components
src/kernel/caches.hsrc/node/caches.cppsrc/node/caches.hsrc/test/caches_tests.cppInspect captured patch +7 / −7
diff --git a/src/kernel/caches.h b/src/kernel/caches.h
index ad65b165..6bc10c92 100644
--- a/src/kernel/caches.h
+++ b/src/kernel/caches.h
@@ -9,7 +9,12 @@
#include <algorithm>
#include <cstdint>
+#include <limits>
+//! 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<uint64_t>::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 37b674fa..c25b3e9a 100644
--- a/src/node/caches.cpp
+++ b/src/node/caches.cpp
@@ -18,7 +18,6 @@
#include <algorithm>
#include <cstdint>
-#include <limits>
#include <string>
// 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<uint64_t>(*db_cache, 20)};
- constexpr uint64_t max_db_cache{sizeof(void*) == 4 ? MAX_32BIT_DBCACHE : std::numeric_limits<uint64_t>::max()};
- return std::max<uint64_t>(MIN_DBCACHE_BYTES, std::min<uint64_t>(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 8bfd499b..756dfa37 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 8144aac0..15820a79 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 <kernel/caches.h>
#include <node/caches.h>
#include <util/byte_units.h>
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.