Skip to content

Commit 05293f3

Browse files
committed
Remove ModifyCoins/ModifyNewCoins
1 parent 961e483 commit 05293f3

File tree

2 files changed

+1
-129
lines changed

2 files changed

+1
-129
lines changed

src/coins.cpp

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
6060

6161
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
6262

63-
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), hasModifier(false), cachedCoinsUsage(0) { }
64-
65-
CCoinsViewCache::~CCoinsViewCache()
66-
{
67-
assert(!hasModifier);
68-
}
63+
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) { }
6964

7065
size_t CCoinsViewCache::DynamicMemoryUsage() const {
7166
return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
@@ -98,62 +93,6 @@ bool CCoinsViewCache::GetCoins(const uint256 &txid, CCoins &coins) const {
9893
return false;
9994
}
10095

101-
CCoinsModifier CCoinsViewCache::ModifyCoins(const uint256 &txid) {
102-
assert(!hasModifier);
103-
std::pair<CCoinsMap::iterator, bool> ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry()));
104-
size_t cachedCoinUsage = 0;
105-
if (ret.second) {
106-
if (!base->GetCoins(txid, ret.first->second.coins)) {
107-
// The parent view does not have this entry; mark it as fresh.
108-
ret.first->second.coins.Clear();
109-
ret.first->second.flags = CCoinsCacheEntry::FRESH;
110-
} else if (ret.first->second.coins.IsPruned()) {
111-
// The parent view only has a pruned entry for this; mark it as fresh.
112-
ret.first->second.flags = CCoinsCacheEntry::FRESH;
113-
}
114-
} else {
115-
cachedCoinUsage = ret.first->second.coins.DynamicMemoryUsage();
116-
}
117-
// Assume that whenever ModifyCoins is called, the entry will be modified.
118-
ret.first->second.flags |= CCoinsCacheEntry::DIRTY;
119-
return CCoinsModifier(*this, ret.first, cachedCoinUsage);
120-
}
121-
122-
/* ModifyNewCoins allows for faster coin modification when creating the new
123-
* outputs from a transaction. It assumes that BIP 30 (no duplicate txids)
124-
* applies and has already been tested for (or the test is not required due to
125-
* BIP 34, height in coinbase). If we can assume BIP 30 then we know that any
126-
* non-coinbase transaction we are adding to the UTXO must not already exist in
127-
* the utxo unless it is fully spent. Thus we can check only if it exists DIRTY
128-
* at the current level of the cache, in which case it is not safe to mark it
129-
* FRESH (b/c then its spentness still needs to flushed). If it's not dirty and
130-
* doesn't exist or is pruned in the current cache, we know it either doesn't
131-
* exist or is pruned in parent caches, which is the definition of FRESH. The
132-
* exception to this is the two historical violations of BIP 30 in the chain,
133-
* both of which were coinbases. We do not mark these fresh so we we can ensure
134-
* that they will still be properly overwritten when spent.
135-
*/
136-
CCoinsModifier CCoinsViewCache::ModifyNewCoins(const uint256 &txid, bool coinbase) {
137-
assert(!hasModifier);
138-
std::pair<CCoinsMap::iterator, bool> ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry()));
139-
if (!coinbase) {
140-
// New coins must not already exist.
141-
if (!ret.first->second.coins.IsPruned())
142-
throw std::logic_error("ModifyNewCoins should not find pre-existing coins on a non-coinbase unless they are pruned!");
143-
144-
if (!(ret.first->second.flags & CCoinsCacheEntry::DIRTY)) {
145-
// If the coin is known to be pruned (have no unspent outputs) in
146-
// the current view and the cache entry is not dirty, we know the
147-
// coin also must be pruned in the parent view as well, so it is safe
148-
// to mark this fresh.
149-
ret.first->second.flags |= CCoinsCacheEntry::FRESH;
150-
}
151-
}
152-
ret.first->second.coins.Clear();
153-
ret.first->second.flags |= CCoinsCacheEntry::DIRTY;
154-
return CCoinsModifier(*this, ret.first, 0);
155-
}
156-
15796
void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
15897
assert(!coin.IsPruned());
15998
if (coin.out.scriptPubKey.IsUnspendable()) return;
@@ -257,7 +196,6 @@ void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
257196
}
258197

