From b119bd4e0d6fc145496c7f574d4b9d8fab6b34c9 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Mon, 30 Jan 2023 11:32:59 -0500 Subject: [PATCH] merge bitcoin#26999: A few follow-ups to bitcoin#17487 --- src/coins.cpp | 9 ++++++--- src/test/coins_tests.cpp | 2 +- src/test/fuzz/coins_view.cpp | 3 +++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/coins.cpp b/src/coins.cpp index 3cb5ef1125e88..5ce4f16346007 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -236,15 +236,18 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn } bool CCoinsViewCache::Flush() { - bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/ true); - cacheCoins.clear(); + bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/true); + if (fOk && !cacheCoins.empty()) { + /* BatchWrite must erase all cacheCoins elements when erase=true. */ + throw std::logic_error("Not all cached coins were erased"); + } cachedCoinsUsage = 0; return fOk; } bool CCoinsViewCache::Sync() { - bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/ false); + bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/false); // Instead of clearing `cacheCoins` as we would in Flush(), just clear the // FRESH/DIRTY flags of any coin that isn't spent. for (auto it = cacheCoins.begin(); it != cacheCoins.end(); ) { diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index bf3519cee0aaf..2c8e0b10133db 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -55,7 +55,7 @@ class CCoinsViewTest : public CCoinsView bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock, bool erase = true) override { - for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : ++it) { + for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : std::next(it)) { if (it->second.flags & CCoinsCacheEntry::DIRTY) { // Same optimization used in CCoinsViewDB is to only write dirty entries. map_[it->first] = it->second.coin; diff --git a/src/test/fuzz/coins_view.cpp b/src/test/fuzz/coins_view.cpp index f5fcbcd84c5a2..5bd9c8a0c77e1 100644 --- a/src/test/fuzz/coins_view.cpp +++ b/src/test/fuzz/coins_view.cpp @@ -76,6 +76,9 @@ FUZZ_TARGET_INIT(coins_view, initialize_coins_view) [&] { (void)coins_view_cache.Flush(); }, + [&] { + (void)coins_view_cache.Sync(); + }, [&] { coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider)); },