Skip to content

Commit 4de934b

Browse files
committed
Convert compression.h to new serialization framework
1 parent ca34c5c commit 4de934b

File tree

5 files changed

+25
-40
lines changed

5 files changed

+25
-40
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/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)