LCOV - code coverage report
Current view: top level - src - dbwrapper.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 75.8 % 194 147
Test Date: 2026-02-10 04:41:42 Functions: 96.7 % 30 29
Branches: 37.9 % 256 97

             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 <dbwrapper.h>
       6                 :             : 
       7                 :             : #include <leveldb/cache.h>
       8                 :             : #include <leveldb/db.h>
       9                 :             : #include <leveldb/env.h>
      10                 :             : #include <leveldb/filter_policy.h>
      11                 :             : #include <leveldb/helpers/memenv/memenv.h>
      12                 :             : #include <leveldb/iterator.h>
      13                 :             : #include <leveldb/options.h>
      14                 :             : #include <leveldb/slice.h>
      15                 :             : #include <leveldb/status.h>
      16                 :             : #include <leveldb/write_batch.h>
      17                 :             : #include <logging.h>
      18                 :             : #include <random.h>
      19                 :             : #include <serialize.h>
      20                 :             : #include <span.h>
      21                 :             : #include <streams.h>
      22                 :             : #include <util/fs.h>
      23                 :             : #include <util/fs_helpers.h>
      24                 :             : #include <util/log.h>
      25                 :             : #include <util/obfuscation.h>
      26                 :             : #include <util/strencodings.h>
      27                 :             : 
      28                 :             : #include <algorithm>
      29                 :             : #include <cassert>
      30                 :             : #include <cstdarg>
      31                 :             : #include <cstdint>
      32                 :             : #include <cstdio>
      33                 :             : #include <memory>
      34                 :             : #include <optional>
      35                 :             : #include <utility>
      36                 :             : 
      37                 :     5044754 : static auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
      38                 :             : 
      39                 :          13 : bool DestroyDB(const std::string& path_str)
      40                 :             : {
      41         [ -  + ]:          13 :     return leveldb::DestroyDB(path_str, {}).ok();
      42                 :             : }
      43                 :             : 
      44                 :             : /** Handle database error by throwing dbwrapper_error exception.
      45                 :             :  */
      46                 :        1899 : static void HandleError(const leveldb::Status& status)
      47                 :             : {
      48         [ +  - ]:        1899 :     if (status.ok())
      49                 :        1899 :         return;
      50         [ #  # ]:           0 :     const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
      51         [ #  # ]:           0 :     LogError("%s", errmsg);
      52         [ #  # ]:           0 :     LogInfo("You can use -debug=leveldb to get more complete diagnostic messages");
      53         [ #  # ]:           0 :     throw dbwrapper_error(errmsg);
      54                 :           0 : }
      55                 :             : 
      56                 :         489 : class CBitcoinLevelDBLogger : public leveldb::Logger {
      57                 :             : public:
      58                 :             :     // This code is adapted from posix_logger.h, which is why it is using vsprintf.
      59                 :             :     // Please do not do this in normal code
      60                 :         841 :     void Logv(const char * format, va_list ap) override {
      61         [ -  + ]:         841 :             if (!LogAcceptCategory(BCLog::LEVELDB, util::log::Level::Debug)) {
      62                 :             :                 return;
      63                 :             :             }
      64                 :             :             char buffer[500];
      65         [ #  # ]:           0 :             for (int iter = 0; iter < 2; iter++) {
      66                 :           0 :                 char* base;
      67                 :           0 :                 int bufsize;
      68         [ #  # ]:           0 :                 if (iter == 0) {
      69                 :             :                     bufsize = sizeof(buffer);
      70                 :             :                     base = buffer;
      71                 :             :                 }
      72                 :             :                 else {
      73                 :           0 :                     bufsize = 30000;
      74                 :           0 :                     base = new char[bufsize];
      75                 :             :                 }
      76                 :           0 :                 char* p = base;
      77                 :           0 :                 char* limit = base + bufsize;
      78                 :             : 
      79                 :             :                 // Print the message
      80         [ #  # ]:           0 :                 if (p < limit) {
      81                 :           0 :                     va_list backup_ap;
      82                 :           0 :                     va_copy(backup_ap, ap);
      83                 :             :                     // Do not use vsnprintf elsewhere in bitcoin source code, see above.
      84                 :           0 :                     p += vsnprintf(p, limit - p, format, backup_ap);
      85                 :           0 :                     va_end(backup_ap);
      86                 :             :                 }
      87                 :             : 
      88                 :             :                 // Truncate to available space if necessary
      89         [ #  # ]:           0 :                 if (p >= limit) {
      90         [ #  # ]:           0 :                     if (iter == 0) {
      91                 :           0 :                         continue;       // Try again with larger buffer
      92                 :             :                     }
      93                 :             :                     else {
      94                 :           0 :                         p = limit - 1;
      95                 :             :                     }
      96                 :             :                 }
      97                 :             : 
      98                 :             :                 // Add newline if necessary
      99   [ #  #  #  # ]:           0 :                 if (p == base || p[-1] != '\n') {
     100                 :           0 :                     *p++ = '\n';
     101                 :             :                 }
     102                 :             : 
     103         [ #  # ]:           0 :                 assert(p <= limit);
     104         [ #  # ]:           0 :                 base[std::min(bufsize - 1, (int)(p - base))] = '\0';
     105         [ #  # ]:           0 :                 LogDebug(BCLog::LEVELDB, "%s\n", util::RemoveSuffixView(base, "\n"));
     106         [ #  # ]:           0 :                 if (base != buffer) {
     107         [ #  # ]:           0 :                     delete[] base;
     108                 :             :                 }
     109                 :             :                 break;
     110                 :             :             }
     111                 :             :     }
     112                 :             : };
     113                 :             : 
     114                 :         489 : static void SetMaxOpenFiles(leveldb::Options *options) {
     115                 :             :     // On most platforms the default setting of max_open_files (which is 1000)
     116                 :             :     // is optimal. On Windows using a large file count is OK because the handles
     117                 :             :     // do not interfere with select() loops. On 64-bit Unix hosts this value is
     118                 :             :     // also OK, because up to that amount LevelDB will use an mmap
     119                 :             :     // implementation that does not use extra file descriptors (the fds are
     120                 :             :     // closed after being mmap'ed).
     121                 :             :     //
     122                 :             :     // Increasing the value beyond the default is dangerous because LevelDB will
     123                 :             :     // fall back to a non-mmap implementation when the file count is too large.
     124                 :             :     // On 32-bit Unix host we should decrease the value because the handles use
     125                 :             :     // up real fds, and we want to avoid fd exhaustion issues.
     126                 :             :     //
     127                 :             :     // See PR #12495 for further discussion.
     128                 :             : 
     129                 :         489 :     int default_open_files = options->max_open_files;
     130                 :             : #ifndef WIN32
     131                 :         489 :     if (sizeof(void*) < 8) {
     132                 :             :         options->max_open_files = 64;
     133                 :             :     }
     134                 :             : #endif
     135         [ -  + ]:         489 :     LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
     136                 :             :              options->max_open_files, default_open_files);
     137                 :         489 : }
     138                 :             : 
     139                 :         489 : static leveldb::Options GetOptions(size_t nCacheSize)
     140                 :             : {
     141                 :         489 :     leveldb::Options options;
     142                 :         489 :     options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
     143                 :         489 :     options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
     144                 :         489 :     options.filter_policy = leveldb::NewBloomFilterPolicy(10);
     145                 :         489 :     options.compression = leveldb::kNoCompression;
     146         [ -  + ]:         489 :     options.info_log = new CBitcoinLevelDBLogger();
     147                 :         489 :     if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
     148                 :             :         // LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
     149                 :             :         // on corruption in later versions.
     150                 :         489 :         options.paranoid_checks = true;
     151                 :             :     }
     152         [ -  + ]:         489 :     options.max_file_size = std::max(options.max_file_size, DBWRAPPER_MAX_FILE_SIZE);
     153                 :         489 :     SetMaxOpenFiles(&options);
     154                 :         489 :     return options;
     155                 :             : }
     156                 :             : 
     157         [ +  - ]:        2804 : struct CDBBatch::WriteBatchImpl {
     158                 :             :     leveldb::WriteBatch batch;
     159                 :             : };
     160                 :             : 
     161                 :        1402 : CDBBatch::CDBBatch(const CDBWrapper& _parent)
     162                 :        1402 :     : parent{_parent},
     163                 :        1402 :       m_impl_batch{std::make_unique<CDBBatch::WriteBatchImpl>()}
     164                 :             : {
     165         [ +  - ]:        1402 :     Clear();
     166                 :        1402 : };
     167                 :             : 
     168                 :        1402 : CDBBatch::~CDBBatch() = default;
     169                 :             : 
     170                 :        1402 : void CDBBatch::Clear()
     171                 :             : {
     172                 :        1402 :     m_impl_batch->batch.Clear();
     173                 :        1402 : }
     174                 :             : 
     175                 :       85747 : void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& ssValue)
     176                 :             : {
     177                 :       85747 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     178         [ -  + ]:       85747 :     dbwrapper_private::GetObfuscation(parent)(ssValue);
     179         [ -  + ]:       85747 :     leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
     180                 :       85747 :     m_impl_batch->batch.Put(slKey, slValue);
     181                 :       85747 : }
     182                 :             : 
     183                 :       15353 : void CDBBatch::EraseImpl(std::span<const std::byte> key)
     184                 :             : {
     185                 :       15353 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     186                 :       15353 :     m_impl_batch->batch.Delete(slKey);
     187                 :       15353 : }
     188                 :             : 
     189                 :       96778 : size_t CDBBatch::ApproximateSize() const
     190                 :             : {
     191                 :       96778 :     return m_impl_batch->batch.ApproximateSize();
     192                 :             : }
     193                 :             : 
     194                 :             : struct LevelDBContext {
     195                 :             :     //! custom environment this database is using (may be nullptr in case of default environment)
     196                 :             :     leveldb::Env* penv;
     197                 :             : 
     198                 :             :     //! database options used
     199                 :             :     leveldb::Options options;
     200                 :             : 
     201                 :             :     //! options used when reading from the database
     202                 :             :     leveldb::ReadOptions readoptions;
     203                 :             : 
     204                 :             :     //! options used when iterating over values of the database
     205                 :             :     leveldb::ReadOptions iteroptions;
     206                 :             : 
     207                 :             :     //! options used when writing to the database
     208                 :             :     leveldb::WriteOptions writeoptions;
     209                 :             : 
     210                 :             :     //! options used when sync writing to the database
     211                 :             :     leveldb::WriteOptions syncoptions;
     212                 :             : 
     213                 :             :     //! the database itself
     214                 :             :     leveldb::DB* pdb;
     215                 :             : };
     216                 :             : 
     217                 :         489 : CDBWrapper::CDBWrapper(const DBParams& params)
     218   [ +  -  -  + ]:        2445 :     : m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}
     219                 :             : {
     220         [ +  - ]:         489 :     DBContext().penv = nullptr;
     221         [ +  - ]:         489 :     DBContext().readoptions.verify_checksums = true;
     222         [ +  - ]:         489 :     DBContext().iteroptions.verify_checksums = true;
     223         [ +  - ]:         489 :     DBContext().iteroptions.fill_cache = false;
     224         [ +  - ]:         489 :     DBContext().syncoptions.sync = true;
     225   [ +  -  +  - ]:         489 :     DBContext().options = GetOptions(params.cache_bytes);
     226         [ +  - ]:         489 :     DBContext().options.create_if_missing = true;
     227         [ +  + ]:         489 :     if (params.memory_only) {
     228   [ +  -  +  -  :         374 :         DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
                   +  - ]
     229   [ +  -  +  - ]:         374 :         DBContext().options.env = DBContext().penv;
     230                 :             :     } else {
     231         [ +  + ]:         115 :         if (params.wipe_data) {
     232   [ -  +  +  - ]:          16 :             LogInfo("Wiping LevelDB in %s", fs::PathToString(params.path));
     233   [ +  -  -  +  :          16 :             leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
                   +  - ]
     234         [ +  - ]:           8 :             HandleError(result);
     235                 :           8 :         }
     236         [ +  - ]:         115 :         TryCreateDirectories(params.path);
     237   [ -  +  +  - ]:         345 :         LogInfo("Opening LevelDB in %s", fs::PathToString(params.path));
     238                 :             :     }
     239                 :             :     // PathToString() return value is safe to pass to leveldb open function,
     240                 :             :     // because on POSIX leveldb passes the byte string directly to ::open(), and
     241                 :             :     // on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
     242                 :             :     // (see env_posix.cc and env_windows.cc).
     243   [ +  -  -  +  :         978 :     leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
             +  -  +  - ]
     244         [ +  - ]:         489 :     HandleError(status);
     245         [ +  - ]:         489 :     LogInfo("Opened LevelDB successfully");
     246                 :             : 
     247         [ -  + ]:         489 :     if (params.options.force_compact) {
     248   [ #  #  #  # ]:           0 :         LogInfo("Starting database compaction of %s", fs::PathToString(params.path));
     249   [ #  #  #  # ]:           0 :         DBContext().pdb->CompactRange(nullptr, nullptr);
     250   [ #  #  #  # ]:           0 :         LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
     251                 :             :     }
     252                 :             : 
     253   [ +  -  +  +  :         489 :     if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
          +  +  +  -  +  
                      + ]
     254                 :             :         // Generate and write the new obfuscation key.
     255                 :         211 :         const Obfuscation obfuscation{FastRandomContext{}.randbytes<Obfuscation::KEY_SIZE>()};
     256         [ -  + ]:         211 :         assert(!m_obfuscation); // Make sure the key is written without obfuscation.
     257         [ +  - ]:         211 :         Write(OBFUSCATION_KEY, obfuscation);
     258                 :         211 :         m_obfuscation = obfuscation;
     259   [ +  -  -  +  :         633 :         LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
                   +  - ]
     260                 :             :     }
     261   [ +  -  -  +  :        1467 :     LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
             +  -  -  + ]
     262                 :         489 : }
     263                 :             : 
     264                 :         489 : CDBWrapper::~CDBWrapper()
     265                 :             : {
     266         [ +  - ]:         489 :     delete DBContext().pdb;
     267                 :         489 :     DBContext().pdb = nullptr;
     268         [ +  - ]:         489 :     delete DBContext().options.filter_policy;
     269                 :         489 :     DBContext().options.filter_policy = nullptr;
     270         [ +  - ]:         489 :     delete DBContext().options.info_log;
     271                 :         489 :     DBContext().options.info_log = nullptr;
     272         [ +  - ]:         489 :     delete DBContext().options.block_cache;
     273                 :         489 :     DBContext().options.block_cache = nullptr;
     274         [ +  + ]:         489 :     delete DBContext().penv;
     275                 :         489 :     DBContext().options.env = nullptr;
     276                 :         489 : }
     277                 :             : 
     278                 :        1402 : void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
     279                 :             : {
     280                 :        1402 :     const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, util::log::Level::Debug);
     281                 :        1402 :     double mem_before = 0;
     282         [ -  + ]:        1402 :     if (log_memory) {
     283                 :           0 :         mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
     284                 :             :     }
     285         [ +  + ]:        1402 :     leveldb::Status status = DBContext().pdb->Write(fSync ? DBContext().syncoptions : DBContext().writeoptions, &batch.m_impl_batch->batch);
     286         [ +  - ]:        1402 :     HandleError(status);
     287         [ -  + ]:        1402 :     if (log_memory) {
     288         [ #  # ]:           0 :         double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;
     289   [ #  #  #  #  :           0 :         LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
                   #  # ]
     290                 :             :                  m_name, mem_before, mem_after);
     291                 :             :     }
     292                 :        1402 : }
     293                 :             : 
     294                 :           0 : size_t CDBWrapper::DynamicMemoryUsage() const
     295                 :             : {
     296         [ #  # ]:           0 :     std::string memory;
     297                 :           0 :     std::optional<size_t> parsed;
     298   [ #  #  #  #  :           0 :     if (!DBContext().pdb->GetProperty("leveldb.approximate-memory-usage", &memory) || !(parsed = ToIntegral<size_t>(memory))) {
             #  #  #  # ]
     299   [ #  #  #  #  :           0 :         LogDebug(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
                   #  # ]
     300                 :           0 :         return 0;
     301                 :             :     }
     302                 :           0 :     return parsed.value();
     303                 :           0 : }
     304                 :             : 
     305                 :     4856663 : std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key) const
     306                 :             : {
     307         [ +  - ]:     4856663 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     308         [ +  - ]:     4856663 :     std::string strValue;
     309   [ +  -  +  -  :     4856663 :     leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
                   +  - ]
     310         [ +  + ]:     4856663 :     if (!status.ok()) {
     311         [ +  - ]:     4767284 :         if (status.IsNotFound())
     312                 :     4767284 :             return std::nullopt;
     313   [ #  #  #  # ]:           0 :         LogError("LevelDB read failure: %s", status.ToString());
     314         [ #  # ]:           0 :         HandleError(status);
     315                 :             :     }
     316                 :       89379 :     return strValue;
     317                 :     4856663 : }
     318                 :             : 
     319                 :         241 : bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
     320                 :             : {
     321         [ +  - ]:         241 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     322                 :             : 
     323         [ +  - ]:         241 :     std::string strValue;
     324   [ +  -  +  -  :         241 :     leveldb::Status status = DBContext().pdb->Get(DBContext().readoptions, slKey, &strValue);
                   +  - ]
     325         [ +  + ]:         241 :     if (!status.ok()) {
     326         [ -  + ]:         229 :         if (status.IsNotFound())
     327                 :             :             return false;
     328   [ #  #  #  # ]:           0 :         LogError("LevelDB read failure: %s", status.ToString());
     329         [ #  # ]:           0 :         HandleError(status);
     330                 :             :     }
     331                 :             :     return true;
     332                 :         241 : }
     333                 :             : 
     334                 :          46 : size_t CDBWrapper::EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const
     335                 :             : {
     336                 :          46 :     leveldb::Slice slKey1(CharCast(key1.data()), key1.size());
     337                 :          46 :     leveldb::Slice slKey2(CharCast(key2.data()), key2.size());
     338                 :          46 :     uint64_t size = 0;
     339                 :          46 :     leveldb::Range range(slKey1, slKey2);
     340                 :          46 :     DBContext().pdb->GetApproximateSizes(&range, 1, &size);
     341                 :          46 :     return size;
     342                 :             : }
     343                 :             : 
     344                 :         215 : bool CDBWrapper::IsEmpty()
     345                 :             : {
     346         [ +  - ]:         215 :     std::unique_ptr<CDBIterator> it(NewIterator());
     347         [ +  - ]:         215 :     it->SeekToFirst();
     348         [ +  - ]:         430 :     return !(it->Valid());
     349                 :         215 : }
     350                 :             : 
     351                 :        1124 : struct CDBIterator::IteratorImpl {
     352                 :             :     const std::unique_ptr<leveldb::Iterator> iter;
     353                 :             : 
     354                 :        1124 :     explicit IteratorImpl(leveldb::Iterator* _iter) : iter{_iter} {}
     355                 :             : };
     356                 :             : 
     357                 :        1124 : CDBIterator::CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter) : parent(_parent),
     358                 :        1124 :                                                                                             m_impl_iter(std::move(_piter)) {}
     359                 :             : 
     360                 :        1124 : CDBIterator* CDBWrapper::NewIterator()
     361                 :             : {
     362   [ +  -  +  -  :        1124 :     return new CDBIterator{*this, std::make_unique<CDBIterator::IteratorImpl>(DBContext().pdb->NewIterator(DBContext().iteroptions))};
          +  -  +  -  +  
                      - ]
     363                 :             : }
     364                 :             : 
     365                 :         911 : void CDBIterator::SeekImpl(std::span<const std::byte> key)
     366                 :             : {
     367                 :         911 :     leveldb::Slice slKey(CharCast(key.data()), key.size());
     368                 :         911 :     m_impl_iter->iter->Seek(slKey);
     369                 :         911 : }
     370                 :             : 
     371                 :       11657 : std::span<const std::byte> CDBIterator::GetKeyImpl() const
     372                 :             : {
     373                 :       11657 :     return MakeByteSpan(m_impl_iter->iter->key());
     374                 :             : }
     375                 :             : 
     376                 :       11458 : std::span<const std::byte> CDBIterator::GetValueImpl() const
     377                 :             : {
     378                 :       11458 :     return MakeByteSpan(m_impl_iter->iter->value());
     379                 :             : }
     380                 :             : 
     381                 :        1124 : CDBIterator::~CDBIterator() = default;
     382                 :       13062 : bool CDBIterator::Valid() const { return m_impl_iter->iter->Valid(); }
     383                 :         215 : void CDBIterator::SeekToFirst() { m_impl_iter->iter->SeekToFirst(); }
     384                 :       11453 : void CDBIterator::Next() { m_impl_iter->iter->Next(); }
     385                 :             : 
     386                 :             : namespace dbwrapper_private {
     387                 :             : 
     388                 :       97219 : const Obfuscation& GetObfuscation(const CDBWrapper& w)
     389                 :             : {
     390                 :       97219 :     return w.m_obfuscation;
     391                 :             : }
     392                 :             : 
     393                 :             : } // namespace dbwrapper_private
        

Generated by: LCOV version 2.0-1