Skip to content

Commit b17e91d

Browse files
committed
refactoring: introduce CChainState::GetCoinsCacheSizeState
This separates out some logic for detecting how full the coins cache is from FlushStateToDisk. We'll want to reuse this logic when deciding when to flush the coins cache during UTXO snapshot activation.
1 parent 54e11a3 commit b17e91d

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/txdb.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class CBlockIndex;
2020
class CCoinsViewDBCursor;
2121
class uint256;
2222

23-
//! No need to periodic flush if at least this much space still available.
24-
static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
2523
//! -dbcache default (MiB)
2624
static const int64_t nDefaultDbCache = 450;
2725
//! -dbbatchsize default (bytes)

src/validation.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,13 +2185,44 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21852185
return true;
21862186
}
21872187

2188+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
2189+
{
2190+
return this->GetCoinsCacheSizeState(
2191+
tx_pool,
2192+
nCoinCacheUsage,
2193+
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
2194+
}
2195+
2196+
CoinsCacheSizeState CChainState::GetCoinsCacheSizeState(
2197+
const CTxMemPool& tx_pool,
2198+
size_t max_coins_cache_size_bytes,
2199+
size_t max_mempool_size_bytes)
2200+
{
2201+
int64_t nMempoolUsage = tx_pool.DynamicMemoryUsage();
2202+
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
2203+
int64_t nTotalSpace =
2204+
max_coins_cache_size_bytes + std::max<int64_t>(max_mempool_size_bytes - nMempoolUsage, 0);
2205+
2206+
//! No need to periodic flush if at least this much space still available.
2207+
static constexpr int64_t MAX_BLOCK_COINSDB_USAGE_BYTES = 10 * 1024 * 1024; // 10MB
2208+
int64_t large_threshold =
2209+
std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE_BYTES);
2210+
2211+
if (cacheSize > nTotalSpace) {
2212+
LogPrintf("Cache size (%s) exceeds total space (%s)\n", cacheSize, nTotalSpace);
2213+
return CoinsCacheSizeState::CRITICAL;
2214+
} else if (cacheSize > large_threshold) {
2215+
return CoinsCacheSizeState::LARGE;
2216+
}
2217+
return CoinsCacheSizeState::OK;
2218+
}
2219+
21882220
bool CChainState::FlushStateToDisk(
21892221
const CChainParams& chainparams,
21902222
BlockValidationState &state,
21912223
FlushStateMode mode,
21922224
int nManualPruneHeight)
21932225
{
2194-
int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
21952226
LOCK(cs_main);
21962227
assert(this->CanFlushToDisk());
21972228
static int64_t nLastWrite = 0;
@@ -2206,6 +2237,7 @@ bool CChainState::FlushStateToDisk(
22062237
{
22072238
bool fFlushForPrune = false;
22082239
bool fDoFullFlush = false;
2240+
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(::mempool);
22092241
LOCK(cs_LastBlockFile);
22102242
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
22112243
if (nManualPruneHeight > 0) {
@@ -2234,13 +2266,10 @@ bool CChainState::FlushStateToDisk(
22342266
if (nLastFlush == 0) {
22352267
nLastFlush = nNow;
22362268
}
2237-
int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
2238-
int64_t cacheSize = CoinsTip().DynamicMemoryUsage();
2239-
int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
22402269
// The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
2241-
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE * 1024 * 1024);
2270+
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
22422271
// The cache is over the limit, we have to write now.
2243-
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cacheSize > nTotalSpace;
2272+
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
22442273
// It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
22452274
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
22462275
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.

src/validation.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ class CoinsViews {
530530
void InitCache() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
531531
};
532532

533+
enum class CoinsCacheSizeState
534+
{
535+
//! The coins cache is in immediate need of a flush.
536+
CRITICAL = 2,
537+
//! The cache is at >= 90% capacity.
538+
LARGE = 1,
539+
OK = 0
540+
};
541+
533542
/**
534543
* CChainState stores and provides an API to update our local knowledge of the
535544
* current best chain.
@@ -721,6 +730,17 @@ class CChainState {
721730
/** Update the chain tip based on database information, i.e. CoinsTip()'s best block. */
722731
bool LoadChainTip(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
723732

733+
//! Dictates whether we need to flush the cache to disk or not.
734+
//!
735+
//! @return the state of the size of the coins cache.
736+
CoinsCacheSizeState GetCoinsCacheSizeState(const CTxMemPool& tx_pool)
737+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
738+
739+
CoinsCacheSizeState GetCoinsCacheSizeState(
740+
const CTxMemPool& tx_pool,
741+
size_t max_coins_cache_size_bytes,
742+
size_t max_mempool_size_bytes) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
743+
724744
private:
725745
bool ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);
726746
bool ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs);

0 commit comments

Comments
 (0)