Skip to content

Commit 73747af

Browse files
committed
Convert merkleblock to new serialization
1 parent d06fedd commit 73747af

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/merkleblock.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99
#include <consensus/consensus.h>
1010

1111

12+
std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits)
13+
{
14+
std::vector<unsigned char> ret((bits.size() + 7) / 8);
15+
for (unsigned int p = 0; p < bits.size(); p++) {
16+
ret[p / 8] |= bits[p] << (p % 8);
17+
}
18+
return ret;
19+
}
20+
21+
std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes)
22+
{
23+
std::vector<bool> ret(bytes.size() * 8);
24+
for (unsigned int p = 0; p < ret.size(); p++) {
25+
ret[p] = (bytes[p / 8] & (1 << (p % 8))) != 0;
26+
}
27+
return ret;
28+
}
29+
1230
CMerkleBlock::CMerkleBlock(const CBlock& block, CBloomFilter* filter, const std::set<uint256>* txids)
1331
{
1432
header = block.GetBlockHeader();

src/merkleblock.h

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
#include <vector>
1515

16+
// Helper functions for serialization.
17+
std::vector<unsigned char> BitsToBytes(const std::vector<bool>& bits);
18+
std::vector<bool> BytesToBits(const std::vector<unsigned char>& bytes);
19+
1620
/** Data structure that represents a partial merkle tree.
1721
*
1822
* It represents a subset of the txid's of a known block, in a way that
@@ -81,27 +85,14 @@ class CPartialMerkleTree
8185

8286
public:
8387

84-
/** serialization implementation */
85-
ADD_SERIALIZE_METHODS;
86-
87-
template <typename Stream, typename Operation>
88-
inline void SerializationOp(Stream& s, Operation ser_action) {
89-
READWRITE(nTransactions);
90-
READWRITE(vHash);
91-
std::vector<unsigned char> vBytes;
92-
if (ser_action.ForRead()) {
93-
READWRITE(vBytes);
94-
CPartialMerkleTree &us = *(const_cast<CPartialMerkleTree*>(this));
95-
us.vBits.resize(vBytes.size() * 8);
96-
for (unsigned int p = 0; p < us.vBits.size(); p++)
97-
us.vBits[p] = (vBytes[p / 8] & (1 << (p % 8))) != 0;
98-
us.fBad = false;
99-
} else {
100-
vBytes.resize((vBits.size()+7)/8);
101-
for (unsigned int p = 0; p < vBits.size(); p++)
102-
vBytes[p / 8] |= vBits[p] << (p % 8);
103-
READWRITE(vBytes);
104-
}
88+
SERIALIZE_METHODS(CPartialMerkleTree, obj)
89+
{
90+
READWRITE(obj.nTransactions, obj.vHash);
91+
std::vector<unsigned char> bytes;
92+
SER_WRITE(obj, bytes = BitsToBytes(obj.vBits));
93+
READWRITE(bytes);
94+
SER_READ(obj, obj.vBits = BytesToBits(bytes));
95+
SER_READ(obj, obj.fBad = false);
10596
}
10697

10798
/** Construct a partial merkle tree from a list of transaction ids, and a mask that selects a subset of them */
@@ -157,13 +148,7 @@ class CMerkleBlock
157148

158149
CMerkleBlock() {}
159150

160-
ADD_SERIALIZE_METHODS;
161-
162-
template <typename Stream, typename Operation>
163-
inline void SerializationOp(Stream& s, Operation ser_action) {
164-
READWRITE(header);
165-
READWRITE(txn);
166-
}
151+
SERIALIZE_METHODS(CMerkleBlock, obj) { READWRITE(obj.header, obj.txn); }
167152

168153
private:
169154
// Combined constructor to consolidate code

0 commit comments

Comments
 (0)