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