From 5ab586f90b74d84e29156ebf6692b9e9055aa047 Mon Sep 17 00:00:00 2001 From: James O'Beirne Date: Mon, 11 Sep 2017 17:42:37 -0700 Subject: [PATCH] Consolidate CMerkleBlock constructor into a single method Incorporates feedback suggested by @sipa, @promag, @TheBlueMatt. --- src/merkleblock.cpp | 37 ++++++++----------------------------- src/merkleblock.h | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/merkleblock.cpp b/src/merkleblock.cpp index f0abea0611..3f07b4dac4 100644 --- a/src/merkleblock.cpp +++ b/src/merkleblock.cpp @@ -9,7 +9,8 @@ #include "consensus/consensus.h" #include "utilstrencodings.h" -CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter) + +CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set* txids) { header = block.GetBlockHeader(); @@ -22,36 +23,14 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter) for (unsigned int i = 0; i < block.vtx.size(); i++) { const uint256& hash = block.vtx[i]->GetHash(); - if (filter.IsRelevantAndUpdate(*block.vtx[i])) - { + if (txids && txids->count(hash)) { vMatch.push_back(true); - vMatchedTxn.push_back(std::make_pair(i, hash)); + } else if (filter && filter->IsRelevantAndUpdate(*block.vtx[i])) { + vMatch.push_back(true); + vMatchedTxn.emplace_back(i, hash); + } else { + vMatch.push_back(false); } - else - vMatch.push_back(false); - vHashes.push_back(hash); - } - - txn = CPartialMerkleTree(vHashes, vMatch); -} - -CMerkleBlock::CMerkleBlock(const CBlock& block, const std::set& txids) -{ - header = block.GetBlockHeader(); - - std::vector vMatch; - std::vector vHashes; - - vMatch.reserve(block.vtx.size()); - vHashes.reserve(block.vtx.size()); - - for (unsigned int i = 0; i < block.vtx.size(); i++) - { - const uint256& hash = block.vtx[i]->GetHash(); - if (txids.count(hash)) - vMatch.push_back(true); - else - vMatch.push_back(false); vHashes.push_back(hash); } diff --git a/src/merkleblock.h b/src/merkleblock.h index 20f2b36886..6c05f2c1f8 100644 --- a/src/merkleblock.h +++ b/src/merkleblock.h @@ -131,8 +131,12 @@ public: CBlockHeader header; CPartialMerkleTree txn; -public: - /** Public only for unit testing and relay testing (not relayed) */ + /** + * Public only for unit testing and relay testing (not relayed). + * + * Used only when a bloom filter is specified to allow + * testing the transactions which matched the bloom filter. + */ std::vector > vMatchedTxn; /** @@ -140,10 +144,10 @@ public: * Note that this will call IsRelevantAndUpdate on the filter for each transaction, * thus the filter will likely be modified. */ - CMerkleBlock(const CBlock& block, CBloomFilter& filter); + CMerkleBlock(const CBlock& block, CBloomFilter& filter) : CMerkleBlock(block, &filter, nullptr) { } // Create from a CBlock, matching the txids in the set - CMerkleBlock(const CBlock& block, const std::set& txids); + CMerkleBlock(const CBlock& block, const std::set& txids) : CMerkleBlock(block, nullptr, &txids) { } CMerkleBlock() {} @@ -154,6 +158,10 @@ public: READWRITE(header); READWRITE(txn); } + +private: + // Combined constructor to consolidate code + CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set* txids); }; #endif // BITCOIN_MERKLEBLOCK_H