Browse Source

Consolidate CMerkleBlock constructor into a single method

Incorporates feedback suggested by @sipa, @promag, @TheBlueMatt.
0.16
James O'Beirne 7 years ago
parent
commit
5ab586f90b
  1. 35
      src/merkleblock.cpp
  2. 16
      src/merkleblock.h

35
src/merkleblock.cpp

@ -9,33 +9,8 @@
#include "consensus/consensus.h" #include "consensus/consensus.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
{
header = block.GetBlockHeader();
std::vector<bool> vMatch;
std::vector<uint256> 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 (filter.IsRelevantAndUpdate(*block.vtx[i]))
{
vMatch.push_back(true);
vMatchedTxn.push_back(std::make_pair(i, hash));
}
else
vMatch.push_back(false);
vHashes.push_back(hash);
}
txn = CPartialMerkleTree(vHashes, vMatch); CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
}
CMerkleBlock::CMerkleBlock(const CBlock& block, const std::set<uint256>& txids)
{ {
header = block.GetBlockHeader(); header = block.GetBlockHeader();
@ -48,10 +23,14 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, const std::set<uint256>& txids)
for (unsigned int i = 0; i < block.vtx.size(); i++) for (unsigned int i = 0; i < block.vtx.size(); i++)
{ {
const uint256& hash = block.vtx[i]->GetHash(); const uint256& hash = block.vtx[i]->GetHash();
if (txids.count(hash)) if (txids && txids->count(hash)) {
vMatch.push_back(true); vMatch.push_back(true);
else } else if (filter && filter->IsRelevantAndUpdate(*block.vtx[i])) {
vMatch.push_back(true);
vMatchedTxn.emplace_back(i, hash);
} else {
vMatch.push_back(false); vMatch.push_back(false);
}
vHashes.push_back(hash); vHashes.push_back(hash);
} }

16
src/merkleblock.h

@ -131,8 +131,12 @@ public:
CBlockHeader header; CBlockHeader header;
CPartialMerkleTree txn; 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<std::pair<unsigned int, uint256> > vMatchedTxn; std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
/** /**
@ -140,10 +144,10 @@ public:
* Note that this will call IsRelevantAndUpdate on the filter for each transaction, * Note that this will call IsRelevantAndUpdate on the filter for each transaction,
* thus the filter will likely be modified. * 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 // Create from a CBlock, matching the txids in the set
CMerkleBlock(const CBlock& block, const std::set<uint256>& txids); CMerkleBlock(const CBlock& block, const std::set<uint256>& txids) : CMerkleBlock(block, nullptr, &txids) { }
CMerkleBlock() {} CMerkleBlock() {}
@ -154,6 +158,10 @@ public:
READWRITE(header); READWRITE(header);
READWRITE(txn); READWRITE(txn);
} }
private:
// Combined constructor to consolidate code
CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids);
}; };
#endif // BITCOIN_MERKLEBLOCK_H #endif // BITCOIN_MERKLEBLOCK_H

Loading…
Cancel
Save