LCOV - code coverage report
Current view: top level - src/index - blockfilterindex.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 3 3
Test Date: 2026-11-25 06:31:16 Functions: 100.0 % 2 2
Branches: 66.7 % 6 4

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2018-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                 :             : #ifndef BITCOIN_INDEX_BLOCKFILTERINDEX_H
       6                 :             : #define BITCOIN_INDEX_BLOCKFILTERINDEX_H
       7                 :             : 
       8                 :             : #include <attributes.h>
       9                 :             : #include <flatfile.h>
      10                 :             : #include <index/base.h>
      11                 :             : #include <interfaces/chain.h>
      12                 :             : #include <sync.h>
      13                 :             : #include <uint256.h>
      14                 :             : #include <util/hasher.h>
      15                 :             : 
      16                 :             : #include <cstddef>
      17                 :             : #include <cstdint>
      18                 :             : #include <memory>
      19                 :             : #include <optional>
      20                 :             : #include <unordered_map>
      21                 :             : #include <vector>
      22                 :             : 
      23                 :             : class BlockFilter;
      24                 :             : class CBlockIndex;
      25                 :             : enum class BlockFilterType : uint8_t;
      26                 :             : 
      27                 :             : inline constexpr const char* DEFAULT_BLOCKFILTERINDEX{"0"};
      28                 :             : 
      29                 :             : /** Interval between compact filter checkpoints. See BIP 157. */
      30                 :             : inline constexpr int CFCHECKPT_INTERVAL = 1000;
      31                 :             : 
      32                 :             : /**
      33                 :             :  * BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of
      34                 :             :  * blocks by height. An index is constructed for each supported filter type with its own database
      35                 :             :  * (ie. filter data for different types are stored in separate databases).
      36                 :             :  *
      37                 :             :  * This index is used to serve BIP 157 net requests.
      38                 :             :  */
      39                 :             : class BlockFilterIndex final : public BaseIndex
      40                 :             : {
      41                 :             : private:
      42                 :             :     BlockFilterType m_filter_type;
      43                 :             :     std::unique_ptr<BaseIndex::DB> m_db;
      44                 :             : 
      45                 :             :     FlatFilePos m_next_filter_pos;
      46                 :             :     std::unique_ptr<FlatFileSeq> m_filter_fileseq;
      47                 :             : 
      48                 :             :     bool ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const;
      49                 :             :     uint64_t WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter);
      50                 :             : 
      51                 :             :     Mutex m_cs_headers_cache;
      52                 :             :     /** cache of block hash to filter header, to avoid disk access when responding to getcfcheckpt. */
      53                 :             :     std::unordered_map<uint256, uint256, BlockHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache);
      54                 :             : 
      55                 :             :     // Last computed header to avoid disk reads on every new block.
      56                 :             :     uint256 m_last_header{};
      57                 :             : 
      58                 :          32 :     bool AllowPrune() const override { return true; }
      59                 :             : 
      60                 :             :     bool Write(const BlockFilter& filter, uint32_t block_height, const uint256& filter_header);
      61                 :             : 
      62                 :             :     std::optional<uint256> ReadFilterHeader(int height, const uint256& expected_block_hash);
      63                 :             : 
      64                 :             : protected:
      65                 :             :     bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;
      66                 :             : 
      67                 :             :     bool CustomCommit(CDBBatch& batch) override;
      68                 :             : 
      69                 :             :     bool CustomAppend(const interfaces::BlockInfo& block) override;
      70                 :             : 
      71                 :             :     bool CustomRemove(const interfaces::BlockInfo& block) override;
      72                 :             : 
      73                 :          17 :     BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }
      74                 :             : 
      75                 :             : public:
      76                 :             :     /** Constructs the index, which becomes available to be queried. */
      77                 :             :     explicit BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
      78                 :             :                               size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
      79                 :             : 
      80                 :             :     interfaces::Chain::NotifyOptions CustomOptions() override;
      81                 :             : 
      82   [ +  +  +  - ]:        1085 :     BlockFilterType GetFilterType() const { return m_filter_type; }
                 [ -  + ]
      83                 :             : 
      84                 :             :     /** Get a single filter by block. */
      85                 :             :     bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const;
      86                 :             : 
      87                 :             :     /** Get a single filter header by block. */
      88                 :             :     bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache);
      89                 :             : 
      90                 :             :     /** Get a range of filters between two heights on a chain. */
      91                 :             :     bool LookupFilterRange(int start_height, const CBlockIndex* stop_index,
      92                 :             :                            std::vector<BlockFilter>& filters_out) const;
      93                 :             : 
      94                 :             :     /** Get a range of filter hashes between two heights on a chain. */
      95                 :             :     bool LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
      96                 :             :                                std::vector<uint256>& hashes_out) const;
      97                 :             : };
      98                 :             : 
      99                 :             : #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H
        

Generated by: LCOV version 2.0-1