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#include <mutex>6#include <set>7#include <string_view>9#include <blockfilter.h>10#include <crypto/siphash.h>11#include <hash.h>12#include <primitives/block.h>13#include <primitives/transaction.h>14#include <script/script.h>15#include <streams.h>16#include <undo.h>17#include <util/golombrice.h>18#include <util/string.h>20using util::Join;22static const std::map<BlockFilterType, std::string> g_filter_types = {23{BlockFilterType::BASIC, "basic"},24};26uint64_t GCSFilter::HashToRange(const Element& element) const27{28uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1)29.Write(element)30.Finalize();31return FastRange64(hash, m_F);32}34std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) const35{36std::vector<uint64_t> hashed_elements;37hashed_elements.reserve(elements.size());38for (const Element& element : elements) {39hashed_elements.push_back(HashToRange(element));40}41std::sort(hashed_elements.begin(), hashed_elements.end());42return hashed_elements;43}45GCSFilter::GCSFilter(const Params& params)46: m_params(params), m_N(0), m_F(0), m_encoded{0}47{}49GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)50: m_params(params), m_encoded(std::move(encoded_filter))51{52SpanReader stream{m_encoded};54uint64_t N = ReadCompactSize(stream);55m_N = static_cast<uint32_t>(N);56if (m_N != N) {57throw std::ios_base::failure("N must be <2^32");58}59m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);61if (skip_decode_check) return;63// Verify that the encoded filter contains exactly N elements. If it has too much or too little64// data, a std::ios_base::failure exception will be raised.65BitStreamReader bitreader{stream};66for (uint64_t i = 0; i < m_N; ++i) {67GolombRiceDecode(bitreader, m_params.m_P);68}69if (!stream.empty()) {70throw std::ios_base::failure("encoded_filter contains excess data");71}72}74GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)75: m_params(params)76{77size_t N = elements.size();78m_N = static_cast<uint32_t>(N);79if (m_N != N) {80throw std::invalid_argument("N must be <2^32");81}82m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);84VectorWriter stream{m_encoded, 0};86WriteCompactSize(stream, m_N);88if (elements.empty()) {89return;90}92BitStreamWriter bitwriter{stream};94uint64_t last_value = 0;95for (uint64_t value : BuildHashedSet(elements)) {96uint64_t delta = value - last_value;97GolombRiceEncode(bitwriter, m_params.m_P, delta);98last_value = value;99}101bitwriter.Flush();102}104bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const105{106SpanReader stream{m_encoded};108// Seek forward by size of N109uint64_t N = ReadCompactSize(stream);110assert(N == m_N);112BitStreamReader bitreader{stream};114uint64_t value = 0;115size_t hashes_index = 0;116for (uint32_t i = 0; i < m_N; ++i) {117uint64_t delta = GolombRiceDecode(bitreader, m_params.m_P);118value += delta;120while (true) {121if (hashes_index == size) {122return false;123} else if (element_hashes[hashes_index] == value) {124return true;125} else if (element_hashes[hashes_index] > value) {126break;127}129hashes_index++;130}131}133return false;134}136bool GCSFilter::Match(const Element& element) const137{138uint64_t query = HashToRange(element);139return MatchInternal(&query, 1);140}142bool GCSFilter::MatchAny(const ElementSet& elements) const143{144const std::vector<uint64_t> queries = BuildHashedSet(elements);145return MatchInternal(queries.data(), queries.size());146}148const std::string& BlockFilterTypeName(BlockFilterType filter_type)149{150static std::string unknown_retval;151auto it = g_filter_types.find(filter_type);152return it != g_filter_types.end() ? it->second : unknown_retval;153}155bool BlockFilterTypeByName(std::string_view name, BlockFilterType& filter_type)156{157for (const auto& entry : g_filter_types) {158if (entry.second == name) {159filter_type = entry.first;160return true;161}162}163return false;164}166const std::set<BlockFilterType>& AllBlockFilterTypes()167{168static std::set<BlockFilterType> types;170static std::once_flag flag;171std::call_once(flag, []() {172for (const auto& entry : g_filter_types) {173types.insert(entry.first);174}175});177return types;178}180const std::string& ListBlockFilterTypes()181{182static std::string type_list{Join(g_filter_types, ", ", [](const auto& entry) { return entry.second; })};184return type_list;185}187static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,188const CBlockUndo& block_undo)189{190GCSFilter::ElementSet elements;192for (const CTransactionRef& tx : block.vtx) {193for (const CTxOut& txout : tx->vout) {194const CScript& script = txout.scriptPubKey;195if (script.empty() || script[0] == OP_RETURN) continue;196elements.emplace(script.begin(), script.end());197}198}200for (const CTxUndo& tx_undo : block_undo.vtxundo) {201for (const Coin& prevout : tx_undo.vprevout) {202const CScript& script = prevout.out.scriptPubKey;203if (script.empty()) continue;204elements.emplace(script.begin(), script.end());205}206}208return elements;209}211BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,212std::vector<unsigned char> filter, bool skip_decode_check)213: m_filter_type(filter_type), m_block_hash(block_hash)214{215GCSFilter::Params params;216if (!BuildParams(params)) {217throw std::invalid_argument("unknown filter_type");218}219m_filter = GCSFilter(params, std::move(filter), skip_decode_check);220}222BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)223: m_filter_type(filter_type), m_block_hash(block.GetHash())224{225GCSFilter::Params params;226if (!BuildParams(params)) {227throw std::invalid_argument("unknown filter_type");228}229m_filter = GCSFilter(params, BasicFilterElements(block, block_undo));230}232bool BlockFilter::BuildParams(GCSFilter::Params& params) const233{234switch (m_filter_type) {235case BlockFilterType::BASIC:236params.m_siphash_k0 = m_block_hash.GetUint64(0);237params.m_siphash_k1 = m_block_hash.GetUint64(1);238params.m_P = BASIC_FILTER_P;239params.m_M = BASIC_FILTER_M;240return true;241case BlockFilterType::INVALID:242return false;243}245return false;246}248uint256 BlockFilter::GetHash() const249{250return Hash(GetEncodedFilter());