You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pmt_tests.cpp 4.4KB

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