refactor: encapsulate flags setting with AddFlags and ClearFlags

No behavior change. This prepares moving the cache entry
flags field to private access.
This commit is contained in:
Andrew Toth 2024-06-28 16:27:48 -04:00
parent 9715d3bf1e
commit 8737c0cefa
No known key found for this signature in database
GPG key ID: 60007AFC8938B018
4 changed files with 14 additions and 9 deletions

View file

@ -51,7 +51,7 @@ CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const
if (ret->second.coin.IsSpent()) {
// The parent only has an empty entry for this outpoint; we can consider our
// version as fresh.
ret->second.flags = CCoinsCacheEntry::FRESH;
ret->second.AddFlags(CCoinsCacheEntry::FRESH);
}
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
return ret;
@ -96,7 +96,7 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
fresh = !it->second.IsDirty();
}
it->second.coin = std::move(coin);
it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
it->second.AddFlags(CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0));
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
TRACE5(utxocache, add,
outpoint.hash.data(),
@ -141,7 +141,7 @@ bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
if (it->second.IsFresh()) {
cacheCoins.erase(it);
} else {
it->second.flags |= CCoinsCacheEntry::DIRTY;
it->second.AddFlags(CCoinsCacheEntry::DIRTY);
it->second.coin.Clear();
}
return true;
@ -203,12 +203,12 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
entry.coin = it->second.coin;
}
cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
entry.flags = CCoinsCacheEntry::DIRTY;
entry.AddFlags(CCoinsCacheEntry::DIRTY);
// 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
if (it->second.IsFresh()) {
entry.flags |= CCoinsCacheEntry::FRESH;
entry.AddFlags(CCoinsCacheEntry::FRESH);
}
}
} else {
@ -238,7 +238,7 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
itUs->second.coin = it->second.coin;
}
cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
itUs->second.flags |= CCoinsCacheEntry::DIRTY;
itUs->second.AddFlags(CCoinsCacheEntry::DIRTY);
// NOTE: It isn't safe to mark the coin as FRESH in the parent
// cache. If it already existed and was spent in the parent
// cache then marking it FRESH would prevent that spentness
@ -273,7 +273,7 @@ bool CCoinsViewCache::Sync()
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
it = cacheCoins.erase(it);
} else {
it->second.flags = 0;
it->second.ClearFlags();
++it;
}
}

View file

@ -131,6 +131,11 @@ struct CCoinsCacheEntry
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
CCoinsCacheEntry(Coin&& coin_, unsigned char flag) : coin(std::move(coin_)), flags(flag) {}
inline void AddFlags(unsigned char flags) noexcept { this->flags |= flags; }
inline void ClearFlags() noexcept
{
flags = 0;
}
inline unsigned char GetFlags() const noexcept { return flags; }
inline bool IsDirty() const noexcept { return flags & DIRTY; }
inline bool IsFresh() const noexcept { return flags & FRESH; }

View file

@ -584,7 +584,7 @@ static size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
}
assert(flags != NO_ENTRY);
CCoinsCacheEntry entry;
entry.flags = flags;
entry.AddFlags(flags);
SetCoinsValue(value, entry.coin);
auto inserted = map.emplace(OUTPOINT, std::move(entry));
assert(inserted.second);

View file

@ -125,7 +125,7 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
{
CCoinsCacheEntry coins_cache_entry;
coins_cache_entry.flags = fuzzed_data_provider.ConsumeIntegral<unsigned char>();
coins_cache_entry.AddFlags(fuzzed_data_provider.ConsumeIntegral<unsigned char>());
if (fuzzed_data_provider.ConsumeBool()) {
coins_cache_entry.coin = random_coin;
} else {