| 136 | } |
| 137 | |
| 138 | CCoinsModifier CCoinsViewCache::ModifyCoins(const uint256& txid) |
| 139 | { |
| 140 | assert(!hasModifier); |
| 141 | std::pair<CCoinsMap::iterator, bool> ret = cacheCoins.insert(std::make_pair(txid, CCoinsCacheEntry())); |
| 142 | size_t cachedCoinUsage = 0; |
| 143 | if (ret.second) { |
| 144 | if (!base->GetCoins(txid, ret.first->second.coins)) { |
| 145 | // The parent view does not have this entry; mark it as fresh. |
| 146 | ret.first->second.coins.Clear(); |
| 147 | // New coins must not already exist. |
| 148 | if (!ret.first->second.coins.IsPruned()) |
| 149 | throw std::logic_error("ModifyCoins should not find pre-existing coins on a non-coinbase unless they are pruned!"); |
| 150 | |
| 151 | if (!(ret.first->second.flags & CCoinsCacheEntry::FRESH)) { |
| 152 | // If the coin is known to be pruned (have no unspent outputs) in |
| 153 | // the current view and the cache entry is not dirty, we know the |
| 154 | // coin also must be pruned in the parent view as well, so it is safe |
| 155 | // to mark this fresh. |
| 156 | ret.first->second.flags |= CCoinsCacheEntry::FRESH; |
| 157 | } |
| 158 | } |
| 159 | } else { |
| 160 | cachedCoinUsage = ret.first->second.coins.DynamicMemoryUsage(); |
| 161 | } |
| 162 | // Assume that whenever ModifyCoins is called, the entry will be modified. |
| 163 | ret.first->second.flags |= CCoinsCacheEntry::DIRTY; |
| 164 | return CCoinsModifier(*this, ret.first, cachedCoinUsage); |
| 165 | } |
| 166 | |
| 167 | CCoinsModifier CCoinsViewCache::ModifyNewCoins(const uint256 &txid, bool coinbase) { |
| 168 | assert(!hasModifier); |