Bitcoin Core  26.1.0
P2P Digital Currency
pmt_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
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>
12 #include <version.h>
13 
14 #include <vector>
15 
16 #include <boost/test/unit_test.hpp>
17 
19 {
20 public:
21  // flip one bit in one of the hashes - this should break the authentication
22  void Damage() {
23  unsigned int n = InsecureRandRange(vHash.size());
24  int bit = InsecureRandBits(8);
25  *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
26  }
27 };
28 
29 BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
30 
32 {
33  static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
34 
35  for (int i = 0; i < 12; i++) {
36  unsigned int nTx = nTxCounts[i];
37 
38  // build a block with some dummy transactions
39  CBlock block;
40  for (unsigned int j=0; j<nTx; j++) {
42  tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique
43  block.vtx.push_back(MakeTransactionRef(std::move(tx)));
44  }
45 
46  // calculate actual merkle root and height
47  uint256 merkleRoot1 = BlockMerkleRoot(block);
48  std::vector<uint256> vTxid(nTx, uint256());
49  for (unsigned int j=0; j<nTx; j++)
50  vTxid[j] = block.vtx[j]->GetHash();
51  int nHeight = 1, nTx_ = nTx;
52  while (nTx_ > 1) {
53  nTx_ = (nTx_+1)/2;
54  nHeight++;
55  }
56 
57  // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128
58  for (int att = 1; att < 15; att++) {
59  // build random subset of txid's
60  std::vector<bool> vMatch(nTx, false);
61  std::vector<uint256> vMatchTxid1;
62  for (unsigned int j=0; j<nTx; j++) {
63  bool fInclude = InsecureRandBits(att / 2) == 0;
64  vMatch[j] = fInclude;
65  if (fInclude)
66  vMatchTxid1.push_back(vTxid[j]);
67  }
68 
69  // build the partial merkle tree
70  CPartialMerkleTree pmt1(vTxid, vMatch);
71 
72  // serialize
73  DataStream ss{};
74  ss << pmt1;
75 
76  // verify CPartialMerkleTree's size guarantees
77  unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight);
78  BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);
79 
80  // deserialize into a tester copy
82  ss >> pmt2;
83 
84  // extract merkle root and matched txids from copy
85  std::vector<uint256> vMatchTxid2;
86  std::vector<unsigned int> vIndex;
87  uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex);
88 
89  // check that it has the same merkle root as the original, and a valid one
90  BOOST_CHECK(merkleRoot1 == merkleRoot2);
91  BOOST_CHECK(!merkleRoot2.IsNull());
92 
93  // check that it contains the matched transactions (in the same order!)
94  BOOST_CHECK(vMatchTxid1 == vMatchTxid2);
95 
96  // check that random bit flips break the authentication
97  for (int j=0; j<4; j++) {
98  CPartialMerkleTreeTester pmt3(pmt2);
99  pmt3.Damage();
100  std::vector<uint256> vMatchTxid3;
101  uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3, vIndex);
102  BOOST_CHECK(merkleRoot3 != merkleRoot1);
103  }
104  }
105  }
106 }
107 
108 BOOST_AUTO_TEST_CASE(pmt_malleability)
109 {
110  std::vector<uint256> vTxid{
111  uint256{1}, uint256{2},
112  uint256{3}, uint256{4},
113  uint256{5}, uint256{6},
114  uint256{7}, uint256{8},
115  uint256{9}, uint256{10},
116  uint256{9}, uint256{10},
117  };
118  std::vector<bool> vMatch = {false, false, false, false, false, false, false, false, false, true, true, false};
119 
120  CPartialMerkleTree tree(vTxid, vMatch);
121  std::vector<unsigned int> vIndex;
122  BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull());
123 }
124 
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid&#39;s represented by this partial merkle tree and their respective indices with...
Definition: block.h:68
unsigned int nHeight
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:54
Basic testing setup.
Definition: setup_common.h:49
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:64
static uint64_t InsecureRandRange(uint64_t range)
Definition: random.h:60
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:192
BOOST_AUTO_TEST_SUITE_END()
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
constexpr bool IsNull() const
Definition: uint256.h:42
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:422
256-bit opaque blob.
Definition: uint256.h:106
std::vector< CTransactionRef > vtx
Definition: block.h:72
BOOST_AUTO_TEST_CASE(pmt_test1)
Definition: pmt_tests.cpp:31
static uint64_t InsecureRandBits(int bits)
Definition: random.h:55
A mutable version of CTransaction.
Definition: transaction.h:379
#define BOOST_CHECK(expr)
Definition: object.cpp:17