Support serializing Span<unsigned char> and use that instead of FLATDATA

This commit is contained in:
Pieter Wuille 2018-04-04 12:40:10 -07:00
parent 833bc08583
commit 9272d70536
3 changed files with 16 additions and 48 deletions

View file

@ -9,6 +9,7 @@
#include <primitives/transaction.h>
#include <script/script.h>
#include <serialize.h>
#include <span.h>
class CKeyID;
class CPubKey;
@ -51,12 +52,12 @@ public:
void Serialize(Stream &s) const {
std::vector<unsigned char> compr;
if (CompressScript(script, compr)) {
s << CFlatData(compr);
s << MakeSpan(compr);
return;
}
unsigned int nSize = script.size() + nSpecialScripts;
s << VARINT(nSize);
s << CFlatData(script);
s << MakeSpan(script);
}
template<typename Stream>
@ -65,7 +66,7 @@ public:
s >> VARINT(nSize);
if (nSize < nSpecialScripts) {
std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
s >> CFlatData(vch);
s >> MakeSpan(vch);
DecompressScript(script, nSize, vch);
return;
}
@ -76,7 +77,7 @@ public:
s.ignore(nSize);
} else {
script.resize(nSize);
s >> CFlatData(script);
s >> MakeSpan(script);
}
}
};

View file

@ -11,6 +11,7 @@
#include <compat.h>
#include <serialize.h>
#include <span.h>
#include <stdint.h>
#include <string>
@ -167,10 +168,13 @@ class CService : public CNetAddr
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(ip);
// TODO: introduce native support for BE serialization in serialize.h
unsigned short portN = htons(port);
READWRITE(FLATDATA(portN));
if (ser_action.ForRead())
READWRITE(Span<unsigned char>((unsigned char*)&portN, 2));
if (ser_action.ForRead()) {
port = ntohs(portN);
}
}
};

View file

@ -22,6 +22,7 @@
#include <vector>
#include <prevector.h>
#include <span.h>
static const unsigned int MAX_SIZE = 0x02000000;
@ -41,7 +42,7 @@ constexpr deserialize_type deserialize {};
/**
* Used to bypass the rule against non-const reference to temporary
* where it makes sense with wrappers such as CFlatData or CTxDB
* where it makes sense with wrappers.
*/
template<typename T>
inline T& REF(const T& val)
@ -185,6 +186,8 @@ template<typename Stream> inline void Serialize(Stream& s, float a ) { ser_wri
template<typename Stream> inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); }
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(a, N); }
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(CharCast(a), N); }
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(CharCast(span.data()), span.size()); }
template<typename Stream> inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
@ -199,6 +202,7 @@ template<typename Stream> inline void Unserialize(Stream& s, float& a ) { a =
template<typename Stream> inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); }
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(a, N); }
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
@ -384,51 +388,10 @@ I ReadVarInt(Stream& is)
}
}
#define FLATDATA(obj) CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))
#define VARINT(obj, ...) WrapVarInt<__VA_ARGS__>(REF(obj))
#define COMPACTSIZE(obj) CCompactSize(REF(obj))
#define LIMITED_STRING(obj,n) LimitedString< n >(REF(obj))
/**
* Wrapper for serializing arrays and POD.
*/
class CFlatData
{
protected:
char* pbegin;
char* pend;
public:
CFlatData(void* pbeginIn, void* pendIn) : pbegin((char*)pbeginIn), pend((char*)pendIn) { }
template <class T, class TAl>
explicit CFlatData(std::vector<T,TAl> &v)
{
pbegin = (char*)v.data();
pend = (char*)(v.data() + v.size());
}
template <unsigned int N, typename T, typename S, typename D>
explicit CFlatData(prevector<N, T, S, D> &v)
{
pbegin = (char*)v.data();
pend = (char*)(v.data() + v.size());
}
char* begin() { return pbegin; }
const char* begin() const { return pbegin; }
char* end() { return pend; }
const char* end() const { return pend; }
template<typename Stream>
void Serialize(Stream& s) const
{
s.write(pbegin, pend - pbegin);
}
template<typename Stream>
void Unserialize(Stream& s)
{
s.read(pbegin, pend - pbegin);
}
};
template<VarIntMode Mode, typename I>
class CVarInt
{