It does make the callsites less elegant. Here's the diff:
diff --git a/src/coins.cpp b/src/coins.cpp
index 5caf0e2789..81be5967b7 100644
--- a/src/coins.cpp
+++ b/src/coins.cpp
@@ -96,7 +96,11 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
}
it->second.coin = std::move(coin);
- CCoinsCacheEntry::SetDirty(*it, m_sentinel, fresh);
+ if (fresh) {
+ CCoinsCacheEntry::SetFreshAndDirty(*it, m_sentinel);
+ } else {
+ CCoinsCacheEntry::SetDirty(*it, m_sentinel);
+ }
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
TRACEPOINT(utxocache, add,
outpoint.hash.data(),
@@ -206,7 +210,11 @@ bool CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &ha
// We can mark it FRESH in the parent if it was FRESH in the child
// Otherwise it might have just been flushed from the parent's cache
// and already exist in the grandparent
- CCoinsCacheEntry::SetDirty(*itUs, m_sentinel, it->second.IsFresh());
+ if (it->second.IsFresh()) {
+ CCoinsCacheEntry::SetFreshAndDirty(*itUs, m_sentinel);
+ } else {
+ CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
+ }
}
} else {
// Found the entry in the parent cache
diff --git a/src/coins.h b/src/coins.h
index e1463d1236..ceb075bd6f 100644
--- a/src/coins.h
+++ b/src/coins.h
@@ -109,7 +109,7 @@ struct CCoinsCacheEntry
private:
/**
* These are used to create a doubly linked list of flagged entries.
- * They are set in SetDirty and unset in SetClean.
+ * They are set in SetDirty/SetFreshAndDirty and unset in SetClean.
* A flagged entry is any entry that is DIRTY or DIRTY-and-FRESH.
*
* DIRTY entries are tracked so that only modified entries can be passed to
@@ -167,9 +167,14 @@ public:
SetClean();
}
- static void SetDirty(CoinsCachePair& pair, CoinsCachePair& sentinel, bool fresh = false) noexcept
+ static void SetDirty(CoinsCachePair& pair, CoinsCachePair& sentinel) noexcept
{
- AddFlags(fresh ? DIRTY | FRESH : DIRTY, pair, sentinel);
+ AddFlags(DIRTY, pair, sentinel);
+ }
+
+ static void SetFreshAndDirty(CoinsCachePair& pair, CoinsCachePair& sentinel) noexcept
+ {
+ AddFlags(DIRTY | FRESH, pair, sentinel);
}
void SetClean() noexcept
diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp
index cc5146a915..c499fde360 100644
--- a/src/test/coins_tests.cpp
+++ b/src/test/coins_tests.cpp
@@ -633,8 +633,10 @@ static size_t InsertCoinsMapEntry(CCoinsMap& map, CoinsCachePair& sentinel, cons
SetCoinsValue(cache_coin.value, entry.coin);
auto [iter, inserted] = map.emplace(OUTPOINT, std::move(entry));
assert(inserted);
- if (cache_coin.IsDirty()) {
- CCoinsCacheEntry::SetDirty(*iter, sentinel, cache_coin.IsDirtyFresh());
+ if (cache_coin.IsDirtyFresh()) {
+ CCoinsCacheEntry::SetFreshAndDirty(*iter, sentinel);
+ } else if (cache_coin.IsDirty()) {
+ CCoinsCacheEntry::SetDirty(*iter, sentinel);
}
return iter->second.coin.DynamicMemoryUsage();
}
diff --git a/src/test/coinscachepair_tests.cpp b/src/test/coinscachepair_tests.cpp
index 1110f1cdd1..73fbf784c5 100644
--- a/src/test/coinscachepair_tests.cpp
+++ b/src/test/coinscachepair_tests.cpp
@@ -155,7 +155,7 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n1);
// Check that setting DIRTY and FRESH on new node inserts it after n1
- CCoinsCacheEntry::SetDirty(n2, sentinel, /*fresh=*/true);
+ CCoinsCacheEntry::SetFreshAndDirty(n2, sentinel);
BOOST_CHECK(n2.second.IsFresh() && n2.second.IsDirty());
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
BOOST_CHECK_EQUAL(n2.second.Prev(), &n1);
@@ -163,7 +163,7 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
// Check that we can set extra state, but they don't change our position
- CCoinsCacheEntry::SetDirty(n1, sentinel, /*fresh=*/true);
+ CCoinsCacheEntry::SetFreshAndDirty(n1, sentinel);
BOOST_CHECK(n1.second.IsDirty() && n1.second.IsFresh());
BOOST_CHECK_EQUAL(n1.second.Next(), &n2);
BOOST_CHECK_EQUAL(n1.second.Prev(), &sentinel);
diff --git a/src/test/fuzz/coins_view.cpp b/src/test/fuzz/coins_view.cpp
index d538a62825..0a2df0fdb6 100644
--- a/src/test/fuzz/coins_view.cpp
+++ b/src/test/fuzz/coins_view.cpp
@@ -143,7 +143,13 @@ void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend
coins_cache_entry.coin = *opt_coin;
}
auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
- if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel, fresh);
+ if (dirty) {
+ if (fresh) {
+ CCoinsCacheEntry::SetFreshAndDirty(*it, sentinel);
+ } else {
+ CCoinsCacheEntry::SetDirty(*it, sentinel);
+ }
+ }
}
bool expected_code_path = false;
try {