Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
pmt_tests.cpp
Go to the documentation of this file.
1 #include <boost/test/unit_test.hpp>
2 
3 #include "uint256.h"
4 #include "main.h"
5 
6 using namespace std;
7 
9 {
10 public:
11  // flip one bit in one of the hashes - this should break the authentication
12  void Damage() {
13  unsigned int n = rand() % vHash.size();
14  int bit = rand() % 256;
15  uint256 &hash = vHash[n];
16  hash ^= ((uint256)1 << bit);
17  }
18 };
19 
20 BOOST_AUTO_TEST_SUITE(pmt_tests)
21 
23 {
24  static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
25 
26  for (int n = 0; n < 12; n++) {
27  unsigned int nTx = nTxCounts[n];
28 
29  // build a block with some dummy transactions
30  CBlock block;
31  for (unsigned int j=0; j<nTx; j++) {
32  CTransaction tx;
33  tx.nLockTime = rand(); // actual transaction data doesn't matter; just make the nLockTime's unique
34  block.vtx.push_back(tx);
35  }
36 
37  // calculate actual merkle root and height
38  uint256 merkleRoot1 = block.BuildMerkleTree();
39  std::vector<uint256> vTxid(nTx, 0);
40  for (unsigned int j=0; j<nTx; j++)
41  vTxid[j] = block.vtx[j].GetHash();
42  int nHeight = 1, nTx_ = nTx;
43  while (nTx_ > 1) {
44  nTx_ = (nTx_+1)/2;
45  nHeight++;
46  }
47 
48  // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128
49  for (int att = 1; att < 15; att++) {
50  // build random subset of txid's
51  std::vector<bool> vMatch(nTx, false);
52  std::vector<uint256> vMatchTxid1;
53  for (unsigned int j=0; j<nTx; j++) {
54  bool fInclude = (rand() & ((1 << (att/2)) - 1)) == 0;
55  vMatch[j] = fInclude;
56  if (fInclude)
57  vMatchTxid1.push_back(vTxid[j]);
58  }
59 
60  // build the partial merkle tree
61  CPartialMerkleTree pmt1(vTxid, vMatch);
62 
63  // serialize
64  CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
65  ss << pmt1;
66 
67  // verify CPartialMerkleTree's size guarantees
68  unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight);
69  BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);
70 
71  // deserialize into a tester copy
73  ss >> pmt2;
74 
75  // extract merkle root and matched txids from copy
76  std::vector<uint256> vMatchTxid2;
77  uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2);
78 
79  // check that it has the same merkle root as the original, and a valid one
80  BOOST_CHECK(merkleRoot1 == merkleRoot2);
81  BOOST_CHECK(merkleRoot2 != 0);
82 
83  // check that it contains the matched transactions (in the same order!)
84  BOOST_CHECK(vMatchTxid1 == vMatchTxid2);
85 
86  // check that random bit flips break the authentication
87  for (int j=0; j<4; j++) {
88  CPartialMerkleTreeTester pmt3(pmt2);
89  pmt3.Damage();
90  std::vector<uint256> vMatchTxid3;
91  uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3);
92  BOOST_CHECK(merkleRoot3 != merkleRoot1);
93  }
94  }
95  }
96 }
97 
98 BOOST_AUTO_TEST_SUITE_END()
Definition: main.h:1334
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:799
Data structure that represents a partial merkle tree.
Definition: main.h:1208
uint256 BuildMerkleTree() const
Definition: main.h:1386
unsigned int nLockTime
Definition: main.h:486
Random rand
Definition: db_bench.cc:290
size_type size() const
Definition: serialize.h:888
256-bit unsigned integer
Definition: uint256.h:537
uint256 ExtractMatches(std::vector< uint256 > &vMatch)
Definition: main.cpp:2544
BOOST_AUTO_TEST_CASE(pmt_test1)
Definition: pmt_tests.cpp:22
std::vector< CTransaction > vtx
Definition: main.h:1338
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: main.h:477
uint32_t hash
Definition: cache.cc:34