Bitcoin Core  31.0.0
P2P Digital Currency
blockfilterindex.h
Go to the documentation of this file.
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 <functional>
19 #include <memory>
20 #include <optional>
21 #include <unordered_map>
22 #include <vector>
23 
24 class BlockFilter;
25 class CBlockIndex;
26 enum class BlockFilterType : uint8_t;
27 
28 static const char* const DEFAULT_BLOCKFILTERINDEX = "0";
29 
31 static constexpr int CFCHECKPT_INTERVAL = 1000;
32 
40 class BlockFilterIndex final : public BaseIndex
41 {
42 private:
44  std::unique_ptr<BaseIndex::DB> m_db;
45 
47  std::unique_ptr<FlatFileSeq> m_filter_fileseq;
48 
49  bool ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const;
50  size_t WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter);
51 
54  std::unordered_map<uint256, uint256, BlockHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache);
55 
56  // Last computed header to avoid disk reads on every new block.
57  uint256 m_last_header{};
58 
59  bool AllowPrune() const override { return true; }
60 
61  bool Write(const BlockFilter& filter, uint32_t block_height, const uint256& filter_header);
62 
63  std::optional<uint256> ReadFilterHeader(int height, const uint256& expected_block_hash);
64 
65 protected:
66  bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;
67 
68  bool CustomCommit(CDBBatch& batch) override;
69 
70  bool CustomAppend(const interfaces::BlockInfo& block) override;
71 
72  bool CustomRemove(const interfaces::BlockInfo& block) override;
73 
74  BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; }
75 
76 public:
78  explicit BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, BlockFilterType filter_type,
79  size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
80 
82 
84 
86  bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const;
87 
89  bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache);
90 
92  bool LookupFilterRange(int start_height, const CBlockIndex* stop_index,
93  std::vector<BlockFilter>& filters_out) const;
94 
96  bool LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
97  std::vector<uint256>& hashes_out) const;
98 };
99 
105 
107 void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn);
108 
113 bool InitBlockFilterIndex(std::function<std::unique_ptr<interfaces::Chain>()> make_chain, BlockFilterType filter_type,
114  size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
115 
121 bool DestroyBlockFilterIndex(BlockFilterType filter_type);
122 
125 
126 #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
Interval between compact filter checkpoints.
virtual interfaces::Chain::NotifyOptions CustomOptions()
Return custom notification options for index.
Definition: base.h:152
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:71
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
virtual bool CustomAppend(const interfaces::BlockInfo &block)
Write update index entries for a newly connected block.
Definition: base.h:129
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:64
BlockFilterType
Definition: blockfilter.h:93
Block data sent with blockConnected, blockDisconnected notifications.
Definition: chain.h:19
Base class for indices of blockchain data.
Definition: base.h:54
#define LIFETIMEBOUND
Definition: attributes.h:16
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
std::optional< uint256 > ReadFilterHeader(int height, const uint256 &expected_block_hash)
bool Write(const BlockFilter &filter, uint32_t block_height, const uint256 &filter_header)
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:115
Options specifying which chain notifications are required.
Definition: chain.h:318
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 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. ...
virtual bool CustomInit(const std::optional< interfaces::BlockRef > &block)
Initialize internal state from the database and block index.
Definition: base.h:126
BlockFilterType m_filter_type
virtual bool CustomRemove(const interfaces::BlockInfo &block)
Rewind index by one block during a chain reorg.
Definition: base.h:136
256-bit opaque blob.
Definition: uint256.h:195
virtual bool CustomCommit(CDBBatch &batch)
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: base.h:133
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
FlatFilePos m_next_filter_pos
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
BlockFilterType GetFilterType() const
#define GUARDED_BY(x)
Definition: threadsafety.h:39
std::unique_ptr< FlatFileSeq > m_filter_fileseq
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.