Bitcoin Core  31.0.0
P2P Digital Currency
merkleblock_tests.cpp
Go to the documentation of this file.
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>
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 
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 
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 
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:130
Definition: block.h:73
BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
Create a CMerkleBlock using a list of txids which will be found in the given block.
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:126
uint256 hashMerkleRoot
Definition: block.h:32
BOOST_AUTO_TEST_SUITE_END()
CPartialMerkleTree txn
Definition: merkleblock.h:131
std::vector< std::pair< unsigned int, Txid > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:139
uint256 GetHash() const
Definition: block.cpp:15
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
uint256 ExtractMatches(std::vector< Txid > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid&#39;s represented by this partial merkle tree and their respective indices with...
std::string GetHex() const
Definition: uint256.cpp:11
CBlock getBlock13b8a()