Skip to content

Commit

Permalink
Convert everything except wallet/qt to new serialization (step 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and furszy committed Jul 3, 2021
1 parent 3d3ee64 commit 0f15784
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 193 deletions.
1 change: 1 addition & 0 deletions src/bench/prevector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
struct nontrivial_t {
int x;
nontrivial_t() :x(-1) {}
SERIALIZE_METHODS(nontrivial_t, obj) { READWRITE(obj.x); }
};
static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value,
"expected nontrivial_t to not be trivially constructible");
Expand Down
17 changes: 4 additions & 13 deletions src/bloom.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ enum bloomflags {
/**
* BloomFilter is a probabilistic filter which SPV clients provide
* so that we can filter the transactions we sends them.
*
*
* This allows for significantly more efficient transaction and block downloads.
*
*
* Because bloom filters are probabilistic, an SPV node can increase the false-
* positive rate, making us send them transactions which aren't actually theirs,
* positive rate, making us send them transactions which aren't actually theirs,
* allowing clients to trade more bandwidth for more privacy by obfuscating which
* keys are owned by them.
*/
Expand Down Expand Up @@ -66,16 +66,7 @@ class CBloomFilter
CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);
CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(vData);
READWRITE(nHashFuncs);
READWRITE(nTweak);
READWRITE(nFlags);
}
SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }

void insert(const std::vector<unsigned char>& vKey);
void insert(const COutPoint& outpoint);
Expand Down
8 changes: 1 addition & 7 deletions src/flatfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ struct FlatFilePos
int nFile;
unsigned int nPos;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(VARINT_MODE(nFile, VarIntMode::NONNEGATIVE_SIGNED));
READWRITE(VARINT(nPos));
}
SERIALIZE_METHODS(FlatFilePos, obj) { READWRITE(VARINT_MODE(obj.nFile, VarIntMode::NONNEGATIVE_SIGNED), VARINT(obj.nPos)); }

FlatFilePos() : nFile(-1), nPos(0) {}

Expand Down
26 changes: 3 additions & 23 deletions src/netaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ class CNetAddr
friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
friend bool operator<(const CNetAddr& a, const CNetAddr& b);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(ip);
}
SERIALIZE_METHODS(CNetAddr, obj) { READWRITE(obj.ip); }

friend class CSubNet;
};
Expand Down Expand Up @@ -128,15 +122,7 @@ class CSubNet
friend bool operator!=(const CSubNet& a, const CSubNet& b);
friend bool operator<(const CSubNet& a, const CSubNet& b);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(network);
READWRITE(netmask);
READWRITE(valid);
}
SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
};

/** A combination of a network address (CNetAddr) and a (TCP) port */
Expand Down Expand Up @@ -166,13 +152,7 @@ class CService : public CNetAddr
CService(const struct in6_addr& ipv6Addr, unsigned short port);
CService(const struct sockaddr_in6& addr);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(ip);
READWRITE(Using<BigEndianFormatter<2>>(port));
}
SERIALIZE_METHODS(CService, obj) { READWRITE(obj.ip, Using<BigEndianFormatter<2>>(obj.port)); }
};

#endif // PIVX_NETADDRESS_H
8 changes: 1 addition & 7 deletions src/policy/feerate.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ class CFeeRate
CFeeRate& operator+=(const CFeeRate& a) { nSatoshisPerK += a.nSatoshisPerK; return *this; }
std::string ToString() const;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(nSatoshisPerK);
}
SERIALIZE_METHODS(CFeeRate, obj) { READWRITE(obj.nSatoshisPerK); }
};

#endif // PIVX_POLICY_FEERATE_H
42 changes: 15 additions & 27 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,16 @@ class CBlockHeader
SetNull();
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nVersion);
READWRITE(hashPrevBlock);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
SERIALIZE_METHODS(CBlockHeader, obj) {
READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce);

//zerocoin active, header changes to include accumulator checksum
if(nVersion > 3 && nVersion < 7)
READWRITE(nAccumulatorCheckpoint);
if(obj.nVersion > 3 && obj.nVersion < 7)
READWRITE(obj.nAccumulatorCheckpoint);

// Sapling active
if (nVersion >= 8)
READWRITE(hashFinalSaplingRoot);
if (obj.nVersion >= 8)
READWRITE(obj.hashFinalSaplingRoot);
}

void SetNull()
Expand Down Expand Up @@ -107,14 +99,12 @@ class CBlock : public CBlockHeader
*(static_cast<CBlockHeader*>(this)) = header;
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITEAS(CBlockHeader, *this);
READWRITE(vtx);
if(vtx.size() > 1 && vtx[1]->IsCoinStake())
READWRITE(vchBlockSig);
SERIALIZE_METHODS(CBlock, obj)
{
READWRITEAS(CBlockHeader, obj);
READWRITE(obj.vtx);
if(obj.vtx.size() > 1 && obj.vtx[1]->IsCoinStake())
READWRITE(obj.vchBlockSig);
}

