Bitcoin ABC  0.24.7
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  size_t operator()(const uint256 &hash) const {
18  return ReadLE64(hash.begin());
19  }
20 };
21 
30 class BlockFilterIndex final : public BaseIndex {
31 private:
33  std::string m_name;
34  std::unique_ptr<BaseIndex::DB> m_db;
35 
37  std::unique_ptr<FlatFileSeq> m_filter_fileseq;
38 
39  bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const;
40  size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter);
41 
47  std::unordered_map<BlockHash, uint256, FilterHeaderHasher>
48  m_headers_cache GUARDED_BY(m_cs_headers_cache);
49 
50 protected:
51  bool Init() override;
52 
53  bool CommitInternal(CDBBatch &batch) override;
54 
55  bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override;
56 
57  bool Rewind(const CBlockIndex *current_tip,
58  const CBlockIndex *new_tip) override;
59 
60  BaseIndex::DB &GetDB() const override { return *m_db; }
61 
62  const char *GetName() const override { return m_name.c_str(); }
63 
64 public:
66  explicit BlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
67  bool f_memory = false, bool f_wipe = false);
68 
70 
72  bool LookupFilter(const CBlockIndex *block_index,
73  BlockFilter &filter_out) const;
74 
76  bool LookupFilterHeader(const CBlockIndex *block_index,
77  uint256 &header_out);
78 
80  bool LookupFilterRange(int start_height, const CBlockIndex *stop_index,
81  std::vector<BlockFilter> &filters_out) const;
82 
84  bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index,
85  std::vector<uint256> &hashes_out) const;
86 };
87 
93 
95 void ForEachBlockFilterIndex(std::function<void(BlockFilterIndex &)> fn);
96 
102 bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size,
103  bool f_memory = false, bool f_wipe = false);
104 
110 bool DestroyBlockFilterIndex(BlockFilterType filter_type);
111 
114 
115 #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H
BlockFilterIndex::m_name
std::string m_name
Definition: blockfilterindex.h:33
BlockFilterIndex::GUARDED_BY
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.
FilterHeaderHasher::operator()
size_t operator()(const uint256 &hash) const
Definition: blockfilterindex.h:17
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:48
BlockFilterIndex::m_db
std::unique_ptr< BaseIndex::DB > m_db
Definition: blockfilterindex.h:34
BlockFilterIndex::WriteFilterToDisk
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter)
Definition: blockfilterindex.cpp:180
flatfile.h
ReadLE64
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
BlockFilterIndex
BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of bloc...
Definition: blockfilterindex.h:30
AnnotatedMixin< std::mutex >
BlockFilterIndex::CommitInternal
bool CommitInternal(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
Definition: blockfilterindex.cpp:141
base.h
BaseIndex
Base class for indices of blockchain data.
Definition: base.h:27
BlockFilterIndex::Init
bool Init() override
Initialize internal state from the database and block index.
Definition: blockfilterindex.cpp:122
BlockFilterIndex::m_filter_type
BlockFilterType m_filter_type
Definition: blockfilterindex.h:32
blockfilter.h
FlatFilePos
Definition: flatfile.h:14
BlockFilterIndex::LookupFilterRange
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.
Definition: blockfilterindex.cpp:448
BlockFilterIndex::Rewind
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
Definition: blockfilterindex.cpp:303
uint256
256-bit opaque blob.
Definition: uint256.h:127
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
GetBlockFilterIndex
BlockFilterIndex * GetBlockFilterIndex(BlockFilterType filter_type)
Get a block filter index by type.
Definition: blockfilterindex.cpp:486
CBlock
Definition: block.h:55
BlockFilterIndex::ReadFilterFromDisk
bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const
Definition: blockfilterindex.cpp:158
BlockFilterIndex::m_next_filter_pos
FlatFilePos m_next_filter_pos
Definition: blockfilterindex.h:36
BlockFilterIndex::LookupFilterHashRange
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.
Definition: blockfilterindex.cpp:468
InitBlockFilterIndex
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.
Definition: blockfilterindex.cpp:497
BlockFilterIndex::WriteBlock
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
Definition: blockfilterindex.cpp:231
CFCHECKPT_INTERVAL
static constexpr int CFCHECKPT_INTERVAL
Interval between compact filter checkpoints.
Definition: blockfilterindex.h:14
BlockFilterIndex::LookupFilter
bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const
Get a single filter by block.
Definition: blockfilterindex.cpp:408
BaseIndex::DB
The database stores a block locator of the chain the database is synced to so that the TxIndex can ef...
Definition: base.h:36
DestroyAllBlockFilterIndexes
void DestroyAllBlockFilterIndexes()
Destroy all open block filter indexes.
Definition: blockfilterindex.cpp:509
BlockFilterType
BlockFilterType
Definition: blockfilter.h:88
BlockFilterIndex::GetFilterType
BlockFilterType GetFilterType() const
Definition: blockfilterindex.h:69
BlockFilterIndex::GetDB
BaseIndex::DB & GetDB() const override
Definition: blockfilterindex.h:60
BlockFilterIndex::m_cs_headers_cache
Mutex m_cs_headers_cache
Definition: blockfilterindex.h:42
ForEachBlockFilterIndex
void ForEachBlockFilterIndex(std::function< void(BlockFilterIndex &)> fn)
Iterate over all running block filter indexes, invoking fn on each.
Definition: blockfilterindex.cpp:491
DestroyBlockFilterIndex
bool DestroyBlockFilterIndex(BlockFilterType filter_type)
Destroy the block filter index with the given type.
Definition: blockfilterindex.cpp:505
BlockFilterIndex::BlockFilterIndex
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.
Definition: blockfilterindex.cpp:103
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
BlockFilterIndex::LookupFilterHeader
bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out)
Get a single filter header by block.
Definition: blockfilterindex.cpp:418
FilterHeaderHasher
Definition: blockfilterindex.h:16
BlockFilterIndex::m_filter_fileseq
std::unique_ptr< FlatFileSeq > m_filter_fileseq
Definition: blockfilterindex.h:37
BlockFilterIndex::GetName
const char * GetName() const override
Get the name of the index for display in logs.
Definition: blockfilterindex.h:62
BlockFilter
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:111