|
13 | 13 |
|
14 | 14 | #include <vector>
|
15 | 15 |
|
| 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 | + |
16 | 20 | /** Data structure that represents a partial merkle tree.
|
17 | 21 | *
|
18 | 22 | * It represents a subset of the txid's of a known block, in a way that
|
@@ -81,27 +85,14 @@ class CPartialMerkleTree
|
81 | 85 |
|
82 | 86 | public:
|
83 | 87 |
|
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); |
105 | 96 | }
|
106 | 97 |
|
107 | 98 | /** 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
|
157 | 148 |
|
158 | 149 | CMerkleBlock() {}
|
159 | 150 |
|
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); } |
167 | 152 |
|
168 | 153 | private:
|
169 | 154 | // Combined constructor to consolidate code
|
|
0 commit comments