refactor: assert newly-created parent cache entry has zero memory usage
What changed, and why it matters
This commit adds a single runtime assertion inside Bitcoin Core's coin cache code. It checks that a newly-created cache entry starts out empty, so the code does not need to adjust its memory-usage bookkeeping before assigning a coin to it. It is a documentation-style safety check, not a fix for a known bug or vulnerability.
No security action required. Treat as normal code-review/refactor commit.
Security signals we found
No security-relevant behavior change: only an assert() was added
No memory corruption, overflow, or privilege change introduced
No bug fix or vulnerability remediation present in diff
Evidence from the diff
In CCoinsViewCache::BatchWrite, when a parent cache entry does not yet exist for a coin being flushed, try_emplace creates it. The new CCoinsCacheEntry is default-constructed, so its coin is empty and DynamicMemoryUsage() is zero. The patch adds assert(entry.coin.DynamicMemoryUsage() == 0) to document that invariant and justify not decrementing cachedCoinsUsage before the subsequent assignment. The change is purely an added assertion; no logic or control flow is altered.
Changed components
src/coins.cppCCoinsViewCache::BatchWriteInspect captured patch +1 / −0
diff --git a/src/coins.cpp b/src/coins.cpp
index 24a102b0..59c7d67c 100644
--- a/src/coins.cpp
+++ b/src/coins.cpp
@@ -195,6 +195,7 @@ bool CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &ha
// and mark it as dirty.
itUs = cacheCoins.try_emplace(it->first).first;
CCoinsCacheEntry& entry{itUs->second};
+ assert(entry.coin.DynamicMemoryUsage() == 0);
if (cursor.WillErase(*it)) {
// Since this entry will be erased,
// we can move the coin into us instead of copying it
Why this scored 13/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.