mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
Merge #17896: Convert chain to new serialization
9b66083788
Partial #17896: Convert VARINT to the formatter/Using approach2f1b2f4ed0
Partial #17896: Add a generic approach for (de)serialization of objectsca62563df3
This commit is contained in:
parent
e483a689e3
commit
46cb6e23dc
58
src/chain.h
58
src/chain.h
@ -39,17 +39,15 @@ public:
|
||||
uint64_t nTimeFirst; //!< earliest time of block in file
|
||||
uint64_t nTimeLast; //!< latest time of block in file
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
READWRITE(VARINT(nBlocks));
|
||||
READWRITE(VARINT(nSize));
|
||||
READWRITE(VARINT(nUndoSize));
|
||||
READWRITE(VARINT(nHeightFirst));
|
||||
READWRITE(VARINT(nHeightLast));
|
||||
READWRITE(VARINT(nTimeFirst));
|
||||
READWRITE(VARINT(nTimeLast));
|
||||
SERIALIZE_METHODS(CBlockFileInfo, obj)
|
||||
{
|
||||
READWRITE(VARINT(obj.nBlocks));
|
||||
READWRITE(VARINT(obj.nSize));
|
||||
READWRITE(VARINT(obj.nUndoSize));
|
||||
READWRITE(VARINT(obj.nHeightFirst));
|
||||
READWRITE(VARINT(obj.nHeightLast));
|
||||
READWRITE(VARINT(obj.nTimeFirst));
|
||||
READWRITE(VARINT(obj.nTimeLast));
|
||||
}
|
||||
|
||||
void SetNull() {
|
||||
@ -383,33 +381,27 @@ public:
|
||||
hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
|
||||
}
|
||||
|
||||
ADD_SERIALIZE_METHODS;
|
||||
|
||||
template <typename Stream, typename Operation>
|
||||
inline void SerializationOp(Stream& s, Operation ser_action) {
|
||||
SERIALIZE_METHODS(CDiskBlockIndex, obj)
|
||||
{
|
||||
int _nVersion = s.GetVersion();
|
||||
if (!(s.GetType() & SER_GETHASH))
|
||||
READWRITE(VARINT(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));
|
||||
if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT(_nVersion, VarIntMode::NONNEGATIVE_SIGNED));
|
||||
|
||||
READWRITE(VARINT(nHeight, VarIntMode::NONNEGATIVE_SIGNED));
|
||||
READWRITE(VARINT(nStatus));
|
||||
READWRITE(VARINT(nTx));
|
||||
if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO))
|
||||
READWRITE(VARINT(nFile, VarIntMode::NONNEGATIVE_SIGNED));
|
||||
if (nStatus & BLOCK_HAVE_DATA)
|
||||
READWRITE(VARINT(nDataPos));
|
||||
if (nStatus & BLOCK_HAVE_UNDO)
|
||||
READWRITE(VARINT(nUndoPos));
|
||||
READWRITE(VARINT(obj.nHeight, VarIntMode::NONNEGATIVE_SIGNED));
|
||||
READWRITE(VARINT(obj.nStatus));
|
||||
READWRITE(VARINT(obj.nTx));
|
||||
if (obj.nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO)) READWRITE(VARINT(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED));
|
||||
if (obj.nStatus & BLOCK_HAVE_DATA) READWRITE(VARINT(obj.nDataPos));
|
||||
if (obj.nStatus & BLOCK_HAVE_UNDO) READWRITE(VARINT(obj.nUndoPos));
|
||||
|
||||
// block hash
|
||||
READWRITE(hash);
|
||||
READWRITE(obj.hash);
|
||||
// block header
|
||||
READWRITE(this->nVersion);
|
||||
READWRITE(hashPrev);
|
||||
READWRITE(hashMerkleRoot);
|
||||
READWRITE(nTime);
|
||||
READWRITE(nBits);
|
||||
READWRITE(nNonce);
|
||||
READWRITE(obj.nVersion);
|
||||
READWRITE(obj.hashPrev);
|
||||
READWRITE(obj.hashMerkleRoot);
|
||||
READWRITE(obj.nTime);
|
||||
READWRITE(obj.nBits);
|
||||
READWRITE(obj.nNonce);
|
||||
}
|
||||
|
||||
uint256 GetBlockHash() const
|
||||
|
@ -448,11 +448,37 @@ I ReadVarInt(Stream& is)
|
||||
}
|
||||
}
|
||||
|
||||
/** Simple wrapper class to serialize objects using a formatter; used by Using(). */
|
||||
template<typename Formatter, typename T>
|
||||
class Wrapper
|
||||
{
|
||||
static_assert(std::is_lvalue_reference<T>::value, "Wrapper needs an lvalue reference type T");
|
||||
protected:
|
||||
T m_object;
|
||||
public:
|
||||
explicit Wrapper(T obj) : m_object(obj) {}
|
||||
template<typename Stream> void Serialize(Stream &s) const { Formatter().Ser(s, m_object); }
|
||||
template<typename Stream> void Unserialize(Stream &s) { Formatter().Unser(s, m_object); }
|
||||
};
|
||||
|
||||
/** Cause serialization/deserialization of an object to be done using a specified formatter class.
|
||||
*
|
||||
* To use this, you need a class Formatter that has public functions Ser(stream, const object&) for
|
||||
* serialization, and Unser(stream, object&) for deserialization. Serialization routines (inside
|
||||
* READWRITE, or directly with << and >> operators), can then use Using<Formatter>(object).
|
||||
*
|
||||
* This works by constructing a Wrapper<Formatter, T>-wrapped version of object, where T is
|
||||
* const during serialization, and non-const during deserialization, which maintains const
|
||||
* correctness.
|
||||
*/
|
||||
template<typename Formatter, typename T>
|
||||
static inline Wrapper<Formatter, T&> Using(T&& t) { return Wrapper<Formatter, T&>(t); }
|
||||
|
||||
#define FIXEDBITSET(obj, size) CFixedBitSet(REF(obj), (size))
|
||||
#define DYNBITSET(obj) CDynamicBitSet(REF(obj))
|
||||
#define FIXEDVARINTSBITSET(obj, size) CFixedVarIntsBitSet(REF(obj), (size))
|
||||
#define AUTOBITSET(obj, size) CAutoBitSet(REF(obj), (size))
|
||||
#define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj))
|
||||
#define VARINT(obj, ...) Using<VarIntFormatter<__VA_ARGS__>>(obj)
|
||||
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
|
||||
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
|
||||
|
||||
@ -612,22 +638,18 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template<VarIntMode Mode, typename I>
|
||||
class CVarInt
|
||||
/** Serialization wrapper class for integers in VarInt format. */
|
||||
template<VarIntMode Mode=VarIntMode::DEFAULT>
|
||||
struct VarIntFormatter
|
||||
{
|
||||
protected:
|
||||
I &n;
|
||||
public:
|
||||
explicit CVarInt(I& nIn) : n(nIn) { }
|
||||
|
||||
template<typename Stream>
|
||||
void Serialize(Stream &s) const {
|
||||
WriteVarInt<Stream,Mode,I>(s, n);
|
||||
template<typename Stream, typename I> void Ser(Stream &s, I v)
|
||||
{
|
||||
WriteVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s, v);
|
||||
}
|
||||
|
||||
template<typename Stream>
|
||||
void Unserialize(Stream& s) {
|
||||
n = ReadVarInt<Stream,Mode,I>(s);
|
||||
template<typename Stream, typename I> void Unser(Stream& s, I& v)
|
||||
{
|
||||
v = ReadVarInt<Stream,Mode,typename std::remove_cv<I>::type>(s);
|
||||
}
|
||||
};
|
||||
|
||||
@ -716,9 +738,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template<VarIntMode Mode=VarIntMode::DEFAULT, typename I>
|
||||
CVarInt<Mode, I> WrapVarInt(I& n) { return CVarInt<Mode, I>{n}; }
|
||||
|
||||
template<typename I>
|
||||
BigEndian<I> WrapBigEndian(I& n) { return BigEndian<I>(n); }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user