PIVX Core  5.6.99
P2P Digital Currency
merkleblock_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2017 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 "uint256.h"
7 #include "test/test_pivx.h"
8 #include "test/util/blocksutil.h"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 
14 
15 
19 BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found)
20 {
21  CBlock block = getBlock13b8a();
22 
23  std::set<uint256> txids;
24 
25  // Last txn in block.
26  uint256 txhash1 = uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20");
27 
28  // Second txn in block.
29  uint256 txhash2 = uint256S("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07");
30 
31  txids.insert(txhash1);
32  txids.insert(txhash2);
33 
34  CMerkleBlock merkleBlock(block, txids);
35 
36  BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
37 
38  // vMatchedTxn is only used when bloom filter is specified.
39  BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0);
40 
41  std::vector<uint256> vMatched;
42 
43  BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched).GetHex(), block.hashMerkleRoot.GetHex());
44  BOOST_CHECK_EQUAL(vMatched.size(), 2);
45 
46  // Ordered by occurrence in depth-first tree traversal.
47  BOOST_CHECK_EQUAL(vMatched[0].ToString(), txhash2.ToString());
48 
49  BOOST_CHECK_EQUAL(vMatched[1].ToString(), txhash1.ToString());
50 }
51 
52 
57 BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found)
58 {
59  CBlock block = getBlock13b8a();
60 
61  std::set<uint256> txids2;
62  txids2.insert(uint256S("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"));
63  CMerkleBlock merkleBlock(block, txids2);
64 
65  BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex());
66  BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0);
67 
68  std::vector<uint256> vMatched;
69 
70  BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched).GetHex(), block.hashMerkleRoot.GetHex());
71  BOOST_CHECK_EQUAL(vMatched.size(), 0);
72 }
73 
CBlock getBlock13b8a()
Definition: blocksutil.cpp:31
uint256 hashMerkleRoot
Definition: block.h:29
uint256 GetHash() const
Definition: block.cpp:15
Definition: block.h:80
Used to relay blocks as header + vector<merkle branch> to filtered nodes.
Definition: merkleblock.h:118
CBlockHeader header
Public only for unit testing.
Definition: merkleblock.h:121
std::vector< std::pair< unsigned int, uint256 > > vMatchedTxn
Public only for unit testing and relay testing (not relayed).
Definition: merkleblock.h:130
CPartialMerkleTree txn
Definition: merkleblock.h:122
uint256 ExtractMatches(std::vector< uint256 > &vMatch)
extract the matching txid's represented by this partial merkle tree.
std::string ToString() const
Definition: uint256.cpp:65
std::string GetHex() const
Definition: uint256.cpp:21
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
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.
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
Basic testing setup.
Definition: test_pivx.h:51
uint256 uint256S(const char *str)
Definition: uint256.h:157