Changeset View
Changeset View
Standalone View
Standalone View
src/index/blockfilterindex.h
// Copyright (c) 2018 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_INDEX_BLOCKFILTERINDEX_H | #ifndef BITCOIN_INDEX_BLOCKFILTERINDEX_H | ||||
#define BITCOIN_INDEX_BLOCKFILTERINDEX_H | #define BITCOIN_INDEX_BLOCKFILTERINDEX_H | ||||
#include <blockfilter.h> | #include <blockfilter.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <flatfile.h> | #include <flatfile.h> | ||||
#include <index/base.h> | #include <index/base.h> | ||||
/** Interval between compact filter checkpoints. See BIP 157. */ | |||||
static constexpr int CFCHECKPT_INTERVAL = 1000; | |||||
struct FilterHeaderHasher { | |||||
size_t operator()(const uint256 &hash) const { | |||||
return ReadLE64(hash.begin()); | |||||
} | |||||
}; | |||||
/** | /** | ||||
* BlockFilterIndex is used to store and retrieve block filters, hashes, and | * BlockFilterIndex is used to store and retrieve block filters, hashes, and | ||||
* headers for a range of blocks by height. An index is constructed for each | * headers for a range of blocks by height. An index is constructed for each | ||||
* supported filter type with its own database (ie. filter data for different | * supported filter type with its own database (ie. filter data for different | ||||
* types are stored in separate databases). | * types are stored in separate databases). | ||||
* | * | ||||
* This index is used to serve BIP 157 net requests. | * This index is used to serve BIP 157 net requests. | ||||
*/ | */ | ||||
class BlockFilterIndex final : public BaseIndex { | class BlockFilterIndex final : public BaseIndex { | ||||
private: | private: | ||||
BlockFilterType m_filter_type; | BlockFilterType m_filter_type; | ||||
std::string m_name; | std::string m_name; | ||||
std::unique_ptr<BaseIndex::DB> m_db; | std::unique_ptr<BaseIndex::DB> m_db; | ||||
FlatFilePos m_next_filter_pos; | FlatFilePos m_next_filter_pos; | ||||
std::unique_ptr<FlatFileSeq> m_filter_fileseq; | std::unique_ptr<FlatFileSeq> m_filter_fileseq; | ||||
bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const; | bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const; | ||||
size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter); | size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter); | ||||
Mutex m_cs_headers_cache; | |||||
std::unordered_map<BlockHash, uint256, FilterHeaderHasher> | |||||
m_headers_cache GUARDED_BY(m_cs_headers_cache); | |||||
protected: | protected: | ||||
bool Init() override; | bool Init() override; | ||||
bool CommitInternal(CDBBatch &batch) override; | bool CommitInternal(CDBBatch &batch) override; | ||||
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override; | bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override; | ||||
bool Rewind(const CBlockIndex *current_tip, | bool Rewind(const CBlockIndex *current_tip, | ||||
Show All 11 Lines | public: | ||||
BlockFilterType GetFilterType() const { return m_filter_type; } | BlockFilterType GetFilterType() const { return m_filter_type; } | ||||
/** Get a single filter by block. */ | /** Get a single filter by block. */ | ||||
bool LookupFilter(const CBlockIndex *block_index, | bool LookupFilter(const CBlockIndex *block_index, | ||||
BlockFilter &filter_out) const; | BlockFilter &filter_out) const; | ||||
/** Get a single filter header by block. */ | /** Get a single filter header by block. */ | ||||
bool LookupFilterHeader(const CBlockIndex *block_index, | bool LookupFilterHeader(const CBlockIndex *block_index, | ||||
uint256 &header_out) const; | uint256 &header_out); | ||||
/** Get a range of filters between two heights on a chain. */ | /** Get a range of filters between two heights on a chain. */ | ||||
bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, | bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, | ||||
std::vector<BlockFilter> &filters_out) const; | std::vector<BlockFilter> &filters_out) const; | ||||
/** Get a range of filter hashes between two heights on a chain. */ | /** Get a range of filter hashes between two heights on a chain. */ | ||||
bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, | bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, | ||||
std::vector<uint256> &hashes_out) const; | std::vector<uint256> &hashes_out) const; | ||||
Show All 30 Lines |