10
10
11
11
class CTxMemPool ;
12
12
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;
27
16
28
17
class DifferenceFormatter
29
18
{
@@ -53,39 +42,9 @@ class BlockTransactionsRequest {
53
42
uint256 blockhash;
54
43
std::vector<uint16_t > indexes;
55
44
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 ));
89
48
}
90
49
};
91
50
@@ -99,24 +58,9 @@ class BlockTransactions {
99
58
explicit BlockTransactions (const BlockTransactionsRequest& req) :
100
59
blockhash(req.blockhash), txn(req.indexes.size()) {}
101
60
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 ));
120
64
}
121
65
};
122
66
@@ -127,17 +71,7 @@ struct PrefilledTransaction {
127
71
uint16_t index;
128
72
CTransactionRef tx;
129
73
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 )); }
141
75
};
142
76
143
77
typedef enum ReadStatus_t
@@ -175,43 +109,15 @@ class CBlockHeaderAndShortTxIDs {
175
109
176
110
size_t BlockTxCount () const { return shorttxids.size () + prefilledtxn.size (); }
177
111
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 );
187
115
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" );
205
118
}
119
+ obj.FillShortTxIDSelector ();
206
120
}
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 ();
215
121
}
216
122
};
217
123
0 commit comments