LCOV - code coverage report
Current view: top level - src - coins.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 91.1 % 224 204
Test Date: 2026-02-22 04:55:57 Functions: 77.1 % 48 37
Branches: 79.3 % 208 165

             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 <random.h>
       9                 :             : #include <uint256.h>
      10                 :             : #include <util/log.h>
      11                 :             : #include <util/trace.h>
      12                 :             : 
      13                 :             : TRACEPOINT_SEMAPHORE(utxocache, add);
      14                 :             : TRACEPOINT_SEMAPHORE(utxocache, spent);
      15                 :             : TRACEPOINT_SEMAPHORE(utxocache, uncache);
      16                 :             : 
      17                 :           3 : std::optional<Coin> CCoinsView::GetCoin(const COutPoint& outpoint) const { return std::nullopt; }
      18                 :       18011 : std::optional<Coin> CCoinsView::PeekCoin(const COutPoint& outpoint) const { return GetCoin(outpoint); }
      19                 :           0 : uint256 CCoinsView::GetBestBlock() const { return uint256(); }
      20                 :           0 : std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
      21                 :           0 : void CCoinsView::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock)
      22                 :             : {
      23         [ #  # ]:           0 :     for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) { }
      24                 :           0 : }
      25                 :             : 
      26                 :           0 : std::unique_ptr<CCoinsViewCursor> CCoinsView::Cursor() const { return nullptr; }
      27                 :             : 
      28                 :           0 : bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
      29                 :             : {
      30                 :           0 :     return GetCoin(outpoint).has_value();
      31                 :             : }
      32                 :             : 
      33                 :      158642 : CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
      34                 :       18649 : std::optional<Coin> CCoinsViewBacked::GetCoin(const COutPoint& outpoint) const { return base->GetCoin(outpoint); }
      35                 :       17608 : std::optional<Coin> CCoinsViewBacked::PeekCoin(const COutPoint& outpoint) const { return base->PeekCoin(outpoint); }
      36                 :           0 : bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
      37                 :         682 : uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
      38                 :           0 : std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
      39                 :         284 : void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
      40                 :          94 : void CCoinsViewBacked::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) { base->BatchWrite(cursor, hashBlock); }
      41                 :           0 : std::unique_ptr<CCoinsViewCursor> CCoinsViewBacked::Cursor() const { return base->Cursor(); }
      42                 :           0 : size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
      43                 :             : 
      44                 :       18468 : std::optional<Coin> CCoinsViewCache::PeekCoin(const COutPoint& outpoint) const
      45                 :             : {
      46         [ +  + ]:       18468 :     if (auto it{cacheCoins.find(outpoint)}; it != cacheCoins.end()) {
      47         [ +  + ]:         457 :         return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin};
      48                 :             :     }
      49                 :       18011 :     return base->PeekCoin(outpoint);
      50                 :             : }
      51                 :             : 
      52                 :      158297 : CCoinsViewCache::CCoinsViewCache(CCoinsView* baseIn, bool deterministic) :
      53                 :      158297 :     CCoinsViewBacked(baseIn), m_deterministic(deterministic),
      54   [ +  -  +  - ]:      158297 :     cacheCoins(0, SaltedOutpointHasher(/*deterministic=*/deterministic), CCoinsMap::key_equal{}, &m_cache_coins_memory_resource)
      55                 :             : {
      56                 :      158297 :     m_sentinel.second.SelfRef(m_sentinel);
      57                 :      158297 : }
      58                 :             : 
      59                 :      176172 : size_t CCoinsViewCache::DynamicMemoryUsage() const {
      60                 :      176172 :     return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
      61                 :             : }
      62                 :             : 
      63                 :    27979935 : std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const
      64                 :             : {
      65                 :    27979935 :     return base->GetCoin(outpoint);
      66                 :             : }
      67                 :             : 
      68                 :    28657340 : CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
      69         [ +  + ]:    28657340 :     const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
      70         [ +  + ]:    28657340 :     if (inserted) {
      71         [ +  + ]:    27998400 :         if (auto coin{FetchCoinFromBase(outpoint)}) {
      72                 :      568704 :             ret->second.coin = std::move(*coin);
      73         [ +  + ]:      568704 :             cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
      74         [ -  + ]:      568704 :             Assert(!ret->second.coin.IsSpent());
      75                 :             :         } else {
      76                 :    27429696 :             cacheCoins.erase(ret);
      77                 :    27429696 :             return cacheCoins.end();
      78                 :    27998400 :         }
      79                 :             :     }
      80                 :     1227644 :     return ret;
      81                 :             : }
      82                 :             : 
      83                 :    16579322 : std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const
      84                 :             : {
      85   [ +  +  +  + ]:    16579322 :     if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin;
      86                 :    16208410 :     return std::nullopt;
      87                 :             : }
      88                 :             : 
      89                 :      164047 : void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
      90         [ -  + ]:      164047 :     assert(!coin.IsSpent());
      91         [ +  + ]:      164047 :     if (coin.out.scriptPubKey.IsUnspendable()) return;
      92                 :      145018 :     CCoinsMap::iterator it;
      93                 :      145018 :     bool inserted;
      94         [ +  + ]:      145018 :     std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
      95                 :      145018 :     bool fresh = false;
      96         [ +  + ]:      145018 :     if (!possible_overwrite) {
      97         [ +  + ]:       95834 :         if (!it->second.coin.IsSpent()) {
      98         [ +  - ]:          17 :             throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
      99                 :             :         }
     100                 :             :         // If the coin exists in this cache as a spent coin and is DIRTY, then
     101                 :             :         // its spentness hasn't been flushed to the parent cache. We're
     102                 :             :         // re-adding the coin to this cache now but we can't mark it as FRESH.
     103                 :             :         // If we mark it FRESH and then spend it before the cache is flushed
     104                 :             :         // we would remove it from this cache and would never flush spentness
     105                 :             :         // to the parent cache.
     106                 :             :         //
     107                 :             :         // Re-adding a spent coin can happen in the case of a re-org (the coin
     108                 :             :         // is 'spent' when the block adding it is disconnected and then
     109                 :             :         // re-added when it is also added in a newly connected block).
     110                 :             :         //
     111                 :             :         // If the coin doesn't exist in the current cache, or is spent but not
     112                 :             :         // DIRTY, then it can be marked FRESH.
     113                 :       95817 :         fresh = !it->second.IsDirty();
     114                 :             :     }
     115         [ +  + ]:      145001 :     if (!inserted) {
     116         [ +  + ]:       10842 :         m_dirty_count -= it->second.IsDirty();
     117         [ +  + ]:       14325 :         cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
     118                 :             :     }
     119                 :      145001 :     it->second.coin = std::move(coin);
     120                 :      145001 :     CCoinsCacheEntry::SetDirty(*it, m_sentinel);
     121                 :      145001 :     ++m_dirty_count;
     122         [ +  + ]:      145001 :     if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel);
     123         [ +  + ]:      231790 :     cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
     124                 :             :     TRACEPOINT(utxocache, add,
     125                 :             :            outpoint.hash.data(),
     126                 :             :            (uint32_t)outpoint.n,
     127                 :             :            (uint32_t)it->second.coin.nHeight,
     128                 :             :            (int64_t)it->second.coin.out.nValue,
     129                 :      164030 :            (bool)it->second.coin.IsCoinBase());
     130                 :             : }
     131                 :             : 
     132                 :       20085 : void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
     133         [ +  + ]:       20085 :     const auto mem_usage{coin.DynamicMemoryUsage()};
     134         [ +  + ]:       20085 :     auto [it, inserted] = cacheCoins.try_emplace(std::move(outpoint), std::move(coin));
     135         [ +  + ]:       20085 :     if (inserted) {
     136                 :       20084 :         CCoinsCacheEntry::SetDirty(*it, m_sentinel);
     137                 :       20084 :         ++m_dirty_count;
     138                 :       20084 :         cachedCoinsUsage += mem_usage;
     139                 :             :     }
     140                 :       20085 : }
     141                 :             : 
     142                 :       55379 : void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
     143                 :       55379 :     bool fCoinbase = tx.IsCoinBase();
     144                 :       55379 :     const Txid& txid = tx.GetHash();
     145   [ -  +  +  + ]:      129432 :     for (size_t i = 0; i < tx.vout.size(); ++i) {
     146         [ -  + ]:       74053 :         bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
     147                 :             :         // Coinbase transactions can always be overwritten, in order to correctly
     148                 :             :         // deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
     149         [ +  - ]:      148106 :         cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
     150                 :             :     }
     151                 :       55379 : }
     152                 :             : 
     153                 :       69357 : bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
     154                 :       69357 :     CCoinsMap::iterator it = FetchCoin(outpoint);
     155         [ +  + ]:       69357 :     if (it == cacheCoins.end()) return false;
     156         [ +  + ]:       69351 :     m_dirty_count -= it->second.IsDirty();
     157         [ +  + ]:       69351 :     cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
     158                 :             :     TRACEPOINT(utxocache, spent,
     159                 :             :            outpoint.hash.data(),
     160                 :             :            (uint32_t)outpoint.n,
     161                 :             :            (uint32_t)it->second.coin.nHeight,
     162                 :             :            (int64_t)it->second.coin.out.nValue,
     163                 :       69351 :            (bool)it->second.coin.IsCoinBase());
     164         [ +  + ]:       69351 :     if (moveout) {
     165                 :       34513 :         *moveout = std::move(it->second.coin);
     166                 :             :     }
     167         [ +  + ]:       69351 :     if (it->second.IsFresh()) {
     168                 :        8007 :         cacheCoins.erase(it);
     169                 :             :     } else {
     170                 :       61344 :         CCoinsCacheEntry::SetDirty(*it, m_sentinel);
     171                 :       61344 :         ++m_dirty_count;
     172                 :       61344 :         it->second.coin.Clear();
     173                 :             :     }
     174                 :             :     return true;
     175                 :             : }
     176                 :             : 
     177                 :             : static const Coin coinEmpty;
     178                 :             : 
     179                 :    10894932 : const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
     180         [ +  + ]:    10894932 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
     181         [ +  + ]:    10894932 :     if (it == cacheCoins.end()) {
     182                 :             :         return coinEmpty;
     183                 :             :     } else {
     184                 :      371893 :         return it->second.coin;
     185                 :             :     }
     186                 :             : }
     187                 :             : 
     188                 :     1113729 : bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
     189         [ +  + ]:     1113729 :     CCoinsMap::const_iterator it = FetchCoin(outpoint);
     190   [ +  +  +  + ]:     1113729 :     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
     191                 :             : }
     192                 :             : 
     193                 :      250726 : bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
     194         [ +  + ]:      250726 :     CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
     195   [ +  +  +  + ]:      250726 :     return (it != cacheCoins.end() && !it->second.coin.IsSpent());
     196                 :             : }
     197                 :             : 
     198                 :       35122 : uint256 CCoinsViewCache::GetBestBlock() const {
     199         [ +  + ]:       70244 :     if (hashBlock.IsNull())
     200                 :       17783 :         hashBlock = base->GetBestBlock();
     201                 :       35122 :     return hashBlock;
     202                 :             : }
     203                 :             : 
     204                 :      447678 : void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
     205                 :      447678 :     hashBlock = hashBlockIn;
     206                 :      447678 : }
     207                 :             : 
     208                 :        9905 : void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlockIn)
     209                 :             : {
     210         [ +  + ]:      209519 :     for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
     211         [ +  + ]:      199622 :         if (!it->second.IsDirty()) { // TODO a cursor can only contain dirty entries
     212                 :          18 :             continue;
     213                 :             :         }
     214         [ +  + ]:      199604 :         auto [itUs, inserted]{cacheCoins.try_emplace(it->first)};
     215         [ +  + ]:      199604 :         if (inserted) {
     216   [ +  +  +  + ]:      151835 :             if (it->second.IsFresh() && it->second.coin.IsSpent()) {
     217                 :           1 :                 cacheCoins.erase(itUs); // TODO fresh coins should have been removed at spend
     218                 :             :             } else {
     219                 :             :                 // The parent cache does not have an entry, while the child cache does.
     220                 :             :                 // Move the data up and mark it as dirty.
     221         [ -  + ]:      151834 :                 CCoinsCacheEntry& entry{itUs->second};
     222   [ -  +  -  - ]:      151834 :                 assert(entry.coin.DynamicMemoryUsage() == 0);
     223         [ +  + ]:      151834 :                 if (cursor.WillErase(*it)) {
     224                 :             :                     // Since this entry will be erased,
     225                 :             :                     // we can move the coin into us instead of copying it
     226                 :      139891 :                     entry.coin = std::move(it->second.coin);
     227                 :             :                 } else {
     228                 :       11943 :                     entry.coin = it->second.coin;
     229                 :             :                 }
     230                 :      151834 :                 CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
     231                 :      151834 :                 ++m_dirty_count;
     232         [ +  + ]:      151834 :                 cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
     233                 :             :                 // We can mark it FRESH in the parent if it was FRESH in the child
     234                 :             :                 // Otherwise it might have just been flushed from the parent's cache
     235                 :             :                 // and already exist in the grandparent
     236         [ +  + ]:      151834 :                 if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel);
     237                 :             :             }
     238                 :             :         } else {
     239                 :             :             // Found the entry in the parent cache
     240   [ +  +  +  + ]:       47769 :             if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) {
     241                 :             :                 // The coin was marked FRESH in the child cache, but the coin
     242                 :             :                 // exists in the parent cache. If this ever happens, it means
     243                 :             :                 // the FRESH flag was misapplied and there is a logic error in
     244                 :             :                 // the calling code.
     245         [ +  - ]:           8 :                 throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
     246                 :             :             }
     247                 :             : 
     248   [ +  +  +  + ]:       47761 :             if (itUs->second.IsFresh() && it->second.coin.IsSpent()) {
     249                 :             :                 // The grandparent cache does not have an entry, and the coin
     250                 :             :                 // has been spent. We can just delete it from the parent cache.
     251         [ +  + ]:        3505 :                 m_dirty_count -= itUs->second.IsDirty();
     252         [ +  + ]:        3505 :                 cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
     253                 :        3505 :                 cacheCoins.erase(itUs);
     254                 :             :             } else {
     255                 :             :                 // A normal modification.
     256         [ +  + ]:       44256 :                 cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
     257         [ +  + ]:       44256 :                 if (cursor.WillErase(*it)) {
     258                 :             :                     // Since this entry will be erased,
     259                 :             :                     // we can move the coin into us instead of copying it
     260                 :       42750 :                     itUs->second.coin = std::move(it->second.coin);
     261                 :             :                 } else {
     262                 :        1506 :                     itUs->second.coin = it->second.coin;
     263                 :             :                 }
     264         [ +  + ]:       44256 :                 cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
     265         [ +  + ]:       44256 :                 if (!itUs->second.IsDirty()) {
     266                 :       39211 :                     CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
     267                 :       39211 :                     ++m_dirty_count;
     268                 :             :                 }
     269                 :             :                 // NOTE: It isn't safe to mark the coin as FRESH in the parent
     270                 :             :                 // cache. If it already existed and was spent in the parent
     271                 :             :                 // cache then marking it FRESH would prevent that spentness
     272                 :             :                 // from being flushed to the grandparent.
     273                 :             :             }
     274                 :             :         }
     275                 :             :     }
     276                 :        9897 :     SetBestBlock(hashBlockIn);
     277                 :        9897 : }
     278                 :             : 
     279                 :        9929 : void CCoinsViewCache::Flush(bool reallocate_cache)
     280                 :             : {
     281                 :        9929 :     auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/true)};
     282                 :        9929 :     base->BatchWrite(cursor, hashBlock);
     283                 :        9929 :     Assume(m_dirty_count == 0);
     284                 :        9929 :     cacheCoins.clear();
     285         [ +  + ]:        9929 :     if (reallocate_cache) {
     286                 :         949 :         ReallocateCache();
     287                 :             :     }
     288                 :        9929 :     cachedCoinsUsage = 0;
     289                 :        9929 : }
     290                 :             : 
     291                 :         249 : void CCoinsViewCache::Sync()
     292                 :             : {
     293                 :         249 :     auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/false)};
     294                 :         249 :     base->BatchWrite(cursor, hashBlock);
     295         [ -  + ]:         249 :     Assume(m_dirty_count == 0);
     296         [ -  + ]:         249 :     if (m_sentinel.second.Next() != &m_sentinel) {
     297                 :             :         /* BatchWrite must clear flags of all entries */
     298         [ #  # ]:           0 :         throw std::logic_error("Not all unspent flagged entries were cleared");
     299                 :             :     }
     300                 :         249 : }
     301                 :             : 
     302                 :        8582 : void CCoinsViewCache::Reset() noexcept
     303                 :             : {
     304                 :        8582 :     cacheCoins.clear();
     305                 :        8582 :     cachedCoinsUsage = 0;
     306                 :        8582 :     m_dirty_count = 0;
     307                 :        8582 :     SetBestBlock(uint256::ZERO);
     308                 :        8582 : }
     309                 :             : 
     310                 :       11873 : void CCoinsViewCache::Uncache(const COutPoint& hash)
     311                 :             : {
     312                 :       11873 :     CCoinsMap::iterator it = cacheCoins.find(hash);
     313   [ +  +  +  + ]:       11873 :     if (it != cacheCoins.end() && !it->second.IsDirty()) {
     314         [ +  + ]:        1214 :         cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
     315                 :             :         TRACEPOINT(utxocache, uncache,
     316                 :             :                hash.hash.data(),
     317                 :             :                (uint32_t)hash.n,
     318                 :             :                (uint32_t)it->second.coin.nHeight,
     319                 :             :                (int64_t)it->second.coin.out.nValue,
     320                 :        1214 :                (bool)it->second.coin.IsCoinBase());
     321                 :        1214 :         cacheCoins.erase(it);
     322                 :             :     }
     323                 :       11873 : }
     324                 :             : 
     325                 :       35263 : unsigned int CCoinsViewCache::GetCacheSize() const {
     326                 :       35263 :     return cacheCoins.size();
     327                 :             : }
     328                 :             : 
     329                 :        1176 : bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
     330                 :             : {
     331         [ +  - ]:        1176 :     if (!tx.IsCoinBase()) {
     332   [ -  +  +  + ]:        2353 :         for (unsigned int i = 0; i < tx.vin.size(); i++) {
     333         [ +  + ]:        1182 :             if (!HaveCoin(tx.vin[i].prevout)) {
     334                 :             :                 return false;
     335                 :             :             }
     336                 :             :         }
     337                 :             :     }
     338                 :             :     return true;
     339                 :             : }
     340                 :             : 
     341                 :         949 : void CCoinsViewCache::ReallocateCache()
     342                 :             : {
     343                 :             :     // Cache should be empty when we're calling this.
     344         [ -  + ]:         949 :     assert(cacheCoins.size() == 0);
     345                 :         949 :     cacheCoins.~CCoinsMap();
     346                 :         949 :     m_cache_coins_memory_resource.~CCoinsMapMemoryResource();
     347                 :         949 :     ::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{};
     348                 :         949 :     ::new (&cacheCoins) CCoinsMap{0, SaltedOutpointHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource};
     349                 :         949 : }
     350                 :             : 
     351                 :         340 : void CCoinsViewCache::SanityCheck() const
     352                 :             : {
     353                 :         340 :     size_t recomputed_usage = 0;
     354                 :         340 :     size_t count_dirty = 0;
     355   [ +  +  +  + ]:      560017 :     for (const auto& [_, entry] : cacheCoins) {
     356         [ +  + ]:      559677 :         if (entry.coin.IsSpent()) {
     357   [ +  -  -  + ]:       27285 :             assert(entry.IsDirty() && !entry.IsFresh()); // A spent coin must be dirty and cannot be fresh
     358                 :             :         } else {
     359   [ +  +  -  + ]:      532392 :             assert(entry.IsDirty() || !entry.IsFresh()); // An unspent coin must not be fresh if not dirty
     360                 :             :         }
     361                 :             : 
     362                 :             :         // Recompute cachedCoinsUsage.
     363         [ +  + ]:      559677 :         recomputed_usage += entry.coin.DynamicMemoryUsage();
     364                 :             : 
     365                 :             :         // Count the number of entries we expect in the linked list.
     366         [ +  + ]:      559677 :         if (entry.IsDirty()) ++count_dirty;
     367                 :             :     }
     368                 :             :     // Iterate over the linked list of flagged entries.
     369                 :         340 :     size_t count_linked = 0;
     370         [ +  + ]:       64484 :     for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) {
     371                 :             :         // Verify linked list integrity.
     372         [ -  + ]:       64144 :         assert(it->second.Next()->second.Prev() == it);
     373         [ -  + ]:       64144 :         assert(it->second.Prev()->second.Next() == it);
     374                 :             :         // Verify they are actually flagged.
     375         [ -  + ]:       64144 :         assert(it->second.IsDirty());
     376                 :             :         // Count the number of entries actually in the list.
     377                 :       64144 :         ++count_linked;
     378                 :             :     }
     379   [ +  -  -  + ]:         340 :     assert(count_dirty == count_linked && count_dirty == m_dirty_count);
     380         [ -  + ]:         340 :     assert(recomputed_usage == cachedCoinsUsage);
     381                 :         340 : }
     382                 :             : 
     383                 :             : static const uint64_t MIN_TRANSACTION_OUTPUT_WEIGHT{WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut())};
     384                 :             : static const uint64_t MAX_OUTPUTS_PER_BLOCK{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT};
     385                 :             : 
     386                 :         171 : const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid)
     387                 :             : {
     388                 :         171 :     COutPoint iter(txid, 0);
     389         [ +  + ]:     9777939 :     while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
     390                 :     9777851 :         const Coin& alternate = view.AccessCoin(iter);
     391         [ +  + ]:     9777851 :         if (!alternate.IsSpent()) return alternate;
     392                 :     9777768 :         ++iter.n;
     393                 :             :     }
     394                 :             :     return coinEmpty;
     395                 :             : }
     396                 :             : 
     397                 :             : template <typename ReturnType, typename Func>
     398                 :       36257 : static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks)
     399                 :             : {
     400                 :             :     try {
     401                 :       36257 :         return func();
     402         [ -  - ]:           0 :     } catch(const std::runtime_error& e) {
     403         [ -  - ]:           0 :         for (const auto& f : err_callbacks) {
     404         [ -  - ]:           0 :             f();
     405                 :             :         }
     406         [ -  - ]:           0 :         LogError("Error reading from database: %s\n", e.what());
     407                 :             :         // Starting the shutdown sequence and returning false to the caller would be
     408                 :             :         // interpreted as 'entry not found' (as opposed to unable to read data), and
     409                 :             :         // could lead to invalid interpretation. Just exit immediately, as we can't
     410                 :             :         // continue anyway, and all writes should be atomic.
     411                 :           0 :         std::abort();
     412                 :             :     }
     413                 :             : }
     414                 :             : 
     415                 :       18649 : std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const
     416                 :             : {
     417         [ +  - ]:       37298 :     return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks);
     418                 :             : }
     419                 :             : 
     420                 :           0 : bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const
     421                 :             : {
     422         [ #  # ]:           0 :     return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
     423                 :             : }
     424                 :             : 
     425                 :       17608 : std::optional<Coin> CCoinsViewErrorCatcher::PeekCoin(const COutPoint& outpoint) const
     426                 :             : {
     427         [ +  - ]:       35216 :     return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::PeekCoin(outpoint); }, m_err_callbacks);
     428                 :             : }
        

Generated by: LCOV version 2.0-1