Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
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
>
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
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
81
BOOST_AUTO_TEST_SUITE_END
()
CBlockHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition
block.h:32
CBlockHeader::GetHash
uint256 GetHash() const
Definition
block.cpp:15
CBlock
Definition
block.h:74
CMerkleBlock
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition
merkleblock.h:127
CMerkleBlock::vMatchedTxn
std::vector< std::pair< unsigned int, Txid > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition
merkleblock.h:139
CMerkleBlock::header
CBlockHeader header
Public only for unit testing.
Definition
merkleblock.h:130
CMerkleBlock::txn
CPartialMerkleTree txn
Definition
merkleblock.h:131
CPartialMerkleTree::ExtractMatches
uint256 ExtractMatches(std::vector< Txid > &vMatch, std::vector< unsigned int > &vnIndex)
extract the matching txid's represented by this partial merkle tree and their respective indices with...
Definition
merkleblock.cpp:152
base_blob::GetHex
std::string GetHex() const
Definition
uint256.cpp:11
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ipc_tests)
Definition
ipc_tests.cpp:13
merkleblock.h
BOOST_AUTO_TEST_CASE
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.
Definition
merkleblock_tests.cpp:21
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition
object.cpp:17
getBlock13b8a
CBlock getBlock13b8a()
Definition
setup_common.cpp:614
setup_common.h
common.h
Txid
transaction_identifier< false > Txid
Txid commits to all transaction fields except the witness.
Definition
transaction_identifier.h:71
uint256.h
Generated on
for Bitcoin Core by
1.17.0