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) 2012-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_COMMON_BLOOM_H6#define BITCOIN_COMMON_BLOOM_H8#include <serialize.h>10#include <cstdint>11#include <span>12#include <vector>14class COutPoint;15class CTransaction;17//! 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%18inline constexpr unsigned int MAX_BLOOM_FILTER_SIZE{36'000}; // bytes19inline constexpr unsigned int MAX_HASH_FUNCS = 50;21/**22* First two bits of nFlags control how much IsRelevantAndUpdate actually updates23* The remaining bits are reserved24*/25enum bloomflags26{27BLOOM_UPDATE_NONE = 0,28BLOOM_UPDATE_ALL = 1,29// Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script30BLOOM_UPDATE_P2PUBKEY_ONLY = 2,31BLOOM_UPDATE_MASK = 3,32};34/**35* BloomFilter is a probabilistic filter which SPV clients provide36* so that we can filter the transactions we send them.37*38* This allows for significantly more efficient transaction and block downloads.39*40* Because bloom filters are probabilistic, a SPV node can increase the false-41* positive rate, making us send it transactions which aren't actually its,42* allowing clients to trade more bandwidth for more privacy by obfuscating which43* keys are controlled by them.44*/45class CBloomFilter46{47private:48std::vector<unsigned char> vData;49unsigned int nHashFuncs;50unsigned int nTweak;51unsigned char nFlags;53unsigned int Hash(unsigned int nHashNum, std::span<const unsigned char> vDataToHash) const;55public:56/**57* Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements58* Note that if the given parameters will result in a filter outside the bounds of the protocol limits,59* the filter created will be as close to the given parameters as possible within the protocol limits.60* This will apply if nFPRate is very low or nElements is unreasonably high.61* nTweak is a constant which is added to the seed value passed to the hash function62* It should generally always be a random value (and is largely only exposed for unit testing)63* nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)64*/65CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);66CBloomFilter() : nHashFuncs(0), nTweak(0), nFlags(0) {}68SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }70void insert(std::span<const unsigned char> vKey);71void insert(const COutPoint& outpoint);73bool contains(std::span<const unsigned char> vKey) const;74bool contains(const COutPoint& outpoint) const;76//! True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS77//! (catch a filter which was just deserialized which was too big)78bool IsWithinSizeConstraints() const;80//! Also adds any outputs which match the filter to the filter (to match their spending txes)81bool IsRelevantAndUpdate(const CTransaction& tx);82};84/**85* RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.86* Construct it with the number of items to keep track of, and a false-positive87* rate. Unlike CBloomFilter, by default nTweak is set to a cryptographically88* secure random value for you. Similarly rather than clear() the method89* reset() is provided, which also changes nTweak to decrease the impact of90* false-positives.91*92* contains(item) will always return true if item was one of the last N to 1.5*N93* insert()'ed ... but may also return true for items that were not inserted.94*95* It needs around 1.8 bytes per element per factor 0.1 of false positive rate.96* For example, if we want 1000 elements, we'd need:97* - ~1800 bytes for a false positive rate of 0.198* - ~3600 bytes for a false positive rate of 0.0199* - ~5400 bytes for a false positive rate of 0.001100*101* If we make these simplifying assumptions:102* - logFpRate / log(0.5) doesn't get rounded or clamped in the nHashFuncs calculation103* - nElements is even, so that nEntriesPerGeneration == nElements / 2104*105* Then we get a more accurate estimate for filter bytes:106*107* 3/(log(256)*log(2)) * log(1/fpRate) * nElements108*/109class CRollingBloomFilter110{111public:112CRollingBloomFilter(unsigned int nElements, double nFPRate);114void insert(std::span<const unsigned char> vKey);115bool contains(std::span<const unsigned char> vKey) const;117void reset();119private:120int nEntriesPerGeneration;121int nEntriesThisGeneration;122int nGeneration;123std::vector<uint64_t> data;124unsigned int nTweak;125int nHashFuncs;126};128#endif // BITCOIN_COMMON_BLOOM_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.