Skip to content

Commit 5ab586f

Browse files
committed
Consolidate CMerkleBlock constructor into a single method
Incorporates feedback suggested by @sipa, @promag, @TheBlueMatt.
1 parent 3255d63 commit 5ab586f

File tree

2 files changed

+19
-32
lines changed

2 files changed

+19
-32
lines changed

src/merkleblock.cpp

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,8 @@
99
#include "consensus/consensus.h"
1010
#include "utilstrencodings.h"
1111

12-
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter& filter)
13-
{
14-
header = block.GetBlockHeader();
1512

16-
std::vector<bool> vMatch;
17-
std::vector<uint256> vHashes;
18-
19-
vMatch.reserve(block.vtx.size());
20-
vHashes.reserve(block.vtx.size());
21-
22-
for (unsigned int i = 0; i < block.vtx.size(); i++)
23-
{
24-
const uint256& hash = block.vtx[i]->GetHash();
25-
if (filter.IsRelevantAndUpdate(*block.vtx[i]))
26-
{
27-
vMatch.push_back(true);
28-
vMatchedTxn.push_back(std::make_pair(i, hash));
29-
}
30-
else
31-
vMatch.push_back(false);
32-
vHashes.push_back(hash);
33-
}
34-
35-
txn = CPartialMerkleTree(vHashes, vMatch);
36-
}
37-
38-
CMerkleBlock::CMerkleBlock(const CBlock& block, const std::set<uint256>& txids)
13+
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
3914
{
4015
header = block.GetBlockHeader();
4116

@@ -48,10 +23,14 @@ CMerkleBlock::CMerkleBlock(const CBlock& block, const std::set<uint256>& txids)
4823
for (unsigned int i = 0; i < block.vtx.size(); i++)
4924
{
5025
const uint256& hash = block.vtx[i]->GetHash();
51-
if (txids.count(hash))
26+
if (txids && txids->count(hash)) {
5227
vMatch.push_back(true);
53-
else
28+
} else if (filter && filter->IsRelevantAndUpdate(*block.vtx[i])) {
29+
vMatch.push_back(true);
30+
vMatchedTxn.emplace_back(i, hash);
31+
} else {
5432
vMatch.push_back(false);
33+
}
5534
vHashes.push_back(hash);
5635
}
5736

src/merkleblock.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,23 @@ class CMerkleBlock
131131
CBlockHeader header;
132132
CPartialMerkleTree txn;
133133

134-
public:
135-
/** Public only for unit testing and relay testing (not relayed) */
134+
/**
135+
* Public only for unit testing and relay testing (not relayed).
136+
*
137+
* Used only when a bloom filter is specified to allow
138+
* testing the transactions which matched the bloom filter.
139+
*/
136140
std::vector<std::pair<unsigned int, uint256> > vMatchedTxn;
137141

138142
/**
139143
* Create from a CBlock, filtering transactions according to filter
140144
* Note that this will call IsRelevantAndUpdate on the filter for each transaction,
141145
* thus the filter will likely be modified.
142146
*/
143-
CMerkleBlock(const CBlock& block, CBloomFilter& filter);
147+
CMerkleBlock(const CBlock& block, CBloomFilter& filter) : CMerkleBlock(block, &filter, nullptr) { }
144148

145149
// Create from a CBlock, matching the txids in the set
146-
CMerkleBlock(const CBlock& block, const std::set<uint256>& txids);
150+
CMerkleBlock(const CBlock& block, const std::set<uint256>& txids) : CMerkleBlock(block, nullptr, &txids) { }
147151

148152
CMerkleBlock() {}
149153

@@ -154,6 +158,10 @@ class CMerkleBlock
154158
READWRITE(header);
155159
READWRITE(txn);
156160
}
161+
162+
private:
163+
// Combined constructor to consolidate code
164+
CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids);
157165
};
158166

159167
#endif // BITCOIN_MERKLEBLOCK_H

0 commit comments

Comments
 (0)