LCOV - code coverage report
Current view: top level - src/test - pmt_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 68 68 100.0 %
Date: 2020-09-26 01:30:44 Functions: 23 23 100.0 %

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

Generated by: LCOV version 1.15