| 77 | } |
| 78 | |
| 79 | void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) { |
| 80 | assert(!coin.IsSpent()); |
| 81 | if (coin.out.scriptPubKey.IsUnspendable()) return; |
| 82 | CCoinsMap::iterator it; |
| 83 | bool inserted; |
| 84 | std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, |
| 85 | std::forward_as_tuple(native_key(outpoint)), std::tuple<>()); |
| 86 | bool fresh = false; |
| 87 | if (!inserted) { |
| 88 | cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage(); |
| 89 | } |
| 90 | if (!possible_overwrite) { |
| 91 | if (!it->second.coin.IsSpent()) { |
| 92 | throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)"); |
| 93 | } |
| 94 | // If the coin exists in this cache as a spent coin and is DIRTY, then |
| 95 | // its spentness hasn't been flushed to the parent cache. We're |
| 96 | // re-adding the coin to this cache now but we can't mark it as FRESH. |
| 97 | // If we mark it FRESH and then spend it before the cache is flushed |
| 98 | // we would remove it from this cache and would never flush spentness |
| 99 | // to the parent cache. |
| 100 | // |
| 101 | // Re-adding a spent coin can happen in the case of a re-org (the coin |
| 102 | // is 'spent' when the block adding it is disconnected and then |
| 103 | // re-added when it is also added in a newly connected block). |
| 104 | // |
| 105 | // If the coin doesn't exist in the current cache, or is spent but not |
| 106 | // DIRTY, then it can be marked FRESH. |
| 107 | fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY); |
| 108 | } |
| 109 | it->second.coin = std::move(coin); |
| 110 | it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0); |
| 111 | cachedCoinsUsage += it->second.coin.DynamicMemoryUsage(); |
| 112 | TRACE5(utxocache, add, |
| 113 | outpoint.hash.data(), |
| 114 | (uint32_t)outpoint.n, |
| 115 | (uint32_t)coin.nHeight, |
| 116 | coin.out.nValue.IsExplicit() ? (int64_t)coin.out.nValue.GetAmount() : 0, |
| 117 | (bool)coin.IsCoinBase()); |
| 118 | } |
| 119 | |
| 120 | void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) { |
| 121 | cachedCoinsUsage += coin.DynamicMemoryUsage(); |