LCOV - code coverage report
Current view: top level - src - coins.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 95.3 % 85 81
Test Date: 2024-11-04 04:15:01 Functions: 86.7 % 15 13
Branches: 76.0 % 200 152

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #ifndef BITCOIN_COINS_H
       7                 :             : #define BITCOIN_COINS_H
       8                 :             : 
       9                 :             : #include <compressor.h>
      10                 :             : #include <core_memusage.h>
      11                 :             : #include <memusage.h>
      12                 :             : #include <primitives/transaction.h>
      13                 :             : #include <serialize.h>
      14                 :             : #include <support/allocators/pool.h>
      15                 :             : #include <uint256.h>
      16                 :             : #include <util/check.h>
      17                 :             : #include <util/hasher.h>
      18                 :             : 
      19                 :             : #include <assert.h>
      20                 :             : #include <stdint.h>
      21                 :             : 
      22                 :             : #include <functional>
      23                 :             : #include <unordered_map>
      24                 :             : 
      25                 :             : /**
      26                 :             :  * A UTXO entry.
      27                 :             :  *
      28                 :             :  * Serialized format:
      29                 :             :  * - VARINT((coinbase ? 1 : 0) | (height << 1))
      30                 :             :  * - the non-spent CTxOut (via TxOutCompression)
      31                 :             :  */
      32   [ +  +  +  + ]:    94889197 : class Coin
         [ +  + ][ -  -  
          +  -  +  -  -  
                -  +  - ]
      33                 :             : {
      34                 :             : public:
      35                 :             :     //! unspent transaction output
      36                 :             :     CTxOut out;
      37                 :             : 
      38                 :             :     //! whether containing transaction was a coinbase
      39                 :             :     unsigned int fCoinBase : 1;
      40                 :             : 
      41                 :             :     //! at which height this containing transaction was included in the active block chain
      42                 :             :     uint32_t nHeight : 31;
      43                 :             : 
      44                 :             :     //! construct a Coin from a CTxOut and height/coinbase information.
      45                 :             :     Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
      46         [ +  + ]:    11202367 :     Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
           [ +  -  +  - ]
           [ +  -  +  -  
                   +  - ]
      47                 :             : 
      48                 :     1135371 :     void Clear() {
      49                 :     1135371 :         out.SetNull();
      50                 :     1135371 :         fCoinBase = false;
      51                 :     1135371 :         nHeight = 0;
      52                 :     1135371 :     }
      53                 :             : 
      54                 :             :     //! empty constructor
      55                 :    28574798 :     Coin() : fCoinBase(false), nHeight(0) { }
      56                 :             : 
      57                 :     2198039 :     bool IsCoinBase() const {
      58         [ +  + ]:     2198039 :         return fCoinBase;
           [ +  +  -  - ]
      59                 :             :     }
      60                 :             : 
      61                 :             :     template<typename Stream>
      62                 :      140907 :     void Serialize(Stream &s) const {
      63         [ -  + ]:      140907 :         assert(!IsSpent());
      64                 :      140907 :         uint32_t code = nHeight * uint32_t{2} + fCoinBase;
      65                 :      140907 :         ::Serialize(s, VARINT(code));
      66                 :      140907 :         ::Serialize(s, Using<TxOutCompression>(out));
      67                 :      140907 :     }
      68                 :             : 
      69                 :             :     template<typename Stream>
      70                 :     5938274 :     void Unserialize(Stream &s) {
      71                 :     5938274 :         uint32_t code = 0;
      72                 :     5938274 :         ::Unserialize(s, VARINT(code));
      73                 :     5937975 :         nHeight = code >> 1;
      74                 :     5937975 :         fCoinBase = code & 1;
      75                 :     5937975 :         ::Unserialize(s, Using<TxOutCompression>(out));
      76                 :     5937402 :     }
      77                 :             : 
      78                 :             :     /** Either this coin never existed (see e.g. coinEmpty in coins.cpp), or it
      79                 :             :       * did exist and has been spent.
      80                 :             :       */
      81                 :    33286532 :     bool IsSpent() const {
      82   [ -  -  +  +  :    32958598 :         return out.IsNull();
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  +  
                +  +  + ]
           [ +  +  +  + ]
         [ +  + ][ -  +  
             -  +  -  + ]
           [ -  -  -  +  
          +  -  -  +  -  
           - ][ -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
          -  +  +  +  +  
           -  -  + ][ +  
          +  +  +  +  +  
                   -  + ]
      83                 :             :     }
      84                 :             : 
      85                 :     5567952 :     size_t DynamicMemoryUsage() const {
      86   [ +  +  +  +  :     9341540 :         return memusage::DynamicUsage(out.scriptPubKey);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
              + ][ +  + ]
      87                 :             :     }
      88                 :             : };
      89                 :             : 
      90                 :             : struct CCoinsCacheEntry;
      91                 :             : using CoinsCachePair = std::pair<const COutPoint, CCoinsCacheEntry>;
      92                 :             : 
      93                 :             : /**
      94                 :             :  * A Coin in one level of the coins database caching hierarchy.
      95                 :             :  *
      96                 :             :  * A coin can either be:
      97                 :             :  * - unspent or spent (in which case the Coin object will be nulled out - see Coin.Clear())
      98                 :             :  * - DIRTY or not DIRTY
      99                 :             :  * - FRESH or not FRESH
     100                 :             :  *
     101                 :             :  * Out of these 2^3 = 8 states, only some combinations are valid:
     102                 :             :  * - unspent, FRESH, DIRTY (e.g. a new coin created in the cache)
     103                 :             :  * - unspent, not FRESH, DIRTY (e.g. a coin changed in the cache during a reorg)
     104                 :             :  * - unspent, not FRESH, not DIRTY (e.g. an unspent coin fetched from the parent cache)
     105                 :             :  * - spent, FRESH, not DIRTY (e.g. a spent coin fetched from the parent cache)
     106                 :             :  * - spent, not FRESH, DIRTY (e.g. a coin is spent and spentness needs to be flushed to the parent)
     107                 :             :  */
     108                 :             : struct CCoinsCacheEntry
     109                 :             : {
     110                 :             : private:
     111                 :             :     /**
     112                 :             :      * These are used to create a doubly linked list of flagged entries.
     113                 :             :      * They are set in AddFlags and unset in ClearFlags.
     114                 :             :      * A flagged entry is any entry that is either DIRTY, FRESH, or both.
     115                 :             :      *
     116                 :             :      * DIRTY entries are tracked so that only modified entries can be passed to
     117                 :             :      * the parent cache for batch writing. This is a performance optimization
     118                 :             :      * compared to giving all entries in the cache to the parent and having the
     119                 :             :      * parent scan for only modified entries.
     120                 :             :      *
     121                 :             :      * FRESH-but-not-DIRTY coins can not occur in practice, since that would
     122                 :             :      * mean a spent coin exists in the parent CCoinsView and not in the child
     123                 :             :      * CCoinsViewCache. Nevertheless, if a spent coin is retrieved from the
     124                 :             :      * parent cache, the FRESH-but-not-DIRTY coin will be tracked by the linked
     125                 :             :      * list and deleted when Sync or Flush is called on the CCoinsViewCache.
     126                 :             :      */
     127                 :             :     CoinsCachePair* m_prev{nullptr};
     128                 :             :     CoinsCachePair* m_next{nullptr};
     129                 :             :     uint8_t m_flags{0};
     130                 :             : 
     131                 :             : public:
     132                 :             :     Coin coin; // The actual cached data.
     133                 :             : 
     134                 :             :     enum Flags {
     135                 :             :         /**
     136                 :             :          * DIRTY means the CCoinsCacheEntry is potentially different from the
     137                 :             :          * version in the parent cache. Failure to mark a coin as DIRTY when
     138                 :             :          * it is potentially different from the parent cache will cause a
     139                 :             :          * consensus failure, since the coin's state won't get written to the
     140                 :             :          * parent when the cache is flushed.
     141                 :             :          */
     142                 :             :         DIRTY = (1 << 0),
     143                 :             :         /**
     144                 :             :          * FRESH means the parent cache does not have this coin or that it is a
     145                 :             :          * spent coin in the parent cache. If a FRESH coin in the cache is
     146                 :             :          * later spent, it can be deleted entirely and doesn't ever need to be
     147                 :             :          * flushed to the parent. This is a performance optimization. Marking a
     148                 :             :          * coin as FRESH when it exists unspent in the parent cache will cause a
     149                 :             :          * consensus failure, since it might not be deleted from the parent
     150                 :             :          * when this cache is flushed.
     151                 :             :          */
     152                 :             :         FRESH = (1 << 1),
     153                 :             :     };
     154                 :             : 
     155                 :     9358591 :     CCoinsCacheEntry() noexcept = default;
     156                 :       57578 :     explicit CCoinsCacheEntry(Coin&& coin_) noexcept : coin(std::move(coin_)) {}
     157                 :     9727464 :     ~CCoinsCacheEntry()
     158                 :             :     {
     159                 :    19454928 :         ClearFlags();
     160                 :     9727464 :     }
     161                 :             : 
     162                 :             :     //! Adding a flag also requires a self reference to the pair that contains
     163                 :             :     //! this entry in the CCoinsCache map and a reference to the sentinel of the
     164                 :             :     //! flagged pair linked list.
     165                 :     2584715 :     inline void AddFlags(uint8_t flags, CoinsCachePair& self, CoinsCachePair& sentinel) noexcept
     166                 :             :     {
     167                 :     2584715 :         Assume(&self.second == this);
     168   [ +  +  +  + ]:     2584715 :         if (!m_flags && flags) {
     169                 :     2182843 :             m_prev = sentinel.second.m_prev;
     170                 :     2182843 :             m_next = &sentinel;
     171                 :     2182843 :             sentinel.second.m_prev = &self;
     172                 :     2182843 :             m_prev->second.m_next = &self;
     173                 :             :         }
     174                 :     2584715 :         m_flags |= flags;
     175                 :     2584715 :     }
     176                 :     9840221 :     inline void ClearFlags() noexcept
     177                 :             :     {
     178         [ +  + ]:     9727464 :         if (!m_flags) return;
     179                 :     3223047 :         m_next->second.m_prev = m_prev;
     180                 :     3223047 :         m_prev->second.m_next = m_next;
     181                 :     3223047 :         m_flags = 0;
     182                 :             :     }
     183                 :             :     inline uint8_t GetFlags() const noexcept { return m_flags; }
     184   [ +  +  +  +  :     2060885 :     inline bool IsDirty() const noexcept { return m_flags & DIRTY; }
             +  +  +  + ]
                 [ +  + ]
     185   [ +  +  +  +  :      594272 :     inline bool IsFresh() const noexcept { return m_flags & FRESH; }
          +  +  +  +  +  
                      + ]
     186                 :             : 
     187                 :             :     //! Only call Next when this entry is DIRTY, FRESH, or both
     188                 :      832058 :     inline CoinsCachePair* Next() const noexcept {
     189                 :      832058 :         Assume(m_flags);
     190                 :      832058 :         return m_next;
     191                 :             :     }
     192                 :             : 
     193                 :             :     //! Only call Prev when this entry is DIRTY, FRESH, or both
     194                 :       28390 :     inline CoinsCachePair* Prev() const noexcept {
     195                 :       28390 :         Assume(m_flags);
     196                 :       28390 :         return m_prev;
     197                 :             :     }
     198                 :             : 
     199                 :             :     //! Only use this for initializing the linked list sentinel
     200                 :     1040204 :     inline void SelfRef(CoinsCachePair& self) noexcept
     201                 :             :     {
     202                 :     1040204 :         Assume(&self.second == this);
     203                 :     1040204 :         m_prev = &self;
     204                 :     1040204 :         m_next = &self;
     205                 :             :         // Set sentinel to DIRTY so we can call Next on it
     206                 :     1040204 :         m_flags = DIRTY;
     207                 :     1040204 :     }
     208                 :             : };
     209                 :             : 
     210                 :             : /**
     211                 :             :  * PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size
     212                 :             :  * of 4 pointers. We do not know the exact node size used in the std::unordered_node implementation
     213                 :             :  * because it is implementation defined. Most implementations have an overhead of 1 or 2 pointers,
     214                 :             :  * so nodes can be connected in a linked list, and in some cases the hash value is stored as well.
     215                 :             :  * Using an additional sizeof(void*)*4 for MAX_BLOCK_SIZE_BYTES should thus be sufficient so that
     216                 :             :  * all implementations can allocate the nodes from the PoolAllocator.
     217                 :             :  */
     218                 :             : using CCoinsMap = std::unordered_map<COutPoint,
     219                 :             :                                      CCoinsCacheEntry,
     220                 :             :                                      SaltedOutpointHasher,
     221                 :             :                                      std::equal_to<COutPoint>,
     222                 :             :                                      PoolAllocator<CoinsCachePair,
     223                 :             :                                                    sizeof(CoinsCachePair) + sizeof(void*) * 4>>;
     224                 :             : 
     225                 :             : using CCoinsMapMemoryResource = CCoinsMap::allocator_type::ResourceType;
     226                 :             : 
     227                 :             : /** Cursor for iterating over CoinsView state */
     228                 :             : class CCoinsViewCursor
     229                 :             : {
     230                 :             : public:
     231                 :      108600 :     CCoinsViewCursor(const uint256 &hashBlockIn): hashBlock(hashBlockIn) {}
     232                 :      108600 :     virtual ~CCoinsViewCursor() = default;
     233                 :             : 
     234                 :             :     virtual bool GetKey(COutPoint &key) const = 0;
     235                 :             :     virtual bool GetValue(Coin &coin) const = 0;
     236                 :             : 
     237                 :             :     virtual bool Valid() const = 0;
     238                 :             :     virtual void Next() = 0;
     239                 :             : 
     240                 :             :     //! Get best block at the time this cursor was created
     241                 :             :     const uint256 &GetBestBlock() const { return hashBlock; }
     242                 :             : private:
     243                 :             :     uint256 hashBlock;
     244                 :             : };
     245                 :             : 
     246                 :             : /**
     247                 :             :  * Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
     248                 :             :  *
     249                 :             :  * This is a helper struct to encapsulate the diverging logic between a non-erasing
     250                 :             :  * CCoinsViewCache::Sync and an erasing CCoinsViewCache::Flush. This allows the receiver
     251                 :             :  * of CCoinsView::BatchWrite to iterate through the flagged entries without knowing
     252                 :             :  * the caller's intent.
     253                 :             :  *
     254                 :             :  * However, the receiver can still call CoinsViewCacheCursor::WillErase to see if the
     255                 :             :  * caller will erase the entry after BatchWrite returns. If so, the receiver can
     256                 :             :  * perform optimizations such as moving the coin out of the CCoinsCachEntry instead
     257                 :             :  * of copying it.
     258                 :             :  */
     259                 :             : struct CoinsViewCacheCursor
     260                 :             : {
     261                 :             :     //! If will_erase is not set, iterating through the cursor will erase spent coins from the map,
     262                 :             :     //! and other coins will be unflagged (removing them from the linked list).
     263                 :             :     //! If will_erase is set, the underlying map and linked list will not be modified,
     264                 :             :     //! as the caller is expected to wipe the entire map anyway.
     265                 :             :     //! This is an optimization compared to erasing all entries as the cursor iterates them when will_erase is set.
     266                 :             :     //! Calling CCoinsMap::clear() afterwards is faster because a CoinsCachePair cannot be coerced back into a
     267                 :             :     //! CCoinsMap::iterator to be erased, and must therefore be looked up again by key in the CCoinsMap before being erased.
     268                 :      397340 :     CoinsViewCacheCursor(size_t& usage LIFETIMEBOUND,
     269                 :             :                         CoinsCachePair& sentinel LIFETIMEBOUND,
     270                 :             :                         CCoinsMap& map LIFETIMEBOUND,
     271                 :             :                         bool will_erase) noexcept
     272                 :      397340 :         : m_usage(usage), m_sentinel(sentinel), m_map(map), m_will_erase(will_erase) {}
     273                 :             : 
     274                 :      307234 :     inline CoinsCachePair* Begin() const noexcept { return m_sentinel.second.Next(); }
     275         [ +  + ]:      716407 :     inline CoinsCachePair* End() const noexcept { return &m_sentinel; }
     276                 :             : 
     277                 :             :     //! Return the next entry after current, possibly erasing current
     278                 :      409173 :     inline CoinsCachePair* NextAndMaybeErase(CoinsCachePair& current) noexcept
     279                 :             :     {
     280                 :      409173 :         const auto next_entry{current.second.Next()};
     281                 :             :         // If we are not going to erase the cache, we must still erase spent entries.
     282                 :             :         // Otherwise clear the flags on the entry.
     283         [ +  + ]:      409173 :         if (!m_will_erase) {
     284         [ +  + ]:      125637 :             if (current.second.coin.IsSpent()) {
     285         [ -  + ]:       12880 :                 m_usage -= current.second.coin.DynamicMemoryUsage();
     286                 :       12880 :                 m_map.erase(current.first);
     287                 :             :             } else {
     288         [ +  - ]:      112757 :                 current.second.ClearFlags();
     289                 :             :             }
     290                 :             :         }
     291                 :      409173 :         return next_entry;
     292                 :             :     }
     293                 :             : 
     294   [ +  +  +  +  :      289188 :     inline bool WillErase(CoinsCachePair& current) const noexcept { return m_will_erase || current.second.coin.IsSpent(); }
             +  +  +  + ]
           [ +  +  +  + ]
     295                 :             : private:
     296                 :             :     size_t& m_usage;
     297                 :             :     CoinsCachePair& m_sentinel;
     298                 :             :     CCoinsMap& m_map;
     299                 :             :     bool m_will_erase;
     300                 :             : };
     301                 :             : 
     302                 :             : /** Abstract view on the open txout dataset. */
     303                 :     2628000 : class CCoinsView
     304                 :             : {
     305                 :             : public:
     306                 :             :     //! Retrieve the Coin (unspent transaction output) for a given outpoint.
     307                 :             :     virtual std::optional<Coin> GetCoin(const COutPoint& outpoint) const;
     308                 :             : 
     309                 :             :     //! Just check whether a given outpoint is unspent.
     310                 :             :     virtual bool HaveCoin(const COutPoint &outpoint) const;
     311                 :             : 
     312                 :             :     //! Retrieve the block hash whose state this CCoinsView currently represents
     313                 :             :     virtual uint256 GetBestBlock() const;
     314                 :             : 
     315                 :             :     //! Retrieve the range of blocks that may have been only partially written.
     316                 :             :     //! If the database is in a consistent state, the result is the empty vector.
     317                 :             :     //! Otherwise, a two-element vector is returned consisting of the new and
     318                 :             :     //! the old block hash, in that order.
     319                 :             :     virtual std::vector<uint256> GetHeadBlocks() const;
     320                 :             : 
     321                 :             :     //! Do a bulk modification (multiple Coin changes + BestBlock change).
     322                 :             :     //! The passed cursor is used to iterate through the coins.
     323                 :             :     virtual bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock);
     324                 :             : 
     325                 :             :     //! Get a cursor to iterate over the whole state
     326                 :             :     virtual std::unique_ptr<CCoinsViewCursor> Cursor() const;
     327                 :             : 
     328                 :             :     //! As we use CCoinsViews polymorphically, have a virtual destructor
     329                 :        8690 :     virtual ~CCoinsView() = default;
     330                 :             : 
     331                 :             :     //! Estimate database size (0 if not implemented)
     332                 :        2164 :     virtual size_t EstimateSize() const { return 0; }
     333                 :             : };
     334                 :             : 
     335                 :             : 
     336                 :             : /** CCoinsView backed by another CCoinsView */
     337                 :           0 : class CCoinsViewBacked : public CCoinsView
     338                 :             : {
     339                 :             : protected:
     340                 :             :     CCoinsView *base;
     341                 :             : 
     342                 :             : public:
     343                 :             :     CCoinsViewBacked(CCoinsView *viewIn);
     344                 :             :     std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
     345                 :             :     bool HaveCoin(const COutPoint &outpoint) const override;
     346                 :             :     uint256 GetBestBlock() const override;
     347                 :             :     std::vector<uint256> GetHeadBlocks() const override;
     348                 :             :     void SetBackend(CCoinsView &viewIn);
     349                 :             :     bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) override;
     350                 :             :     std::unique_ptr<CCoinsViewCursor> Cursor() const override;
     351                 :             :     size_t EstimateSize() const override;
     352                 :             : };
     353                 :             : 
     354                 :             : 
     355                 :             : /** CCoinsView that adds a memory cache for transactions to another CCoinsView */
     356                 :             : class CCoinsViewCache : public CCoinsViewBacked
     357                 :             : {
     358                 :             : private:
     359                 :             :     const bool m_deterministic;
     360                 :             : 
     361                 :             : protected:
     362                 :             :     /**
     363                 :             :      * Make mutable so that we can "fill the cache" even from Get-methods
     364                 :             :      * declared as "const".
     365                 :             :      */
     366                 :             :     mutable uint256 hashBlock;
     367                 :             :     mutable CCoinsMapMemoryResource m_cache_coins_memory_resource{};
     368                 :             :     /* The starting sentinel of the flagged entry circular doubly linked list. */
     369                 :             :     mutable CoinsCachePair m_sentinel;
     370                 :             :     mutable CCoinsMap cacheCoins;
     371                 :             : 
     372                 :             :     /* Cached dynamic memory usage for the inner Coin objects. */
     373                 :             :     mutable size_t cachedCoinsUsage{0};
     374                 :             : 
     375                 :             : public:
     376                 :             :     CCoinsViewCache(CCoinsView *baseIn, bool deterministic = false);
     377                 :             : 
     378                 :             :     /**
     379                 :             :      * By deleting the copy constructor, we prevent accidentally using it when one intends to create a cache on top of a base cache.
     380                 :             :      */
     381                 :             :     CCoinsViewCache(const CCoinsViewCache &) = delete;
     382                 :             : 
     383                 :             :     // Standard CCoinsView methods
     384                 :             :     std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
     385                 :             :     bool HaveCoin(const COutPoint &outpoint) const override;
     386                 :             :     uint256 GetBestBlock() const override;
     387                 :             :     void SetBestBlock(const uint256 &hashBlock);
     388                 :             :     bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) override;
     389                 :        2164 :     std::unique_ptr<CCoinsViewCursor> Cursor() const override {
     390         [ +  - ]:        2164 :         throw std::logic_error("CCoinsViewCache cursor iteration not supported.");
     391                 :             :     }
     392                 :             : 
     393                 :             :     /**
     394                 :             :      * Check if we have the given utxo already loaded in this cache.
     395                 :             :      * The semantics are the same as HaveCoin(), but no calls to
     396                 :             :      * the backing CCoinsView are made.
     397                 :             :      */
     398                 :             :     bool HaveCoinInCache(const COutPoint &outpoint) const;
     399                 :             : 
     400                 :             :     /**
     401                 :             :      * Return a reference to Coin in the cache, or coinEmpty if not found. This is
     402                 :             :      * more efficient than GetCoin.
     403                 :             :      *
     404                 :             :      * Generally, do not hold the reference returned for more than a short scope.
     405                 :             :      * While the current implementation allows for modifications to the contents
     406                 :             :      * of the cache while holding the reference, this behavior should not be relied
     407                 :             :      * on! To be safe, best to not hold the returned reference through any other
     408                 :             :      * calls to this cache.
     409                 :             :      */
     410                 :             :     const Coin& AccessCoin(const COutPoint &output) const;
     411                 :             : 
     412                 :             :     /**
     413                 :             :      * Add a coin. Set possible_overwrite to true if an unspent version may
     414                 :             :      * already exist in the cache.
     415                 :             :      */
     416                 :             :     void AddCoin(const COutPoint& outpoint, Coin&& coin, bool possible_overwrite);
     417                 :             : 
     418                 :             :     /**
     419                 :             :      * Emplace a coin into cacheCoins without performing any checks, marking
     420                 :             :      * the emplaced coin as dirty.
     421                 :             :      *
     422                 :             :      * NOT FOR GENERAL USE. Used only when loading coins from a UTXO snapshot.
     423                 :             :      * @sa ChainstateManager::PopulateAndValidateSnapshot()
     424                 :             :      */
     425                 :             :     void EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin);
     426                 :             : 
     427                 :             :     /**
     428                 :             :      * Spend a coin. Pass moveto in order to get the deleted data.
     429                 :             :      * If no unspent output exists for the passed outpoint, this call
     430                 :             :      * has no effect.
     431                 :             :      */
     432                 :             :     bool SpendCoin(const COutPoint &outpoint, Coin* moveto = nullptr);
     433                 :             : 
     434                 :             :     /**
     435                 :             :      * Push the modifications applied to this cache to its base and wipe local state.
     436                 :             :      * Failure to call this method or Sync() before destruction will cause the changes
     437                 :             :      * to be forgotten.
     438                 :             :      * If false is returned, the state of this cache (and its backing view) will be undefined.
     439                 :             :      */
     440                 :             :     bool Flush();
     441                 :             : 
     442                 :             :     /**
     443                 :             :      * Push the modifications applied to this cache to its base while retaining
     444                 :             :      * the contents of this cache (except for spent coins, which we erase).
     445                 :             :      * Failure to call this method or Flush() before destruction will cause the changes
     446                 :             :      * to be forgotten.
     447                 :             :      * If false is returned, the state of this cache (and its backing view) will be undefined.
     448                 :             :      */
     449                 :             :     bool Sync();
     450                 :             : 
     451                 :             :     /**
     452                 :             :      * Removes the UTXO with the given outpoint from the cache, if it is
     453                 :             :      * not modified.
     454                 :             :      */
     455                 :             :     void Uncache(const COutPoint &outpoint);
     456                 :             : 
     457                 :             :     //! Calculate the size of the cache (in number of transaction outputs)
     458                 :             :     unsigned int GetCacheSize() const;
     459                 :             : 
     460                 :             :     //! Calculate the size of the cache (in bytes)
     461                 :             :     size_t DynamicMemoryUsage() const;
     462                 :             : 
     463                 :             :     //! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
     464                 :             :     bool HaveInputs(const CTransaction& tx) const;
     465                 :             : 
     466                 :             :     //! Force a reallocation of the cache map. This is required when downsizing
     467                 :             :     //! the cache because the map's allocator may be hanging onto a lot of
     468                 :             :     //! memory despite having called .clear().
     469                 :             :     //!
     470                 :             :     //! See: https://stackoverflow.com/questions/42114044/how-to-release-unordered-map-memory
     471                 :             :     void ReallocateCache();
     472                 :             : 
     473                 :             :     //! Run an internal sanity check on the cache data structure. */
     474                 :             :     void SanityCheck() const;
     475                 :             : 
     476                 :             : private:
     477                 :             :     /**
     478                 :             :      * @note this is marked const, but may actually append to `cacheCoins`, increasing
     479                 :             :      * memory usage.
     480                 :             :      */
     481                 :             :     CCoinsMap::iterator FetchCoin(const COutPoint &outpoint) const;
     482                 :             : };
     483                 :             : 
     484                 :             : //! Utility function to add all of a transaction's outputs to a cache.
     485                 :             : //! When check is false, this assumes that overwrites are only possible for coinbase transactions.
     486                 :             : //! When check is true, the underlying view may be queried to determine whether an addition is
     487                 :             : //! an overwrite.
     488                 :             : // TODO: pass in a boolean to limit these possible overwrites to known
     489                 :             : // (pre-BIP34) cases.
     490                 :             : void AddCoins(CCoinsViewCache& cache, const CTransaction& tx, int nHeight, bool check = false);
     491                 :             : 
     492                 :             : //! Utility function to find any unspent output with a given txid.
     493                 :             : //! This function can be quite expensive because in the event of a transaction
     494                 :             : //! which is not found in the cache, it can cause up to MAX_OUTPUTS_PER_BLOCK
     495                 :             : //! lookups to database, so it should be used with care.
     496                 :             : const Coin& AccessByTxid(const CCoinsViewCache& cache, const Txid& txid);
     497                 :             : 
     498                 :             : /**
     499                 :             :  * This is a minimally invasive approach to shutdown on LevelDB read errors from the
     500                 :             :  * chainstate, while keeping user interface out of the common library, which is shared
     501                 :             :  * between bitcoind, and bitcoin-qt and non-server tools.
     502                 :             :  *
     503                 :             :  * Writes do not need similar protection, as failure to write is handled by the caller.
     504                 :             : */
     505                 :             : class CCoinsViewErrorCatcher final : public CCoinsViewBacked
     506                 :             : {
     507                 :             : public:
     508         [ +  - ]:        2530 :     explicit CCoinsViewErrorCatcher(CCoinsView* view) : CCoinsViewBacked(view) {}
     509                 :             : 
     510                 :           0 :     void AddReadErrCallback(std::function<void()> f) {
     511         [ #  # ]:           0 :         m_err_callbacks.emplace_back(std::move(f));
     512                 :           0 :     }
     513                 :             : 
     514                 :             :     std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
     515                 :             :     bool HaveCoin(const COutPoint &outpoint) const override;
     516                 :             : 
     517                 :             : private:
     518                 :             :     /** A list of callbacks to execute upon leveldb read error. */
     519                 :             :     std::vector<std::function<void()>> m_err_callbacks;
     520                 :             : 
     521                 :             : };
     522                 :             : 
     523                 :             : #endif // BITCOIN_COINS_H
        

Generated by: LCOV version 2.0-1