Line data Source code
1 : // Copyright (c) 2012-2013 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 "merkleblock.h"
6 : #include "serialize.h"
7 : #include "streams.h"
8 : #include "uint256.h"
9 : #include "arith_uint256.h"
10 : #include "version.h"
11 : #include "random.h"
12 : #include "test/test_bitcoin.h"
13 :
14 : #include <vector>
15 :
16 : #include <boost/assign/list_of.hpp>
17 : #include <boost/test/unit_test.hpp>
18 :
19 : using namespace std;
20 :
21 1680 : class CPartialMerkleTreeTester : public CPartialMerkleTree
22 : {
23 : public:
24 : // flip one bit in one of the hashes - this should break the authentication
25 672 : void Damage() {
26 1344 : unsigned int n = insecure_rand() % vHash.size();
27 672 : int bit = insecure_rand() % 256;
28 2016 : *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
29 672 : }
30 : };
31 :
32 1 : BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
33 :
34 6 : BOOST_AUTO_TEST_CASE(pmt_test1)
35 : {
36 1 : seed_insecure_rand(false);
37 : static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
38 :
39 13 : for (int n = 0; n < 12; n++) {
40 12 : unsigned int nTx = nTxCounts[n];
41 :
42 : // build a block with some dummy transactions
43 12 : CBlock block;
44 6500 : for (unsigned int j=0; j<nTx; j++) {
45 6488 : CMutableTransaction tx;
46 6488 : tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique
47 12976 : block.vtx.push_back(CTransaction(tx));
48 : }
49 :
50 : // calculate actual merkle root and height
51 12 : uint256 merkleRoot1 = block.ComputeMerkleRoot();
52 24 : std::vector<uint256> vTxid(nTx, uint256());
53 6500 : for (unsigned int j=0; j<nTx; j++)
54 19464 : vTxid[j] = block.vtx[j].GetHash();
55 12 : int nHeight = 1, nTx_ = nTx;
56 103 : while (nTx_ > 1) {
57 79 : nTx_ = (nTx_+1)/2;
58 79 : nHeight++;
59 : }
60 :
61 : // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128
62 168 : for (int att = 1; att < 15; att++) {
63 : // build random subset of txid's
64 336 : std::vector<bool> vMatch(nTx, false);
65 : std::vector<uint256> vMatchTxid1;
66 91000 : for (unsigned int j=0; j<nTx; j++) {
67 90832 : bool fInclude = (insecure_rand() & ((1 << (att/2)) - 1)) == 0;
68 181664 : vMatch[j] = fInclude;
69 90832 : if (fInclude)
70 38834 : vMatchTxid1.push_back(vTxid[j]);
71 : }
72 :
73 : // build the partial merkle tree
74 336 : CPartialMerkleTree pmt1(vTxid, vMatch);
75 :
76 : // serialize
77 : CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
78 : ss << pmt1;
79 :
80 : // verify CPartialMerkleTree's size guarantees
81 504 : unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight);
82 1344 : BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);
83 :
84 : // deserialize into a tester copy
85 : CPartialMerkleTreeTester pmt2;
86 : ss >> pmt2;
87 :
88 : // extract merkle root and matched txids from copy
89 : std::vector<uint256> vMatchTxid2;
90 168 : uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2);
91 :
92 : // check that it has the same merkle root as the original, and a valid one
93 1344 : BOOST_CHECK(merkleRoot1 == merkleRoot2);
94 1344 : BOOST_CHECK(!merkleRoot2.IsNull());
95 :
96 : // check that it contains the matched transactions (in the same order!)
97 1344 : BOOST_CHECK(vMatchTxid1 == vMatchTxid2);
98 :
99 : // check that random bit flips break the authentication
100 672 : for (int j=0; j<4; j++) {
101 : CPartialMerkleTreeTester pmt3(pmt2);
102 672 : pmt3.Damage();
103 : std::vector<uint256> vMatchTxid3;
104 672 : uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3);
105 5376 : BOOST_CHECK(merkleRoot3 != merkleRoot1);
106 : }
107 : }
108 : }
109 1 : }
110 :
111 6 : BOOST_AUTO_TEST_CASE(pmt_malleability)
112 : {
113 : std::vector<uint256> vTxid = boost::assign::list_of
114 3 : (ArithToUint256(1))(ArithToUint256(2))
115 4 : (ArithToUint256(3))(ArithToUint256(4))
116 4 : (ArithToUint256(5))(ArithToUint256(6))
117 4 : (ArithToUint256(7))(ArithToUint256(8))
118 4 : (ArithToUint256(9))(ArithToUint256(10))
119 4 : (ArithToUint256(9))(ArithToUint256(10));
120 13 : std::vector<bool> vMatch = boost::assign::list_of(false)(false)(false)(false)(false)(false)(false)(false)(false)(true)(true)(false);
121 :
122 2 : CPartialMerkleTree tree(vTxid, vMatch);
123 : std::vector<uint256> vTxid2;
124 9 : BOOST_CHECK(tree.ExtractMatches(vTxid).IsNull());
125 1 : }
126 :
127 3 : BOOST_AUTO_TEST_SUITE_END()
|