LCOV - code coverage report
Current view: top level - src - coins.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 185 203 91.1 %
Date: 2021-06-29 14:35:33 Functions: 27 36 75.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 74 78 94.9 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2012-2020 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 <version.h>
#      11                 :            : 
#      12                 :          4 : bool CCoinsView::GetCoin(const COutPoint &outpoint, Coin &coin) const { return false; }
#      13                 :          0 : uint256 CCoinsView::GetBestBlock() const { return uint256(); }
#      14                 :          0 : std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
#      15                 :          0 : bool CCoinsView::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return false; }
#      16                 :          0 : CCoinsViewCursor *CCoinsView::Cursor() const { return nullptr; }
#      17                 :            : 
#      18                 :            : bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
#      19                 :          0 : {
#      20                 :          0 :     Coin coin;
#      21                 :          0 :     return GetCoin(outpoint, coin);
#      22                 :          0 : }
#      23                 :            : 
#      24                 :     783405 : CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
#      25                 :     974578 : bool CCoinsViewBacked::GetCoin(const COutPoint &outpoint, Coin &coin) const { return base->GetCoin(outpoint, coin); }
#      26                 :          0 : bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
#      27                 :      27892 : uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
#      28                 :          0 : std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
#      29                 :      57672 : void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
#      30                 :       1581 : bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { return base->BatchWrite(mapCoins, hashBlock); }
#      31                 :          0 : CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
#      32                 :          0 : size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
#      33                 :            : 
#      34                 :     743592 : CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {}
#      35                 :            : 
#      36                 :     551892 : size_t CCoinsViewCache::DynamicMemoryUsage() const {
#      37                 :     551892 :     return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
#      38                 :     551892 : }
#      39                 :            : 
#      40                 :   91990479 : CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
#      41                 :   91990479 :     CCoinsMap::iterator it = cacheCoins.find(outpoint);
#      42         [ +  + ]:   91990479 :     if (it != cacheCoins.end())
#      43                 :   33284790 :         return it;
#      44                 :   58705689 :     Coin tmp;
#      45         [ +  + ]:   58705689 :     if (!base->GetCoin(outpoint, tmp))
#      46                 :   49614612 :         return cacheCoins.end();
#      47                 :    9091077 :     CCoinsMap::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
#      48         [ +  + ]:    9091077 :     if (ret->second.coin.IsSpent()) {
#      49                 :            :         // The parent only has an empty entry for this outpoint; we can consider our
#      50                 :            :         // version as fresh.
#      51                 :     326560 :         ret->second.flags = CCoinsCacheEntry::FRESH;
#      52                 :     326560 :     }
#      53                 :    9091077 :     cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
#      54                 :    9091077 :     return ret;
#      55                 :    9091077 : }
#      56                 :            : 
#      57                 :   39377267 : bool CCoinsViewCache::GetCoin(const COutPoint &outpoint, Coin &coin) const {
#      58                 :   39377267 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
#      59         [ +  + ]:   39377267 :     if (it != cacheCoins.end()) {
#      60                 :    9071189 :         coin = it->second.coin;
#      61                 :    9071189 :         return !coin.IsSpent();
#      62                 :    9071189 :     }
#      63                 :   30306078 :     return false;
#      64                 :   30306078 : }
#      65                 :            : 
#      66                 :   11246215 : void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
#      67                 :   11246215 :     assert(!coin.IsSpent());
#      68         [ +  + ]:   11246215 :     if (coin.out.scriptPubKey.IsUnspendable()) return;
#      69                 :   11022871 :     CCoinsMap::iterator it;
#      70                 :   11022871 :     bool inserted;
#      71                 :   11022871 :     std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
#      72                 :   11022871 :     bool fresh = false;
#      73         [ +  + ]:   11022871 :     if (!inserted) {
#      74                 :      24652 :         cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
#      75                 :      24652 :     }
#      76         [ +  + ]:   11022871 :     if (!possible_overwrite) {
#      77         [ +  + ]:   10822051 :         if (!it->second.coin.IsSpent()) {
#      78                 :         24 :             throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
#      79                 :         24 :         }
#      80                 :            :         // If the coin exists in this cache as a spent coin and is DIRTY, then
#      81                 :            :         // its spentness hasn't been flushed to the parent cache. We're
#      82                 :            :         // re-adding the coin to this cache now but we can't mark it as FRESH.
#      83                 :            :         // If we mark it FRESH and then spend it before the cache is flushed
#      84                 :            :         // we would remove it from this cache and would never flush spentness
#      85                 :            :         // to the parent cache.
#      86                 :            :         //
#      87                 :            :         // Re-adding a spent coin can happen in the case of a re-org (the coin
#      88                 :            :         // is 'spent' when the block adding it is disconnected and then
#      89                 :            :         // re-added when it is also added in a newly connected block).
#      90                 :            :         //
#      91                 :            :         // If the coin doesn't exist in the current cache, or is spent but not
#      92                 :            :         // DIRTY, then it can be marked FRESH.
#      93                 :   10822027 :         fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY);
#      94                 :   10822027 :     }
#      95                 :   11022871 :     it->second.coin = std::move(coin);
#      96         [ +  + ]:   11022847 :     it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
#      97                 :   11022847 :     cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
#      98                 :   11022847 : }
#      99                 :            : 
#     100                 :        438 : void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
#     101                 :        438 :     cachedCoinsUsage += coin.DynamicMemoryUsage();
#     102                 :        438 :     cacheCoins.emplace(
#     103                 :        438 :         std::piecewise_construct,
#     104                 :        438 :         std::forward_as_tuple(std::move(outpoint)),
#     105                 :        438 :         std::forward_as_tuple(std::move(coin), CCoinsCacheEntry::DIRTY));
#     106                 :        438 : }
#     107                 :            : 
#     108                 :    5468495 : void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
#     109                 :    5468495 :     bool fCoinbase = tx.IsCoinBase();
#     110                 :    5468495 :     const uint256& txid = tx.GetHash();
#     111         [ +  + ]:   16596624 :     for (size_t i = 0; i < tx.vout.size(); ++i) {
#     112         [ -  + ]:   11128129 :         bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
#     113                 :            :         // Coinbase transactions can always be overwritten, in order to correctly
#     114                 :            :         // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
#     115                 :   11128129 :         cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
#     116                 :   11128129 :     }
#     117                 :    5468495 : }
#     118                 :            : 
#     119                 :    7816863 : bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
#     120                 :    7816863 :     CCoinsMap::iterator it = FetchCoin(outpoint);
#     121         [ +  + ]:    7816863 :     if (it == cacheCoins.end()) return false;
#     122                 :    7816859 :     cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
#     123         [ +  + ]:    7816859 :     if (moveout) {
#     124                 :    7748695 :         *moveout = std::move(it->second.coin);
#     125                 :    7748695 :     }
#     126         [ +  + ]:    7816859 :     if (it->second.flags & CCoinsCacheEntry::FRESH) {
#     127                 :      87946 :         cacheCoins.erase(it);
#     128                 :    7728913 :     } else {
#     129                 :    7728913 :         it->second.flags |= CCoinsCacheEntry::DIRTY;
#     130                 :    7728913 :         it->second.coin.Clear();
#     131                 :    7728913 :     }
#     132                 :    7816859 :     return true;
#     133                 :    7816859 : }
#     134                 :            : 
#     135                 :            : static const Coin coinEmpty;
#     136                 :            : 
#     137                 :   26647170 : const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
#     138                 :   26647170 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
#     139         [ +  + ]:   26647170 :     if (it == cacheCoins.end()) {
#     140                 :   17258058 :         return coinEmpty;
#     141                 :   17258058 :     } else {
#     142                 :    9389112 :         return it->second.coin;
#     143                 :    9389112 :     }
#     144                 :   26647170 : }
#     145                 :            : 
#     146                 :   18149179 : bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
#     147                 :   18149179 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
#     148 [ +  + ][ +  + ]:   18149179 :     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
#     149                 :   18149179 : }
#     150                 :            : 
#     151                 :     499324 : bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
#     152                 :     499324 :     CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
#     153 [ +  + ][ +  + ]:     499324 :     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
#     154                 :     499324 : }
#     155                 :            : 
#     156                 :     474916 : uint256 CCoinsViewCache::GetBestBlock() const {
#     157         [ +  + ]:     474916 :     if (hashBlock.IsNull())
#     158                 :     220101 :         hashBlock = base->GetBestBlock();
#     159                 :     474916 :     return hashBlock;
#     160                 :     474916 : }
#     161                 :            : 
#     162                 :     915401 : void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
#     163                 :     915401 :     hashBlock = hashBlockIn;
#     164                 :     915401 : }
#     165                 :            : 
#     166                 :      74698 : bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn) {
#     167         [ +  + ]:    1313460 :     for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = mapCoins.erase(it)) {
#     168                 :            :         // Ignore non-dirty entries (optimization).
#     169         [ +  + ]:    1238778 :         if (!(it->second.flags & CCoinsCacheEntry::DIRTY)) {
#     170                 :     504806 :             continue;
#     171                 :     504806 :         }
#     172                 :     733972 :         CCoinsMap::iterator itUs = cacheCoins.find(it->first);
#     173         [ +  + ]:     733972 :         if (itUs == cacheCoins.end()) {
#     174                 :            :             // The parent cache does not have an entry, while the child cache does.
#     175                 :            :             // We can ignore it if it's both spent and FRESH in the child
#     176 [ +  + ][ +  + ]:     576320 :             if (!(it->second.flags & CCoinsCacheEntry::FRESH && it->second.coin.IsSpent())) {
#     177                 :            :                 // Create the coin in the parent cache, move the data up
#     178                 :            :                 // and mark it as dirty.
#     179                 :     576318 :                 CCoinsCacheEntry& entry = cacheCoins[it->first];
#     180                 :     576318 :                 entry.coin = std::move(it->second.coin);
#     181                 :     576318 :                 cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
#     182                 :     576318 :                 entry.flags = CCoinsCacheEntry::DIRTY;
#     183                 :            :                 // We can mark it FRESH in the parent if it was FRESH in the child
#     184                 :            :                 // Otherwise it might have just been flushed from the parent's cache
#     185                 :            :                 // and already exist in the grandparent
#     186         [ +  + ]:     576318 :                 if (it->second.flags & CCoinsCacheEntry::FRESH) {
#     187                 :     305916 :                     entry.flags |= CCoinsCacheEntry::FRESH;
#     188                 :     305916 :                 }
#     189                 :     576318 :             }
#     190                 :     576320 :         } else {
#     191                 :            :             // Found the entry in the parent cache
#     192 [ +  + ][ +  + ]:     157652 :             if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coin.IsSpent()) {
#     193                 :            :                 // The coin was marked FRESH in the child cache, but the coin
#     194                 :            :                 // exists in the parent cache. If this ever happens, it means
#     195                 :            :                 // the FRESH flag was misapplied and there is a logic error in
#     196                 :            :                 // the calling code.
#     197                 :         16 :                 throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
#     198                 :         16 :             }
#     199                 :            : 
#     200 [ +  + ][ +  + ]:     157636 :             if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coin.IsSpent()) {
#     201                 :            :                 // The grandparent cache does not have an entry, and the coin
#     202                 :            :                 // has been spent. We can just delete it from the parent cache.
#     203                 :      70416 :                 cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
#     204                 :      70416 :                 cacheCoins.erase(itUs);
#     205                 :      87220 :             } else {
#     206                 :            :                 // A normal modification.
#     207                 :      87220 :                 cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
#     208                 :      87220 :                 itUs->second.coin = std::move(it->second.coin);
#     209                 :      87220 :                 cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
#     210                 :      87220 :                 itUs->second.flags |= CCoinsCacheEntry::DIRTY;
#     211                 :            :                 // NOTE: It isn't safe to mark the coin as FRESH in the parent
#     212                 :            :                 // cache. If it already existed and was spent in the parent
#     213                 :            :                 // cache then marking it FRESH would prevent that spentness
#     214                 :            :                 // from being flushed to the grandparent.
#     215                 :      87220 :             }
#     216                 :     157636 :         }
#     217                 :     733972 :     }
#     218                 :      74698 :     hashBlock = hashBlockIn;
#     219                 :      74682 :     return true;
#     220                 :      74698 : }
#     221                 :            : 
#     222                 :      76567 : bool CCoinsViewCache::Flush() {
#     223                 :      76567 :     bool fOk = base->BatchWrite(cacheCoins, hashBlock);
#     224                 :      76567 :     cacheCoins.clear();
#     225                 :      76567 :     cachedCoinsUsage = 0;
#     226                 :      76567 :     return fOk;
#     227                 :      76567 : }
#     228                 :            : 
#     229                 :            : void CCoinsViewCache::Uncache(const COutPoint& hash)
#     230                 :      27763 : {
#     231                 :      27763 :     CCoinsMap::iterator it = cacheCoins.find(hash);
#     232 [ +  + ][ +  + ]:      27763 :     if (it != cacheCoins.end() && it->second.flags == 0) {
#                 [ +  + ]
#     233                 :       1985 :         cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
#     234                 :       1985 :         cacheCoins.erase(it);
#     235                 :       1985 :     }
#     236                 :      27763 : }
#     237                 :            : 
#     238                 :     312285 : unsigned int CCoinsViewCache::GetCacheSize() const {
#     239                 :     312285 :     return cacheCoins.size();
#     240                 :     312285 : }
#     241                 :            : 
#     242                 :            : bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
#     243                 :    5354831 : {
#     244         [ +  - ]:    5354831 :     if (!tx.IsCoinBase()) {
#     245         [ +  + ]:   13110592 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
#     246         [ +  + ]:    7760084 :             if (!HaveCoin(tx.vin[i].prevout)) {
#     247                 :       4323 :                 return false;
#     248                 :       4323 :             }
#     249                 :    7760084 :         }
#     250                 :    5354831 :     }
#     251                 :    5354831 :     return true;
#     252                 :    5354831 : }
#     253                 :            : 
#     254                 :            : void CCoinsViewCache::ReallocateCache()
#     255                 :         13 : {
#     256                 :            :     // Cache should be empty when we're calling this.
#     257                 :         13 :     assert(cacheCoins.size() == 0);
#     258                 :         13 :     cacheCoins.~CCoinsMap();
#     259                 :         13 :     ::new (&cacheCoins) CCoinsMap();
#     260                 :         13 : }
#     261                 :            : 
#     262                 :            : static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut(), PROTOCOL_VERSION);
#     263                 :            : static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT;
#     264                 :            : 
#     265                 :            : const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
#     266                 :        324 : {
#     267                 :        324 :     COutPoint iter(txid, 0);
#     268         [ +  + ]:   15666975 :     while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
#     269                 :   15666834 :         const Coin& alternate = view.AccessCoin(iter);
#     270         [ +  + ]:   15666834 :         if (!alternate.IsSpent()) return alternate;
#     271                 :   15666651 :         ++iter.n;
#     272                 :   15666651 :     }
#     273                 :        324 :     return coinEmpty;
#     274                 :        324 : }
#     275                 :            : 
#     276                 :     974578 : bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) const {
#     277                 :     974578 :     try {
#     278                 :     974578 :         return CCoinsViewBacked::GetCoin(outpoint, coin);
#     279                 :     974578 :     } catch(const std::runtime_error& e) {
#     280         [ #  # ]:          0 :         for (auto f : m_err_callbacks) {
#     281                 :          0 :             f();
#     282                 :          0 :         }
#     283                 :          0 :         LogPrintf("Error reading from database: %s\n", e.what());
#     284                 :            :         // Starting the shutdown sequence and returning false to the caller would be
#     285                 :            :         // interpreted as 'entry not found' (as opposed to unable to read data), and
#     286                 :            :         // could lead to invalid interpretation. Just exit immediately, as we can't
#     287                 :            :         // continue anyway, and all writes should be atomic.
#     288                 :          0 :         std::abort();
#     289                 :          0 :     }
#     290                 :     974578 : }

Generated by: LCOV version 1.14