LCOV - code coverage report
Current view: top level - src/index - blockfilterindex.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 4 0.0 %
Date: 2023-11-06 23:13:05 Functions: 0 5 0.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2018-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_BLOCKFILTERINDEX_H
       6                 :            : #define BITCOIN_INDEX_BLOCKFILTERINDEX_H
       7                 :            : 
       8                 :            : #include <attributes.h>
       9                 :            : #include <blockfilter.h>
      10                 :            : #include <chain.h>
      11                 :            : #include <flatfile.h>
      12                 :            : #include <index/base.h>
      13                 :            : #include <util/hasher.h>
      14                 :            : 
      15                 :            : #include <unordered_map>
      16                 :            : 
      17                 :            : static const char* const DEFAULT_BLOCKFILTERINDEX = "0";
      18                 :            : 
      19                 :            : /** Interval between compact filter checkpoints. See BIP 157. */
      20                 :            : static constexpr int CFCHECKPT_INTERVAL = 1000;
      21                 :            : 
      22                 :            : /**
      23                 :            :  * BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of
      24                 :            :  * blocks by height. An index is constructed for each supported filter type with its own database
      25                 :            :  * (ie. filter data for different types are stored in separate databases).
      26                 :            :  *
      27                 :            :  * This index is used to serve BIP 157 net requests.
      28                 :            :  */
      29                 :          0 : class BlockFilterIndex final : public BaseIndex
      30                 :            : {
      31                 :            : private:
      32                 :            :     BlockFilterType m_filter_type;
      33                 :            :     std::unique_ptr<BaseIndex::DB> m_db;
      34                 :            : 
      35                 :            :     FlatFilePos m_next_filter_pos;
      36                 :            :     std::unique_ptr<FlatFileSeq> m_filter_fileseq;
      37                 :            : 
      38                 :            :     bool ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const;
      39                 :            :     size_t WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter);
      40                 :            : 
      41                 :            :     Mutex m_cs_headers_cache;
      42                 :            :     /** cache of block hash to filter header, to avoid disk access when responding to getcfcheckpt. */
      43                 :            :     std::unordered_map<uint256, uint256, FilterHeaderHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache);
      44                 :            : 
      45                 :          0 :     bool AllowPrune() const override { return true; }
      46                 :            : 
      47                 :            : protected:
      48                 :            :     bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
      49                 :            : 
      50                 :            :     bool CustomCommit(CDBBatch& batch) override;
      51                 :            : 
      52                 :            :     bool CustomAppend(const interfaces::BlockInfo& block) override;
      53                 :            : 
      54                 :            :     bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
      55                 :            : 
      56                 :          0 :     BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }
      57                 :            : 
      58                 :            : public:
      59                 :            :     /** Constructs the index, which becomes available to be queried. */
      60                 :            :     explicit BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
      61                 :            :                               size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
      62                 :            : 
      63                 :          0 :     BlockFilterType GetFilterType() const { return m_filter_type; }
      64                 :            : 
      65                 :            :     /** Get a single filter by block. */
      66                 :            :     bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const;
      67                 :            : 
      68                 :            :     /** Get a single filter header by block. */
      69                 :            :     bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache);
      70                 :            : 
      71                 :            :     /** Get a range of filters between two heights on a chain. */
      72                 :            :     bool LookupFilterRange(int start_height, const CBlockIndex* stop_index,
      73                 :            :                            std::vector<BlockFilter>& filters_out) const;
      74                 :            : 
      75                 :            :     /** Get a range of filter hashes between two heights on a chain. */
      76                 :            :     bool LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
      77                 :            :                                std::vector<uint256>& hashes_out) const;
      78                 :            : };
      79                 :            : 
      80                 :            : /**
      81                 :            :  * Get a block filter index by type. Returns nullptr if index has not been initialized or was
      82                 :            :  * already destroyed.
      83                 :            :  */
      84                 :            : BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type);
      85                 :            : 
      86                 :            : /** Iterate over all running block filter indexes, invoking fn on each. */
      87                 :            : void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn);
      88                 :            : 
      89                 :            : /**
      90                 :            :  * Initialize a block filter index for the given type if one does not already exist. Returns true if
      91                 :            :  * a new index is created and false if one has already been initialized.
      92                 :            :  */
      93                 :            : bool InitBlockFilterIndex(std::function<std::unique_ptr<interfaces::Chain>()> make_chain, BlockFilterType filter_type,
      94                 :            :                           size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
      95                 :            : 
      96                 :            : /**
      97                 :            :  * Destroy the block filter index with the given type. Returns false if no such index exists. This
      98                 :            :  * just releases the allocated memory and closes the database connection, it does not delete the
      99                 :            :  * index data.
     100                 :            :  */
     101                 :            : bool DestroyBlockFilterIndex(BlockFilterType filter_type);
     102                 :            : 
     103                 :            : /** Destroy all open block filter indexes. */
     104                 :            : void DestroyAllBlockFilterIndexes();
     105                 :            : 
     106                 :            : #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H

Generated by: LCOV version 1.14