void SetNull()
Expand Down Expand Up @@ -171,14 +161,12 @@ struct CBlockLocator
vHave = vHaveIn;
}

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
SERIALIZE_METHODS(CBlockLocator, obj)
{
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH))
READWRITE(nVersion);
READWRITE(vHave);
READWRITE(obj.vHave);
}

void SetNull()
Expand Down
25 changes: 3 additions & 22 deletions src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ class BaseOutPoint
BaseOutPoint(const uint256& hashIn, const uint32_t nIn, bool isTransparentIn = true) :
hash(hashIn), n(nIn), isTransparent(isTransparentIn) { }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(hash);
READWRITE(n);
}
SERIALIZE_METHODS(BaseOutPoint, obj) { READWRITE(obj.hash, obj.n); }

void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
Expand Down Expand Up @@ -110,14 +104,7 @@ class CTxIn
explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);
CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(prevout);
READWRITE(scriptSig);
READWRITE(nSequence);
}
SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }

bool IsFinal() const { return nSequence == SEQUENCE_FINAL; }
bool IsNull() const { return prevout.IsNull() && scriptSig.empty() && IsFinal(); }
Expand Down Expand Up @@ -159,13 +146,7 @@ class CTxOut

CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(nValue);
READWRITE(scriptPubKey);
}
SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }

void SetNull()
{
Expand Down
42 changes: 11 additions & 31 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ class CMessageHeader
std::string GetCommand() const;
bool IsValid(const MessageStartChars& messageStart) const;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(pchMessageStart);
READWRITE(pchCommand);
READWRITE(nMessageSize);
READWRITE(pchChecksum);
}
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }

// TODO: make private (improves encapsulation)
public:
Expand Down Expand Up @@ -319,23 +310,19 @@ class CAddress : public CService

void Init();

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(CAddress, obj)
{
if (ser_action.ForRead())
Init();
SER_READ(obj, obj.Init());
int nVersion = s.GetVersion();
if (s.GetType() & SER_DISK)
if (s.GetType() & SER_DISK) {
READWRITE(nVersion);
}
if ((s.GetType() & SER_DISK) ||
(nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH)))
READWRITE(nTime);
uint64_t nServicesInt = nServices;
READWRITE(nServicesInt);
nServices = static_cast<ServiceFlags>(nServicesInt);
READWRITEAS(CService, *this);
(nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH))) {
READWRITE(obj.nTime);
}
READWRITE(Using<CustomUintFormatter<8>>(obj.nServices));
READWRITEAS(CService, obj);
}

// TODO: make private (improves encapsulation)
Expand All @@ -353,14 +340,7 @@ class CInv
CInv();
CInv(int typeIn, const uint256& hashIn);

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(type);
READWRITE(hash);
}
SERIALIZE_METHODS(CInv, obj) { READWRITE(obj.type, obj.hash); }

friend bool operator<(const CInv& a, const CInv& b);

Expand Down
9 changes: 2 additions & 7 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,13 @@ struct CCoin {
uint32_t nHeight;
CTxOut out;

ADD_SERIALIZE_METHODS;

CCoin() : nHeight(0) {}
CCoin(Coin&& in) : nHeight(in.nHeight), out(std::move(in.out)) {}

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
SERIALIZE_METHODS(CCoin, obj)
{
uint32_t nTxVerDummy = 0;
READWRITE(nTxVerDummy);
READWRITE(nHeight);
READWRITE(out);
READWRITE(nTxVerDummy, obj.nHeight, obj.out);
}
};

Expand Down
8 changes: 1 addition & 7 deletions src/script/keyorigin.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ struct KeyOriginInfo
return std::equal(std::begin(a.fingerprint), std::end(a.fingerprint), std::begin(b.fingerprint)) && a.path == b.path;
}

ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(fingerprint);
READWRITE(path);
}
SERIALIZE_METHODS(KeyOriginInfo, obj) { READWRITE(obj.fingerprint, obj.path); }

void clear()
{
Expand Down
7 changes: 1 addition & 6 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,7 @@ class CScript : public CScriptBase
CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITEAS(CScriptBase, *this);
}
SERIALIZE_METHODS(CScript, obj) { READWRITEAS(CScriptBase, obj); }

CScript& operator+=(const CScript& b)
{
Expand Down
36 changes: 19 additions & 17 deletions src/test/dbwrapper_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,24 +239,26 @@ struct StringContentsSerializer {
}
StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
if (ser_action.ForRead()) {
str.clear();
char c = 0;
while (true) {
try {
READWRITE(c);
str.push_back(c);
} catch (const std::ios_base::failure& e) {
break;
}
template<typename Stream>
void Serialize(Stream& s) const
{
for (size_t i = 0; i < str.size(); i++) {
s << str[i];
}
}

template<typename Stream>
void Unserialize(Stream& s)
{
str.clear();
char c = 0;
while (true) {
try {
s >> c;
str.push_back(c);
} catch (const std::ios_base::failure&) {
break;
}
} else {
for (size_t i = 0; i < str.size(); i++)
READWRITE(str[i]);
}
}
};
Expand Down
Loading

0 comments on commit 0f15784

Please sign in to comment.