Bitcoin Core  0.20.99
P2P Digital Currency
blockfilterindex.h
Go to the documentation of this file.
1 // Copyright (c) 2018 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 <blockfilter.h>
9 #include <chain.h>
10 #include <flatfile.h>
11 #include <index/base.h>
12 
14 static constexpr int CFCHECKPT_INTERVAL = 1000;
15 
17 {
18  size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
19 };
20 
28 class BlockFilterIndex final : public BaseIndex
29 {
30 private:
32  std::string m_name;
33  std::unique_ptr<BaseIndex::DB> m_db;
34 
36  std::unique_ptr<FlatFileSeq> m_filter_fileseq;
37 
38  bool ReadFilterFromDisk(const FlatFilePos& pos, 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 protected:
46  bool Init() override;
47 
48  bool CommitInternal(CDBBatch& batch) override;
49 
50  bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
51 
52  bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
53 
54  BaseIndex::DB& GetDB() const override { return *m_db; }
55 
56  const char* GetName() const override { return m_name.c_str(); }
57 
58 public:
60  explicit BlockFilterIndex(BlockFilterType filter_type,
61  size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
62 
63  BlockFilterType GetFilterType() const { return m_filter_type; }
64 
66  bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const;
67 
69  bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out);
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(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
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:46
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
Definition: block.h:62
std::unique_ptr< BaseIndex::DB > m_db
unsigned char * begin()
Definition: uint256.h:54
BlockFilterType
Definition: blockfilter.h:88
Base class for indices of blockchain data.
Definition: base.h:21
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:110
std::string m_name
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
BlockFilterType m_filter_type
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
256-bit opaque blob.
Definition: uint256.h:120
bool InitBlockFilterIndex(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. ...
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
FlatFilePos m_next_filter_pos
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
BlockFilterType GetFilterType() const
size_t operator()(const uint256 &hash) const
BaseIndex::DB & GetDB() const override
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
#define GUARDED_BY(x)
Definition: threadsafety.h:38
std::unique_ptr< FlatFileSeq > m_filter_fileseq
const char * GetName() const override
Get the name of the index for display in logs.