Branch data Line data Source code
# 1 : : // Copyright (c) 2012-2021 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <coins.h>
# 6 : :
# 7 : : #include <consensus/consensus.h>
# 8 : : #include <logging.h>
# 9 : : #include <random.h>
# 10 : : #include <util/trace.h>
# 11 : : #include <version.h>
# 12 : :
# 13 : 4 : bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
# 14 : 0 : uint256 CCoinsView::GetBestBlock() const { return uint256(); }
# 15 : 0 : std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
# 16 : 0 : bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
# 17 : 0 : std::unique_ptr<CCoinsViewCursor> CCoinsView::Cursor() const { return nullptr; }
# 18 : :
# 19 : : bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
# 20 : 0 : {
# 21 : 0 : Coin coin;
# 22 : 0 : return GetCoin(outpoint, coin);
# 23 : 0 : }
# 24 : :
# 25 : 562702 : CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
# 26 : 928745 : bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { return base->GetCoin(outpoint, coin); }
# 27 : 0 : bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
# 28 : 34214 : uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
# 29 : 0 : std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
# 30 : 69448 : void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
# 31 : 1616 : bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
# 32 : 0 : std::unique_ptr<CCoinsViewCursor> CCoinsViewBacked::Cursor() const { return base->Cursor(); }
# 33 : 0 : size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
# 34 : :
# 35 : 516327 : CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {}
# 36 : :
# 37 : 523646 : size_t CCoinsViewCache::DynamicMemoryUsage() const {
# 38 : 523646 : return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
# 39 : 523646 : }
# 40 : :
# 41 : 131201227 : CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
# 42 : 131201227 : CCoinsMap::iterator it = cacheCoins.find(outpoint);
# 43 [ + + ]: 131201227 : if (it != cacheCoins.end())
# 44 : 59109800 : return it;
# 45 : 72091427 : Coin tmp;
# 46 [ + + ]: 72091427 : if (!base->GetCoin(outpoint, tmp))
# 47 : 56684197 : return cacheCoins.end();
# 48 : 15407230 : CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
# 49 [ + + ]: 15407230 : if (ret->second.coin.IsSpent()) {
# 50 : : // The parent only has an empty entry for this outpoint; we can consider our
# 51 : : // version as fresh.
# 52 : 307024 : ret->second.flags = CCoinsCacheEntry::FRESH;
# 53 : 307024 : }
# 54 : 15407230 : cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
# 55 : 15407230 : return ret;
# 56 : 72091427 : }
# 57 : :
# 58 : 50215754 : bool CCoinsViewCache::GetCoin(const COutPoint &outpoint, Coin &coin) const {
# 59 : 50215754 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
# 60 [ + + ]: 50215754 : if (it != cacheCoins.end()) {
# 61 : 15434662 : coin = it->second.coin;
# 62 : 15434662 : return !coin.IsSpent();
# 63 : 15434662 : }
# 64 : 34781092 : return false;
# 65 : 50215754 : }
# 66 : :
# 67 : 24947753 : void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
# 68 : 24947753 : assert(!coin.IsSpent());
# 69 [ + + ]: 24947753 : if (coin.out.scriptPubKey.IsUnspendable()) return;
# 70 : 22749059 : CCoinsMap::iterator it;
# 71 : 22749059 : bool inserted;
# 72 : 22749059 : std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
# 73 : 22749059 : bool fresh = false;
# 74 [ + + ]: 22749059 : if (!inserted) {
# 75 : 24079 : cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
# 76 : 24079 : }
# 77 [ + + ]: 22749059 : if (!possible_overwrite) {
# 78 [ + + ]: 22554715 : if (!it->second.coin.IsSpent()) {
# 79 : 24 : throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
# 80 : 24 : }
# 81 : : // If the coin exists in this cache as a spent coin and is DIRTY, then
# 82 : : // its spentness hasn't been flushed to the parent cache. We're
# 83 : : // re-adding the coin to this cache now but we can't mark it as FRESH.
# 84 : : // If we mark it FRESH and then spend it before the cache is flushed
# 85 : : // we would remove it from this cache and would never flush spentness
# 86 : : // to the parent cache.
# 87 : : //
# 88 : : // Re-adding a spent coin can happen in the case of a re-org (the coin
# 89 : : // is 'spent' when the block adding it is disconnected and then
# 90 : : // re-added when it is also added in a newly connected block).
# 91 : : //
# 92 : : // If the coin doesn't exist in the current cache, or is spent but not
# 93 : : // DIRTY, then it can be marked FRESH.
# 94 : 22554691 : fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY);
# 95 : 22554691 : }
# 96 : 22749035 : it->second.coin = std::move(coin);
# 97 [ + + ]: 22749035 : it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
# 98 : 22749035 : cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
# 99 : 22749035 : TRACE5(utxocache, add,
# 100 : 22749035 : outpoint.hash.data(),
# 101 : 22749035 : (uint32_t)outpoint.n,
# 102 : 22749035 : (uint32_t)coin.nHeight,
# 103 : 22749035 : (int64_t)coin.out.nValue,
# 104 : 22749035 : (bool)coin.IsCoinBase());
# 105 : 22749035 : }
# 106 : :
# 107 : 548 : void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
# 108 : 548 : cachedCoinsUsage += coin.DynamicMemoryUsage();
# 109 : 548 : cacheCoins.emplace(
# 110 : 548 : std::piecewise_construct,
# 111 : 548 : std::forward_as_tuple(std::move(outpoint)),
# 112 : 548 : std::forward_as_tuple(std::move(coin), CCoinsCacheEntry::DIRTY));
# 113 : 548 : }
# 114 : :
# 115 : 11240107 : void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
# 116 : 11240107 : bool fCoinbase = tx.IsCoinBase();
# 117 : 11240107 : const uint256& txid = tx.GetHash();
# 118 [ + + ]: 36071151 : for (size_t i = 0; i < tx.vout.size(); ++i) {
# 119 [ - + ]: 24831044 : bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
# 120 : : // Coinbase transactions can always be overwritten, in order to correctly
# 121 : : // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
# 122 : 24831044 : cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
# 123 : 24831044 : }
# 124 : 11240107 : }
# 125 : :
# 126 : 14279399 : bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
# 127 : 14279399 : CCoinsMap::iterator it = FetchCoin(outpoint);
# 128 [ + + ]: 14279399 : if (it == cacheCoins.end()) return false;
# 129 : 14279395 : cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
# 130 : 14279395 : TRACE5(utxocache, spent,
# 131 : 14279395 : outpoint.hash.data(),
# 132 : 14279395 : (uint32_t)outpoint.n,
# 133 : 14279395 : (uint32_t)it->second.coin.nHeight,
# 134 : 14279395 : (int64_t)it->second.coin.out.nValue,
# 135 : 14279395 : (bool)it->second.coin.IsCoinBase());
# 136 [ + + ]: 14279395 : if (moveout) {
# 137 : 182449 : *moveout = std::move(it->second.coin);
# 138 : 182449 : }
# 139 [ + + ]: 14279395 : if (it->second.flags & CCoinsCacheEntry::FRESH) {
# 140 : 208631 : cacheCoins.erase(it);
# 141 : 14070764 : } else {
# 142 : 14070764 : it->second.flags |= CCoinsCacheEntry::DIRTY;
# 143 : 14070764 : it->second.coin.Clear();
# 144 : 14070764 : }
# 145 : 14279395 : return true;
# 146 : 14279399 : }
# 147 : :
# 148 : : static const Coin coinEmpty;
# 149 : :
# 150 : 35703156 : const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
# 151 : 35703156 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
# 152 [ + + ]: 35703156 : if (it == cacheCoins.end()) {
# 153 : 19911088 : return coinEmpty;
# 154 : 19911088 : } else {
# 155 : 15792068 : return it->second.coin;
# 156 : 15792068 : }
# 157 : 35703156 : }
# 158 : :
# 159 : 31002918 : bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
# 160 : 31002918 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
# 161 [ + + ][ + + ]: 31002918 : return (it != cacheCoins.end() && !it->second.coin.IsSpent());
# 162 : 31002918 : }
# 163 : :
# 164 : 472951 : bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
# 165 : 472951 : CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
# 166 [ + + ][ + + ]: 472951 : return (it != cacheCoins.end() && !it->second.coin.IsSpent());
# 167 : 472951 : }
# 168 : :
# 169 : 306611 : uint256 CCoinsViewCache::GetBestBlock() const {
# 170 [ + + ]: 306611 : if (hashBlock.IsNull())
# 171 : 132646 : hashBlock = base->GetBestBlock();
# 172 : 306611 : return hashBlock;
# 173 : 306611 : }
# 174 : :
# 175 : 909093 : void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
# 176 : 909093 : hashBlock = hashBlockIn;
# 177 : 909093 : }
# 178 : :
# 179 : 67709 : bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
# 180 [ + + ]: 1314844 : for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = mapCoins.erase(it)) {
# 181 : : // Ignore non-dirty entries (optimization).
# 182 [ + + ]: 1247151 : if (!(it->second.flags & CCoinsCacheEntry::DIRTY)) {
# 183 : 519536 : continue;
# 184 : 519536 : }
# 185 : 727615 : CCoinsMap::iterator itUs = cacheCoins.find(it->first);
# 186 [ + + ]: 727615 : if (itUs == cacheCoins.end()) {
# 187 : : // The parent cache does not have an entry, while the child cache does.
# 188 : : // We can ignore it if it's both spent and FRESH in the child
# 189 [ + + ][ + + ]: 580085 : if (!(it->second.flags & CCoinsCacheEntry::FRESH && it->second.coin.IsSpent())) {
# 190 : : // Create the coin in the parent cache, move the data up
# 191 : : // and mark it as dirty.
# 192 : 580083 : CCoinsCacheEntry& entry = cacheCoins[it->first];
# 193 : 580083 : entry.coin = std::move(it->second.coin);
# 194 : 580083 : cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
# 195 : 580083 : entry.flags = CCoinsCacheEntry::DIRTY;
# 196 : : // We can mark it FRESH in the parent if it was FRESH in the child
# 197 : : // Otherwise it might have just been flushed from the parent's cache
# 198 : : // and already exist in the grandparent
# 199 [ + + ]: 580083 : if (it->second.flags & CCoinsCacheEntry::FRESH) {
# 200 : 288659 : entry.flags |= CCoinsCacheEntry::FRESH;
# 201 : 288659 : }
# 202 : 580083 : }
# 203 : 580085 : } else {
# 204 : : // Found the entry in the parent cache
# 205 [ + + ][ + + ]: 147530 : if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coin.IsSpent()) {
# 206 : : // The coin was marked FRESH in the child cache, but the coin
# 207 : : // exists in the parent cache. If this ever happens, it means
# 208 : : // the FRESH flag was misapplied and there is a logic error in
# 209 : : // the calling code.
# 210 : 16 : throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
# 211 : 16 : }
# 212 : :
# 213 [ + + ][ + + ]: 147514 : if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coin.IsSpent()) {
# 214 : : // The grandparent cache does not have an entry, and the coin
# 215 : : // has been spent. We can just delete it from the parent cache.
# 216 : 51124 : cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
# 217 : 51124 : cacheCoins.erase(itUs);
# 218 : 96390 : } else {
# 219 : : // A normal modification.
# 220 : 96390 : cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
# 221 : 96390 : itUs->second.coin = std::move(it->second.coin);
# 222 : 96390 : cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
# 223 : 96390 : itUs->second.flags |= CCoinsCacheEntry::DIRTY;
# 224 : : // NOTE: It isn't safe to mark the coin as FRESH in the parent
# 225 : : // cache. If it already existed and was spent in the parent
# 226 : : // cache then marking it FRESH would prevent that spentness
# 227 : : // from being flushed to the grandparent.
# 228 : 96390 : }
# 229 : 147514 : }
# 230 : 727615 : }
# 231 : 67693 : hashBlock = hashBlockIn;
# 232 : 67693 : return true;
# 233 : 67709 : }
# 234 : :
# 235 : 69559 : bool CCoinsViewCache::Flush() {
# 236 : 69559 : bool fOk = base->BatchWrite(cacheCoins, hashBlock);
# 237 : 69559 : cacheCoins.clear();
# 238 : 69559 : cachedCoinsUsage = 0;
# 239 : 69559 : return fOk;
# 240 : 69559 : }
# 241 : :
# 242 : : void CCoinsViewCache::Uncache(const COutPoint& hash)
# 243 : 28568 : {
# 244 : 28568 : CCoinsMap::iterator it = cacheCoins.find(hash);
# 245 [ + + ][ + + ]: 28568 : if (it != cacheCoins.end() && it->second.flags == 0) {
# [ + + ]
# 246 : 2140 : cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
# 247 : 2140 : TRACE5(utxocache, uncache,
# 248 : 2140 : hash.hash.data(),
# 249 : 2140 : (uint32_t)hash.n,
# 250 : 2140 : (uint32_t)it->second.coin.nHeight,
# 251 : 2140 : (int64_t)it->second.coin.out.nValue,
# 252 : 2140 : (bool)it->second.coin.IsCoinBase());
# 253 : 2140 : cacheCoins.erase(it);
# 254 : 2140 : }
# 255 : 28568 : }
# 256 : :
# 257 : 293951 : unsigned int CCoinsViewCache::GetCacheSize() const {
# 258 : 293951 : return cacheCoins.size();
# 259 : 293951 : }
# 260 : :
# 261 : : bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
# 262 : 11107126 : {
# 263 [ + - ]: 11107126 : if (!tx.IsCoinBase()) {
# 264 [ + + ]: 25301622 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
# 265 [ + + ]: 14194519 : if (!HaveCoin(tx.vin[i].prevout)) {
# 266 : 23 : return false;
# 267 : 23 : }
# 268 : 14194519 : }
# 269 : 11107126 : }
# 270 : 11107103 : return true;
# 271 : 11107126 : }
# 272 : :
# 273 : : void CCoinsViewCache::ReallocateCache()
# 274 : 14 : {
# 275 : : // Cache should be empty when we're calling this.
# 276 : 14 : assert(cacheCoins.size() == 0);
# 277 : 0 : cacheCoins.~CCoinsMap();
# 278 : 14 : ::new (&cacheCoins) CCoinsMap();
# 279 : 14 : }
# 280 : :
# 281 : : static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut(), PROTOCOL_VERSION);
# 282 : : static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT;
# 283 : :
# 284 : : const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
# 285 : 360 : {
# 286 : 360 : COutPoint iter(txid, 0);
# 287 [ + + ]: 18333675 : while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
# 288 : 18333510 : const Coin& alternate = view.AccessCoin(iter);
# 289 [ + + ]: 18333510 : if (!alternate.IsSpent()) return alternate;
# 290 : 18333315 : ++iter.n;
# 291 : 18333315 : }
# 292 : 165 : return coinEmpty;
# 293 : 360 : }
# 294 : :
# 295 : 928745 : bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) const {
# 296 : 928745 : try {
# 297 : 928745 : return CCoinsViewBacked::GetCoin(outpoint, coin);
# 298 : 928745 : } catch(const std::runtime_error& e) {
# 299 [ # # ]: 0 : for (auto f : m_err_callbacks) {
# 300 : 0 : f();
# 301 : 0 : }
# 302 : 0 : LogPrintf("Error reading from database: %s\n", e.what());
# 303 : : // Starting the shutdown sequence and returning false to the caller would be
# 304 : : // interpreted as 'entry not found' (as opposed to unable to read data), and
# 305 : : // could lead to invalid interpretation. Just exit immediately, as we can't
# 306 : : // continue anyway, and all writes should be atomic.
# 307 : 0 : std::abort();
# 308 : 0 : }
# 309 : 928745 : }
|