Bitcoin ABC  0.29.2
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 #include <util/hasher.h>
13 
14 static const char *const DEFAULT_BLOCKFILTERINDEX = "0";
15 
17 static constexpr int CFCHECKPT_INTERVAL = 1000;
18 
27 class BlockFilterIndex final : public BaseIndex {
28 private:
30  std::string m_name;
31  std::unique_ptr<BaseIndex::DB> m_db;
32 
34  std::unique_ptr<FlatFileSeq> m_filter_fileseq;
35 
36  bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const;
37  size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter);
38 
44  std::unordered_map<BlockHash, uint256, FilterHeaderHasher>
45  m_headers_cache GUARDED_BY(m_cs_headers_cache);
46 
47 protected:
48  bool Init() override;
49 
50  bool CommitInternal(CDBBatch &batch) override;
51 
52  bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override;
53 
54  bool Rewind(const CBlockIndex *current_tip,
55  const CBlockIndex *new_tip) override;
56 
57  BaseIndex::DB &GetDB() const override { return *m_db; }
58 
59  const char *GetName() const override { return m_name.c_str(); }
60 
61 public:
63  explicit BlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
64  bool f_memory = false, bool f_wipe = false);
65 
67 
69  bool LookupFilter(const CBlockIndex *block_index,
70  BlockFilter &filter_out) const;
71 
73  bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out)
75 
77  bool LookupFilterRange(int start_height, const CBlockIndex *stop_index,
78  std::vector<BlockFilter> &filters_out) const;
79 
81  bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index,
82  std::vector<uint256> &hashes_out) const;
83 };
84 
90 
92 void ForEachBlockFilterIndex(std::function<void(BlockFilterIndex &)> fn);
93 
99 bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
100  bool f_memory = false, bool f_wipe = false);
101 
107 bool DestroyBlockFilterIndex(BlockFilterType filter_type);
108 
111 
112 #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H
BlockFilterType
Definition: blockfilter.h:88
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
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.
static const char *const DEFAULT_BLOCKFILTERINDEX
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
The database stores a block locator of the chain the database is synced to so that the TxIndex can ef...
Definition: base.h:36
Base class for indices of blockchain data.
Definition: base.h:27
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:111
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
std::unordered_map< BlockHash, 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.
std::unique_ptr< BaseIndex::DB > m_db
bool CommitInternal(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
bool ReadFilterFromDisk(const FlatFilePos &pos, 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.
BlockFilterType GetFilterType() const
BlockFilterType m_filter_type
BaseIndex::DB & GetDB() const override
bool Init() override
Initialize internal state from the database and block index.
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
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.
std::unique_ptr< FlatFileSeq > m_filter_fileseq
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
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.
const char * GetName() const override
Get the name of the index for display in logs.
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache)
Get a single filter header by block.
std::string m_name
FlatFilePos m_next_filter_pos
Definition: block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:54
256-bit opaque blob.
Definition: uint256.h:129
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56