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

          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      361358 :     std::unique_ptr<CEvoDBScopedCommitter> BeginTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs)
      60             :     {
      61      361358 :         LOCK(cs);
      62      361358 :         return std::make_unique<CEvoDBScopedCommitter>(*this);
      63      361358 :     }
      64             : 
      65     1909051 :     CurTransaction& GetCurTransaction() EXCLUSIVE_LOCKS_REQUIRED(cs)
      66             :     {
      67     1909051 :         AssertLockHeld(cs); // lock must be held from outside as long as the DB transaction is used
      68     1909051 :         return curDBTransaction;
      69             :     }
      70             : 
      71             :     template <typename K, typename V>
      72      917090 :     bool Read(const K& key, V& value) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      73             :     {
      74      917090 :         LOCK(cs);
      75      917090 :         return curDBTransaction.Read(key, value);
      76      917090 :     }
      77             : 
      78             :     template <typename K, typename V>
      79      873144 :     void Write(const K& key, const V& value) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      80             :     {
      81      873144 :         LOCK(cs);
      82      873144 :         curDBTransaction.Write(key, value);
      83      873144 :     }
      84             : 
      85             :     template <typename K>
      86       32373 :     bool Exists(const K& key) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      87             :     {
      88       32373 :         LOCK(cs);
      89       32373 :         return curDBTransaction.Exists(key);
      90       32373 :     }
      91             : 
      92             :     template <typename K>
      93         182 :     void Erase(const K& key) EXCLUSIVE_LOCKS_REQUIRED(!cs)
      94             :     {
      95         182 :         LOCK(cs);
      96         182 :         curDBTransaction.Erase(key);
      97         182 :     }
      98             : 
      99       13644 :     CDBWrapper& GetRawDB()
     100             :     {
     101       13644 :         return *db;
     102             :     }
     103             : 
     104      535584 :     [[nodiscard]] size_t GetMemoryUsage() const
     105             :     {
     106      535584 :         return rootDBTransaction.GetMemoryUsage();
     107             :     }
     108             : 
     109             :     bool CommitRootTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs);
     110             : 
     111        1073 :     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