LCOV - code coverage report
Current view: top level - src/test - pmt_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.9 % 94 93
Test Date: 2026-10-29 06:16:59 Functions: 100.0 % 8 8
Branches: 52.7 % 374 197

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-present 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/consensus.h>
       6                 :             : #include <consensus/merkle.h>
       7                 :             : #include <merkleblock.h>
       8                 :             : #include <serialize.h>
       9                 :             : #include <streams.h>
      10                 :             : #include <test/util/common.h>
      11                 :             : #include <test/util/random.h>
      12                 :             : #include <test/util/setup_common.h>
      13                 :             : #include <uint256.h>
      14                 :             : 
      15                 :             : #include <vector>
      16                 :             : 
      17                 :             : #include <boost/test/unit_test.hpp>
      18                 :             : 
      19                 :           0 : class CPartialMerkleTreeTester : public CPartialMerkleTree
      20                 :             : {
      21                 :             : public:
      22                 :         336 :     CPartialMerkleTreeTester(FastRandomContext& rng) : m_rng{rng} {}
      23                 :             : 
      24                 :             :     // flip one bit in one of the hashes - this should break the authentication
      25                 :         672 :     void Damage() {
      26         [ -  + ]:         672 :         unsigned int n = m_rng.randrange(vHash.size());
      27                 :         672 :         int bit = m_rng.randbits(8);
      28                 :         672 :         *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
      29                 :         672 :     }
      30                 :             : 
      31                 :             :     FastRandomContext& m_rng;
      32                 :             : };
      33                 :             : 
      34                 :             : BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
      35                 :             : 
      36   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(pmt_test1)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      37                 :             : {
      38                 :           1 :     static const unsigned int tx_counts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
      39                 :             : 
      40         [ +  + ]:          13 :     for (int i = 0; i < 12; i++) {
      41                 :          12 :         unsigned int nTx = tx_counts[i];
      42                 :             : 
      43                 :             :         // build a block with some dummy transactions
      44                 :          12 :         CBlock block;
      45         [ +  + ]:        6500 :         for (unsigned int j=0; j<nTx; j++) {
      46         [ +  - ]:        6488 :             CMutableTransaction tx;
      47                 :        6488 :             tx.nLockTime = j; // actual transaction data doesn't matter; just make the nLockTime's unique
      48   [ +  -  +  -  :       12976 :             block.vtx.push_back(MakeTransactionRef(std::move(tx)));
                   -  + ]
      49                 :        6488 :         }
      50                 :             : 
      51                 :             :         // calculate actual merkle root and height
      52         [ +  - ]:          12 :         uint256 merkleRoot1 = BlockMerkleRoot(block);
      53         [ +  - ]:          12 :         std::vector<Txid> vTxid(nTx);
      54         [ +  + ]:        6500 :         for (unsigned int j=0; j<nTx; j++)
      55                 :        6488 :             vTxid[j] = block.vtx[j]->GetHash();
      56                 :          12 :         int nHeight = 1, nTx_ = nTx;
      57         [ +  + ]:          91 :         while (nTx_ > 1) {
      58                 :          79 :             nTx_ = (nTx_+1)/2;
      59                 :          79 :             nHeight++;
      60                 :             :         }
      61                 :             : 
      62                 :             :         // check with random subsets with inclusion chances 1, 1/2, 1/4, ..., 1/128
      63         [ +  + ]:         180 :         for (int att = 1; att < 15; att++) {
      64                 :             :             // build random subset of txid's
      65         [ +  - ]:         168 :             std::vector<bool> vMatch(nTx, false);
      66                 :         168 :             std::vector<Txid> vMatchTxid1;
      67         [ +  + ]:       91000 :             for (unsigned int j=0; j<nTx; j++) {
      68                 :       90832 :                 bool fInclude = m_rng.randbits(att / 2) == 0;
      69                 :       90832 :                 vMatch[j] = fInclude;
      70         [ +  + ]:       90832 :                 if (fInclude)
      71         [ +  - ]:       19217 :                     vMatchTxid1.push_back(vTxid[j]);
      72                 :             :             }
      73                 :             : 
      74                 :             :             // build the partial merkle tree
      75         [ +  - ]:         168 :             CPartialMerkleTree pmt1(vTxid, vMatch);
      76                 :             : 
      77                 :             :             // serialize
      78                 :         168 :             DataStream ss{};
      79         [ +  - ]:         168 :             ss << pmt1;
      80                 :             : 
      81                 :             :             // verify CPartialMerkleTree's size guarantees
      82   [ -  +  +  + ]:         168 :             unsigned int n = std::min<unsigned int>(nTx, 1 + vMatchTxid1.size()*nHeight);
      83   [ +  -  -  +  :         336 :             BOOST_CHECK(ss.size() <= 10 + (258*n+7)/8);
          -  +  +  -  +  
                      - ]
      84                 :             : 
      85                 :             :             // deserialize into a tester copy
      86         [ +  - ]:         168 :             CPartialMerkleTreeTester pmt2{m_rng};
      87         [ +  - ]:         168 :             ss >> pmt2;
      88                 :             : 
      89                 :             :             // extract merkle root and matched txids from copy
      90                 :         168 :             std::vector<Txid> vMatchTxid2;
      91                 :         168 :             std::vector<unsigned int> vIndex;
      92         [ +  - ]:         168 :             uint256 merkleRoot2 = pmt2.ExtractMatches(vMatchTxid2, vIndex);
      93                 :             : 
      94                 :             :             // check that it has the same merkle root as the original, and a valid one
      95   [ +  -  -  +  :         336 :             BOOST_CHECK(merkleRoot1 == merkleRoot2);
             +  -  +  - ]
      96   [ +  -  +  -  :         504 :             BOOST_CHECK(!merkleRoot2.IsNull());
                   +  - ]
      97                 :             : 
      98                 :             :             // check that it contains the matched transactions (in the same order!)
      99   [ +  -  +  - ]:         336 :             BOOST_CHECK(vMatchTxid1 == vMatchTxid2);
     100                 :             : 
     101                 :             :             // check that random bit flips break the authentication
     102         [ +  + ]:         840 :             for (int j=0; j<4; j++) {
     103         [ +  - ]:         672 :                 CPartialMerkleTreeTester pmt3(pmt2);
     104                 :         672 :                 pmt3.Damage();
     105                 :         672 :                 std::vector<Txid> vMatchTxid3;
     106         [ +  - ]:         672 :                 uint256 merkleRoot3 = pmt3.ExtractMatches(vMatchTxid3, vIndex);
     107   [ +  -  +  -  :        2016 :                 BOOST_CHECK(merkleRoot3 != merkleRoot1);
                   +  - ]
     108                 :        1344 :             }
     109                 :         504 :         }
     110                 :          12 :     }
     111                 :           1 : }
     112                 :             : 
     113   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(pmt_malleability)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     114                 :             : {
     115                 :           1 :     std::vector<Txid> vTxid{
     116                 :           1 :         Txid::FromUint256(uint256{1}), Txid::FromUint256(uint256{2}),
     117                 :           1 :         Txid::FromUint256(uint256{3}), Txid::FromUint256(uint256{4}),
     118                 :           1 :         Txid::FromUint256(uint256{5}), Txid::FromUint256(uint256{6}),
     119                 :           1 :         Txid::FromUint256(uint256{7}), Txid::FromUint256(uint256{8}),
     120                 :           1 :         Txid::FromUint256(uint256{9}), Txid::FromUint256(uint256{10}),
     121                 :           1 :         Txid::FromUint256(uint256{9}), Txid::FromUint256(uint256{10}),
     122                 :           1 :     };
     123         [ +  - ]:           1 :     std::vector<bool> vMatch = {false, false, false, false, false, false, false, false, false, true, true, false};
     124                 :             : 
     125         [ +  - ]:           1 :     CPartialMerkleTree tree(vTxid, vMatch);
     126                 :           1 :     std::vector<unsigned int> vIndex;
     127   [ +  -  +  -  :           3 :     BOOST_CHECK(tree.ExtractMatches(vTxid, vIndex).IsNull());
                   +  - ]
     128                 :           2 : }
     129                 :             : 
     130   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(pmt_malformed)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     131                 :             : {
     132                 :             :     // Deserialize a partial merkle tree with the given fields and extract its matches.
     133                 :          10 :     const auto extract{[](uint32_t tx_count, uint32_t hash_count, std::vector<unsigned char> bits) {
     134                 :           9 :         std::vector<uint256> hashes;
     135   [ +  -  +  + ]:          34 :         for (uint32_t i{1}; i <= hash_count; ++i) hashes.emplace_back(i);
     136                 :           9 :         DataStream ss;
     137   [ +  -  +  -  :           9 :         ss << tx_count << hashes << bits;
                   +  - ]
     138         [ +  - ]:           9 :         CPartialMerkleTree tree;
     139         [ +  - ]:           9 :         ss >> tree;
     140                 :           9 :         std::vector<Txid> matches;
     141                 :           9 :         std::vector<unsigned int> indexes;
     142         [ +  - ]:           9 :         return tree.ExtractMatches(matches, indexes);
     143                 :          18 :     }};
     144                 :             : 
     145                 :             :     // The transaction count is limited to MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT.
     146                 :           1 :     constexpr uint32_t max_tx_count{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT};
     147                 :           1 :     static_assert(max_tx_count == 16666);
     148   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(extract(max_tx_count, 1, {0x00}), uint256{1});
     149   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(max_tx_count + 1, 1, {0x00}).IsNull());
                   +  - ]
     150                 :             :     // An empty tree, more hashes than transactions, and fewer bits than hashes are
     151                 :             :     // rejected before traversal. Traversal would reject each of them anyway, so these
     152                 :             :     // check the result, not the early returns.
     153   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(0, 0, {0x00}).IsNull());
                   +  - ]
     154   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(1, 2, {0x00}).IsNull());
                   +  - ]
     155   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(9, 9, {0xff}).IsNull());
                   +  - ]
     156                 :             :     // Traversal runs out of hashes: the root is a parent of a match, so both of its
     157                 :             :     // leaves need a hash, but only one is provided.
     158   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(2, 1, {0x01}).IsNull());
                   +  - ]
     159                 :             :     // Traversal runs out of bits: eight bits reach the third leaf but not the fourth.
     160   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(16, 8, {0xff}).IsNull());
                   +  - ]
     161                 :             :     // Not all bits are consumed: a second byte of bits is more than padding.
     162   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(1, 1, {0x00, 0x00}).IsNull());
                   +  - ]
     163                 :             :     // Not all hashes are consumed: the root is not a parent of a match, so only its
     164                 :             :     // own hash is used.
     165   [ +  -  +  -  :           3 :     BOOST_CHECK(extract(2, 2, {0x00}).IsNull());
                   +  - ]
     166                 :           1 : }
     167                 :             : 
     168                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1