Skip to content

Commit 0e89187

Browse files
sipaandrewtoth
authored andcommitted
Add linked-list test to CCoinsViewCache::SanityCheck
1 parent 05cf4e1 commit 0e89187

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/coins.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ void CCoinsViewCache::ReallocateCache()
320320
void CCoinsViewCache::SanityCheck() const
321321
{
322322
size_t recomputed_usage = 0;
323+
size_t count_flagged = 0;
323324
for (const auto& [_, entry] : cacheCoins) {
324325
unsigned attr = 0;
325326
if (entry.IsDirty()) attr |= 1;
@@ -330,7 +331,22 @@ void CCoinsViewCache::SanityCheck() const
330331

331332
// Recompute cachedCoinsUsage.
332333
recomputed_usage += entry.coin.DynamicMemoryUsage();
334+
335+
// Count the number of entries we expect in the linked list.
336+
if (entry.IsDirty() || entry.IsFresh()) ++count_flagged;
337+
}
338+
// Iterate over the linked list of flagged entries.
339+
size_t count_linked = 0;
340+
for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) {
341+
// Verify linked list integrity.
342+
assert(it->second.Next()->second.Prev() == it);
343+
assert(it->second.Prev()->second.Next() == it);
344+
// Verify they are actually flagged.
345+
assert(it->second.IsDirty() || it->second.IsFresh());
346+
// Count the number of entries actually in the list.
347+
++count_linked;
333348
}
349+
assert(count_linked == count_flagged);
334350
assert(recomputed_usage == cachedCoinsUsage);
335351
}
336352

0 commit comments

Comments
 (0)