259198
bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
260-
assert(!hasModifier);
261199
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
262200
if (it->second.flags & CCoinsCacheEntry::DIRTY) { // Ignore non-dirty entries (optimization).
263201
CCoinsMap::iterator itUs = cacheCoins.find(it->first);
@@ -366,25 +304,6 @@ bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
366304
return true;
367305
}
368306

369-
CCoinsModifier::CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_, size_t usage) : cache(cache_), it(it_), cachedCoinUsage(usage) {
370-
assert(!cache.hasModifier);
371-
cache.hasModifier = true;
372-
}
373-
374-
CCoinsModifier::~CCoinsModifier()
375-
{
376-
assert(cache.hasModifier);
377-
cache.hasModifier = false;
378-
it->second.coins.Cleanup();
379-
cache.cachedCoinsUsage -= cachedCoinUsage; // Subtract the old usage
380-
if ((it->second.flags & CCoinsCacheEntry::FRESH) && it->second.coins.IsPruned()) {
381-
cache.cacheCoins.erase(it);
382-
} else {
383-
// If the coin still exists after the modification, add the new usage
384-
cache.cachedCoinsUsage += it->second.coins.DynamicMemoryUsage();
385-
}
386-
}
387-
388307
CCoinsViewCursor::~CCoinsViewCursor()
389308
{
390309
}

src/coins.h

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -405,36 +405,10 @@ class CCoinsViewBacked : public CCoinsView
405405
};
406406

407407

408-
class CCoinsViewCache;
409-
410-
/**
411-
* A reference to a mutable cache entry. Encapsulating it allows us to run
412-
* cleanup code after the modification is finished, and keeping track of
413-
* concurrent modifications.
414-
*/
415-
class CCoinsModifier
416-
{
417-
private:
418-
CCoinsViewCache& cache;
419-
CCoinsMap::iterator it;
420-
size_t cachedCoinUsage; // Cached memory usage of the CCoins object before modification
421-
CCoinsModifier(CCoinsViewCache& cache_, CCoinsMap::iterator it_, size_t usage);
422-
423-
public:
424-
CCoins* operator->() { return &it->second.coins; }
425-
CCoins& operator*() { return it->second.coins; }
426-
~CCoinsModifier();
427-
friend class CCoinsViewCache;
428-
};
429-
430408
/** CCoinsView that adds a memory cache for transactions to another CCoinsView */
431409
class CCoinsViewCache : public CCoinsViewBacked
432410
{
433411
protected:
434-
/* Whether this cache has an active modifier. */
435-
bool hasModifier;
436-
437-
438412
/**
439413
* Make mutable so that we can "fill the cache" even from Get-methods
440414
* declared as "const".
@@ -447,7 +421,6 @@ class CCoinsViewCache : public CCoinsViewBacked
447421

448422
public:
449423
CCoinsViewCache(CCoinsView *baseIn);
450-
~CCoinsViewCache();
451424

452425
// Standard CCoinsView methods
453426
bool GetCoins(const uint256 &txid, CCoins &coins) const;
@@ -479,24 +452,6 @@ class CCoinsViewCache : public CCoinsViewBacked
479452
*/
480453
const Coin AccessCoin(const COutPoint &output) const;
481454

482-
/**
483-
* Return a modifiable reference to a CCoins. If no entry with the given
484-
* txid exists, a new one is created. Simultaneous modifications are not
485-
* allowed.
486-
*/
487-
CCoinsModifier ModifyCoins(const uint256 &txid);
488-
489-
/**
490-
* Return a modifiable reference to a CCoins. Assumes that no entry with the given
491-
* txid exists and creates a new one. This saves a database access in the case where
492-
* the coins were to be wiped out by FromTx anyway. This should not be called with
493-
* the 2 historical coinbase duplicate pairs because the new coins are marked fresh, and
494-
* in the event the duplicate coinbase was spent before a flush, the now pruned coins
495-
* would not properly overwrite the first coinbase of the pair. Simultaneous modifications
496-
* are not allowed.
497-
*/
498-
CCoinsModifier ModifyNewCoins(const uint256 &txid, bool coinbase);
499-
500455
/**
501456
* Add a coin. Set potential_overwrite to true if a non-pruned version may
502457
* already exist.
@@ -544,8 +499,6 @@ class CCoinsViewCache : public CCoinsViewBacked
544499

545500
const CTxOut &GetOutputFor(const CTxIn& input) const;
546501

547-
friend class CCoinsModifier;
548-
549502
private:
550503
CCoinsMap::iterator FetchCoins(const uint256 &txid) const;
551504

0 commit comments

Comments
 (0)