From b0ff31084006ac7d4a7afba3190ca75f5f8441af Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 1 Feb 2023 18:28:08 -0500 Subject: Add CCoinsViewCache::SanityCheck() and use it in fuzz test --- src/coins.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/coins.cpp') diff --git a/src/coins.cpp b/src/coins.cpp index e98bf816ab..8d99019bb0 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -314,6 +314,23 @@ void CCoinsViewCache::ReallocateCache() ::new (&cacheCoins) CCoinsMap(); } +void CCoinsViewCache::SanityCheck() const +{ + size_t recomputed_usage = 0; + for (const auto& [_, entry] : cacheCoins) { + unsigned attr = 0; + if (entry.flags & CCoinsCacheEntry::DIRTY) attr |= 1; + if (entry.flags & CCoinsCacheEntry::FRESH) attr |= 2; + if (entry.coin.IsSpent()) attr |= 4; + // Only 5 combinations are possible. + assert(attr != 2 && attr != 4 && attr != 7); + + // Recompute cachedCoinsUsage. + recomputed_usage += entry.coin.DynamicMemoryUsage(); + } + assert(recomputed_usage == cachedCoinsUsage); +} + static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut(), PROTOCOL_VERSION); static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT; -- cgit v1.2.3