Files
bitcoin/src/node/caches.h
MarcoFalke fabafd91f1 refactor: Use u64 over size_t for all cache sizes to fix a 32-bit overflow
This is a refactor on 64-bit systems, because size_t is equal to u64.

However, on 32-bit systems, it fixes an integer overflow while calculating the cache sizes:

src/node/caches.cpp:71:49: runtime error: unsigned integer overflow: 471859200 * 10 cannot be represented in type size_t (aka "unsigned int")

This happens while multiplying the default cache size (450MiB) by 10:

index_sizes.tx_index = std::min(total_cache * 10 / 100, ...)
                                ^^^^^^^^^^^^^^^^

The issue was introduced in commit d06dabf26b.

====

Also, add missing includes in touched files, according to IWYU.
2026-07-08 16:39:18 +02:00

43 lines
1.2 KiB
C++

// Copyright (c) 2021-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_NODE_CACHES_H
#define BITCOIN_NODE_CACHES_H
#include <kernel/caches.h>
#include <util/byte_units.h>
#include <cstddef>
#include <cstdint>
class ArgsManager;
//! min. -dbcache (bytes)
static constexpr uint64_t MIN_DB_CACHE{4_MiB};
//! -dbcache default (bytes)
static constexpr uint64_t DEFAULT_DB_CACHE{DEFAULT_KERNEL_CACHE};
namespace node {
uint64_t GetDefaultDBCache();
struct IndexCacheSizes {
uint64_t tx_index{0};
uint64_t filter_index{0};
uint64_t txospender_index{0};
};
struct CacheSizes {
IndexCacheSizes index;
kernel::CacheSizes kernel;
};
CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
constexpr bool ShouldWarnOversizedDbCache(uint64_t dbcache, uint64_t total_ram) noexcept
{
const uint64_t cap{(total_ram < 2_GiB) ? DEFAULT_DB_CACHE : (total_ram / 100) * 75};
return dbcache > cap;
}
void LogOversizedDbCache(const ArgsManager& args) noexcept;
} // namespace node
#endif // BITCOIN_NODE_CACHES_H