From 0e8918755f725b6269ed2be5a0b46f1611233515 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 16 Jul 2024 10:37:44 -0400 Subject: [PATCH] Add linked-list test to CCoinsViewCache::SanityCheck --- src/coins.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/coins.cpp b/src/coins.cpp index 0bcb4a30f6b..91e02331f51 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -320,6 +320,7 @@ void CCoinsViewCache::ReallocateCache() void CCoinsViewCache::SanityCheck() const { size_t recomputed_usage = 0; + size_t count_flagged = 0; for (const auto& [_, entry] : cacheCoins) { unsigned attr = 0; if (entry.IsDirty()) attr |= 1; @@ -330,7 +331,22 @@ void CCoinsViewCache::SanityCheck() const // Recompute cachedCoinsUsage. recomputed_usage += entry.coin.DynamicMemoryUsage(); + + // Count the number of entries we expect in the linked list. + if (entry.IsDirty() || entry.IsFresh()) ++count_flagged; + } + // Iterate over the linked list of flagged entries. + size_t count_linked = 0; + for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) { + // Verify linked list integrity. + assert(it->second.Next()->second.Prev() == it); + assert(it->second.Prev()->second.Next() == it); + // Verify they are actually flagged. + assert(it->second.IsDirty() || it->second.IsFresh()); + // Count the number of entries actually in the list. + ++count_linked; } + assert(count_linked == count_flagged); assert(recomputed_usage == cachedCoinsUsage); }