Bitcoin Core  28.1.0
P2P Digital Currency
merkleblock_tests.cpp
Go to the documentation of this file.
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>
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 
20 BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
21 {
22  CBlock block = getBlock13b8a();
23 
24  std::set<Txid> txids;
25 
26  // Last txn in block.
27  Txid txhash1{Txid::FromHex("74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value()};
28 
29  // Second txn in block.
30  Txid txhash2{Txid::FromHex("f9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07").value()};
31 
32  txids.insert(txhash1);
33  txids.insert(txhash2);
34 
35  CMerkleBlock merkleBlock(block, txids);
36 
37  BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
38 
39  // vMatchedTxn is only used when bloom filter is specified.
40  BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0U);
41 
42  std::vector<uint256> vMatched;
43  std::vector<unsigned int> vIndex;
44 
45  BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex());
46  BOOST_CHECK_EQUAL(vMatched.size(), 2U);
47 
48  // Ordered by occurrence in depth-first tree traversal.
49  BOOST_CHECK_EQUAL(vMatched[0].ToString(), txhash2.ToString());
50  BOOST_CHECK_EQUAL(vIndex[0], 1U);
51 
52  BOOST_CHECK_EQUAL(vMatched[1].ToString(), txhash1.ToString());
53  BOOST_CHECK_EQUAL(vIndex[1], 8U);
54 }
55 
56 
61 BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
62 {
63  CBlock block = getBlock13b8a();
64 
65  std::set<Txid> txids2;
66  txids2.insert(Txid::FromHex("c0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20").value());
67  CMerkleBlock merkleBlock(block, txids2);
68 
69  BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
70  BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0U);
71 
72  std::vector<uint256> vMatched;
73  std::vector<unsigned int> vIndex;
74 
75  BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex());
76  BOOST_CHECK_EQUAL(vMatched.size(), 0U);
77  BOOST_CHECK_EQUAL(vIndex.size(), 0U);
78 }
79 
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:129
uint256 ExtractMatches(std::vector< uint256 > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid&#39;s represented by this partial merkle tree and their respective indices with...
Definition: block.h:68
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:125
uint256 hashMerkleRoot
Definition: block.h:27
BOOST_AUTO_TEST_SUITE_END()
CPartialMerkleTree txn
Definition: merkleblock.h:130
uint256 GetHash() const
Definition: block.cpp:11
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:138
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
std::string GetHex() const
Definition: uint256.cpp:11
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
CBlock getBlock13b8a()
static std::optional< transaction_identifier > FromHex(std::string_view hex)
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:156