Skip to content

Commit c1607b5

Browse files
committed
Merge #17957: Serialization improvements step 3 (compression.h)
4de934b Convert compression.h to new serialization framework (Pieter Wuille) ca34c5c Add FORMATTER_METHODS, similar to SERIALIZE_METHODS, but for formatters (Pieter Wuille) Pull request description: This is the next piece of the puzzle from #10785. It includes: * The `FORMATTER_METHODS` macro, similar to `SERIALIZE_METHODS`, for defining a formatter with a unified serialization/deserialization implementation. * Updating `compression.h` to consist of 3 formatters, rather than old-style wrappers (`ScriptCompression`, `AmountCompression`, `TxOutCompression`). ACKs for top commit: laanwj: code review ACK 4de934b ryanofsky: Code review ACK 4de934b. Only change since last review is removing REF usages Tree-SHA512: d52ca21eb1ce87d9bc3c90d00c905bd4fada522759aaa144c02a58b4d738d5e8647c0558b8ce393c707f6e3c4d20bf93781a2dcc1e1dcbd276d9b5ffd0e02cd6
2 parents 3b5b276 + 4de934b commit c1607b5

File tree

6 files changed

+52
-45
lines changed

6 files changed

+52
-45
lines changed

src/coins.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Coin
6161
assert(!IsSpent());
6262
uint32_t code = nHeight * 2 + fCoinBase;
6363
::Serialize(s, VARINT(code));
64-
::Serialize(s, CTxOutCompressor(REF(out)));
64+
::Serialize(s, Using<TxOutCompression>(out));
6565
}
6666

6767
template<typename Stream>
@@ -70,7 +70,7 @@ class Coin
7070
::Unserialize(s, VARINT(code));
7171
nHeight = code >> 1;
7272
fCoinBase = code & 1;
73-
::Unserialize(s, CTxOutCompressor(out));
73+
::Unserialize(s, Using<TxOutCompression>(out));
7474
}
7575

