Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2018-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#ifndef BITCOIN_BLOCKFILTER_H6#define BITCOIN_BLOCKFILTER_H8#include <cstddef>9#include <cstdint>10#include <ios>11#include <set>12#include <string>13#include <string_view>14#include <unordered_set>15#include <utility>16#include <vector>18#include <attributes.h>19#include <uint256.h>20#include <util/bytevectorhash.h>22class CBlock;23class CBlockUndo;25/**26* This implements a Golomb-coded set as defined in BIP 158. It is a27* compact, probabilistic data structure for testing set membership.28*/29class GCSFilter30{31public:32typedef std::vector<unsigned char> Element;33typedef std::unordered_set<Element, ByteVectorHash> ElementSet;35struct Params36{37uint64_t m_siphash_k0;38uint64_t m_siphash_k1;39uint8_t m_P; //!< Golomb-Rice coding parameter40uint32_t m_M; //!< Inverse false positive rate42Params(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 1)43: m_siphash_k0(siphash_k0), m_siphash_k1(siphash_k1), m_P(P), m_M(M)44{}45};47private:48Params m_params;49uint32_t m_N; //!< Number of elements in the filter50uint64_t m_F; //!< Range of element hashes, F = N * M51std::vector<unsigned char> m_encoded;53/** Hash a data element to an integer in the range [0, N * M). */54uint64_t HashToRange(const Element& element) const;56std::vector<uint64_t> BuildHashedSet(const ElementSet& elements) const;58/** Helper method used to implement Match and MatchAny */59bool MatchInternal(const uint64_t* sorted_element_hashes, size_t size) const;61public:63/** Constructs an empty filter. */64explicit GCSFilter(const Params& params = Params());66/** Reconstructs an already-created filter from an encoding. */67GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check);69/** Builds a new filter from the params and set of elements. */70GCSFilter(const Params& params, const ElementSet& elements);72uint32_t GetN() const { return m_N; }73const Params& GetParams() const LIFETIMEBOUND { return m_params; }74const std::vector<unsigned char>& GetEncoded() const LIFETIMEBOUND { return m_encoded; }76/**77* Checks if the element may be in the set. False positives are possible78* with probability 1/M.79*/80bool Match(const Element& element) const;82/**83* Checks if any of the given elements may be in the set. False positives84* are possible with probability 1/M per element checked. This is more85* efficient that checking Match on multiple elements separately.86*/87bool MatchAny(const ElementSet& elements) const;88};90inline constexpr uint8_t BASIC_FILTER_P = 19;91inline constexpr uint32_t BASIC_FILTER_M = 784931;93enum class BlockFilterType : uint8_t94{95BASIC = 0,96INVALID = 255,97};99/** Get the human-readable name for a filter type. Returns empty string for unknown types. */100const std::string& BlockFilterTypeName(BlockFilterType filter_type);102/** Find a filter type by its human-readable name. */103bool BlockFilterTypeByName(std::string_view name, BlockFilterType& filter_type);105/** Get a list of known filter types. */106const std::set<BlockFilterType>& AllBlockFilterTypes();108/** Get a comma-separated list of known filter type names. */109const std::string& ListBlockFilterTypes();111/**112* Complete block filter struct as defined in BIP 157. Serialization matches113* payload of "cfilter" messages.114*/115class BlockFilter116{117private:118BlockFilterType m_filter_type = BlockFilterType::INVALID;119uint256 m_block_hash;120GCSFilter m_filter;122bool BuildParams(GCSFilter::Params& params) const;124public:126BlockFilter() = default;128//! Reconstruct a BlockFilter from parts.129BlockFilter(BlockFilterType filter_type, const uint256& block_hash,130std::vector<unsigned char> filter, bool skip_decode_check);132//! Construct a new BlockFilter of the specified type from a block.133BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);135BlockFilterType GetFilterType() const { return m_filter_type; }136const uint256& GetBlockHash() const LIFETIMEBOUND { return m_block_hash; }137const GCSFilter& GetFilter() const LIFETIMEBOUND { return m_filter; }139const std::vector<unsigned char>& GetEncodedFilter() const LIFETIMEBOUND140{141return m_filter.GetEncoded();142}144//! Compute the filter hash.145uint256 GetHash() const;147//! Compute the filter header given the previous one.148uint256 ComputeHeader(const uint256& prev_header) const;150template <typename Stream>151void Serialize(Stream& s) const {152s << static_cast<uint8_t>(m_filter_type)153<< m_block_hash154<< m_filter.GetEncoded();155}157template <typename Stream>158void Unserialize(Stream& s) {159std::vector<unsigned char> encoded_filter;160uint8_t filter_type;162s >> filter_type163>> m_block_hash164>> encoded_filter;166m_filter_type = static_cast<BlockFilterType>(filter_type);168GCSFilter::Params params;169if (!BuildParams(params)) {170throw std::ios_base::failure("unknown filter_type");171}172m_filter = GCSFilter(params, std::move(encoded_filter), /*skip_decode_check=*/false);173}174};176#endif // BITCOIN_BLOCKFILTER_H