LCOV - code coverage report
Current view: top level - src - txdb.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 83.1 % 136 113
Test Date: 2026-06-23 06:58:32 Functions: 87.5 % 24 21
Branches: 45.7 % 186 85

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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                 :             : #include <txdb.h>
       7                 :             : 
       8                 :             : #include <coins.h>
       9                 :             : #include <dbwrapper.h>
      10                 :             : #include <logging/timer.h>
      11                 :             : #include <primitives/transaction.h>
      12                 :             : #include <random.h>
      13                 :             : #include <serialize.h>
      14                 :             : #include <uint256.h>
      15                 :             : #include <util/byte_units.h>
      16                 :             : #include <util/log.h>
      17                 :             : #include <util/threadnames.h>
      18                 :             : #include <util/vector.h>
      19                 :             : 
      20                 :             : #include <cassert>
      21                 :             : #include <chrono>
      22                 :             : #include <cstdlib>
      23                 :             : #include <exception>
      24                 :             : #include <future>
      25                 :             : #include <iterator>
      26                 :             : #include <utility>
      27                 :             : 
      28                 :             : static constexpr uint8_t DB_COIN{'C'};
      29                 :             : static constexpr uint8_t DB_BEST_BLOCK{'B'};
      30                 :             : static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
      31                 :             : // Keys used in previous version that might still be found in the DB:
      32                 :             : static constexpr uint8_t DB_COINS{'c'};
      33                 :             : 
      34                 :             : // Threshold for warning when writing this many dirty cache entries to disk.
      35                 :             : static constexpr size_t WARN_FLUSH_COINS_COUNT{10'000'000};
      36                 :             : 
      37                 :        3211 : bool CCoinsViewDB::NeedsUpgrade()
      38                 :             : {
      39         [ +  - ]:        3211 :     std::unique_ptr<CDBIterator> cursor{m_db->NewIterator()};
      40                 :             :     // DB_COINS was deprecated in v0.15.0, commit
      41                 :             :     // 1088b02f0ccd7358d2b7076bb9e122d59d502d02
      42         [ +  - ]:        3211 :     cursor->Seek(std::make_pair(DB_COINS, uint256{}));
      43         [ +  - ]:        6422 :     return cursor->Valid();
      44                 :        3211 : }
      45                 :             : 
      46                 :             : namespace {
      47                 :             : 
      48                 :             : struct CoinEntry {
      49                 :             :     COutPoint* outpoint;
      50                 :             :     uint8_t key{DB_COIN};
      51         [ +  - ]:     4343098 :     explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)) {}
      52                 :             : 
      53                 :    20607920 :     SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
      54                 :             : };
      55                 :             : 
      56                 :             : } // namespace
      57                 :             : 
      58                 :       10164 : CCoinsViewDB::CCoinsViewDB(DBParams db_params, CoinsViewOptions options) :
      59                 :       10164 :     m_db_params{std::move(db_params)},
      60                 :       10164 :     m_options{std::move(options)},
      61         [ +  - ]:       10164 :     m_db{std::make_unique<CDBWrapper>(m_db_params)} { }
      62                 :             : 
      63                 :       10164 : CCoinsViewDB::~CCoinsViewDB()
      64                 :             : {
      65         [ +  + ]:       10164 :     if (m_compaction.valid()) {
      66         [ +  + ]:          50 :         if (m_compaction.wait_for(std::chrono::seconds{0}) != std::future_status::ready) {
      67         [ -  + ]:           2 :             LogInfo("Waiting for background chainstate compaction of %s", fs::PathToString(m_db_params.path));
      68                 :             :         }
      69                 :          50 :         m_compaction.wait();
      70                 :             :     }
      71         [ +  + ]:       10214 : }
      72                 :             : 
      73                 :        4860 : void CCoinsViewDB::ResizeCache(size_t new_cache_size)
      74                 :             : {
      75                 :             :     // We can't do this operation with an in-memory DB since we'll lose all the coins upon
      76                 :             :     // reset.
      77         [ -  + ]:        4860 :     if (!m_db_params.memory_only) {
      78                 :           0 :         LOCK(m_db_mutex);
      79                 :             :         // Have to do a reset first to get the original `m_db` state to release its
      80                 :             :         // filesystem lock.
      81         [ #  # ]:           0 :         m_db.reset();
      82                 :           0 :         m_db_params.cache_bytes = new_cache_size;
      83                 :           0 :         m_db_params.wipe_data = false;
      84   [ #  #  #  # ]:           0 :         m_db = std::make_unique<CDBWrapper>(m_db_params);
      85                 :           0 :     }
      86                 :        4860 : }
      87                 :             : 
      88                 :     4339890 : std::optional<Coin> CCoinsViewDB::GetCoin(const COutPoint& outpoint) const
      89                 :             : {
      90   [ +  -  +  + ]:     4339890 :     if (Coin coin; m_db->Read(CoinEntry(&outpoint), coin)) {
      91         [ -  + ]:      286853 :         Assert(!coin.IsSpent()); // The UTXO database should never contain spent coins
      92                 :      286853 :         return coin;
      93                 :      286853 :     }
      94                 :     4053037 :     return std::nullopt;
      95                 :             : }
      96                 :             : 
      97                 :     1347190 : std::optional<Coin> CCoinsViewDB::PeekCoin(const COutPoint& outpoint) const
      98                 :             : {
      99                 :     1347190 :     return GetCoin(outpoint);
     100                 :             : }
     101                 :             : 
     102                 :        3208 : bool CCoinsViewDB::HaveCoin(const COutPoint& outpoint) const
     103                 :             : {
     104                 :        3208 :     return m_db->Exists(CoinEntry(&outpoint));
     105                 :             : }
     106                 :             : 
     107                 :     4341033 : uint256 CCoinsViewDB::GetBestBlock() const {
     108                 :     4341033 :     uint256 hashBestChain;
     109         [ +  + ]:     4341033 :     if (!m_db->Read(DB_BEST_BLOCK, hashBestChain))
     110                 :       17929 :         return uint256();
     111                 :     4323104 :     return hashBestChain;
     112                 :             : }
     113                 :             : 
     114                 :       16274 : std::vector<uint256> CCoinsViewDB::GetHeadBlocks() const {
     115                 :       16274 :     std::vector<uint256> vhashHeadBlocks;
     116   [ +  -  +  - ]:       16274 :     if (!m_db->Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
     117                 :       16274 :         return std::vector<uint256>();
     118                 :             :     }
     119                 :           0 :     return vhashHeadBlocks;
     120                 :       16274 : }
     121                 :             : 
     122                 :     4208276 : void CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash)
     123                 :             : {
     124                 :     4208276 :     CDBBatch batch(*m_db);
     125                 :     4208276 :     size_t count = 0;
     126                 :     4208276 :     const size_t dirty_count{cursor.GetDirtyCount()};
     127         [ -  + ]:     8416552 :     assert(!block_hash.IsNull());
     128                 :             : 
     129         [ +  - ]:     4208276 :     uint256 old_tip = GetBestBlock();
     130         [ +  + ]:     4208276 :     if (old_tip.IsNull()) {
     131                 :             :         // We may be in the middle of replaying.
     132         [ +  - ]:        6647 :         std::vector<uint256> old_heads = GetHeadBlocks();
     133   [ -  +  -  + ]:        6647 :         if (old_heads.size() == 2) {
     134         [ #  # ]:           0 :             if (old_heads[0] != block_hash) {
     135         [ #  # ]:           0 :                 LogError("The coins database detected an inconsistent state, likely due to a previous crash or shutdown. You will need to restart bitcoind with the -reindex-chainstate or -reindex configuration option.\n");
     136                 :             :             }
     137         [ #  # ]:           0 :             assert(old_heads[0] == block_hash);
     138                 :           0 :             old_tip = old_heads[1];
     139                 :             :         }
     140                 :        6647 :     }
     141                 :             : 
     142   [ -  +  -  - ]:     4208276 :     if (dirty_count > WARN_FLUSH_COINS_COUNT) LogWarning("Flushing large (%d entries) UTXO set to disk, it may take several minutes", dirty_count);
     143   [ +  -  +  -  :     8416552 :     LOG_TIME_MILLIS_WITH_CATEGORY(strprintf("write coins cache to disk (%d out of %d cached coins)",
                   +  - ]
     144                 :             :         dirty_count, cursor.GetTotalCount()), BCLog::BENCH);
     145                 :             : 
     146                 :             :     // In the first batch, mark the database as being in the middle of a
     147                 :             :     // transition from old_tip to block_hash.
     148                 :             :     // A vector is used for future extensibility, as we may want to support
     149                 :             :     // interrupting after partial writes from multiple independent reorgs.
     150         [ +  - ]:     4208276 :     batch.Erase(DB_BEST_BLOCK);
     151   [ +  -  +  - ]:     4208276 :     batch.Write(DB_HEAD_BLOCKS, Vector(block_hash, old_tip));
     152                 :             : 
     153         [ +  + ]:     4446591 :     for (auto it{cursor.Begin()}; it != cursor.End();) {
     154         [ +  - ]:      238315 :         if (it->second.IsDirty()) {
     155                 :      238315 :             CoinEntry entry(&it->first);
     156         [ +  + ]:      238315 :             if (it->second.coin.IsSpent()) {
     157         [ +  - ]:       18914 :                 batch.Erase(entry);
     158                 :             :             } else {
     159         [ +  - ]:      219401 :                 batch.Write(entry, it->second.coin);
     160                 :             :             }
     161                 :             :         }
     162                 :      238315 :         count++;
     163                 :      238315 :         it = cursor.NextAndMaybeErase(*it);
     164   [ +  -  -  + ]:      238315 :         if (batch.ApproximateSize() > m_options.batch_write_bytes) {
     165   [ #  #  #  #  :           0 :             LogDebug(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.ApproximateSize() / double(1_MiB));
             #  #  #  # ]
     166                 :             : 
     167         [ #  # ]:           0 :             m_db->WriteBatch(batch);
     168         [ #  # ]:           0 :             batch.Clear();
     169         [ #  # ]:           0 :             if (m_options.simulate_crash_ratio) {
     170   [ #  #  #  # ]:           0 :                 static FastRandomContext rng;
     171         [ #  # ]:           0 :                 if (rng.randrange(m_options.simulate_crash_ratio) == 0) {
     172         [ #  # ]:           0 :                     LogError("Simulating a crash. Goodbye.");
     173                 :           0 :                     _Exit(0);
     174                 :             :                 }
     175                 :             :             }
     176                 :             :         }
     177                 :             :     }
     178                 :             : 
     179                 :             :     // In the last batch, mark the database as consistent with block_hash again.
     180         [ +  - ]:     4208276 :     batch.Erase(DB_HEAD_BLOCKS);
     181         [ +  - ]:     4208276 :     batch.Write(DB_BEST_BLOCK, block_hash);
     182                 :             : 
     183   [ +  -  +  +  :     4208276 :     LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.ApproximateSize() / double(1_MiB));
             +  -  +  - ]
     184         [ +  - ]:     4208276 :     m_db->WriteBatch(batch);
     185   [ +  -  +  +  :     4208276 :     LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...", (unsigned int)dirty_count, (unsigned int)count);
                   +  - ]
     186                 :     4208276 : }
     187                 :             : 
     188                 :      122930 : size_t CCoinsViewDB::EstimateSize() const
     189                 :             : {
     190                 :      122930 :     return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
     191                 :             : }
     192                 :             : 
     193                 :           0 : std::optional<std::string> CCoinsViewDB::GetDBProperty(const std::string& property)
     194                 :             : {
     195                 :           0 :     return m_db->GetProperty(property);
     196                 :             : }
     197                 :             : 
     198                 :          55 : std::shared_future<void> CCoinsViewDB::CompactFull()
     199                 :             : {
     200                 :          55 :     AssertLockHeld(::cs_main);
     201   [ +  +  -  +  :          55 :     if (m_compaction.valid() && m_compaction.wait_for(std::chrono::seconds{0}) != std::future_status::ready) return m_compaction;
                   -  - ]
     202                 :          55 :     m_compaction = std::async(std::launch::async, [this] {
     203                 :          55 :         try {
     204   [ +  -  +  - ]:          55 :             util::ThreadRename("utxocompact");
     205         [ +  - ]:          55 :             LOCK(m_db_mutex);
     206                 :             : 
     207   [ +  -  +  -  :         110 :             LogDebug(BCLog::COINDB, "Starting chainstate compaction of %s", fs::PathToString(m_db_params.path));
             -  +  +  - ]
     208         [ +  - ]:          55 :             m_db->CompactFull();
     209   [ +  -  +  -  :         110 :             LogDebug(BCLog::COINDB, "Finished chainstate compaction of %s", fs::PathToString(m_db_params.path));
          -  +  +  -  +  
                      - ]
     210         [ -  - ]:          55 :         } catch (const std::exception& e) {
     211         [ -  - ]:           0 :             LogWarning("Failed chainstate compaction (%s)", e.what());
     212                 :           0 :         }
     213   [ -  +  -  + ]:         110 :     }).share();
     214         [ +  - ]:          55 :     return m_compaction;
     215                 :             : }
     216                 :             : 
     217                 :             : /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
     218                 :             : class CCoinsViewDBCursor: public CCoinsViewCursor
     219                 :             : {
     220                 :             : public:
     221                 :             :     // Prefer using CCoinsViewDB::Cursor() since we want to perform some
     222                 :             :     // cache warmup on instantiation.
     223                 :      119876 :     CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256& in_block_hash):
     224                 :      119876 :         CCoinsViewCursor(in_block_hash), pcursor(pcursorIn) {}
     225                 :      119876 :     ~CCoinsViewDBCursor() = default;
     226                 :             : 
     227                 :             :     bool GetKey(COutPoint &key) const override;
     228                 :             :     bool GetValue(Coin &coin) const override;
     229                 :             : 
     230                 :             :     bool Valid() const override;
     231                 :             :     void Next() override;
     232                 :             : 
     233                 :             : private:
     234                 :             :     std::unique_ptr<CDBIterator> pcursor;
     235                 :             :     std::pair<char, COutPoint> keyTmp;
     236                 :             : 
     237                 :             :     friend class CCoinsViewDB;
     238                 :             : };
     239                 :             : 
     240                 :      119876 : std::unique_ptr<CCoinsViewCursor> CCoinsViewDB::Cursor() const
     241                 :             : {
     242                 :      119876 :     auto i = std::make_unique<CCoinsViewDBCursor>(
     243                 :      119876 :         const_cast<CDBWrapper&>(*m_db).NewIterator(), GetBestBlock());
     244                 :             :     /* It seems that there are no "const iterators" for LevelDB.  Since we
     245                 :             :        only need read operations on it, use a const-cast to get around
     246                 :             :        that restriction.  */
     247         [ +  - ]:      119876 :     i->pcursor->Seek(DB_COIN);
     248                 :             :     // Cache key of first record
     249   [ +  -  +  + ]:      119876 :     if (i->pcursor->Valid()) {
     250         [ +  - ]:      116202 :         CoinEntry entry(&i->keyTmp.second);
     251         [ +  - ]:      116202 :         i->pcursor->GetKey(entry);
     252                 :      116202 :         i->keyTmp.first = entry.key;
     253                 :             :     } else {
     254                 :        3674 :         i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
     255                 :             :     }
     256                 :      119876 :     return i;
     257                 :      119876 : }
     258                 :             : 
     259                 :     5721440 : bool CCoinsViewDBCursor::GetKey(COutPoint &key) const
     260                 :             : {
     261                 :             :     // Return cached key
     262         [ +  - ]:     5721440 :     if (keyTmp.first == DB_COIN) {
     263                 :     5721440 :         key = keyTmp.second;
     264                 :     5721440 :         return true;
     265                 :             :     }
     266                 :             :     return false;
     267                 :             : }
     268                 :             : 
     269                 :     5721440 : bool CCoinsViewDBCursor::GetValue(Coin &coin) const
     270                 :             : {
     271                 :     5721440 :     return pcursor->GetValue(coin);
     272                 :             : }
     273                 :             : 
     274                 :     5838108 : bool CCoinsViewDBCursor::Valid() const
     275                 :             : {
     276                 :     5838108 :     return keyTmp.first == DB_COIN;
     277                 :             : }
     278                 :             : 
     279                 :     5721440 : void CCoinsViewDBCursor::Next()
     280                 :             : {
     281                 :     5721440 :     pcursor->Next();
     282                 :     5721440 :     CoinEntry entry(&keyTmp.second);
     283   [ +  +  -  + ]:     5721440 :     if (!pcursor->Valid() || !pcursor->GetKey(entry)) {
     284                 :      115095 :         keyTmp.first = 0; // Invalidate cached key after last record so that Valid() and GetKey() return false
     285                 :             :     } else {
     286                 :     5606345 :         keyTmp.first = entry.key;
     287                 :             :     }
     288                 :     5721440 : }
        

Generated by: LCOV version 2.0-1