LCOV - code coverage report
Current view: top level - src/index - base.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 7 0.0 %
Date: 2024-01-03 14:57:27 Functions: 0 7 0.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2017-2022 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                 :            : #ifndef BITCOIN_INDEX_BASE_H
       6                 :            : #define BITCOIN_INDEX_BASE_H
       7                 :            : 
       8                 :            : #include <dbwrapper.h>
       9                 :            : #include <interfaces/chain.h>
      10                 :            : #include <util/threadinterrupt.h>
      11                 :            : #include <validationinterface.h>
      12                 :            : 
      13                 :            : #include <string>
      14                 :            : 
      15                 :            : class CBlock;
      16                 :            : class CBlockIndex;
      17                 :            : class Chainstate;
      18                 :            : class ChainstateManager;
      19                 :            : namespace interfaces {
      20                 :            : class Chain;
      21                 :            : } // namespace interfaces
      22                 :            : 
      23                 :          0 : struct IndexSummary {
      24                 :            :     std::string name;
      25                 :            :     bool synced{false};
      26                 :            :     int best_block_height{0};
      27                 :            :     uint256 best_block_hash;
      28                 :            : };
      29                 :            : 
      30                 :            : /**
      31                 :            :  * Base class for indices of blockchain data. This implements
      32                 :            :  * CValidationInterface and ensures blocks are indexed sequentially according
      33                 :            :  * to their position in the active chain.
      34                 :            :  *
      35                 :            :  * In the presence of multiple chainstates (i.e. if a UTXO snapshot is loaded),
      36                 :            :  * only the background "IBD" chainstate will be indexed to avoid building the
      37                 :            :  * index out of order. When the background chainstate completes validation, the
      38                 :            :  * index will be reinitialized and indexing will continue.
      39                 :            :  */
      40                 :            : class BaseIndex : public CValidationInterface
      41                 :            : {
      42                 :            : protected:
      43                 :            :     /**
      44                 :            :      * The database stores a block locator of the chain the database is synced to
      45                 :            :      * so that the index can efficiently determine the point it last stopped at.
      46                 :            :      * A locator is used instead of a simple hash of the chain tip because blocks
      47                 :            :      * and block index entries may not be flushed to disk until after this database
      48                 :            :      * is updated.
      49                 :            :     */
      50                 :          0 :     class DB : public CDBWrapper
      51                 :            :     {
      52                 :            :     public:
      53                 :            :         DB(const fs::path& path, size_t n_cache_size,
      54                 :            :            bool f_memory = false, bool f_wipe = false, bool f_obfuscate = false);
      55                 :            : 
      56                 :            :         /// Read block locator of the chain that the index is in sync with.
      57                 :            :         bool ReadBestBlock(CBlockLocator& locator) const;
      58                 :            : 
      59                 :            :         /// Write block locator of the chain that the index is in sync with.
      60                 :            :         void WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator);
      61                 :            :     };
      62                 :            : 
      63                 :            : private:
      64                 :            :     /// Whether the index has been initialized or not.
      65                 :            :     std::atomic<bool> m_init{false};
      66                 :            :     /// Whether the index is in sync with the main chain. The flag is flipped
      67                 :            :     /// from false to true once, after which point this starts processing
      68                 :            :     /// ValidationInterface notifications to stay in sync.
      69                 :            :     ///
      70                 :            :     /// Note that this will latch to true *immediately* upon startup if
      71                 :            :     /// `m_chainstate->m_chain` is empty, which will be the case upon startup
      72                 :            :     /// with an empty datadir if, e.g., `-txindex=1` is specified.
      73                 :            :     std::atomic<bool> m_synced{false};
      74                 :            : 
      75                 :            :     /// The last block in the chain that the index is in sync with.
      76                 :            :     std::atomic<const CBlockIndex*> m_best_block_index{nullptr};
      77                 :            : 
      78                 :            :     std::thread m_thread_sync;
      79                 :            :     CThreadInterrupt m_interrupt;
      80                 :            : 
      81                 :            :     /// Sync the index with the block index starting from the current best block.
      82                 :            :     /// Intended to be run in its own thread, m_thread_sync, and can be
      83                 :            :     /// interrupted with m_interrupt. Once the index gets in sync, the m_synced
      84                 :            :     /// flag is set and the BlockConnected ValidationInterface callback takes
      85                 :            :     /// over and the sync thread exits.
      86                 :            :     void ThreadSync();
      87                 :            : 
      88                 :            :     /// Write the current index state (eg. chain block locator and subclass-specific items) to disk.
      89                 :            :     ///
      90                 :            :     /// Recommendations for error handling:
      91                 :            :     /// If called on a successor of the previous committed best block in the index, the index can
      92                 :            :     /// continue processing without risk of corruption, though the index state will need to catch up
      93                 :            :     /// from further behind on reboot. If the new state is not a successor of the previous state (due
      94                 :            :     /// to a chain reorganization), the index must halt until Commit succeeds or else it could end up
      95                 :            :     /// getting corrupted.
      96                 :            :     bool Commit();
      97                 :            : 
      98                 :            :     /// Loop over disconnected blocks and call CustomRewind.
      99                 :            :     bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
     100                 :            : 
     101                 :            :     virtual bool AllowPrune() const = 0;
     102                 :            : 
     103                 :            :     template <typename... Args>
     104                 :            :     void FatalErrorf(const char* fmt, const Args&... args);
     105                 :            : 
     106                 :            : protected:
     107                 :            :     std::unique_ptr<interfaces::Chain> m_chain;
     108                 :            :     Chainstate* m_chainstate{nullptr};
     109                 :            :     const std::string m_name;
     110                 :            : 
     111                 :            :     void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override;
     112                 :            : 
     113                 :            :     void ChainStateFlushed(ChainstateRole role, const CBlockLocator& locator) override;
     114                 :            : 
     115                 :            :     /// Initialize internal state from the database and block index.
     116                 :          0 :     [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
     117                 :            : 
     118                 :            :     /// Write update index entries for a newly connected block.
     119                 :          0 :     [[nodiscard]] virtual bool CustomAppend(const interfaces::BlockInfo& block) { return true; }
     120                 :            : 
     121                 :            :     /// Virtual method called internally by Commit that can be overridden to atomically
     122                 :            :     /// commit more index state.
     123                 :          0 :     virtual bool CustomCommit(CDBBatch& batch) { return true; }
     124                 :            : 
     125                 :            :     /// Rewind index to an earlier chain tip during a chain reorg. The tip must
     126                 :            :     /// be an ancestor of the current best block.
     127                 :          0 :     [[nodiscard]] virtual bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) { return true; }
     128                 :            : 
     129                 :            :     virtual DB& GetDB() const = 0;
     130                 :            : 
     131                 :            :     /// Update the internal best block index as well as the prune lock.
     132                 :            :     void SetBestBlockIndex(const CBlockIndex* block);
     133                 :            : 
     134                 :            : public:
     135                 :            :     BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name);
     136                 :            :     /// Destructor interrupts sync thread if running and blocks until it exits.
     137                 :            :     virtual ~BaseIndex();
     138                 :            : 
     139                 :            :     /// Get the name of the index for display in logs.
     140                 :          0 :     const std::string& GetName() const LIFETIMEBOUND { return m_name; }
     141                 :            : 
     142                 :            :     /// Blocks the current thread until the index is caught up to the current
     143                 :            :     /// state of the block chain. This only blocks if the index has gotten in
     144                 :            :     /// sync once and only needs to process blocks in the ValidationInterface
     145                 :            :     /// queue. If the index is catching up from far behind, this method does
     146                 :            :     /// not block and immediately returns false.
     147                 :            :     bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
     148                 :            : 
     149                 :            :     void Interrupt();
     150                 :            : 
     151                 :            :     /// Initializes the sync state and registers the instance to the
     152                 :            :     /// validation interface so that it stays in sync with blockchain updates.
     153                 :            :     [[nodiscard]] bool Init();
     154                 :            : 
     155                 :            :     /// Starts the initial sync process.
     156                 :            :     [[nodiscard]] bool StartBackgroundSync();
     157                 :            : 
     158                 :            :     /// Stops the instance from staying in sync with blockchain updates.
     159                 :            :     void Stop();
     160                 :            : 
     161                 :            :     /// Get a summary of the index and its state.
     162                 :            :     IndexSummary GetSummary() const;
     163                 :            : };
     164                 :            : 
     165                 :            : #endif // BITCOIN_INDEX_BASE_H

Generated by: LCOV version 1.14