From 1b40dc02a6a292239037ac5a44e0d0c9506d5fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Sat, 19 Jul 2025 17:19:21 -0700 Subject: [PATCH] refactor: extract `LargeCoinsCacheThreshold` from `GetCoinsCacheSizeState` Move-only commit, enabled reusing the large cache size calculation logic later. The only difference is the removal of the `static` keyword (since in a constexpr function it's a C++23 extension) --- src/validation.cpp | 7 +------ src/validation.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index b62691ca4c0..c8c496a59ab 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2786,15 +2786,10 @@ CoinsCacheSizeState Chainstate::GetCoinsCacheSizeState( int64_t nTotalSpace = max_coins_cache_size_bytes + std::max(int64_t(max_mempool_size_bytes) - nMempoolUsage, 0); - //! No need to periodic flush if at least this much space still available. - static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024; // 10MB - int64_t large_threshold = - std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES); - if (cacheSize > nTotalSpace) { LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace); return CoinsCacheSizeState::CRITICAL; - } else if (cacheSize > large_threshold) { + } else if (cacheSize > LargeCoinsCacheThreshold(nTotalSpace)) { return CoinsCacheSizeState::LARGE; } return CoinsCacheSizeState::OK; diff --git a/src/validation.h b/src/validation.h index c25dd2de2d9..8998dccedcf 100644 --- a/src/validation.h +++ b/src/validation.h @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -503,6 +504,15 @@ enum class CoinsCacheSizeState OK = 0 }; +constexpr int64_t LargeCoinsCacheThreshold(int64_t nTotalSpace) noexcept +{ + //! No need to periodic flush if at least this much space still available. + constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024; // 10MB + int64_t large_threshold = + std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES); + return large_threshold; +} + /** * Chainstate stores and provides an API to update our local knowledge of the * current best chain.