mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
Merge #17957: Convert compression.h to new serialization framework
4de934b9b5
Partial #17957: Add FORMATTER_METHODS, similar to SERIALIZE_METHODS, but for formattersca34c5cba5
This commit is contained in:
parent
2f542a93a1
commit
bfe5971359
@ -60,7 +60,7 @@ public:
|
|||||||
assert(!IsSpent());
|
assert(!IsSpent());
|
||||||
uint32_t code = nHeight * 2 + fCoinBase;
|
uint32_t code = nHeight * 2 + fCoinBase;
|
||||||
::Serialize(s, VARINT(code));
|
::Serialize(s, VARINT(code));
|
||||||
::Serialize(s, CTxOutCompressor(REF(out)));
|
::Serialize(s, Using<TxOutCompression>(out));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
@ -69,7 +69,7 @@ public:
|
|||||||
::Unserialize(s, VARINT(code));
|
::Unserialize(s, VARINT(code));
|
||||||
nHeight = code >> 1;
|
nHeight = code >> 1;
|
||||||
fCoinBase = code & 1;
|
fCoinBase = code & 1;
|
||||||
::Unserialize(s, CTxOutCompressor(out));
|
::Unserialize(s, Using<TxOutCompression>(out));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsSpent() const {
|
bool IsSpent() const {
|
||||||
|
@ -11,10 +11,6 @@
|
|||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
#include <span.h>
|
#include <span.h>
|
||||||
|
|
||||||
class CKeyID;
|
|
||||||
class CPubKey;
|
|
||||||
class CScriptID;
|
|
||||||
|
|
||||||
bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
|
bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
|
||||||
unsigned int GetSpecialScriptSize(unsigned int nSize);
|
unsigned int GetSpecialScriptSize(unsigned int nSize);
|
||||||
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
|
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
|
||||||
@ -33,9 +29,8 @@ uint64_t DecompressAmount(uint64_t nAmount);
|
|||||||
* Other scripts up to 121 bytes require 1 byte + script length. Above
|
* Other scripts up to 121 bytes require 1 byte + script length. Above
|
||||||
* that, scripts up to 16505 bytes require 2 bytes + script length.
|
* 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)
|
* make this static for now (there are only 6 special scripts defined)
|
||||||
* this can potentially be extended together with a new nVersion for
|
* this can potentially be extended together with a new nVersion for
|
||||||
@ -44,12 +39,8 @@ private:
|
|||||||
*/
|
*/
|
||||||
static const unsigned int nSpecialScripts = 6;
|
static const unsigned int nSpecialScripts = 6;
|
||||||
|
|
||||||
CScript &script;
|
|
||||||
public:
|
|
||||||
explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream &s) const {
|
void Ser(Stream &s, const CScript& script) {
|
||||||
std::vector<unsigned char> compr;
|
std::vector<unsigned char> compr;
|
||||||
if (CompressScript(script, compr)) {
|
if (CompressScript(script, compr)) {
|
||||||
s << MakeSpan(compr);
|
s << MakeSpan(compr);
|
||||||
@ -61,7 +52,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Unserialize(Stream &s) {
|
void Unser(Stream &s, CScript& script) {
|
||||||
unsigned int nSize = 0;
|
unsigned int nSize = 0;
|
||||||
s >> VARINT(nSize);
|
s >> VARINT(nSize);
|
||||||
if (nSize < nSpecialScripts) {
|
if (nSize < nSpecialScripts) {
|
||||||
@ -82,30 +73,24 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** wrapper for CTxOut that provides a more compact serialization */
|
struct AmountCompression
|
||||||
class CTxOutCompressor
|
|
||||||
{
|
{
|
||||||
private:
|
template<typename Stream, typename I> void Ser(Stream& s, I val)
|
||||||
CTxOut &txout;
|
{
|
||||||
|
s << VARINT(CompressAmount(val));
|
||||||
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));
|
template<typename Stream, typename I> void Unser(Stream& s, I& val)
|
||||||
READWRITE(cscript);
|
{
|
||||||
|
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
|
#endif // BITCOIN_COMPRESSOR_H
|
||||||
|
@ -206,6 +206,30 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
|
|||||||
SerializationOp(s, CSerActionUnserialize()); \
|
SerializationOp(s, CSerActionUnserialize()); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implement the Ser and Unser methods needed for implementing a formatter (see Using below).
|
||||||
|
*
|
||||||
|
* Both Ser and Unser are delegated to a single static method SerializationOps, which is polymorphic
|
||||||
|
* in the serialized/deserialized type (allowing it to be const when serializing, and non-const when
|
||||||
|
* deserializing).
|
||||||
|
*
|
||||||
|
* Example use:
|
||||||
|
* struct FooFormatter {
|
||||||
|
* FORMATTER_METHODS(Class, obj) { READWRITE(obj.val1, VARINT(obj.val2)); }
|
||||||
|
* }
|
||||||
|
* would define a class FooFormatter that defines a serialization of Class objects consisting
|
||||||
|
* of serializing its val1 member using the default serialization, and its val2 member using
|
||||||
|
* VARINT serialization. That FooFormatter can then be used in statements like
|
||||||
|
* READWRITE(Using<FooFormatter>(obj.bla)).
|
||||||
|
*/
|
||||||
|
#define FORMATTER_METHODS(cls, obj) \
|
||||||
|
template<typename Stream> \
|
||||||
|
static void Ser(Stream& s, const cls& obj) { SerializationOps(obj, s, CSerActionSerialize()); } \
|
||||||
|
template<typename Stream> \
|
||||||
|
static void Unser(Stream& s, cls& obj) { SerializationOps(obj, s, CSerActionUnserialize()); } \
|
||||||
|
template<typename Stream, typename Type, typename Operation> \
|
||||||
|
static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implement the Serialize and Unserialize methods by delegating to a single templated
|
* Implement the Serialize and Unserialize methods by delegating to a single templated
|
||||||
* static method that takes the to-be-(de)serialized object as a parameter. This approach
|
* static method that takes the to-be-(de)serialized object as a parameter. This approach
|
||||||
@ -218,17 +242,15 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
|
|||||||
void Serialize(Stream& s) const \
|
void Serialize(Stream& s) const \
|
||||||
{ \
|
{ \
|
||||||
static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
|
static_assert(std::is_same<const cls&, decltype(*this)>::value, "Serialize type mismatch"); \
|
||||||
SerializationOps(*this, s, CSerActionSerialize()); \
|
Ser(s, *this); \
|
||||||
} \
|
} \
|
||||||
template<typename Stream> \
|
template<typename Stream> \
|
||||||
void Unserialize(Stream& s) \
|
void Unserialize(Stream& s) \
|
||||||
{ \
|
{ \
|
||||||
static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
|
static_assert(std::is_same<cls&, decltype(*this)>::value, "Unserialize type mismatch"); \
|
||||||
SerializationOps(*this, s, CSerActionUnserialize()); \
|
Unser(s, *this); \
|
||||||
} \
|
} \
|
||||||
template<typename Stream, typename Type, typename Operation> \
|
FORMATTER_METHODS(cls, obj)
|
||||||
static inline void SerializationOps(Type& obj, Stream& s, Operation ser_action) \
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef CHAR_EQUALS_INT8
|
#ifndef CHAR_EQUALS_INT8
|
||||||
template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
|
template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
|
||||||
|
@ -241,7 +241,7 @@ int test_one_input(std::vector<uint8_t> buffer) {
|
|||||||
case CTXOUTCOMPRESSOR_DESERIALIZE:
|
case CTXOUTCOMPRESSOR_DESERIALIZE:
|
||||||
{
|
{
|
||||||
CTxOut to;
|
CTxOut to;
|
||||||
CTxOutCompressor toc(to);
|
auto toc = Using<TxOutCompression>(to);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ds >> toc;
|
ds >> toc;
|
||||||
|
@ -502,7 +502,7 @@ public:
|
|||||||
vout.assign(vAvail.size(), CTxOut());
|
vout.assign(vAvail.size(), CTxOut());
|
||||||
for (unsigned int i = 0; i < vAvail.size(); i++) {
|
for (unsigned int i = 0; i < vAvail.size(); i++) {
|
||||||
if (vAvail[i])
|
if (vAvail[i])
|
||||||
::Unserialize(s, CTxOutCompressor(vout[i]));
|
::Unserialize(s, Using<TxOutCompression>(vout[i]));
|
||||||
}
|
}
|
||||||
// coinbase height
|
// coinbase height
|
||||||
::Unserialize(s, VARINT(nHeight, VarIntMode::NONNEGATIVE_SIGNED));
|
::Unserialize(s, VARINT(nHeight, VarIntMode::NONNEGATIVE_SIGNED));
|
||||||
|
@ -28,7 +28,7 @@ struct TxInUndoFormatter
|
|||||||
// Required to maintain compatibility with older undo format.
|
// Required to maintain compatibility with older undo format.
|
||||||
::Serialize(s, (unsigned char)0);
|
::Serialize(s, (unsigned char)0);
|
||||||
}
|
}
|
||||||
::Serialize(s, CTxOutCompressor(REF(txout.out)));
|
::Serialize(s, Using<TxOutCompression>(REF(txout.out)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
@ -44,7 +44,7 @@ struct TxInUndoFormatter
|
|||||||
unsigned int nVersionDummy;
|
unsigned int nVersionDummy;
|
||||||
::Unserialize(s, VARINT(nVersionDummy));
|
::Unserialize(s, VARINT(nVersionDummy));
|
||||||
}
|
}
|
||||||
::Unserialize(s, CTxOutCompressor(REF(txout.out)));
|
::Unserialize(s, Using<TxOutCompression>(REF(txout.out)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user