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