LCOV - code coverage report
Current view: top level - src/test - pmt_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.4 % 63 62
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 5 5
Branches: 54.4 % 204 111

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

Generated by: LCOV version 2.0-1