mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 11:13:23 -03:00
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:
parent
9715d3bf1e
commit
8737c0cefa
4 changed files with 14 additions and 9 deletions
|
@ -51,7 +51,7 @@ CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const
|
||||||
if (ret->second.coin.IsSpent()) {
|
if (ret->second.coin.IsSpent()) {
|
||||||
// The parent only has an empty entry for this outpoint; we can consider our
|
// The parent only has an empty entry for this outpoint; we can consider our
|
||||||
// version as fresh.
|
// version as fresh.
|
||||||
ret->second.flags = CCoinsCacheEntry::FRESH;
|
ret->second.AddFlags(CCoinsCacheEntry::FRESH);
|
||||||
}
|
}
|
||||||
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
|
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -96,7 +96,7 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
|
||||||
fresh = !it->second.IsDirty();
|
fresh = !it->second.IsDirty();
|
||||||
}
|
}
|
||||||
it->second.coin = std::move(coin);
|
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();
|
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
|
||||||
TRACE5(utxocache, add,
|
TRACE5(utxocache, add,
|
||||||
outpoint.hash.data(),
|
outpoint.hash.data(),
|
||||||
|
@ -141,7 +141,7 @@ bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
|
||||||
if (it->second.IsFresh()) {
|
if (it->second.IsFresh()) {
|
||||||
cacheCoins.erase(it);
|
cacheCoins.erase(it);
|
||||||
} else {
|
} else {
|
||||||
it->second.flags |= CCoinsCacheEntry::DIRTY;
|
it->second.AddFlags(CCoinsCacheEntry::DIRTY);
|
||||||
it->second.coin.Clear();
|
it->second.coin.Clear();
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -203,12 +203,12 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
|
||||||
entry.coin = it->second.coin;
|
entry.coin = it->second.coin;
|
||||||
}
|
}
|
||||||
cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
|
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
|
// 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
|
// Otherwise it might have just been flushed from the parent's cache
|
||||||
// and already exist in the grandparent
|
// and already exist in the grandparent
|
||||||
if (it->second.IsFresh()) {
|
if (it->second.IsFresh()) {
|
||||||
entry.flags |= CCoinsCacheEntry::FRESH;
|
entry.AddFlags(CCoinsCacheEntry::FRESH);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -238,7 +238,7 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
|
||||||
itUs->second.coin = it->second.coin;
|
itUs->second.coin = it->second.coin;
|
||||||
}
|
}
|
||||||
cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
|
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
|
// 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. If it already existed and was spent in the parent
|
||||||
// cache then marking it FRESH would prevent that spentness
|
// cache then marking it FRESH would prevent that spentness
|
||||||
|
@ -273,7 +273,7 @@ bool CCoinsViewCache::Sync()
|
||||||
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
|
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
|
||||||
it = cacheCoins.erase(it);
|
it = cacheCoins.erase(it);
|
||||||
} else {
|
} else {
|
||||||
it->second.flags = 0;
|
it->second.ClearFlags();
|
||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,6 +131,11 @@ struct CCoinsCacheEntry
|
||||||
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
|
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
|
||||||
CCoinsCacheEntry(Coin&& coin_, unsigned char flag) : coin(std::move(coin_)), flags(flag) {}
|
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 unsigned char GetFlags() const noexcept { return flags; }
|
||||||
inline bool IsDirty() const noexcept { return flags & DIRTY; }
|
inline bool IsDirty() const noexcept { return flags & DIRTY; }
|
||||||
inline bool IsFresh() const noexcept { return flags & FRESH; }
|
inline bool IsFresh() const noexcept { return flags & FRESH; }
|
||||||
|
|
|
@ -584,7 +584,7 @@ static size_t InsertCoinsMapEntry(CCoinsMap& map, CAmount value, char flags)
|
||||||
}
|
}
|
||||||
assert(flags != NO_ENTRY);
|
assert(flags != NO_ENTRY);
|
||||||
CCoinsCacheEntry entry;
|
CCoinsCacheEntry entry;
|
||||||
entry.flags = flags;
|
entry.AddFlags(flags);
|
||||||
SetCoinsValue(value, entry.coin);
|
SetCoinsValue(value, entry.coin);
|
||||||
auto inserted = map.emplace(OUTPOINT, std::move(entry));
|
auto inserted = map.emplace(OUTPOINT, std::move(entry));
|
||||||
assert(inserted.second);
|
assert(inserted.second);
|
||||||
|
|
|
@ -125,7 +125,7 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
|
||||||
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
|
LIMITED_WHILE(good_data && fuzzed_data_provider.ConsumeBool(), 10'000)
|
||||||
{
|
{
|
||||||
CCoinsCacheEntry coins_cache_entry;
|
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()) {
|
if (fuzzed_data_provider.ConsumeBool()) {
|
||||||
coins_cache_entry.coin = random_coin;
|
coins_cache_entry.coin = random_coin;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue