From 6956ee9cc76c7815e53fbdee01edc4379d8caafc Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Sat, 31 Aug 2024 15:11:21 -0400 Subject: [PATCH] coins: set all inserted spent coins to fresh A spent coins must be DIRTY, so remove references to spent but not DIRTY coins. The only way a spent coin can be not DIRTY is when creating the CCoinsCacheEntry with an empty coin. This can be made more clear by setting fresh if inserted, instead of checking if an unspent coin is not DIRTY. --- src/coins.cpp | 12 +++++------- src/test/coins_tests.cpp | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/coins.cpp b/src/coins.cpp index 4fc06d79cd8..29579908726 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -79,20 +79,18 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi if (!it->second.coin.IsSpent()) { throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)"); } - // If the coin exists in this cache as a spent coin and is DIRTY, then + // If the coin exists in this cache as a spent coin, then // its spentness hasn't been flushed to the parent cache. We're // re-adding the coin to this cache now but we can't mark it as FRESH. - // If we mark it FRESH and then spend it before the cache is flushed - // we would remove it from this cache and would never flush spentness - // to the parent cache. + // A spent FRESH coin cannot exist in the cache because a FRESH coin + // is simply erased when it is spent. // // Re-adding a spent coin can happen in the case of a re-org (the coin // is 'spent' when the block adding it is disconnected and then // re-added when it is also added in a newly connected block). // - // If the coin doesn't exist in the current cache, or is spent but not - // DIRTY, then it can be marked FRESH. - fresh = !it->second.IsDirty(); + // If the coin doesn't exist in the current cache then it can be marked FRESH. + fresh = inserted; } it->second.coin = std::move(coin); CCoinsCacheEntry::SetDirty(*it, m_sentinel); diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index 8738e728c17..facc308dd7f 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -756,7 +756,7 @@ BOOST_AUTO_TEST_CASE(ccoins_add) CheckAddCoin(base_value, MISSING, VALUE3, VALUE3_DIRTY_FRESH, false); CheckAddCoin(base_value, MISSING, VALUE3, VALUE3_DIRTY, true ); - CheckAddCoin(base_value, SPENT_CLEAN, VALUE3, VALUE3_DIRTY_FRESH, false); + CheckAddCoin(base_value, SPENT_CLEAN, VALUE3, VALUE3_DIRTY, false); CheckAddCoin(base_value, SPENT_CLEAN, VALUE3, VALUE3_DIRTY, true ); CheckAddCoin(base_value, SPENT_FRESH, VALUE3, VALUE3_DIRTY_FRESH, false); CheckAddCoin(base_value, SPENT_FRESH, VALUE3, VALUE3_DIRTY_FRESH, true );