Skip to content

Commit

Permalink
Convert compression.h to new serialization framework
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Jan 22, 2020
1 parent ca34c5c commit 4de934b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/coins.h
Expand Up @@ -61,7 +61,7 @@ class Coin
assert(!IsSpent());
uint32_t code = nHeight * 2 + fCoinBase;
::Serialize(s, VARINT(code));
::Serialize(s, CTxOutCompressor(REF(out)));
::Serialize(s, Using<TxOutCompression>(out));
}

template<typename Stream>
Expand All @@ -70,7 +70,7 @@ class Coin
::Unserialize(s, VARINT(code));
nHeight = code >> 1;
fCoinBase = code & 1;
::Unserialize(s, CTxOutCompressor(out));
::Unserialize(s, Using<TxOutCompression>(out));
}

bool IsSpent() const {
Expand Down
53 changes: 19 additions & 34 deletions src/compressor.h
Expand Up @@ -11,10 +11,6 @@
#include <serialize.h>
#include <span.h>

class CKeyID;
class CPubKey;
class CScriptID;

bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
unsigned int GetSpecialScriptSize(unsigned int nSize);
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
Expand All @@ -33,9 +29,8 @@ uint64_t DecompressAmount(uint64_t nAmount);
* Other scripts up to 121 bytes require 1 byte + script length. Above
* that, scripts up to 16505 bytes require 2 bytes + script length.
*/
class CScriptCompressor
struct ScriptCompression
{
private:
/**
* make this static for now (there are only 6 special scripts defined)
* this can potentially be extended together with a new nVersion for
Expand All @@ -44,12 +39,8 @@ class CScriptCompressor
*/
static const unsigned int nSpecialScripts = 6;

CScript &script;
public:
explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }

template<typename Stream>
void Serialize(Stream &s) const {
void Ser(Stream &s, const CScript& script) {
std::vector<unsigned char> compr;
if (CompressScript(script, compr)) {
s << MakeSpan(compr);
Expand All @@ -61,7 +52,7 @@ class CScriptCompressor
}

template<typename Stream>
void Unserialize(Stream &s) {
void Unser(Stream &s, CScript& script) {
unsigned int nSize = 0;
s >> VARINT(nSize);
if (nSize < nSpecialScripts) {
Expand All @@ -82,30 +73,24 @@ class CScriptCompressor
}
};

/** wrapper for CTxOut that provides a more compact serialization */
class CTxOutCompressor
struct AmountCompression
{
private:
CTxOut &txout;

public:
explicit CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
if (!ser_action.ForRead()) {
uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal));
} else {
uint64_t nVal = 0;
READWRITE(VARINT(nVal));
txout.nValue = DecompressAmount(nVal);
}
CScriptCompressor cscript(REF(txout.scriptPubKey));
READWRITE(cscript);
template<typename Stream, typename I> void Ser(Stream& s, I val)
{
s << VARINT(CompressAmount(val));
}
template<typename Stream, typename I> void Unser(Stream& s, I& val)
{
uint64_t v;
s >> VARINT(v);
val = DecompressAmount(v);
}
};

/** wrapper for CTxOut that provides a more compact serialization */
struct TxOutCompression
{
FORMATTER_METHODS(CTxOut, obj) { READWRITE(Using<AmountCompression>(obj.nValue), Using<ScriptCompression>(obj.scriptPubKey)); }
};

#endif // BITCOIN_COMPRESSOR_H
2 changes: 1 addition & 1 deletion src/test/fuzz/deserialize.cpp
Expand Up @@ -206,7 +206,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
DeserializeFromFuzzingInput(buffer, dbi);
#elif TXOUTCOMPRESSOR_DESERIALIZE
CTxOut to;
CTxOutCompressor toc(to);
auto toc = Using<TxOutCompression>(to);
DeserializeFromFuzzingInput(buffer, toc);
#elif BLOCKTRANSACTIONS_DESERIALIZE
BlockTransactions bt;
Expand Down
2 changes: 1 addition & 1 deletion src/txdb.cpp
Expand Up @@ -336,7 +336,7 @@ class CCoins
vout.assign(vAvail.size(), CTxOut());
for (unsigned int i = 0; i < vAvail.size(); i++) {
if (vAvail[i])
::Unserialize(s, CTxOutCompressor(vout[i]));
::Unserialize(s, Using<TxOutCompression>(vout[i]));
}
// coinbase height
::Unserialize(s, VARINT(nHeight, VarIntMode::NONNEGATIVE_SIGNED));
Expand Down
4 changes: 2 additions & 2 deletions src/undo.h
Expand Up @@ -32,7 +32,7 @@ class TxInUndoSerializer
// Required to maintain compatibility with older undo format.
::Serialize(s, (unsigned char)0);
}
::Serialize(s, CTxOutCompressor(REF(txout->out)));
::Serialize(s, Using<TxOutCompression>(REF(txout->out)));
}

explicit TxInUndoSerializer(const Coin* coin) : txout(coin) {}
Expand All @@ -56,7 +56,7 @@ class TxInUndoDeserializer
unsigned int nVersionDummy;
::Unserialize(s, VARINT(nVersionDummy));
}
::Unserialize(s, CTxOutCompressor(REF(txout->out)));
::Unserialize(s, Using<TxOutCompression>(REF(txout->out)));
}

explicit TxInUndoDeserializer(Coin* coin) : txout(coin) {}
Expand Down

0 comments on commit 4de934b

Please sign in to comment.