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) 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#include <common/bloom.h>7#include <clientversion.h>8#include <common/system.h>9#include <key.h>10#include <key_io.h>11#include <merkleblock.h>12#include <primitives/block.h>13#include <random.h>14#include <serialize.h>15#include <streams.h>16#include <test/util/common.h>17#include <test/util/random.h>18#include <test/util/setup_common.h>19#include <uint256.h>20#include <util/strencodings.h>22#include <vector>24#include <boost/test/unit_test.hpp>26using namespace util::hex_literals;28namespace bloom_tests {29struct BloomTest : public BasicTestingSetup {30std::vector<unsigned char> RandomData();31};32} // namespace bloom_tests34BOOST_FIXTURE_TEST_SUITE(bloom_tests, BloomTest)36BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize)37{38CBloomFilter filter(3, 0.01, 0, BLOOM_UPDATE_ALL);40BOOST_CHECK_MESSAGE( !filter.contains("99108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8), "Bloom filter should be empty!");41filter.insert("99108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8);42BOOST_CHECK_MESSAGE( filter.contains("99108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8), "Bloom filter doesn't contain just-inserted object!");43// One bit different in first byte44BOOST_CHECK_MESSAGE(!filter.contains("19108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8), "Bloom filter contains something it shouldn't!");46filter.insert("b5a2c786d9ef4658287ced5914b37a1b4aa32eee"_hex_u8);47BOOST_CHECK_MESSAGE(filter.contains("b5a2c786d9ef4658287ced5914b37a1b4aa32eee"_hex_u8), "Bloom filter doesn't contain just-inserted object (2)!");49filter.insert("b9300670b4c5366e95b2699e8b18bc75e5f729c5"_hex_u8);50BOOST_CHECK_MESSAGE(filter.contains("b9300670b4c5366e95b2699e8b18bc75e5f729c5"_hex_u8), "Bloom filter doesn't contain just-inserted object (3)!");52DataStream stream{};53stream << filter;55constexpr auto expected{"03614e9b050000000000000001"_hex};56BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end());58BOOST_CHECK_MESSAGE( filter.contains("99108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8), "Bloom filter doesn't contain just-inserted object!");59}61BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak)62{63// Same test as bloom_create_insert_serialize, but we add a nTweak of 10064CBloomFilter filter(3, 0.01, 2147483649UL, BLOOM_UPDATE_ALL);66filter.insert("99108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8);67BOOST_CHECK_MESSAGE( filter.contains("99108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8), "Bloom filter doesn't contain just-inserted object!");68// One bit different in first byte69BOOST_CHECK_MESSAGE(!filter.contains("19108ad8ed9bb6274d3980bab5a85c048f0950c8"_hex_u8), "Bloom filter contains something it shouldn't!");71filter.insert("b5a2c786d9ef4658287ced5914b37a1b4aa32eee"_hex_u8);72BOOST_CHECK_MESSAGE(filter.contains("b5a2c786d9ef4658287ced5914b37a1b4aa32eee"_hex_u8), "Bloom filter doesn't contain just-inserted object (2)!");74filter.insert("b9300670b4c5366e95b2699e8b18bc75e5f729c5"_hex_u8);75BOOST_CHECK_MESSAGE(filter.contains("b9300670b4c5366e95b2699e8b18bc75e5f729c5"_hex_u8), "Bloom filter doesn't contain just-inserted object (3)!");77DataStream stream{};78stream << filter;80constexpr auto expected{"03ce4299050000000100008001"_hex};81BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end());82}84BOOST_AUTO_TEST_CASE(bloom_create_insert_key)85{86std::string strSecret = std::string("5Kg1gnAjaLfKiwhhPpGS3QfRg2m6awQvaj98JCZBZQ5SuS2F15C");87CKey key = DecodeSecret(strSecret);88CPubKey pubkey = key.GetPubKey();89std::vector<unsigned char> vchPubKey(pubkey.begin(), pubkey.end());91CBloomFilter filter(2, 0.001, 0, BLOOM_UPDATE_ALL);92filter.insert(vchPubKey);93uint160 hash = pubkey.GetID();94filter.insert(hash);96DataStream stream{};97stream << filter;99constexpr auto expected{"038fc16b080000000000000001"_hex};100BOOST_CHECK_EQUAL_COLLECTIONS(stream.begin(), stream.end(), expected.begin(), expected.end());101}103BOOST_AUTO_TEST_CASE(bloom_match)104{105// Random real transaction (b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b)106DataStream stream{107"01000000010b26e9b7735eb6aabdf358bab62f9816a21ba9ebdb719d5299e88607d722c190000000008b4830450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a0141046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339ffffffff021bff3d11000000001976a91404943fdd508053c75000106d3bc6e2754dbcff1988ac2f15de00000000001976a914a266436d2965547608b9e15d9032a7b9d64fa43188ac00000000"_hex,108};109CTransaction tx(deserialize, TX_WITH_WITNESS, stream);111// and one which spends it (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)112unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce,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, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};113std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);114SpanReader spendStream{vch};115CTransaction spendingTx(deserialize, TX_WITH_WITNESS, spendStream);117CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL);118filter.insert(uint256{"b4749f017444b051c44dfd2720e88f314ff94f3dd6d56d40ef65854fcd7fff6b"});119BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match tx hash");121filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);122// byte-reversed tx hash123filter.insert("6bff7fcd4f8565ef406dd5d63d4ff94f318fe82027fd4dc451b04474019f74b4"_hex_u8);124BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized tx hash");126filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);127filter.insert("30450220070aca44506c5cef3a16ed519d7c3c39f8aab192c4e1c90d065f37b8a4af6141022100a8e160b856c2d43d27d8fba71e5aef6405b8643ac4cb7cb3c462aced7f14711a01"_hex_u8);128BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match input signature");130filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);131filter.insert("046d11fee51b0e60666d5049a9101a72741df480b96ee26488a4d3466b95c9a40ac5eeef87e10a5cd336c19a84565f80fa6c547957b7700ff4dfbdefe76036c339"_hex_u8);132BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match input pub key");134filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);135filter.insert("04943fdd508053c75000106d3bc6e2754dbcff19"_hex_u8);136BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address");137BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(spendingTx), "Simple Bloom filter didn't add output");139filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);140filter.insert("a266436d2965547608b9e15d9032a7b9d64fa431"_hex_u8);141BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match output address");143filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);144filter.insert(COutPoint{Txid{"90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 0});145BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match COutPoint");147filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);148COutPoint prevOutPoint{Txid{"90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 0};149{150std::vector<unsigned char> data(32 + sizeof(unsigned int));151memcpy(data.data(), prevOutPoint.hash.begin(), 32);152memcpy(data.data()+32, &prevOutPoint.n, sizeof(unsigned int));153filter.insert(data);154}155BOOST_CHECK_MESSAGE(filter.IsRelevantAndUpdate(tx), "Simple Bloom filter didn't match manually serialized COutPoint");157filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);158filter.insert(uint256{"00000009e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436"});159BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random tx hash");161filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);162filter.insert("0000006d2965547608b9e15d9032a7b9d64fa431"_hex_u8);163BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched random address");165filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);166filter.insert(COutPoint{Txid{"90c122d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 1});167BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about");169filter = CBloomFilter(10, 0.000001, 0, BLOOM_UPDATE_ALL);170filter.insert(COutPoint{Txid{"000000d70786e899529d71dbeba91ba216982fb6ba58f3bdaab65e73b7e9260b"}, 0});171BOOST_CHECK_MESSAGE(!filter.IsRelevantAndUpdate(tx), "Simple Bloom filter matched COutPoint for an output we didn't care about");172}174BOOST_AUTO_TEST_CASE(merkle_block_1)175{176CBlock block = getBlock13b8a();177CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL);178// Match the last transaction179filter.insert(uint256{"74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"});181CMerkleBlock merkleBlock(block, filter);182BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());184BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 1U);185std::pair<unsigned int, Txid> pair = merkleBlock.vMatchedTxn[0];187BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == Txid::FromUint256(uint256{"74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"}));188BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 8);190std::vector<Txid> vMatched;191std::vector<unsigned int> vIndex;192BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot);193BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size());194for (unsigned int i = 0; i < vMatched.size(); i++)195BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second);197// Also match the 8th transaction198filter.insert(uint256{"dd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053"});199merkleBlock = CMerkleBlock(block, filter);200BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash());202BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 2);204BOOST_CHECK(merkleBlock.vMatchedTxn[1] == pair);206BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == Txid::FromUint256(uint256{"dd1fd2a6fc16404faf339881a90adbde7f4f728691ac62e8f168809cdfae1053"}));207BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 7);209BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot);210BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size());211for (unsigned int i = 0; i < vMatched.size(); i++)212BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second);213}215BOOST_AUTO_TEST_CASE(merkle_block_2)216{217// Random real block (000000005a4ded781e667e06ceefafb71410b511fe0d5adc3e5a27ecbec34ae6)218// With 4 txes219CBlock block;220DataStream stream{221"0100000075616236cc2126035fadb38deb65b9102cc2c41c09cdf29fc051906800000000fe7d5e12ef0ff901f6050211249919b1c0653771832b3a80c66cea42847f0ae1d4d26e49ffff001d00f0a4410401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d029105ffffffff0100f2052a010000004341046d8709a041d34357697dfcb30a9d05900a6294078012bf3bb09c6f9b525f1d16d5503d7905db1ada9501446ea00728668fc5719aa80be2fdfc8a858a4dbdd4fbac00000000010000000255605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d28350000000049483045022100aa46504baa86df8a33b1192b1b9367b4d729dc41e389f2c04f3e5c7f0559aae702205e82253a54bf5c4f65b7428551554b2045167d6d206dfe6a2e198127d3f7df1501ffffffff55605dc6f5c3dc148b6da58442b0b2cd422be385eab2ebea4119ee9c268d2835010000004847304402202329484c35fa9d6bb32a55a70c0982f606ce0e3634b69006138683bcd12cbb6602200c28feb1e2555c3210f1dddb299738b4ff8bbe9667b68cb8764b5ac17b7adf0001ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac0000000001000000025f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028000000004847304402205d6058484157235b06028c30736c15613a28bdb768ee628094ca8b0030d4d6eb0220328789c9a2ec27ddaec0ad5ef58efded42e6ea17c2e1ce838f3d6913f5e95db601ffffffff5f9a06d3acdceb56be1bfeaa3e8a25e62d182fa24fefe899d1c17f1dad4c2028010000004a493046022100c45af050d3cea806cedd0ab22520c53ebe63b987b8954146cdca42487b84bdd6022100b9b027716a6b59e640da50a864d6dd8a0ef24c76ce62391fa3eabaf4d2886d2d01ffffffff0200e1f505000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac000000000100000002e2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b0000000048473044022016e7a727a061ea2254a6c358376aaa617ac537eb836c77d646ebda4c748aac8b0220192ce28bf9f2c06a6467e6531e27648d2b3e2e2bae85159c9242939840295ba501ffffffffe2274e5fea1bf29d963914bd301aa63b64daaf8a3e88f119b5046ca5738a0f6b010000004a493046022100b7a1a755588d4190118936e15cd217d133b0e4a53c3c15924010d5648d8925c9022100aaef031874db2114f2d869ac2de4ae53908fbfea5b2b1862e181626bb9005c9f01ffffffff0200e1f505000000004341044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac00180d8f000000004341046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac00000000"_hex,222};223stream >> TX_WITH_WITNESS(block);225CBloomFilter filter(10, 0.000001, 0, BLOOM_UPDATE_ALL);226// Match the first transaction227filter.insert(uint256{"e980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70"});229CMerkleBlock merkleBlock(block, filter);230BOOST_CHECK(merkleBlock.header.GetHash() == block.GetHash());232BOOST_CHECK(merkleBlock.vMatchedTxn.size() == 1);233std::pair<unsigned int, Txid> pair = merkleBlock.vMatchedTxn[0];235BOOST_CHECK(merkleBlock.vMatchedTxn[0].second == Txid::FromUint256(uint256{"e980fe9f792d014e73b95203dc1335c5f9ce19ac537a419e6df5b47aecb93b70"}));236BOOST_CHECK(merkleBlock.vMatchedTxn[0].first == 0);238std::vector<Txid> vMatched;239std::vector<unsigned int> vIndex;240BOOST_CHECK(merkleBlock.txn.ExtractMatches(vMatched, vIndex) == block.hashMerkleRoot);241BOOST_CHECK(vMatched.size() == merkleBlock.vMatchedTxn.size());242for (unsigned int i = 0; i < vMatched.size(); i++)243BOOST_CHECK(vMatched[i] == merkleBlock.vMatchedTxn[i].second);245// Match an output from the second transaction (the pubkey for address 1DZTzaBHUDM7T3QvUKBz4qXMRpkg8jsfB5)246// This should match the third transaction because it spends the output matched247// It also matches the fourth transaction, which spends to the pubkey again248filter.insert("044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45af"_hex_u8);250merkleBlock = CMerkleBlock(block, filter);
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.