Bitcoin Core  26.1.0
P2P Digital Currency
blockfilterindex.h
Go to the documentation of this file.
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 
20 static constexpr int CFCHECKPT_INTERVAL = 1000;
21 
29 class BlockFilterIndex final : public BaseIndex
30 {
31 private:
33  std::unique_ptr<BaseIndex::DB> m_db;
34 
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 
43  std::unordered_map<uint256, uint256, FilterHeaderHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache);
44 
45  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  BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }
57 
58 public:
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 
64 
66  bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const;
67 
70 
72  bool LookupFilterRange(int start_height, const CBlockIndex* stop_index,
73  std::vector<BlockFilter>& filters_out) const;
74 
76  bool LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
77  std::vector<uint256>& hashes_out) const;
78 };
79 
85 
87 void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn);
88 
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 
101 bool DestroyBlockFilterIndex(BlockFilterType filter_type);
102 
105 
106 #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:72
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
static const char *const DEFAULT_BLOCKFILTERINDEX
bool AllowPrune() const override
std::unique_ptr< BaseIndex::DB > m_db
The database stores a block locator of the chain the database is synced to so that the index can effi...
Definition: base.h:50
BlockFilterType
Definition: blockfilter.h:92
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:83
Base class for indices of blockchain data.
Definition: base.h:40
#define LIFETIMEBOUND
Definition: attributes.h:16
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
bool CustomInit(const std::optional< interfaces::BlockKey > &block) override
Initialize internal state from the database and block index.
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:114
BaseIndex::DB & GetDB() const LIFETIMEBOUND override
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
BlockFilterIndex(std::unique_ptr< interfaces::Chain > chain, BlockFilterType filter_type, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Constructs the index, which becomes available to be queried.
bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, std::vector< uint256 > &hashes_out) const
Get a range of filter hashes between two heights on a chain.
bool CustomRewind(const interfaces::BlockKey &current_tip, const interfaces::BlockKey &new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
bool InitBlockFilterIndex(std::function< std::unique_ptr< interfaces::Chain >()> make_chain, BlockFilterType filter_type, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
Initialize a block filter index for the given type if one does not already exist. ...
BlockFilterType m_filter_type
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
256-bit opaque blob.
Definition: uint256.h:106
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:144
FlatFilePos m_next_filter_pos
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
BlockFilterType GetFilterType() const
Hash/height pair to help track and identify blocks.
Definition: chain.h:44
bool CustomCommit(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
std::unique_ptr< FlatFileSeq > m_filter_fileseq
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
bool ReadFilterFromDisk(const FlatFilePos &pos, const uint256 &hash, BlockFilter &filter) const
bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, std::vector< BlockFilter > &filters_out) const
Get a range of filters between two heights on a chain.
std::unordered_map< uint256, uint256, FilterHeaderHasher > m_headers_cache GUARDED_BY(m_cs_headers_cache)
cache of block hash to filter header, to avoid disk access when responding to getcfcheckpt.