Bitcoin Core  0.18.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 
20 class BlockFilterIndex final : public BaseIndex
21 {
22 private:
24  std::string m_name;
25  std::unique_ptr<BaseIndex::DB> m_db;
26 
28  std::unique_ptr<FlatFileSeq> m_filter_fileseq;
29 
30  bool ReadFilterFromDisk(const FlatFilePos& pos, BlockFilter& filter) const;
31  size_t WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter);
32 
33 protected:
34  bool Init() override;
35 
36  bool CommitInternal(CDBBatch& batch) override;
37 
38  bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
39 
40  bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
41 
42  BaseIndex::DB& GetDB() const override { return *m_db; }
43 
44  const char* GetName() const override { return m_name.c_str(); }
45 
46 public:
48  explicit BlockFilterIndex(BlockFilterType filter_type,
49  size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
50 
52 
54  bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const;
55 
57  bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) const;
58 
60  bool LookupFilterRange(int start_height, const CBlockIndex* stop_index,
61  std::vector<BlockFilter>& filters_out) const;
62 
64  bool LookupFilterHashRange(int start_height, const CBlockIndex* stop_index,
65  std::vector<uint256>& hashes_out) const;
66 };
67 
73 
75 void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn);
76 
81 bool InitBlockFilterIndex(BlockFilterType filter_type,
82  size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
83 
90 
93 
94 #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:47
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
Definition: block.h:72
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
std::unique_ptr< BaseIndex::DB > m_db
BlockFilterType
Definition: blockfilter.h:87
Base class for indices of blockchain data.
Definition: base.h:22
bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:109
std::string m_name
BlockFilterIndex(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.
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
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.
BlockFilterType m_filter_type
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) const
Get a single filter header by block.
256-bit opaque blob.
Definition: uint256.h:121
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:139
FlatFilePos m_next_filter_pos
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
BlockFilterType GetFilterType() const
BaseIndex::DB & GetDB() const override
bool CommitInternal(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 Init() override
Initialize internal state from the database and block index.
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.
const char * GetName() const override
Get the name of the index for display in logs.