7676
bool IsSpent() const {

src/compressor.h

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include <serialize.h>
1212
#include <span.h>
1313

14-
class CKeyID;
15-
class CPubKey;
16-
class CScriptID;
17-
1814
bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
1915
unsigned int GetSpecialScriptSize(unsigned int nSize);
2016
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
@@ -33,9 +29,8 @@ uint64_t DecompressAmount(uint64_t nAmount);
3329
* Other scripts up to 121 bytes require 1 byte + script length. Above
3430
* that, scripts up to 16505 bytes require 2 bytes + script length.
3531
*/
36-
class CScriptCompressor
32+
struct ScriptCompression
3733
{
38-
private:
3934
/**
4035
* make this static for now (there are only 6 special scripts defined)
4136
* this can potentially be extended together with a new nVersion for
@@ -44,12 +39,8 @@ class CScriptCompressor
4439
*/
4540
static const unsigned int nSpecialScripts = 6;
4641

47-
CScript &script;
48-
public:
49-
explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
50-
5142
template<typename Stream>
52-
void Serialize(Stream &s) const {
43+
void Ser(Stream &s, const CScript& script) {
5344
std::vector<unsigned char> compr;
5445
if (CompressScript(script, compr)) {
5546
s << MakeSpan(compr);
@@ -61,7 +52,7 @@ class CScriptCompressor
6152
}
6253

6354
template<typename Stream>
64-
void Unserialize(Stream &s) {
55+
void Unser(Stream &s, CScript& script) {
6556
unsigned int nSize = 0;
6657
s >> VARINT(nSize);
6758
if (nSize < nSpecialScripts) {
@@ -82,30 +73,24 @@ class CScriptCompressor
8273
}
8374
};
8475

85-
/** wrapper for CTxOut that provides a more compact serialization */
86-
class CTxOutCompressor
76+
struct AmountCompression
8777
{
88-
private:
89-
CTxOut &txout;
90-
91-
public:
92-
explicit CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
93-
94-
ADD_SERIALIZE_METHODS;
95-
96-
template <typename Stream, typename Operation>
97-
inline void SerializationOp(Stream& s, Operation ser_action) {
98-
if (!ser_action.ForRead()) {
99-
uint64_t nVal = CompressAmount(txout.nValue);
100-
READWRITE(VARINT(nVal));
101-
} else {
102-
uint64_t nVal = 0;
103-
READWRITE(VARINT(nVal));
104-
txout.nValue = DecompressAmount(nVal);
105-
}
106-
CScriptCompressor cscript(REF(txout.scriptPubKey));
107-
READWRITE(cscript);
78+
template<typename Stream, typename I> void Ser(Stream& s, I val)
79+
{
80+
s << VARINT(CompressAmount(val));
81+
}
82+
template<typename Stream, typename I> void Unser(Stream& s, I& val)
83+
{
84+
uint64_t v;
85+
s >> VARINT(v);
86+
val = DecompressAmount(v);
10887
}
10988
};
11089

90+
/** wrapper for CTxOut that provides a more compact serialization */
91+
struct TxOutCompression
92+
{
93+
FORMATTER_METHODS(CTxOut, obj) { READWRITE(Using<AmountCompression>(obj.nValue), Using<ScriptCompression>(obj.scriptPubKey)); }
94+
};
95+
11196
#endif // BITCOIN_COMPRESSOR_H

src/serialize.h

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,30 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
199199
SerializationOp(s, CSerActionUnserialize()); \
200200
}
201201

202+
/**
203+
* Implement the Ser and Unser methods needed for implementing a formatter (see Using below).
204+
*
205+
* Both Ser and Unser are delegated to a single static method SerializationOps, which is polymorphic
206+
* in the serialized/deserialized type (allowing it to be const when serializing, and non-const when
207+
* deserializing).
208+
*
209+
* Example use:
210+
* struct FooFormatter {
211+
* FORMATTER_METHODS(Class, obj) { READWRITE(obj.val1, VARINT(obj.val2)); }
212+
* }
213+
* would define a class FooFormatter that defines a serialization of Class objects consisting
214+
* of serializing its val1 member using the default serialization, and its val2 member using
215+
* VARINT serialization. That FooFormatter can then be used in statements like
216+
* READWRITE(Using<FooFormatter>(obj.bla)).
217+
*/
218+
#define FORMATTER_METHODS(cls, obj) \
219+
template<typename Stream> \
220+
static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, CSerActionSerialize()); } \
221+
template<typename Stream> \
222+
static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, CSerActionUnserialize()); } \
223+
template<typename Stream, typename Type, typename Operation> \
224+
static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
225+
202226
/**
203227
* Implement the Serialize and Unserialize methods by delegating to a single templated
204228
* static method that takes the to-be-(de)serialized object as a parameter. This approach
@@ -211,17 +235,15 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
211235
void Serialize(Stream& s) const \
212236
{ \
213237
static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
214-
SerializationOps(*this, s, CSerActionSerialize()); \
238+
Ser(s, *this); \
215239
} \
216240
template<typename Stream> \
217241
void Unserialize(Stream& s) \
218242
{ \
219243
static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
220-
SerializationOps(*this, s, CSerActionUnserialize()); \
244+
Unser(s, *this); \
221245
} \
222-
template<typename Stream, typename Type, typename Operation> \
223-
static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
224-
246+
FORMATTER_METHODS(cls, obj)
225247

226248
#ifndef CHAR_EQUALS_INT8
227249
template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char

src/test/fuzz/deserialize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
206206
DeserializeFromFuzzingInput(buffer, dbi);
207207
#elif TXOUTCOMPRESSOR_DESERIALIZE
208208
CTxOut to;
209-
CTxOutCompressor toc(to);
209+
auto toc = Using<TxOutCompression>(to);
210210
DeserializeFromFuzzingInput(buffer, toc);
211211
#elif BLOCKTRANSACTIONS_DESERIALIZE
212212
BlockTransactions bt;

src/txdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ class CCoins
336336
vout.assign(vAvail.size(), CTxOut());
337337
for (unsigned int i = 0; i < vAvail.size(); i++) {
338338
if (vAvail[i])
339-
::Unserialize(s, CTxOutCompressor(vout[i]));
339+
::Unserialize(s, Using<TxOutCompression>(vout[i]));
340340
}
341341
// coinbase height
342342
::Unserialize(s, VARINT(nHeight, VarIntMode::NONNEGATIVE_SIGNED));

src/undo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TxInUndoSerializer
3232
// Required to maintain compatibility with older undo format.
3333
::Serialize(s, (unsigned char)0);
3434
}
35-
::Serialize(s, CTxOutCompressor(REF(txout->out)));
35+
::Serialize(s, Using<TxOutCompression>(REF(txout->out)));
3636
}
3737

3838
explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
@@ -56,7 +56,7 @@ class TxInUndoDeserializer
5656
unsigned int nVersionDummy;
5757
::Unserialize(s, VARINT(nVersionDummy));
5858
}
59-
::Unserialize(s, CTxOutCompressor(REF(txout->out)));
59+
::Unserialize(s, Using<TxOutCompression>(REF(txout->out)));
6060
}
6161

6262
explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}

0 commit comments

Comments
 (0)