Line data Source code
1 : // Copyright (c) 2018-2021 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 <attributes.h> 9 : #include <blockfilter.h> 10 : #include <chain.h> 11 : #include <flatfile.h> 12 : #include <index/base.h> 13 : #include <util/hasher.h> 14 : 15 : static const char* const DEFAULT_BLOCKFILTERINDEX = "0"; 16 : 17 : /** Interval between compact filter checkpoints. See BIP 157. */ 18 : static constexpr int CFCHECKPT_INTERVAL = 1000; 19 : 20 : /** 21 : * BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of 22 : * blocks by height. An index is constructed for each supported filter type with its own database 23 : * (ie. filter data for different types are stored in separate databases). 24 : * 25 : * This index is used to serve BIP 157 net requests. 26 : */ 27 : class BlockFilterIndex final : public BaseIndex 28 : { 29 : private: 30 : /** Version of the blockfilter index format. Increment this when breaking changes are made. */ 31 : static constexpr int CURRENT_VERSION = 2; 32 : 33 : BlockFilterType m_filter_type; 34 : std::string m_name; 35 : std::unique_ptr<BaseIndex::DB> m_db; 36 : 37 : FlatFilePos m_next_filter_pos; 38 : std::unique_ptr<FlatFileSeq> m_filter_fileseq; 39 : 40 : bool ReadFilterFromDisk(const FlatFilePos& pos, const uint256& hash, BlockFilter& filter) const; 41 : size_t WriteFilterToDisk(FlatFilePos& pos, const BlockFilter& filter); 42 : 43 : Mutex m_cs_headers_cache; 44 : /** cache of block hash to filter header, to avoid disk access when responding to getcfcheckpt. */ 45 : std::unordered_map<uint256, uint256, FilterHeaderHasher> m_headers_cache GUARDED_BY(m_cs_headers_cache); 46 : 47 17 : bool AllowPrune() const override { return true; } 48 : 49 : protected: 50 : bool Init() override; 51 : 52 : bool CommitInternal(CDBBatch& batch) override; 53 : 54 : bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override; 55 : 56 : bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override; 57 : 58 22 : BaseIndex::DB& GetDB() const LIFETIMEBOUND override { return *m_db; } 59 : 60 23 : const char* GetName() const LIFETIMEBOUND override { return m_name.c_str(); } 61 : 62 : public: 63 : /** Constructs the index, which becomes available to be queried. */ 64 : explicit BlockFilterIndex(BlockFilterType filter_type, 65 : size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 66 : 67 558 : BlockFilterType GetFilterType() const { return m_filter_type; } 68 : 69 : /** Get a single filter by block. */ 70 : bool LookupFilter(const CBlockIndex* block_index, BlockFilter& filter_out) const; 71 : 72 : /** Get a single filter header by block. */ 73 : bool LookupFilterHeader(const CBlockIndex* block_index, uint256& header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache); 74 : 75 : /** Get a range of filters between two heights on a chain. */ 76 : bool LookupFilterRange(int start_height, const CBlockIndex* stop_index, 77 : std::vector<BlockFilter>& filters_out) const; 78 : 79 : /** Get a range of filter hashes between two heights on a chain. */ 80 : bool LookupFilterHashRange(int start_height, const CBlockIndex* stop_index, 81 : std::vector<uint256>& hashes_out) const; 82 : }; 83 : 84 : /** 85 : * Get a block filter index by type. Returns nullptr if index has not been initialized or was 86 : * already destroyed. 87 : */ 88 : BlockFilterIndex* GetBlockFilterIndex(BlockFilterType filter_type); 89 : 90 : /** Iterate over all running block filter indexes, invoking fn on each. */ 91 : void ForEachBlockFilterIndex(std::function<void (BlockFilterIndex&)> fn); 92 : 93 : /** 94 : * Initialize a block filter index for the given type if one does not already exist. Returns true if 95 : * a new index is created and false if one has already been initialized. 96 : */ 97 : bool InitBlockFilterIndex(BlockFilterType filter_type, 98 : size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 99 : 100 : /** 101 : * Destroy the block filter index with the given type. Returns false if no such index exists. This 102 : * just releases the allocated memory and closes the database connection, it does not delete the 103 : * index data. 104 : */ 105 : bool DestroyBlockFilterIndex(BlockFilterType filter_type); 106 : 107 : /** Destroy all open block filter indexes. */ 108 : void DestroyAllBlockFilterIndexes(); 109 : 110 : #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H