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 <consensus/merkle.h>6#include <merkleblock.h>7#include <serialize.h>8#include <streams.h>9#include <test/util/random.h>10#include <test/util/setup_common.h>11#include <uint256.h>13#include <vector>15#include <boost/test/unit_test.hpp>17class CPartialMerkleTreeTester : public CPartialMerkleTree18{19public:20CPartialMerkleTreeTester(FastRandomContext& rng) : m_rng{rng} {}22// flip one bit in one of the hashes - this should break the authentication23void Damage() {24unsigned int n = m_rng.randrange(vHash.size());25int bit = m_rng.randbits(8);26*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);27}29FastRandomContext& m_rng;30};32BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)34BOOST_AUTO_TEST_CASE(pmt_test1)35{36static const unsigned int tx_counts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};38for (int i = 0; i < 12; i++) {39unsigned int nTx = tx_counts[i];41// build a block with some dummy transactions42CBlock block;43for (unsigned int j=0; j<nTx; j++) {44CMutableTransaction tx;45tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique46block.vtx.push_back(MakeTransactionRef(std::move(tx)));47}49// calculate actual merkle root and height50uint256 merkleRoot1 = BlockMerkleRoot(block);51std::vector<Txid> vTxid(nTx);52for (unsigned int j=0; j<nTx; j++)53vTxid[j] = block.vtx[j]->GetHash();54int nHeight = 1, nTx_ = nTx;55while (nTx_ > 1) {56nTx_ = (nTx_+1)/2;57nHeight++;58}60// check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/12861for (int att = 1; att < 15; att++) {62// build random subset of txid's63std::vector<bool> vMatch(nTx, false);64std::vector<Txid> vMatchTxid1;65for (unsigned int j=0; j<nTx; j++) {66bool fInclude = m_rng.randbits(att / 2) == 0;67vMatch[j] = fInclude;68if (fInclude)69vMatchTxid1.push_back(vTxid[j]);70}72// build the partial merkle tree73CPartialMerkleTree pmt1(vTxid, vMatch);75// serialize76DataStream ss{};77ss << pmt1;79// verify CPartialMerkleTree's size guarantees80unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight);81BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);83// deserialize into a tester copy84CPartialMerkleTreeTester pmt2{m_rng};85ss >> pmt2;87// extract merkle root and matched txids from copy88std::vector<Txid> vMatchTxid2;89std::vector<unsigned int> vIndex;90uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex);92// check that it has the same merkle root as the original, and a valid one93BOOST_CHECK(merkleRoot1 == merkleRoot2);94BOOST_CHECK(!merkleRoot2.IsNull());96// check that it contains the matched transactions (in the same order!)97BOOST_CHECK(vMatchTxid1 == vMatchTxid2);99// check that random bit flips break the authentication100for (int j=0; j<4; j++) {101CPartialMerkleTreeTester pmt3(pmt2);102pmt3.Damage();103std::vector<Txid> vMatchTxid3;104uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3, vIndex);105BOOST_CHECK(merkleRoot3 != merkleRoot1);106}107}108}109}111BOOST_AUTO_TEST_CASE(pmt_malleability)112{113std::vector<Txid> vTxid{114Txid::FromUint256(uint256{1}), Txid::FromUint256(uint256{2}),115Txid::FromUint256(uint256{3}), Txid::FromUint256(uint256{4}),116Txid::FromUint256(uint256{5}), Txid::FromUint256(uint256{6}),117Txid::FromUint256(uint256{7}), Txid::FromUint256(uint256{8}),118Txid::FromUint256(uint256{9}), Txid::FromUint256(uint256{10}),119Txid::FromUint256(uint256{9}), Txid::FromUint256(uint256{10}),120};121std::vector<bool> vMatch = {false, false, false, false, false, false, false, false, false, true, true, false};123CPartialMerkleTree tree(vTxid, vMatch);124std::vector<unsigned int> vIndex;125BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull());126}128BOOST_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.