LCOV - code coverage report
Current view: top level - src/evo - evodb.h (source / functions) Hit Total Coverage
Test: test_dash_coverage.info Lines: 24 28 85.7 %
Date: 2026-06-25 07:23:51 Functions: 16 26 61.5 %

          Line data    Source code
       1             : // Copyright (c) 2018-2025 The Dash 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             : #ifndef BITCOIN_EVO_EVODB_H
       6             : #define BITCOIN_EVO_EVODB_H
       7             : 
       8             : #include <dbwrapper.h>
       9             : #include <sync.h>
      10             : 
      11             : class uint256;
      12             : namespace util {
      13             : struct DbWrapperParams;
      14             : } // namespace util
      15             : 
      16             : // "b_b" was used in the initial version of deterministic MN storage
      17             : // "b_b2" was used after compact diffs were introduced
      18             : // "b_b3" was used after masternode type introduction in evoDB
      19             : // "b_b4" was used after storing protx version for each masternode in evoDB
      20             : static const std::string EVODB_BEST_BLOCK = "b_b4";
      21             : 
      22             : class CEvoDB;
      23             : 
      24             : class CEvoDBScopedCommitter
      25             : {
      26             : private:
      27             :     CEvoDB& evoDB;
      28             :     bool didCommitOrRollback{false};
      29             : 
      30             : public:
      31             :     explicit CEvoDBScopedCommitter(CEvoDB& _evoDB);
      32             :     ~CEvoDBScopedCommitter();
      33             : 
      34             :     void Commit();
      35             :     void Rollback();
      36             : };
      37             : 
      38             : class CEvoDB
      39             : {
      40             : public:
      41             :     Mutex cs;
      42             : private:
      43             :     std::unique_ptr<CDBWrapper> db;
      44             : 
      45             :     using RootTransaction = CDBTransaction<CDBWrapper, CDBBatch>;
      46             :     using CurTransaction = CDBTransaction<RootTransaction, RootTransaction>;
      47             : 
      48             :     CDBBatch rootBatch;
      49             :     RootTransaction rootDBTransaction;
      50             :     CurTransaction curDBTransaction;
      51             : 
      52             : public:
      53             :     CEvoDB() = delete;
      54             :     CEvoDB(const CEvoDB&) = delete;
      55             :     CEvoDB& operator=(const CEvoDB&) = delete;
      56             :     explicit CEvoDB(const util::DbWrapperParams& db_params);
      57             :     ~CEvoDB();
      58             : 
      59       49759 :     std::unique_ptr<CEvoDBScopedCommitter> BeginTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs)
      60             :     {
      61       49759 :         LOCK(cs);
      62       49759 :         return std::make_unique<CEvoDBScopedCommitter>(*this);
      63       49759 :     }
      64             : 
      65      218820 :     CurTransaction& GetCurTransaction() EXCLUSIVE_LOCKS_REQUIRED(cs)
      66             :     {
      67      218820 :         AssertLockHeld(cs); // lock must be held from outside as long as the DB transaction is used
      68      218820 :         return curDBTransaction;
      69             :     }
      70             : 
      71             :     template <typename K, typename V>
      72       19525 :     bool Read(const K& key, V& value) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      73             :     {
      74       19525 :         LOCK(cs);
      75       19525 :         return curDBTransaction.Read(key, value);
      76       19525 :     }
      77             : 
      78             :     template <typename K, typename V>
      79       90979 :     void Write(const K& key, const V& value) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      80             :     {
      81       90979 :         LOCK(cs);
      82       90979 :         curDBTransaction.Write(key, value);
      83       90979 :     }
      84             : 
      85             :     template <typename K>
      86        1613 :     bool Exists(const K& key) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      87             :     {
      88        1613 :         LOCK(cs);
      89        1613 :         return curDBTransaction.Exists(key);
      90        1613 :     }
      91             : 
      92             :     template <typename K>
      93           0 :     void Erase(const K& key) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      94             :     {
      95           0 :         LOCK(cs);
      96           0 :         curDBTransaction.Erase(key);
      97           0 :     }
      98             : 
      99         702 :     CDBWrapper& GetRawDB()
     100             :     {
     101         702 :         return *db;
     102             :     }
     103             : 
     104       50128 :     [[nodiscard]] size_t GetMemoryUsage() const
     105             :     {
     106       50128 :         return rootDBTransaction.GetMemoryUsage();
     107             :     }
     108             : 
     109             :     bool CommitRootTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs);
     110             : 
     111         178 :     bool IsEmpty() { return db->IsEmpty(); }
     112             : 
     113             :     bool VerifyBestBlock(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(!cs);
     114             :     void WriteBestBlock(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(!cs);
     115             : 
     116             : private:
     117             :     // only CEvoDBScopedCommitter is allowed to invoke these
     118             :     friend class CEvoDBScopedCommitter;
     119             :     void CommitCurTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs);
     120             :     void RollbackCurTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs);
     121             : };
     122             : 
     123             : #endif // BITCOIN_EVO_EVODB_H

Generated by: LCOV version 1.16