LCOV - code coverage report
Current view: top level - src - coins.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 96.6 % 232 224
Test Date: 2026-07-11 06:48:01 Functions: 91.9 % 37 34
Branches: 79.0 % 252 199

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-present 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 <primitives/block.h>
       9                 :             : #include <random.h>
      10                 :             : #include <uint256.h>
      11                 :             : #include <util/log.h>
      12                 :             : #include <util/threadpool.h>
      13                 :             : #include <util/trace.h>
      14                 :             : 
      15                 :             : #include <ranges>
      16                 :             : #include <unordered_set>
      17                 :             : 
      18                 :             : TRACEPOINT_SEMAPHORE(utxocache, add);
      19                 :             : TRACEPOINT_SEMAPHORE(utxocache, spent);
      20                 :             : TRACEPOINT_SEMAPHORE(utxocache, uncache);
      21                 :             : 
      22                 :         588 : CoinsViewEmpty& CoinsViewEmpty::Get()
      23                 :             : {
      24   [ +  +  +  - ]:         588 :     static CoinsViewEmpty instance;
      25                 :         588 :     return instance;
      26                 :             : }
      27                 :             : 
      28                 :       19847 : std::optional<Coin> CCoinsViewCache::PeekCoin(const COutPoint& outpoint) const
      29                 :             : {
      30         [ +  + ]:       19847 :     if (auto it{cacheCoins.find(outpoint)}; it != cacheCoins.end()) {
      31         [ +  + ]:        1166 :         return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin};
      32                 :             :     }
      33                 :       18681 :     return base->PeekCoin(outpoint);
      34                 :             : }
      35                 :             : 
      36                 :      158730 : CCoinsViewCache::CCoinsViewCache(CCoinsView* in_base, bool deterministic) :
      37                 :      158730 :     CCoinsViewBacked(in_base), m_deterministic(deterministic),
      38   [ +  -  +  - ]:      158730 :     cacheCoins(0, SaltedOutpointHasher(/*deterministic=*/deterministic), CCoinsMap::key_equal{}, &m_cache_coins_memory_resource)
      39                 :             : {
      40                 :      158730 :     m_sentinel.second.SelfRef(m_sentinel);
      41                 :      158730 : }
      42                 :             : 
      43                 :      177615 : size_t CCoinsViewCache::DynamicMemoryUsage() const {
      44                 :      177615 :     return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
      45                 :             : }
      46                 :             : 
      47                 :    22937512 : std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const
      48                 :             : {
      49                 :    22937512 :     return base->GetCoin(outpoint);
      50                 :             : }
      51                 :             : 
      52                 :    23515104 : CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
      53         [ +  + ]:    23515104 :     const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
      54         [ +  + ]:    23515104 :     if (inserted) {
      55         [ +  + ]:    22957261 :         if (auto coin{FetchCoinFromBase(outpoint)}) {
      56                 :      498115 :             ret->second.coin = std::move(*coin);
      57         [ +  + ]:      498115 :             cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
      58         [ -  + ]:      498115 :             Assert(!ret->second.coin.IsSpent());
      59                 :             :         } else {
      60                 :    22459146 :             cacheCoins.erase(ret);
      61                 :    22459146 :             return cacheCoins.end();
      62                 :    22957261 :         }
      63                 :             :     }
      64                 :     1055958 :     return ret;
      65                 :             : }
      66                 :             : 
      67                 :    13125688 : std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const
      68                 :             : {
      69   [ +  +  +  + ]:    13125688 :     if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin;
      70                 :    12825833 :     return std::nullopt;
      71                 :             : }
      72                 :             : 
      73                 :      164834 : void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
      74         [ -  + ]:      164834 :     assert(!coin.IsSpent());
      75         [ +  + ]:      164834 :     if (coin.out.scriptPubKey.IsUnspendable()) return;
      76                 :      145360 :     CCoinsMap::iterator it;
      77                 :      145360 :     bool inserted;
      78         [ +  + ]:      145360 :     std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
      79                 :      145360 :     bool fresh = false;
      80         [ +  + ]:      145360 :     if (!possible_overwrite) {
      81         [ +  + ]:       95965 :         if (!it->second.coin.IsSpent()) {
      82         [ +  - ]:          17 :             throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
      83                 :             :         }
      84                 :             :         // If the coin exists in this cache as a spent coin and is DIRTY, then
      85                 :             :         // its spentness hasn't been flushed to the parent cache. We're
      86                 :             :         // re-adding the coin to this cache now but we can't mark it as FRESH.
      87                 :             :         // If we mark it FRESH and then spend it before the cache is flushed
      88                 :             :         // we would remove it from this cache and would never flush spentness
      89                 :             :         // to the parent cache.
      90                 :             :         //
      91                 :             :         // Re-adding a spent coin can happen in the case of a re-org (the coin
      92                 :             :         // is 'spent' when the block adding it is disconnected and then
      93                 :             :         // re-added when it is also added in a newly connected block).
      94                 :             :         //
      95                 :             :         // If the coin doesn't exist in the current cache, or is spent but not
      96                 :             :         // DIRTY, then it can be marked FRESH.
      97                 :       95948 :         fresh = !it->second.IsDirty();
      98                 :             :     }
      99         [ +  + ]:      145343 :     if (!inserted) {
     100   [ +  -  +  + ]:       20948 :         Assume(TrySub(m_dirty_count, it->second.IsDirty()));
     101   [ +  +  +  - ]:       24265 :         Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
     102                 :             :     }
     103                 :      145343 :     it->second.coin = std::move(coin);
     104                 :      145343 :     CCoinsCacheEntry::SetDirty(*it, m_sentinel);
     105                 :      145343 :     ++m_dirty_count;
     106         [ +  + ]:      145343 :     if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel);
     107         [ +  + ]:      231999 :     cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
     108                 :             :     TRACEPOINT(utxocache, add,
     109                 :             :            outpoint.hash.data(),
     110                 :             :            (uint32_t)outpoint.n,
     111                 :             :            (uint32_t)it->second.coin.nHeight,
     112                 :             :            (int64_t)it->second.coin.out.nValue,
     113                 :      164817 :            (bool)it->second.coin.IsCoinBase());
     114                 :             : }
     115                 :             : 
     116                 :       20719 : void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
     117         [ +  + ]:       20719 :     const auto mem_usage{coin.DynamicMemoryUsage()};
     118         [ +  + ]:       20719 :     auto [it, inserted] = cacheCoins.try_emplace(std::move(outpoint), std::move(coin));
     119         [ +  + ]:       20719 :     if (inserted) {
     120                 :       20718 :         CCoinsCacheEntry::SetDirty(*it, m_sentinel);
     121                 :       20718 :         ++m_dirty_count;
     122                 :       20718 :         cachedCoinsUsage += mem_usage;
     123                 :             :     }
     124                 :       20719 : }
     125                 :             : 
     126                 :       55910 : void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
     127                 :       55910 :     bool fCoinbase = tx.IsCoinBase();
     128                 :       55910 :     const Txid& txid = tx.GetHash();
     129   [ -  +  +  + ]:      131020 :     for (size_t i = 0; i < tx.vout.size(); ++i) {
     130         [ -  + ]:       75110 :         bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
     131                 :             :         // Coinbase transactions can always be overwritten, in order to correctly
     132                 :             :         // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
     133         [ +  - ]:      150220 :         cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
     134                 :             :     }
     135                 :       55910 : }
     136                 :             : 
     137                 :       69454 : bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
     138                 :       69454 :     CCoinsMap::iterator it = FetchCoin(outpoint);
     139         [ +  + ]:       69454 :     if (it == cacheCoins.end()) return false;
     140   [ +  -  +  + ]:      138896 :     Assume(TrySub(m_dirty_count, it->second.IsDirty()));
     141   [ +  +  +  -  :      167650 :     Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
                   +  + ]
     142                 :             :     TRACEPOINT(utxocache, spent,
     143                 :             :            outpoint.hash.data(),
     144                 :             :            (uint32_t)outpoint.n,
     145                 :             :            (uint32_t)it->second.coin.nHeight,
     146                 :             :            (int64_t)it->second.coin.out.nValue,
     147                 :       69448 :            (bool)it->second.coin.IsCoinBase());
     148         [ +  + ]:       69448 :     if (moveout) {
     149                 :       34598 :         *moveout = std::move(it->second.coin);
     150                 :             :     }
     151         [ +  + ]:       69448 :     if (it->second.IsFresh()) {
     152                 :        8036 :         cacheCoins.erase(it);
     153                 :             :     } else {
     154                 :       61412 :         CCoinsCacheEntry::SetDirty(*it, m_sentinel);
     155                 :       61412 :         ++m_dirty_count;
     156                 :       61412 :         it->second.coin.Clear();
     157                 :             :     }
     158                 :             :     return true;
     159                 :             : }
     160                 :             : 
     161                 :             : static const Coin coinEmpty;
     162                 :             : 
     163                 :     9273048 : const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
     164         [ +  + ]:     9273048 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
     165         [ +  + ]:     9273048 :     if (it == cacheCoins.end()) {
     166                 :             :         return coinEmpty;
     167                 :             :     } else {
     168                 :      331939 :         return it->second.coin;
     169                 :             :     }
     170                 :             : }
     171                 :             : 
     172                 :     1046914 : bool CCoinsViewCache::HaveCoin(const COutPoint& outpoint) const
     173                 :             : {
     174         [ +  + ]:     1046914 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
     175   [ +  +  +  + ]:     1046914 :     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
     176                 :             : }
     177                 :             : 
     178                 :      212804 : bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
     179         [ +  + ]:      212804 :     CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
     180   [ +  +  +  + ]:      212804 :     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
     181                 :             : }
     182                 :             : 
     183                 :       36061 : uint256 CCoinsViewCache::GetBestBlock() const {
     184         [ +  + ]:       72122 :     if (m_block_hash.IsNull())
     185                 :       18169 :         m_block_hash = base->GetBestBlock();
     186                 :       36061 :     return m_block_hash;
     187                 :             : }
     188                 :             : 
     189                 :      448368 : void CCoinsViewCache::SetBestBlock(const uint256& in_block_hash)
     190                 :             : {
     191                 :      448368 :     m_block_hash = in_block_hash;
     192                 :      448368 : }
     193                 :             : 
     194                 :       10112 : void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& in_block_hash)
     195                 :             : {
     196         [ +  + ]:      191743 :     for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
     197         [ +  + ]:      181639 :         if (!it->second.IsDirty()) { // TODO a cursor can only contain dirty entries
     198                 :          18 :             continue;
     199                 :             :         }
     200         [ +  + ]:      181621 :         auto [itUs, inserted]{cacheCoins.try_emplace(it->first)};
     201         [ +  + ]:      181621 :         if (inserted) {
     202   [ +  +  +  + ]:      137737 :             if (it->second.IsFresh() && it->second.coin.IsSpent()) {
     203                 :           1 :                 cacheCoins.erase(itUs); // TODO fresh coins should have been removed at spend
     204                 :             :             } else {
     205                 :             :                 // The parent cache does not have an entry, while the child cache does.
     206                 :             :                 // Move the data up and mark it as dirty.
     207         [ -  + ]:      137736 :                 CCoinsCacheEntry& entry{itUs->second};
     208   [ -  +  -  - ]:      137736 :                 assert(entry.coin.DynamicMemoryUsage() == 0);
     209         [ +  + ]:      137736 :                 if (cursor.WillErase(*it)) {
     210                 :             :                     // Since this entry will be erased,
     211                 :             :                     // we can move the coin into us instead of copying it
     212                 :      126408 :                     entry.coin = std::move(it->second.coin);
     213                 :             :                 } else {
     214                 :       11328 :                     entry.coin = it->second.coin;
     215                 :             :                 }
     216                 :      137736 :                 CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
     217                 :      137736 :                 ++m_dirty_count;
     218         [ +  + ]:      137736 :                 cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
     219                 :             :                 // We can mark it FRESH in the parent if it was FRESH in the child
     220                 :             :                 // Otherwise it might have just been flushed from the parent's cache
     221                 :             :                 // and already exist in the grandparent
     222         [ +  + ]:      137736 :                 if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel);
     223                 :             :             }
     224                 :             :         } else {
     225                 :             :             // Found the entry in the parent cache
     226   [ +  +  +  + ]:       43884 :             if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) {
     227                 :             :                 // The coin was marked FRESH in the child cache, but the coin
     228                 :             :                 // exists in the parent cache. If this ever happens, it means
     229                 :             :                 // the FRESH flag was misapplied and there is a logic error in
     230                 :             :                 // the calling code.
     231         [ +  - ]:           8 :                 throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
     232                 :             :             }
     233                 :             : 
     234   [ +  +  +  + ]:       43876 :             if (itUs->second.IsFresh() && it->second.coin.IsSpent()) {
     235                 :             :                 // The grandparent cache does not have an entry, and the coin
     236                 :             :                 // has been spent. We can just delete it from the parent cache.
     237   [ +  -  +  + ]:        7150 :                 Assume(TrySub(m_dirty_count, itUs->second.IsDirty()));
     238   [ +  +  +  - ]:        8672 :                 Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage()));
     239                 :        3575 :                 cacheCoins.erase(itUs);
     240                 :             :             } else {
     241                 :             :                 // A normal modification.
     242   [ +  +  +  -  :       96712 :                 Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage()));
                   +  + ]
     243         [ +  + ]:       40301 :                 if (cursor.WillErase(*it)) {
     244                 :             :                     // Since this entry will be erased,
     245                 :             :                     // we can move the coin into us instead of copying it
     246                 :       39129 :                     itUs->second.coin = std::move(it->second.coin);
     247                 :             :                 } else {
     248                 :        1172 :                     itUs->second.coin = it->second.coin;
     249                 :             :                 }
     250         [ +  + ]:       40301 :                 cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
     251         [ +  + ]:       40301 :                 if (!itUs->second.IsDirty()) {
     252                 :       35888 :                     CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
     253                 :       35888 :                     ++m_dirty_count;
     254                 :             :                 }
     255                 :             :                 // NOTE: It isn't safe to mark the coin as FRESH in the parent
     256                 :             :                 // cache. If it already existed and was spent in the parent
     257                 :             :                 // cache then marking it FRESH would prevent that spentness
     258                 :             :                 // from being flushed to the grandparent.
     259                 :             :             }
     260                 :             :         }
     261                 :             :     }
     262                 :       10104 :     SetBestBlock(in_block_hash);
     263                 :       10104 : }
     264                 :             : 
     265                 :       10151 : void CCoinsViewCache::Flush(bool reallocate_cache)
     266                 :             : {
     267                 :       10151 :     auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/true)};
     268                 :       10151 :     base->BatchWrite(cursor, m_block_hash);
     269                 :       10151 :     Assume(m_dirty_count == 0);
     270                 :       10151 :     cacheCoins.clear();
     271         [ +  + ]:       10151 :     if (reallocate_cache) {
     272                 :         936 :         ReallocateCache();
     273                 :             :     }
     274                 :       10151 :     cachedCoinsUsage = 0;
     275                 :       10151 : }
     276                 :             : 
     277                 :         245 : void CCoinsViewCache::Sync()
     278                 :             : {
     279                 :         245 :     auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/false)};
     280                 :         245 :     base->BatchWrite(cursor, m_block_hash);
     281         [ -  + ]:         245 :     Assume(m_dirty_count == 0);
     282         [ -  + ]:         245 :     if (m_sentinel.second.Next() != &m_sentinel) {
     283                 :             :         /* BatchWrite must clear flags of all entries */
     284         [ #  # ]:           0 :         throw std::logic_error("Not all unspent flagged entries were cleared");
     285                 :             :     }
     286                 :         245 : }
     287                 :             : 
     288                 :        8824 : void CCoinsViewCache::Reset() noexcept
     289                 :             : {
     290                 :        8824 :     cacheCoins.clear();
     291                 :        8824 :     cachedCoinsUsage = 0;
     292                 :        8824 :     m_dirty_count = 0;
     293                 :        8824 :     SetBestBlock(uint256::ZERO);
     294                 :        8824 : }
     295                 :             : 
     296                 :       11816 : void CCoinsViewCache::Uncache(const COutPoint& hash)
     297                 :             : {
     298                 :       11816 :     CCoinsMap::iterator it = cacheCoins.find(hash);
     299   [ +  +  +  + ]:       11816 :     if (it != cacheCoins.end() && !it->second.IsDirty()) {
     300   [ +  +  +  - ]:        2531 :         Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
     301                 :             :         TRACEPOINT(utxocache, uncache,
     302                 :             :                hash.hash.data(),
     303                 :             :                (uint32_t)hash.n,
     304                 :             :                (uint32_t)it->second.coin.nHeight,
     305                 :             :                (int64_t)it->second.coin.out.nValue,
     306                 :        1052 :                (bool)it->second.coin.IsCoinBase());
     307                 :        1052 :         cacheCoins.erase(it);
     308                 :             :     }
     309                 :       11816 : }
     310                 :             : 
     311                 :       36089 : unsigned int CCoinsViewCache::GetCacheSize() const {
     312                 :       36089 :     return cacheCoins.size();
     313                 :             : }
     314                 :             : 
     315                 :        1231 : bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
     316                 :             : {
     317         [ +  - ]:        1231 :     if (!tx.IsCoinBase()) {
     318   [ -  +  +  + ]:        2474 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
     319         [ +  + ]:        1247 :             if (!HaveCoin(tx.vin[i].prevout)) {
     320                 :             :                 return false;
     321                 :             :             }
     322                 :             :         }
     323                 :             :     }
     324                 :             :     return true;
     325                 :             : }
     326                 :             : 
     327                 :         936 : void CCoinsViewCache::ReallocateCache()
     328                 :             : {
     329                 :             :     // Cache should be empty when we're calling this.
     330         [ -  + ]:         936 :     assert(cacheCoins.size() == 0);
     331                 :         936 :     cacheCoins.~CCoinsMap();
     332                 :         936 :     m_cache_coins_memory_resource.~CCoinsMapMemoryResource();
     333                 :         936 :     ::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{};
     334                 :         936 :     ::new (&cacheCoins) CCoinsMap{0, SaltedOutpointHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource};
     335                 :         936 : }
     336                 :             : 
     337                 :         290 : void CCoinsViewCache::SanityCheck() const
     338                 :             : {
     339                 :         290 :     size_t recomputed_usage = 0;
     340                 :         290 :     size_t count_dirty = 0;
     341   [ +  +  +  + ]:      424404 :     for (const auto& [_, entry] : cacheCoins) {
     342         [ +  + ]:      424114 :         if (entry.coin.IsSpent()) {
     343   [ +  -  -  + ]:       14786 :             assert(entry.IsDirty() && !entry.IsFresh()); // A spent coin must be dirty and cannot be fresh
     344                 :             :         } else {
     345   [ +  +  -  + ]:      409328 :             assert(entry.IsDirty() || !entry.IsFresh()); // An unspent coin must not be fresh if not dirty
     346                 :             :         }
     347                 :             : 
     348                 :             :         // Recompute cachedCoinsUsage.
     349         [ +  + ]:      424114 :         recomputed_usage += entry.coin.DynamicMemoryUsage();
     350                 :             : 
     351                 :             :         // Count the number of entries we expect in the linked list.
     352         [ +  + ]:      424114 :         if (entry.IsDirty()) ++count_dirty;
     353                 :             :     }
     354                 :             :     // Iterate over the linked list of flagged entries.
     355                 :         290 :     size_t count_linked = 0;
     356         [ +  + ]:       36563 :     for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) {
     357                 :             :         // Verify linked list integrity.
     358         [ -  + ]:       36273 :         assert(it->second.Next()->second.Prev() == it);
     359         [ -  + ]:       36273 :         assert(it->second.Prev()->second.Next() == it);
     360                 :             :         // Verify they are actually flagged.
     361         [ -  + ]:       36273 :         assert(it->second.IsDirty());
     362                 :             :         // Count the number of entries actually in the list.
     363                 :       36273 :         ++count_linked;
     364                 :             :     }
     365   [ +  -  -  + ]:         290 :     assert(count_dirty == count_linked && count_dirty == m_dirty_count);
     366         [ -  + ]:         290 :     assert(recomputed_usage == cachedCoinsUsage);
     367                 :         290 : }
     368                 :             : 
     369                 :        8822 : CCoinsViewCache::ResetGuard CoinsViewOverlay::StartFetching(const CBlock& block LIFETIMEBOUND) noexcept
     370                 :             : {
     371         [ -  + ]:        8822 :     Assert(m_futures.empty());
     372         [ -  + ]:        8822 :     Assert(m_inputs.empty());
     373         [ -  + ]:        8822 :     Assert(m_input_head.load(std::memory_order_relaxed) == 0);
     374         [ -  + ]:        8822 :     Assert(m_input_tail == 0);
     375         [ +  + ]:        8822 :     if (const auto workers_count{m_thread_pool->WorkersCount()}; workers_count > 0) {
     376                 :             :         // Loop through the block inputs and set their prevouts in the queue.
     377                 :             :         // Filter inputs that spend outputs created earlier in the same block. These outputs will be created
     378                 :             :         // directly in the cache from the tx that creates them, so they will not be requested from a base view.
     379                 :        8821 :         std::unordered_set<Txid, SaltedTxidHasher> earlier_txids;
     380         [ -  + ]:        8821 :         earlier_txids.reserve(block.vtx.size());
     381         [ +  + ]:        9842 :         for (const auto& tx : block.vtx | std::views::drop(1)) {
     382         [ +  + ]:        2054 :             for (const auto& input : tx->vin) {
     383         [ +  + ]:        1033 :                 if (!earlier_txids.contains(input.prevout.hash)) m_inputs.emplace_back(input.prevout);
     384                 :             :             }
     385                 :        1021 :             earlier_txids.emplace(tx->GetHash());
     386                 :             :         }
     387                 :             :         // Only submit tasks if we have something to fetch.
     388   [ -  +  +  + ]:        8821 :         if (m_inputs.size()) {
     389                 :         158 :             std::vector<std::function<void()>> tasks(workers_count, [this] {
     390         [ +  + ]:        1506 :                 while (ProcessInput()) {}
     391                 :          79 :             });
     392         [ +  + ]:          79 :             if (auto futures{m_thread_pool->Submit(std::move(tasks))}) {
     393                 :          78 :                 m_futures = std::move(*futures);
     394                 :             :             } else {
     395                 :             :                 // Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or
     396                 :             :                 // Interrupt() on a different thread after we call WorkersCount() above. In that case parallel
     397                 :             :                 // fetching will not make progress, so we clear the inputs to fall back to single threaded fetching.
     398                 :           1 :                 LogWarning("Failed to submit prevout fetch tasks; falling back to single-threaded fetching for this block.");
     399                 :           1 :                 m_inputs.clear();
     400                 :           1 :                 StopFetching(); // Assert nothing changed if we failed to start tasks.
     401                 :          79 :             }
     402                 :          79 :         }
     403                 :        8821 :     }
     404                 :        8822 :     return CreateResetGuard();
     405                 :             : }
     406                 :             : 
     407                 :             : static const uint64_t MIN_TRANSACTION_OUTPUT_WEIGHT{WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut())};
     408                 :             : static const uint64_t MAX_OUTPUTS_PER_BLOCK{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT};
     409                 :             : 
     410                 :         140 : const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid)
     411                 :             : {
     412                 :         140 :     COutPoint iter(txid, 0);
     413         [ +  + ]:     8222354 :     while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
     414                 :     8222280 :         const Coin& alternate = view.AccessCoin(iter);
     415         [ +  + ]:     8222280 :         if (!alternate.IsSpent()) return alternate;
     416                 :     8222214 :         ++iter.n;
     417                 :             :     }
     418                 :             :     return coinEmpty;
     419                 :             : }
     420                 :             : 
     421                 :             : template <typename ReturnType, typename Func>
     422                 :       37294 : static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks)
     423                 :             : {
     424                 :             :     try {
     425         [ +  - ]:       37294 :         return func();
     426         [ -  - ]:           0 :     } catch(const std::runtime_error& e) {
     427         [ -  - ]:           0 :         for (const auto& f : err_callbacks) {
     428         [ -  - ]:           0 :             f();
     429                 :             :         }
     430         [ -  - ]:           0 :         LogError("Error reading from database: %s\n", e.what());
     431                 :             :         // Starting the shutdown sequence and returning false to the caller would be
     432                 :             :         // interpreted as 'entry not found' (as opposed to unable to read data), and
     433                 :             :         // could lead to invalid interpretation. Just exit immediately, as we can't
     434                 :             :         // continue anyway, and all writes should be atomic.
     435                 :           0 :         std::abort();
     436                 :             :     }
     437                 :             : }
     438                 :             : 
     439                 :       19089 : std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const
     440                 :             : {
     441                 :       38178 :     return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks);
     442                 :             : }
     443                 :             : 
     444                 :           0 : bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const
     445                 :             : {
     446                 :           0 :     return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
     447                 :             : }
     448                 :             : 
     449                 :       18205 : std::optional<Coin> CCoinsViewErrorCatcher::PeekCoin(const COutPoint& outpoint) const
     450                 :             : {
     451                 :       36410 :     return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::PeekCoin(outpoint); }, m_err_callbacks);
     452                 :             : }
        

Generated by: LCOV version 2.0-1