Bitcoin Core  22.99.0
P2P Digital Currency
pmt_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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 <arith_uint256.h>
6 #include <consensus/merkle.h>
7 #include <merkleblock.h>
8 #include <serialize.h>
9 #include <streams.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 
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
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 = {
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 
arith_uint256.h
merkleblock.h
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:14
CPartialMerkleTree
Data structure that represents a partial merkle tree.
Definition: merkleblock.h:54
streams.h
setup_common.h
uint256.h
InsecureRandRange
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:68
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
version.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
CMutableTransaction::nLockTime
uint32_t nLockTime
Definition: transaction.h:349
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
uint256
256-bit opaque blob.
Definition: uint256.h:124
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(pmt_test1)
Definition: pmt_tests.cpp:31
CPartialMerkleTree::ExtractMatches
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid's represented by this partial merkle tree and their respective indices with...
Definition: merkleblock.cpp:149
CDataStream::size
size_type size() const
Definition: streams.h:255
merkle.h
CBlock
Definition: block.h:62
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:31
CBlock::vtx
std::vector< CTransactionRef > vtx
Definition: block.h:66
CPartialMerkleTreeTester::Damage
void Damage()
Definition: pmt_tests.cpp:22
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
InsecureRandBits
static uint64_t InsecureRandBits(int bits)
Definition: setup_common.h:67
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
CPartialMerkleTree::vHash
std::vector< uint256 > vHash
txids and internal hashes
Definition: merkleblock.h:64
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
CPartialMerkleTreeTester
Definition: pmt_tests.cpp:18
ArithToUint256
uint256 ArithToUint256(const arith_uint256 &a)
Definition: arith_uint256.cpp:246
BlockMerkleRoot
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12