merkleblock_tests.cpp raw

   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 <merkleblock.h>
   6  #include <test/util/common.h>
   7  #include <test/util/setup_common.h>
   8  #include <uint256.h>
   9  
  10  #include <boost/test/unit_test.hpp>
  11  
  12  #include <set>
  13  #include <vector>
  14  
  15  BOOST_AUTO_TEST_SUITE(merkleblock_tests)
  16  
  17  /**
  18   * Create a CMerkleBlock using a list of txids which will be found in the
  19   * given block.
  20   */
  21  BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
  22  {
  23      CBlock block = getBlock13b8a();
  24  
  25      std::set<Txid> txids;
  26  
  27      // Last txn in block.
  28      constexpr Txid txhash1{"74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"};
  29  
  30      // Second txn in block.
  31      constexpr Txid txhash2{"f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07"};
  32  
  33      txids.insert(txhash1);
  34      txids.insert(txhash2);
  35  
  36      CMerkleBlock merkleBlock(block, txids);
  37  
  38      BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
  39  
  40      // vMatchedTxn is only used when bloom filter is specified.
  41      BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0U);
  42  
  43      std::vector<Txid> vMatched;
  44      std::vector<unsigned int> vIndex;
  45  
  46      BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex());
  47      BOOST_CHECK_EQUAL(vMatched.size(), 2U);
  48  
  49      // Ordered by occurrence in depth-first tree traversal.
  50      BOOST_CHECK_EQUAL(vMatched[0], txhash2);
  51      BOOST_CHECK_EQUAL(vIndex[0], 1U);
  52  
  53      BOOST_CHECK_EQUAL(vMatched[1], txhash1);
  54      BOOST_CHECK_EQUAL(vIndex[1], 8U);
  55  }
  56  
  57  
  58  /**
  59   * Create a CMerkleBlock using a list of txids which will not be found in the
  60   * given block.
  61   */
  62  BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
  63  {
  64      CBlock block = getBlock13b8a();
  65  
  66      std::set<Txid> txids2;
  67      txids2.insert(Txid{"c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"});
  68      CMerkleBlock merkleBlock(block, txids2);
  69  
  70      BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
  71      BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0U);
  72  
  73      std::vector<Txid> vMatched;
  74      std::vector<unsigned int> vIndex;
  75  
  76      BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex());
  77      BOOST_CHECK_EQUAL(vMatched.size(), 0U);
  78      BOOST_CHECK_EQUAL(vIndex.size(), 0U);
  79  }
  80  
  81  BOOST_AUTO_TEST_SUITE_END()
  82