Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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 <test/data/blockfilters.json.h>6#include <test/util/common.h>8#include <blockfilter.h>9#include <core_io.h>10#include <primitives/block.h>11#include <serialize.h>12#include <streams.h>13#include <undo.h>14#include <univalue.h>15#include <util/strencodings.h>17#include <boost/test/unit_test.hpp>19BOOST_AUTO_TEST_SUITE(blockfilter_tests)21BOOST_AUTO_TEST_CASE(gcsfilter_test)22{23GCSFilter::ElementSet included_elements, excluded_elements;24for (int i = 0; i < 100; ++i) {25GCSFilter::Element element1(32);26element1[0] = i;27included_elements.insert(std::move(element1));29GCSFilter::Element element2(32);30element2[1] = i;31excluded_elements.insert(std::move(element2));32}34GCSFilter filter({0, 0, 10, 1 << 10}, included_elements);35for (const auto& element : included_elements) {36BOOST_CHECK(filter.Match(element));38auto insertion = excluded_elements.insert(element);39BOOST_CHECK(filter.MatchAny(excluded_elements));40excluded_elements.erase(insertion.first);41}42}44BOOST_AUTO_TEST_CASE(gcsfilter_default_constructor)45{46GCSFilter filter;47BOOST_CHECK_EQUAL(filter.GetN(), 0U);48BOOST_CHECK_EQUAL(filter.GetEncoded().size(), 1U);50const GCSFilter::Params& params = filter.GetParams();51BOOST_CHECK_EQUAL(params.m_siphash_k0, 0U);52BOOST_CHECK_EQUAL(params.m_siphash_k1, 0U);53BOOST_CHECK_EQUAL(params.m_P, 0);54BOOST_CHECK_EQUAL(params.m_M, 1U);55}57BOOST_AUTO_TEST_CASE(blockfilter_basic_test)58{59CScript included_scripts[5], excluded_scripts[4];61// First two are outputs on a single transaction.62included_scripts[0] << std::vector<unsigned char>(65, 0) << OP_CHECKSIG;63included_scripts[1] << OP_DUP << OP_HASH160 << std::vector<unsigned char>(20, 1) << OP_EQUALVERIFY << OP_CHECKSIG;65// Third is an output on in a second transaction.66included_scripts[2] << OP_1 << std::vector<unsigned char>(33, 2) << OP_1 << OP_CHECKMULTISIG;68// Last two are spent by a single transaction.69included_scripts[3] << OP_0 << std::vector<unsigned char>(32, 3);70included_scripts[4] << OP_4 << OP_ADD << OP_8 << OP_EQUAL;72// OP_RETURN output is an output on the second transaction.73excluded_scripts[0] << OP_RETURN << std::vector<unsigned char>(40, 4);75// This script is not related to the block at all.76excluded_scripts[1] << std::vector<unsigned char>(33, 5) << OP_CHECKSIG;78// OP_RETURN is non-standard since it's not followed by a data push, but is still excluded from79// filter.80excluded_scripts[2] << OP_RETURN << OP_4 << OP_ADD << OP_8 << OP_EQUAL;82CMutableTransaction tx_1;83tx_1.vout.emplace_back(100, included_scripts[0]);84tx_1.vout.emplace_back(200, included_scripts[1]);85tx_1.vout.emplace_back(0, excluded_scripts[0]);87CMutableTransaction tx_2;88tx_2.vout.emplace_back(300, included_scripts[2]);89tx_2.vout.emplace_back(0, excluded_scripts[2]);90tx_2.vout.emplace_back(400, excluded_scripts[3]); // Script is empty92CBlock block;93block.vtx.push_back(MakeTransactionRef(tx_1));94block.vtx.push_back(MakeTransactionRef(tx_2));96CBlockUndo block_undo;97block_undo.vtxundo.emplace_back();98block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(500, included_scripts[3]), 1000, true);99block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(600, included_scripts[4]), 10000, false);100block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(700, excluded_scripts[3]), 100000, false);102BlockFilter block_filter(BlockFilterType::BASIC, block, block_undo);103const GCSFilter& filter = block_filter.GetFilter();105for (const CScript& script : included_scripts) {106BOOST_CHECK(filter.Match(GCSFilter::Element(script.begin(), script.end())));107}108for (const CScript& script : excluded_scripts) {109BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));110}112// Test serialization/unserialization.113BlockFilter block_filter2;115DataStream stream{};116stream << block_filter;117stream >> block_filter2;119BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());120BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());121BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());123BlockFilter default_ctor_block_filter_1;124BlockFilter default_ctor_block_filter_2;125BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetFilterType(), default_ctor_block_filter_2.GetFilterType());126BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetBlockHash(), default_ctor_block_filter_2.GetBlockHash());127BOOST_CHECK(default_ctor_block_filter_1.GetEncodedFilter() == default_ctor_block_filter_2.GetEncodedFilter());128}130BOOST_AUTO_TEST_CASE(blockfilters_json_test)131{132UniValue json;133if (!json.read(json_tests::blockfilters) || !json.isArray()) {134BOOST_ERROR("Parse error.");135return;136}138const UniValue& tests = json.get_array();139for (unsigned int i = 0; i < tests.size(); i++) {140const UniValue& test = tests[i];141std::string strTest = test.write();143if (test.size() == 1) {144continue;145} else if (test.size() < 7) {146BOOST_ERROR("Bad test: " << strTest);147continue;148}150unsigned int pos = 0;151/*int block_height =*/ test[pos++].getInt<int>();152BOOST_CHECK(uint256::FromHex(test[pos++].get_str()));154CBlock block;155BOOST_REQUIRE(DecodeHexBlk(block, test[pos++].get_str()));157CBlockUndo block_undo;158block_undo.vtxundo.emplace_back();159CTxUndo& tx_undo = block_undo.vtxundo.back();160const UniValue& prev_scripts = test[pos++].get_array();161for (unsigned int ii = 0; ii < prev_scripts.size(); ii++) {162std::vector<unsigned char> raw_script = ParseHex(prev_scripts[ii].get_str());163CTxOut txout(0, CScript(raw_script.begin(), raw_script.end()));164tx_undo.vprevout.emplace_back(txout, 0, false);165}167uint256 prev_filter_header_basic{*Assert(uint256::FromHex(test[pos++].get_str()))};168std::vector<unsigned char> filter_basic = ParseHex(test[pos++].get_str());169uint256 filter_header_basic{*Assert(uint256::FromHex(test[pos++].get_str()))};171BlockFilter computed_filter_basic(BlockFilterType::BASIC, block, block_undo);172BOOST_CHECK(computed_filter_basic.GetFilter().GetEncoded() == filter_basic);174uint256 computed_header_basic = computed_filter_basic.ComputeHeader(prev_filter_header_basic);175BOOST_CHECK(computed_header_basic == filter_header_basic);176}177}179BOOST_AUTO_TEST_CASE(blockfilter_type_names)180{181BOOST_CHECK_EQUAL(BlockFilterTypeName(BlockFilterType::BASIC), "basic");182BOOST_CHECK_EQUAL(BlockFilterTypeName(static_cast<BlockFilterType>(255)), "");184BlockFilterType filter_type;185BOOST_CHECK(BlockFilterTypeByName("basic", filter_type));186BOOST_CHECK_EQUAL(filter_type, BlockFilterType::BASIC);188BOOST_CHECK(!BlockFilterTypeByName("unknown", filter_type));189}191BOOST_AUTO_TEST_SUITE_END()
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.