Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #11293: Deduplicate CMerkleBlock construction code, add test co…
…verage 46ce223 Add tests for CMerkleBlock usage with txids specified (James O'Beirne) 5ab586f Consolidate CMerkleBlock constructor into a single method (James O'Beirne) Pull request description: What started as a simple task to add test coverage ended up giving way to a light refactoring. This consolidates the mostly-identical `CMerkleBlock` constructors into one (using C++11 constructor delegation) and adds coverage for the by-txids construction case. ### Before ![selection_006](https://user-images.githubusercontent.com/73197/30242104-0f381fe4-9545-11e7-9617-83b87fce0456.png) ### After ![selection_008](https://user-images.githubusercontent.com/73197/30242107-1425dfaa-9545-11e7-9e6b-2c3432517dd1.png) Tree-SHA512: eed84ed3e8bfc43473077b575c8252759a857e37275e4b36ca7cc2c17a65895e5f494bfd9d4aeab09fc6e98fc6a9c641ac7ecc0ddbeefe01a9e4308e7909e529
- Loading branch information
Showing
7 changed files
with
119 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) 2012-2017 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include "merkleblock.h" | ||
#include "uint256.h" | ||
#include "test/test_bitcoin.h" | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
|
||
BOOST_FIXTURE_TEST_SUITE(merkleblock_tests, BasicTestingSetup) | ||
|
||
/** | ||
* Create a CMerkleBlock using a list of txids which will be found in the | ||
* given block. | ||
*/ | ||
BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_found) | ||
{ | ||
CBlock block = getBlock13b8a(); | ||
|
||
std::set<uint256> txids; | ||
|
||
// Last txn in block. | ||
uint256 txhash1 = uint256S("0x74d681e0e03bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20"); | ||
|
||
// Second txn in block. | ||
uint256 txhash2 = uint256S("0xf9fc751cb7dc372406a9f8d738d5e6f8f63bab71986a39cf36ee70ee17036d07"); | ||
|
||
txids.insert(txhash1); | ||
txids.insert(txhash2); | ||
|
||
CMerkleBlock merkleBlock(block, txids); | ||
|
||
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex()); | ||
|
||
// vMatchedTxn is only used when bloom filter is specified. | ||
BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0); | ||
|
||
std::vector<uint256> vMatched; | ||
std::vector<unsigned int> vIndex; | ||
|
||
BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex()); | ||
BOOST_CHECK_EQUAL(vMatched.size(), 2); | ||
|
||
// Ordered by occurrence in depth-first tree traversal. | ||
BOOST_CHECK_EQUAL(vMatched[0].ToString(), txhash2.ToString()); | ||
BOOST_CHECK_EQUAL(vIndex[0], 1); | ||
|
||
BOOST_CHECK_EQUAL(vMatched[1].ToString(), txhash1.ToString()); | ||
BOOST_CHECK_EQUAL(vIndex[1], 8); | ||
} | ||
|
||
|
||
/** | ||
* Create a CMerkleBlock using a list of txids which will not be found in the | ||
* given block. | ||
*/ | ||
BOOST_AUTO_TEST_CASE(merkleblock_construct_from_txids_not_found) | ||
{ | ||
CBlock block = getBlock13b8a(); | ||
|
||
std::set<uint256> txids2; | ||
txids2.insert(uint256S("0xc0ffee00003bafa802c8aa084379aa98d9fcd632ddc2ed9782b586ec87451f20")); | ||
CMerkleBlock merkleBlock(block, txids2); | ||
|
||
BOOST_CHECK_EQUAL(merkleBlock.header.GetHash().GetHex(), block.GetHash().GetHex()); | ||
BOOST_CHECK_EQUAL(merkleBlock.vMatchedTxn.size(), 0); | ||
|
||
std::vector<uint256> vMatched; | ||
std::vector<unsigned int> vIndex; | ||
|
||
BOOST_CHECK_EQUAL(merkleBlock.txn.ExtractMatches(vMatched, vIndex).GetHex(), block.hashMerkleRoot.GetHex()); | ||
BOOST_CHECK_EQUAL(vMatched.size(), 0); | ||
BOOST_CHECK_EQUAL(vIndex.size(), 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.