LCOV - code coverage report
Current view: top level - src - merkleblock.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 112 112 100.0 %
Date: 2023-10-05 15:40:34 Functions: 9 9 100.0 %
Branches: 83 104 79.8 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2020 The Bitcoin Core developers
       3                 :            : // Distributed under the MIT software license, see the accompanying
       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :            : 
       6                 :            : #include <merkleblock.h>
       7                 :            : 
       8                 :            : #include <hash.h>
       9                 :            : #include <consensus/consensus.h>
      10                 :            : 
      11                 :            : 
      12                 :       2763 : std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits)
      13                 :            : {
      14         [ +  - ]:       2763 :     std::vector<unsigned char> ret((bits.size() + 7) / 8);
      15         [ +  + ]:   11367424 :     for (unsigned int p = 0; p < bits.size(); p++) {
      16         [ +  - ]:   11364661 :         ret[p / 8] |= bits[p] << (p % 8);
      17                 :   11364661 :     }
      18                 :       2763 :     return ret;
      19         [ +  - ]:       2763 : }
      20                 :            : 
      21                 :       2230 : std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes)
      22                 :            : {
      23         [ +  - ]:       2230 :     std::vector<bool> ret(bytes.size() * 8);
      24         [ +  + ]:   25233334 :     for (unsigned int p = 0; p < ret.size(); p++) {
      25         [ +  - ]:   25231104 :         ret[p] = (bytes[p / 8] & (1 << (p % 8))) != 0;
      26                 :   25231104 :     }
      27                 :       2230 :     return ret;
      28         [ +  - ]:       2230 : }
      29                 :            : 
      30                 :       2720 : CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
      31                 :            : {
      32         [ +  - ]:       2720 :     header = block.GetBlockHeader();
      33                 :            : 
      34                 :       2720 :     std::vector<bool> vMatch;
      35                 :       2720 :     std::vector<uint256> vHashes;
      36                 :            : 
      37         [ +  - ]:       2720 :     vMatch.reserve(block.vtx.size());
      38         [ +  - ]:       2720 :     vHashes.reserve(block.vtx.size());
      39                 :            : 
      40         [ +  + ]:     253903 :     for (unsigned int i = 0; i < block.vtx.size(); i++)
      41                 :            :     {
      42         [ +  - ]:     251183 :         const uint256& hash = block.vtx[i]->GetHash();
      43   [ +  +  +  -  :     251183 :         if (txids && txids->count(hash)) {
                   +  + ]
      44         [ +  - ]:      72133 :             vMatch.push_back(true);
      45   [ +  +  +  -  :     251183 :         } else if (filter && filter->IsRelevantAndUpdate(*block.vtx[i])) {
                   +  + ]
      46         [ +  - ]:      96526 :             vMatch.push_back(true);
      47         [ +  - ]:      96526 :             vMatchedTxn.emplace_back(i, hash);
      48                 :      96526 :         } else {
      49         [ +  - ]:      82524 :             vMatch.push_back(false);
      50                 :            :         }
      51         [ +  - ]:     251183 :         vHashes.push_back(hash);
      52                 :     251183 :     }
      53                 :            : 
      54         [ +  - ]:       2720 :     txn = CPartialMerkleTree(vHashes, vMatch);
      55                 :       2720 : }
      56                 :            : 
      57                 :     332228 : uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::vector<uint256> &vTxid) {
      58                 :            :     //we can never have zero txs in a merkle block, we always need the coinbase tx
      59                 :            :     //if we do not have this assert, we can hit a memory access violation when indexing into vTxid
      60         [ +  - ]:     332228 :     assert(vTxid.size() != 0);
      61         [ +  + ]:     332228 :     if (height == 0) {
      62                 :            :         // hash at height 0 is the txids themselves
      63                 :     251183 :         return vTxid[pos];
      64                 :            :     } else {
      65                 :            :         // calculate left hash
      66                 :      81045 :         uint256 left = CalcHash(height-1, pos*2, vTxid), right;
      67                 :            :         // calculate right hash if not beyond the end of the array - copy left hash otherwise
      68         [ +  + ]:      81045 :         if (pos*2+1 < CalcTreeWidth(height-1))
      69                 :      80989 :             right = CalcHash(height-1, pos*2+1, vTxid);
      70                 :            :         else
      71                 :         56 :             right = left;
      72                 :            :         // combine subhashes
      73                 :      81045 :         return Hash(left, right);
      74                 :            :     }
      75                 :     332228 : }
      76                 :            : 
      77                 :     337767 : void CPartialMerkleTree::TraverseAndBuild(int height, unsigned int pos, const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) {
      78                 :            :     // determine whether this node is the parent of at least one matched txid
      79                 :     337767 :     bool fParentOfMatch = false;
      80   [ +  +  +  + ]:    3208407 :     for (unsigned int p = pos << height; p < (pos+1) << height && p < nTransactions; p++)
      81                 :    2870640 :         fParentOfMatch |= vMatch[p];
      82                 :            :     // store as flag bit
      83                 :     337767 :     vBits.push_back(fParentOfMatch);
      84   [ +  +  +  + ]:     337767 :     if (height==0 || !fParentOfMatch) {
      85                 :            :         // if at height 0, or nothing interesting below, store hash and stop
      86                 :     170194 :         vHash.push_back(CalcHash(height, pos, vTxid));
      87                 :     170194 :     } else {
      88                 :            :         // otherwise, don't store any hash, but descend into the subtrees
      89                 :     167573 :         TraverseAndBuild(height-1, pos*2, vTxid, vMatch);
      90         [ +  + ]:     167573 :         if (pos*2+1 < CalcTreeWidth(height-1))
      91                 :     167474 :             TraverseAndBuild(height-1, pos*2+1, vTxid, vMatch);
      92                 :            :     }
      93                 :     337767 : }
      94                 :            : 
      95                 :      78360 : uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, unsigned int &nBitsUsed, unsigned int &nHashUsed, std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
      96         [ +  + ]:      78360 :     if (nBitsUsed >= vBits.size()) {
      97                 :            :         // overflowed the bits array - failure
      98                 :         49 :         fBad = true;
      99                 :         49 :         return uint256();
     100                 :            :     }
     101                 :      78311 :     bool fParentOfMatch = vBits[nBitsUsed++];
     102   [ +  +  +  + ]:      78311 :     if (height==0 || !fParentOfMatch) {
     103                 :            :         // if at height 0, or nothing interesting below, use stored hash and do not descend
     104         [ +  + ]:      39126 :         if (nHashUsed >= vHash.size()) {
     105                 :            :             // overflowed the hash array - failure
     106                 :       1681 :             fBad = true;
     107                 :       1681 :             return uint256();
     108                 :            :         }
     109                 :      37445 :         const uint256 &hash = vHash[nHashUsed++];
     110   [ +  +  +  + ]:      37445 :         if (height==0 && fParentOfMatch) { // in case of height 0, we have a matched txid
     111                 :      36455 :             vMatch.push_back(hash);
     112                 :      36455 :             vnIndex.push_back(pos);
     113                 :      36455 :         }
     114                 :      37445 :         return hash;
     115                 :            :     } else {
     116                 :            :         // otherwise, descend into the subtrees to extract matched txids and hashes
     117                 :      39185 :         uint256 left = TraverseAndExtract(height-1, pos*2, nBitsUsed, nHashUsed, vMatch, vnIndex), right;
     118         [ +  + ]:      39185 :         if (pos*2+1 < CalcTreeWidth(height-1)) {
     119                 :      39033 :             right = TraverseAndExtract(height-1, pos*2+1, nBitsUsed, nHashUsed, vMatch, vnIndex);
     120         [ +  + ]:      39033 :             if (right == left) {
     121                 :            :                 // The left and right branches should never be identical, as the transaction
     122                 :            :                 // hashes covered by them must each be unique.
     123                 :      36166 :                 fBad = true;
     124                 :      36166 :             }
     125                 :      39033 :         } else {
     126                 :        152 :             right = left;
     127                 :            :         }
     128                 :            :         // and combine them before returning
     129                 :      39185 :         return Hash(left, right);
     130                 :            :     }
     131                 :      78360 : }
     132                 :            : 
     133                 :       2720 : CPartialMerkleTree::CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch) : nTransactions(vTxid.size()), fBad(false) {
     134                 :            :     // reset state
     135                 :       2720 :     vBits.clear();
     136                 :       2720 :     vHash.clear();
     137                 :            : 
     138                 :            :     // calculate height of tree
     139                 :       2720 :     int nHeight = 0;
     140   [ +  -  +  + ]:       3101 :     while (CalcTreeWidth(nHeight) > 1)
     141                 :        381 :         nHeight++;
     142                 :            : 
     143                 :            :     // traverse the partial tree
     144         [ +  - ]:       2720 :     TraverseAndBuild(nHeight, 0, vTxid, vMatch);
     145                 :       2720 : }
     146                 :            : 
     147                 :      28920 : CPartialMerkleTree::CPartialMerkleTree() : nTransactions(0), fBad(true) {}
     148                 :            : 
     149                 :        280 : uint256 CPartialMerkleTree::ExtractMatches(std::vector<uint256> &vMatch, std::vector<unsigned int> &vnIndex) {
     150                 :        280 :     vMatch.clear();
     151                 :            :     // An empty set will not work
     152         [ +  + ]:        280 :     if (nTransactions == 0)
     153                 :        125 :         return uint256();
     154                 :            :     // check for excessively high numbers of transactions
     155         [ +  + ]:        155 :     if (nTransactions > MAX_BLOCK_WEIGHT / MIN_TRANSACTION_WEIGHT)
     156                 :          9 :         return uint256();
     157                 :            :     // there can never be more hashes provided than one for every txid
     158         [ +  + ]:        146 :     if (vHash.size() > nTransactions)
     159                 :          2 :         return uint256();
     160                 :            :     // there must be at least one bit per node in the partial tree, and at least one node per hash
     161         [ +  + ]:        144 :     if (vBits.size() < vHash.size())
     162                 :          2 :         return uint256();
     163                 :            :     // calculate height of tree
     164                 :        142 :     int nHeight = 0;
     165         [ +  + ]:       1073 :     while (CalcTreeWidth(nHeight) > 1)
     166                 :        931 :         nHeight++;
     167                 :            :     // traverse the partial tree
     168                 :        142 :     unsigned int nBitsUsed = 0, nHashUsed = 0;
     169                 :        142 :     uint256 hashMerkleRoot = TraverseAndExtract(nHeight, 0, nBitsUsed, nHashUsed, vMatch, vnIndex);
     170                 :            :     // verify that no problems occurred during the tree traversal
     171         [ +  + ]:        142 :     if (fBad)
     172                 :         82 :         return uint256();
     173                 :            :     // verify that all bits were consumed (except for the padding caused by serializing it as a byte sequence)
     174         [ +  + ]:         60 :     if ((nBitsUsed+7)/8 != (vBits.size()+7)/8)
     175                 :          5 :         return uint256();
     176                 :            :     // verify that all hashes were consumed
     177         [ +  + ]:         55 :     if (nHashUsed != vHash.size())
     178                 :          3 :         return uint256();
     179                 :         52 :     return hashMerkleRoot;
     180                 :        280 : }

Generated by: LCOV version 1.14