Skip to content

Commit 353f376

Browse files
committed
Convert blockencodings.h to new serialization framework
1 parent e574fff commit 353f376

File tree

1 file changed

+16
-110
lines changed

1 file changed

+16
-110
lines changed

src/blockencodings.h

Lines changed: 16 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,9 @@
1010

1111
class CTxMemPool;
1212

13-
// Dumb helper to handle CTransaction compression at serialize-time
14-
struct TransactionCompressor {
15-
private:
16-
CTransactionRef& tx;
17-
public:
18-
explicit TransactionCompressor(CTransactionRef& txIn) : tx(txIn) {}
19-
20-
ADD_SERIALIZE_METHODS;
21-
22-
template <typename Stream, typename Operation>
23-
inline void SerializationOp(Stream& s, Operation ser_action) {
24-
READWRITE(tx); //TODO: Compress tx encoding
25-
}
26-
};
13+
// Transaction compression schemes for compact block relay can be introduced by writing
14+
// an actual formatter here.
15+
using TransactionCompression = DefaultFormatter;
2716

2817
class DifferenceFormatter
2918
{
@@ -53,39 +42,9 @@ class BlockTransactionsRequest {
5342
uint256 blockhash;
5443
std::vector<uint16_t> indexes;
5544

56-
ADD_SERIALIZE_METHODS;
57-
58-
template <typename Stream, typename Operation>
59-
inline void SerializationOp(Stream& s, Operation ser_action) {
60-
READWRITE(blockhash);
61-
uint64_t indexes_size = (uint64_t)indexes.size();
62-
READWRITE(COMPACTSIZE(indexes_size));
63-
if (ser_action.ForRead()) {
64-
size_t i = 0;
65-
while (indexes.size() < indexes_size) {
66-
indexes.resize(std::min((uint64_t)(1000 + indexes.size()), indexes_size));
67-
for (; i < indexes.size(); i++) {
68-
uint64_t index = 0;
69-
READWRITE(COMPACTSIZE(index));
70-
if (index > std::numeric_limits<uint16_t>::max())
71-
throw std::ios_base::failure("index overflowed 16 bits");
72-
indexes[i] = index;
73-
}
74-
}
75-
76-
int32_t offset = 0;
77-
for (size_t j = 0; j < indexes.size(); j++) {
78-
if (int32_t(indexes[j]) + offset > std::numeric_limits<uint16_t>::max())
79-
throw std::ios_base::failure("indexes overflowed 16 bits");
80-
indexes[j] = indexes[j] + offset;
81-
offset = int32_t(indexes[j]) + 1;
82-
}
83-
} else {
84-
for (size_t i = 0; i < indexes.size(); i++) {
85-
uint64_t index = indexes[i] - (i == 0 ? 0 : (indexes[i - 1] + 1));
86-
READWRITE(COMPACTSIZE(index));
87-
}
88-
}
45+
SERIALIZE_METHODS(BlockTransactionsRequest, obj)
46+
{
47+
READWRITE(obj.blockhash, Using<VectorFormatter<DifferenceFormatter>>(obj.indexes));
8948
}
9049
};
9150

@@ -99,24 +58,9 @@ class BlockTransactions {
9958
explicit BlockTransactions(const BlockTransactionsRequest& req) :
10059
blockhash(req.blockhash), txn(req.indexes.size()) {}
10160

102-
ADD_SERIALIZE_METHODS;
103-
104-
template <typename Stream, typename Operation>
105-
inline void SerializationOp(Stream& s, Operation ser_action) {
106-
READWRITE(blockhash);
107-
uint64_t txn_size = (uint64_t)txn.size();
108-
READWRITE(COMPACTSIZE(txn_size));
109-
if (ser_action.ForRead()) {
110-
size_t i = 0;
111-
while (txn.size() < txn_size) {
112-
txn.resize(std::min((uint64_t)(1000 + txn.size()), txn_size));
113-
for (; i < txn.size(); i++)
114-
READWRITE(TransactionCompressor(txn[i]));
115-
}
116-
} else {
117-
for (size_t i = 0; i < txn.size(); i++)
118-
READWRITE(TransactionCompressor(txn[i]));
119-
}
61+
SERIALIZE_METHODS(BlockTransactions, obj)
62+
{
63+
READWRITE(obj.blockhash, Using<VectorFormatter<TransactionCompression>>(obj.txn));
12064
}
12165
};
12266

@@ -127,17 +71,7 @@ struct PrefilledTransaction {
12771
uint16_t index;
12872
CTransactionRef tx;
12973

130-
ADD_SERIALIZE_METHODS;
131-
132-
template <typename Stream, typename Operation>
133-
inline void SerializationOp(Stream& s, Operation ser_action) {
134-
uint64_t idx = index;
135-
READWRITE(COMPACTSIZE(idx));
136-
if (idx > std::numeric_limits<uint16_t>::max())
137-
throw std::ios_base::failure("index overflowed 16-bits");
138-
index = idx;
139-
READWRITE(TransactionCompressor(tx));
140-
}
74+
SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), Using<TransactionCompression>(obj.tx)); }
14175
};
14276

14377
typedef enum ReadStatus_t
@@ -175,43 +109,15 @@ class CBlockHeaderAndShortTxIDs {
175109

176110
size_t BlockTxCount() const { return shorttxids.size() + prefilledtxn.size(); }
177111

178-
ADD_SERIALIZE_METHODS;
179-
180-
template <typename Stream, typename Operation>
181-
inline void SerializationOp(Stream& s, Operation ser_action) {
182-
READWRITE(header);
183-
READWRITE(nonce);
184-
185-
uint64_t shorttxids_size = (uint64_t)shorttxids.size();
186-
READWRITE(COMPACTSIZE(shorttxids_size));
112+
SERIALIZE_METHODS(CBlockHeaderAndShortTxIDs, obj)
113+
{
114+
READWRITE(obj.header, obj.nonce, Using<VectorFormatter<CustomUintFormatter<SHORTTXIDS_LENGTH>>>(obj.shorttxids), obj.prefilledtxn);
187115
if (ser_action.ForRead()) {
188-
size_t i = 0;
189-
while (shorttxids.size() < shorttxids_size) {
190-
shorttxids.resize(std::min((uint64_t)(1000 + shorttxids.size()), shorttxids_size));
191-
for (; i < shorttxids.size(); i++) {
192-
uint32_t lsb = 0; uint16_t msb = 0;
193-
READWRITE(lsb);
194-
READWRITE(msb);
195-
shorttxids[i] = (uint64_t(msb) << 32) | uint64_t(lsb);
196-
static_assert(SHORTTXIDS_LENGTH == 6, "shorttxids serialization assumes 6-byte shorttxids");
197-
}
198-
}
199-
} else {
200-
for (size_t i = 0; i < shorttxids.size(); i++) {
201-
uint32_t lsb = shorttxids[i] & 0xffffffff;
202-
uint16_t msb = (shorttxids[i] >> 32) & 0xffff;
203-
READWRITE(lsb);
204-
READWRITE(msb);
116+
if (obj.BlockTxCount() > std::numeric_limits<uint16_t>::max()) {
117+
throw std::ios_base::failure("indexes overflowed 16 bits");
205118
}
119+
obj.FillShortTxIDSelector();
206120
}
207-
208-
READWRITE(prefilledtxn);
209-
210-
if (BlockTxCount() > std::numeric_limits<uint16_t>::max())
211-
throw std::ios_base::failure("indexes overflowed 16 bits");
212-
213-
if (ser_action.ForRead())
214-
FillShortTxIDSelector();
215121
}
216122
};
217123

0 commit comments

Comments
 (0)