Skip to content

Commit df34a94

Browse files
andrewtothl0rinc
andcommitted
refactor: encapsulate flags access for dirty and fresh checks
No behavior change. This prepares moving the cache entry flags field to private access. Co-Authored-By: l0rinc <[email protected]>
1 parent 9774a95 commit df34a94

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

src/coins.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
9393
//
9494
// If the coin doesn't exist in the current cache, or is spent but not
9595
// DIRTY, then it can be marked FRESH.
96-
fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY);
96+
fresh = !it->second.IsDirty();
9797
}
9898
it->second.coin = std::move(coin);
9999
it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
@@ -138,7 +138,7 @@ bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
138138
if (moveout) {
139139
*moveout = std::move(it->second.coin);
140140
}
141-
if (it->second.flags & CCoinsCacheEntry::FRESH) {
141+
if (it->second.IsFresh()) {
142142
cacheCoins.erase(it);
143143
} else {
144144
it->second.flags |= CCoinsCacheEntry::DIRTY;
@@ -183,14 +183,14 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
183183
it != mapCoins.end();
184184
it = erase ? mapCoins.erase(it) : std::next(it)) {
185185
// Ignore non-dirty entries (optimization).
186-
if (!(it->second.flags & CCoinsCacheEntry::DIRTY)) {
186+
if (!it->second.IsDirty()) {
187187
continue;
188188
}
189189
CCoinsMap::iterator itUs = cacheCoins.find(it->first);
190190
if (itUs == cacheCoins.end()) {
191191
// The parent cache does not have an entry, while the child cache does.
192192
// We can ignore it if it's both spent and FRESH in the child
193-
if (!(it->second.flags & CCoinsCacheEntry::FRESH && it->second.coin.IsSpent())) {
193+
if (!(it->second.IsFresh() && it->second.coin.IsSpent())) {
194194
// Create the coin in the parent cache, move the data up
195195
// and mark it as dirty.
196196
CCoinsCacheEntry& entry = cacheCoins[it->first];
@@ -207,21 +207,21 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
207207
// We can mark it FRESH in the parent if it was FRESH in the child
208208
// Otherwise it might have just been flushed from the parent's cache
209209
// and already exist in the grandparent
210-
if (it->second.flags & CCoinsCacheEntry::FRESH) {
210+
if (it->second.IsFresh()) {
211211
entry.flags |= CCoinsCacheEntry::FRESH;
212212
}
213213
}
214214
} else {
215215
// Found the entry in the parent cache
216-
if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coin.IsSpent()) {
216+
if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) {
217217
// The coin was marked FRESH in the child cache, but the coin
218218
// exists in the parent cache. If this ever happens, it means
219219
// the FRESH flag was misapplied and there is a logic error in
220220
// the calling code.
221221
throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
222222
}
223223

224-
if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coin.IsSpent()) {
224+
if (itUs->second.IsFresh() && it->second.coin.IsSpent()) {
225225
// The grandparent cache does not have an entry, and the coin
226226
// has been spent. We can just delete it from the parent cache.
227227
cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
@@ -283,7 +283,7 @@ bool CCoinsViewCache::Sync()
283283
void CCoinsViewCache::Uncache(const COutPoint& hash)
284284
{
285285
CCoinsMap::iterator it = cacheCoins.find(hash);
286-
if (it != cacheCoins.end() && it->second.flags == 0) {
286+
if (it != cacheCoins.end() && !it->second.IsDirty() && !it->second.IsFresh()) {
287287
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
288288
TRACE5(utxocache, uncache,
289289
hash.hash.data(),
@@ -326,8 +326,8 @@ void CCoinsViewCache::SanityCheck() const
326326
size_t recomputed_usage = 0;
327327
for (const auto& [_, entry] : cacheCoins) {
328328
unsigned attr = 0;
329-
if (entry.flags & CCoinsCacheEntry::DIRTY) attr |= 1;
330-
if (entry.flags & CCoinsCacheEntry::FRESH) attr |= 2;
329+
if (entry.IsDirty()) attr |= 1;
330+
if (entry.IsFresh()) attr |= 2;
331331
if (entry.coin.IsSpent()) attr |= 4;
332332
// Only 5 combinations are possible.
333333
assert(attr != 2 && attr != 4 && attr != 7);

src/coins.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ struct CCoinsCacheEntry
130130
CCoinsCacheEntry() : flags(0) {}
131131
explicit CCoinsCacheEntry(Coin&& coin_) : coin(std::move(coin_)), flags(0) {}
132132
CCoinsCacheEntry(Coin&& coin_, unsigned char flag) : coin(std::move(coin_)), flags(flag) {}
133+
134+
inline bool IsDirty() const noexcept { return flags & DIRTY; }
135+
inline bool IsFresh() const noexcept { return flags & FRESH; }
133136
};
134137

135138
/**

src/test/coins_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class CCoinsViewTest : public CCoinsView
5858
bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock, bool erase = true) override
5959
{
6060
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : std::next(it)) {
61-
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
61+
if (it->second.IsDirty()) {
6262
// Same optimization used in CCoinsViewDB is to only write dirty entries.
6363
map_[it->first] = it->second.coin;
6464
if (it->second.coin.IsSpent() && InsecureRandRange(3) == 0) {

src/test/fuzz/coinscache_sim.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class CoinsViewBottom final : public CCoinsView
175175
bool BatchWrite(CCoinsMap& data, const uint256&, bool erase) final
176176
{
177177
for (auto it = data.begin(); it != data.end(); it = erase ? data.erase(it) : std::next(it)) {
178-
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
178+
if (it->second.IsDirty()) {
179179
if (it->second.coin.IsSpent() && (it->first.n % 5) != 4) {
180180
m_data.erase(it->first);
181181
} else if (erase) {

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo
115115
batch.Write(DB_HEAD_BLOCKS, Vector(hashBlock, old_tip));
116116

117117
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
118-
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
118+
if (it->second.IsDirty()) {
119119
CoinEntry entry(&it->first);
120120
if (it->second.coin.IsSpent())
121121
batch.Erase(entry);

0 commit comments

Comments
 (0)