Split out of #32296 (comment) since this needed more complicated production code changes.
The changes ensure cachedCoinsUsage
remains balanced throughout all coin operations and that the sanitizers will catch future violations.
The change was tested with the related fuzz test, and asserted before/after each cachedCoinsUsage
change (in production code and fuzz) that the calculations are still correct by recalculating it from scratch:
0bool CCoinsViewCache::CacheUsageValid() const
1{
2 size_t actual{0};
3 for (auto& entry : cacheCoins | std::views::values) actual += entry.coin.DynamicMemoryUsage();
4 return actual == cachedCoinsUsage;
5}
This part wasn’t committed to the code.