Merge pull request #4737

31e9a83 Use CSizeComputer to avoid counting sizes in SerializationOp (Pieter Wuille)
84881f8 rework overhauled serialization methods to non-static (Kamil Domanski)
5d96b4a remove fields of ser_streamplaceholder (Kamil Domanski)
3d796f8 overhaul serialization code (Kamil Domanski)
This commit is contained in:
Pieter Wuille 2014-09-01 21:00:32 +02:00
commit 2e731f24b5
No known key found for this signature in database
GPG key ID: 8F653255C87992E0
13 changed files with 277 additions and 199 deletions

View file

@ -46,13 +46,15 @@ private:
public: public:
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
CAddress* pthis = (CAddress*)(this);
READWRITE(*pthis); template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(*(CAddress*)this);
READWRITE(source); READWRITE(source);
READWRITE(nLastSuccess); READWRITE(nLastSuccess);
READWRITE(nAttempts); READWRITE(nAttempts);
) }
void Init() void Init()
{ {

View file

@ -46,8 +46,10 @@ public:
std::string strStatusBar; std::string strStatusBar;
std::string strReserved; std::string strReserved;
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(nRelayUntil); READWRITE(nRelayUntil);
@ -63,7 +65,7 @@ public:
READWRITE(LIMITED_STRING(strComment, 65536)); READWRITE(LIMITED_STRING(strComment, 65536));
READWRITE(LIMITED_STRING(strStatusBar, 256)); READWRITE(LIMITED_STRING(strStatusBar, 256));
READWRITE(LIMITED_STRING(strReserved, 256)); READWRITE(LIMITED_STRING(strReserved, 256));
) }
void SetNull(); void SetNull();
@ -82,11 +84,13 @@ public:
SetNull(); SetNull();
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(vchMsg); READWRITE(vchMsg);
READWRITE(vchSig); READWRITE(vchSig);
) }
void SetNull(); void SetNull();
bool IsNull() const; bool IsNull() const;

View file

@ -62,13 +62,15 @@ public:
CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn); CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);
CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {} CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {}
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(vData); READWRITE(vData);
READWRITE(nHashFuncs); READWRITE(nHashFuncs);
READWRITE(nTweak); READWRITE(nTweak);
READWRITE(nFlags); READWRITE(nFlags);
) }
void insert(const std::vector<unsigned char>& vKey); void insert(const std::vector<unsigned char>& vKey);
void insert(const COutPoint& outpoint); void insert(const COutPoint& outpoint);

View file

@ -30,7 +30,14 @@ public:
COutPoint() { SetNull(); } COutPoint() { SetNull(); }
COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; } COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
IMPLEMENT_SERIALIZE( READWRITE(FLATDATA(*this)); )
IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(FLATDATA(*this));
}
void SetNull() { hash = 0; n = (uint32_t) -1; } void SetNull() { hash = 0; n = (uint32_t) -1; }
bool IsNull() const { return (hash == 0 && n == (uint32_t) -1); } bool IsNull() const { return (hash == 0 && n == (uint32_t) -1); }
@ -84,12 +91,14 @@ public:
explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max()); explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max());
CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<uint32_t>::max()); CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<uint32_t>::max());
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(prevout); READWRITE(prevout);
READWRITE(scriptSig); READWRITE(scriptSig);
READWRITE(nSequence); READWRITE(nSequence);
) }
bool IsFinal() const bool IsFinal() const
{ {
@ -136,7 +145,12 @@ public:
friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; } friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
std::string ToString() const; std::string ToString() const;
IMPLEMENT_SERIALIZE( READWRITE(nSatoshisPerK); ) IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(nSatoshisPerK);
}
}; };
@ -156,11 +170,13 @@ public:
CTxOut(int64_t nValueIn, CScript scriptPubKeyIn); CTxOut(int64_t nValueIn, CScript scriptPubKeyIn);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(nValue); READWRITE(nValue);
READWRITE(scriptPubKey); READWRITE(scriptPubKey);
) }
void SetNull() void SetNull()
{ {
@ -237,7 +253,12 @@ public:
CTransaction& operator=(const CTransaction& tx); CTransaction& operator=(const CTransaction& tx);
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
READWRITE(*const_cast<int32_t*>(&this->nVersion)); READWRITE(*const_cast<int32_t*>(&this->nVersion));
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(*const_cast<std::vector<CTxIn>*>(&vin)); READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
@ -245,7 +266,7 @@ public:
READWRITE(*const_cast<uint32_t*>(&nLockTime)); READWRITE(*const_cast<uint32_t*>(&nLockTime));
if (fRead) if (fRead)
UpdateHash(); UpdateHash();
) }
bool IsNull() const { bool IsNull() const {
return vin.empty() && vout.empty(); return vin.empty() && vout.empty();
@ -292,13 +313,16 @@ struct CMutableTransaction
CMutableTransaction(); CMutableTransaction();
CMutableTransaction(const CTransaction& tx); CMutableTransaction(const CTransaction& tx);
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(vin); READWRITE(vin);
READWRITE(vout); READWRITE(vout);
READWRITE(nLockTime); READWRITE(nLockTime);
) }
/** Compute the hash of this CMutableTransaction. This is computed on the /** Compute the hash of this CMutableTransaction. This is computed on the
* fly, as opposed to GetHash() in CTransaction, which uses a cached result. * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
@ -318,7 +342,11 @@ public:
CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { } CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
IMPLEMENT_SERIALIZE(({ IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
if (!fRead) { if (!fRead) {
uint64_t nVal = CompressAmount(txout.nValue); uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal)); READWRITE(VARINT(nVal));
@ -329,7 +357,7 @@ public:
} }
CScriptCompressor cscript(REF(txout.scriptPubKey)); CScriptCompressor cscript(REF(txout.scriptPubKey));
READWRITE(cscript); READWRITE(cscript);
});) }
}; };
/** Undo information for a CTxIn /** Undo information for a CTxIn
@ -382,9 +410,12 @@ public:
// undo information for all txins // undo information for all txins
std::vector<CTxInUndo> vprevout; std::vector<CTxInUndo> vprevout;
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(vprevout); READWRITE(vprevout);
) }
}; };
@ -412,8 +443,10 @@ public:
SetNull(); SetNull();
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(hashPrevBlock); READWRITE(hashPrevBlock);
@ -421,7 +454,7 @@ public:
READWRITE(nTime); READWRITE(nTime);
READWRITE(nBits); READWRITE(nBits);
READWRITE(nNonce); READWRITE(nNonce);
) }
void SetNull() void SetNull()
{ {
@ -467,11 +500,13 @@ public:
*((CBlockHeader*)this) = header; *((CBlockHeader*)this) = header;
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(*(CBlockHeader*)this); READWRITE(*(CBlockHeader*)this);
READWRITE(vtx); READWRITE(vtx);
) }
void SetNull() void SetNull()
{ {
@ -515,12 +550,14 @@ struct CBlockLocator
vHave = vHaveIn; vHave = vHaveIn;
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(vHave); READWRITE(vHave);
) }
void SetNull() void SetNull()
{ {

View file

@ -43,14 +43,17 @@ public:
// such as the various parameters to scrypt // such as the various parameters to scrypt
std::vector<unsigned char> vchOtherDerivationParameters; std::vector<unsigned char> vchOtherDerivationParameters;
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(vchCryptedKey); READWRITE(vchCryptedKey);
READWRITE(vchSalt); READWRITE(vchSalt);
READWRITE(nDerivationMethod); READWRITE(nDerivationMethod);
READWRITE(nDeriveIterations); READWRITE(nDeriveIterations);
READWRITE(vchOtherDerivationParameters); READWRITE(vchOtherDerivationParameters);
) }
CMasterKey() CMasterKey()
{ {
// 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M

View file

@ -197,10 +197,13 @@ struct CDiskBlockPos
int nFile; int nFile;
unsigned int nPos; unsigned int nPos;
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(VARINT(nFile)); READWRITE(VARINT(nFile));
READWRITE(VARINT(nPos)); READWRITE(VARINT(nPos));
) }
CDiskBlockPos() { CDiskBlockPos() {
SetNull(); SetNull();
@ -227,10 +230,13 @@ struct CDiskTxPos : public CDiskBlockPos
{ {
unsigned int nTxOffset; // after header unsigned int nTxOffset; // after header
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(*(CDiskBlockPos*)this); READWRITE(*(CDiskBlockPos*)this);
READWRITE(VARINT(nTxOffset)); READWRITE(VARINT(nTxOffset));
) }
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) { CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
} }
@ -307,9 +313,12 @@ class CBlockUndo
public: public:
std::vector<CTxUndo> vtxundo; // for all but the coinbase std::vector<CTxUndo> vtxundo; // for all but the coinbase
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(vtxundo); READWRITE(vtxundo);
) }
bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock); bool WriteToDisk(CDiskBlockPos &pos, const uint256 &hashBlock);
bool ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock); bool ReadFromDisk(const CDiskBlockPos &pos, const uint256 &hashBlock);
@ -411,7 +420,12 @@ protected:
public: public:
// serialization implementation // serialization implementation
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
READWRITE(nTransactions); READWRITE(nTransactions);
READWRITE(vHash); READWRITE(vHash);
std::vector<unsigned char> vBytes; std::vector<unsigned char> vBytes;
@ -428,7 +442,7 @@ public:
vBytes[p / 8] |= vBits[p] << (p % 8); vBytes[p / 8] |= vBits[p] << (p % 8);
READWRITE(vBytes); READWRITE(vBytes);
} }
) }
// Construct a partial merkle tree from a list of transaction id's, and a mask that selects a subset of them // Construct a partial merkle tree from a list of transaction id's, and a mask that selects a subset of them
CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch); CPartialMerkleTree(const std::vector<uint256> &vTxid, const std::vector<bool> &vMatch);
@ -484,7 +498,10 @@ public:
uint64_t nTimeFirst; // earliest time of block in file uint64_t nTimeFirst; // earliest time of block in file
uint64_t nTimeLast; // latest time of block in file uint64_t nTimeLast; // latest time of block in file
IMPLEMENT_SERIALIZE( IMPLEMENT_SERIALIZE;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(VARINT(nBlocks)); READWRITE(VARINT(nBlocks));
READWRITE(VARINT(nSize)); READWRITE(VARINT(nSize));
READWRITE(VARINT(nUndoSize)); READWRITE(VARINT(nUndoSize));
@ -492,7 +509,7 @@ public:
READWRITE(VARINT(nHeightLast)); READWRITE(VARINT(nHeightLast));
READWRITE(VARINT(nTimeFirst)); READWRITE(VARINT(nTimeFirst));
READWRITE(VARINT(nTimeLast)); READWRITE(VARINT(nTimeLast));
) }
void SetNull() { void SetNull() {
nBlocks = 0; nBlocks = 0;
@ -755,8 +772,10 @@ public:
hashPrev = (pprev ? pprev->GetBlockHash() : 0); hashPrev = (pprev ? pprev->GetBlockHash() : 0);
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(VARINT(nVersion)); READWRITE(VARINT(nVersion));
@ -777,7 +796,7 @@ public:
READWRITE(nTime); READWRITE(nTime);
READWRITE(nBits); READWRITE(nBits);
READWRITE(nNonce); READWRITE(nNonce);
) }
uint256 GetBlockHash() const uint256 GetBlockHash() const
{ {
@ -975,11 +994,13 @@ public:
// thus the filter will likely be modified. // thus the filter will likely be modified.
CMerkleBlock(const CBlock& block, CBloomFilter& filter); CMerkleBlock(const CBlock& block, CBloomFilter& filter);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(header); READWRITE(header);
READWRITE(txn); READWRITE(txn);
) }
}; };

View file

@ -88,10 +88,12 @@ class CNetAddr
friend bool operator!=(const CNetAddr& a, const CNetAddr& b); friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
friend bool operator<(const CNetAddr& a, const CNetAddr& b); friend bool operator<(const CNetAddr& a, const CNetAddr& b);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(FLATDATA(ip)); READWRITE(FLATDATA(ip));
) }
}; };
class CSubNet class CSubNet
@ -148,15 +150,17 @@ class CService : public CNetAddr
CService(const struct in6_addr& ipv6Addr, unsigned short port); CService(const struct in6_addr& ipv6Addr, unsigned short port);
CService(const struct sockaddr_in6& addr); CService(const struct sockaddr_in6& addr);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
CService* pthis = const_cast<CService*>(this); template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
READWRITE(FLATDATA(ip)); READWRITE(FLATDATA(ip));
unsigned short portN = htons(port); unsigned short portN = htons(port);
READWRITE(portN); READWRITE(portN);
if (fRead) if (fRead)
pthis->port = ntohs(portN); port = ntohs(portN);
) }
}; };
typedef CService proxyType; typedef CService proxyType;

View file

@ -35,13 +35,15 @@ class CMessageHeader
std::string GetCommand() const; std::string GetCommand() const;
bool IsValid() const; bool IsValid() const;
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(FLATDATA(pchMessageStart)); READWRITE(FLATDATA(pchMessageStart));
READWRITE(FLATDATA(pchCommand)); READWRITE(FLATDATA(pchCommand));
READWRITE(nMessageSize); READWRITE(nMessageSize);
READWRITE(nChecksum); READWRITE(nChecksum);
) }
// TODO: make private (improves encapsulation) // TODO: make private (improves encapsulation)
public: public:
@ -83,10 +85,13 @@ class CAddress : public CService
void Init(); void Init();
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
CAddress* pthis = const_cast<CAddress*>(this); CAddress* pthis = const_cast<CAddress*>(this);
CService* pip = (CService*)pthis;
if (fRead) if (fRead)
pthis->Init(); pthis->Init();
if (nType & SER_DISK) if (nType & SER_DISK)
@ -95,8 +100,8 @@ class CAddress : public CService
(nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
READWRITE(nTime); READWRITE(nTime);
READWRITE(nServices); READWRITE(nServices);
READWRITE(*pip); READWRITE(*(CService*)this);
) }
// TODO: make private (improves encapsulation) // TODO: make private (improves encapsulation)
public: public:
@ -117,11 +122,13 @@ class CInv
CInv(int typeIn, const uint256& hashIn); CInv(int typeIn, const uint256& hashIn);
CInv(const std::string& strType, const uint256& hashIn); CInv(const std::string& strType, const uint256& hashIn);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(type); READWRITE(type);
READWRITE(hash); READWRITE(hash);
) }
friend bool operator<(const CInv& a, const CInv& b); friend bool operator<(const CInv& a, const CInv& b);

View file

@ -24,8 +24,12 @@ public:
QDateTime date; QDateTime date;
SendCoinsRecipient recipient; SendCoinsRecipient recipient;
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
RecentRequestEntry* pthis = const_cast<RecentRequestEntry*>(this); RecentRequestEntry* pthis = const_cast<RecentRequestEntry*>(this);
unsigned int nDate = date.toTime_t(); unsigned int nDate = date.toTime_t();
@ -37,8 +41,8 @@ public:
READWRITE(recipient); READWRITE(recipient);
if (fRead) if (fRead)
pthis->date = QDateTime::fromTime_t(nDate); date = QDateTime::fromTime_t(nDate);
) }
}; };
class RecentRequestEntryLessThan class RecentRequestEntryLessThan

View file

@ -59,8 +59,12 @@ public:
static const int CURRENT_VERSION = 1; static const int CURRENT_VERSION = 1;
int nVersion; int nVersion;
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
SendCoinsRecipient* pthis = const_cast<SendCoinsRecipient*>(this); SendCoinsRecipient* pthis = const_cast<SendCoinsRecipient*>(this);
std::string sAddress = pthis->address.toStdString(); std::string sAddress = pthis->address.toStdString();
@ -89,7 +93,7 @@ public:
pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size())); pthis->paymentRequest.parse(QByteArray::fromRawData(sPaymentRequest.data(), sPaymentRequest.size()));
pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant); pthis->authenticatedMerchant = QString::fromStdString(sAuthenticatedMerchant);
} }
) }
}; };
/** Interface to Bitcoin wallet from Qt view code. */ /** Interface to Bitcoin wallet from Qt view code. */

View file

@ -37,6 +37,14 @@ inline T& REF(const T& val)
return const_cast<T&>(val); return const_cast<T&>(val);
} }
// Used to acquire a non-const pointer "this" to generate bodies
// of const serialization operations from a template
template<typename T>
inline T* NCONST_PTR(const T* val)
{
return const_cast<T*>(val);
}
/** Get begin pointer of vector (non-const version). /** Get begin pointer of vector (non-const version).
* @note These functions avoid the undefined case of indexing into an empty * @note These functions avoid the undefined case of indexing into an empty
* vector, as well as that of indexing after the end of the vector. * vector, as well as that of indexing after the end of the vector.
@ -79,49 +87,27 @@ enum
SER_GETHASH = (1 << 2), SER_GETHASH = (1 << 2),
}; };
#define IMPLEMENT_SERIALIZE(statements) \ #define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action))
unsigned int GetSerializeSize(int nType, int nVersion) const \
{ \ /* Implement three methods for serializable objects. These are actually wrappers over
CSerActionGetSerializeSize ser_action; \ * "SerializationOp" template, which implements the body of each class' serialization
const bool fGetSize = true; \ * code. Adding "IMPLEMENT_SERIALIZE" in the body of the class causes these wrappers to be
const bool fWrite = false; \ * added as members. */
const bool fRead = false; \ #define IMPLEMENT_SERIALIZE \
unsigned int nSerSize = 0; \ size_t GetSerializeSize(int nType, int nVersion) const { \
ser_streamplaceholder s; \ CSizeComputer s(nType, nVersion); \
assert(fGetSize||fWrite||fRead); /* suppress warning */ \ NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
s.nType = nType; \ return s.size(); \
s.nVersion = nVersion; \
{statements} \
return nSerSize; \
} \ } \
template<typename Stream> \ template<typename Stream> \
void Serialize(Stream& s, int nType, int nVersion) const \ void Serialize(Stream& s, int nType, int nVersion) const { \
{ \ NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
CSerActionSerialize ser_action; \
const bool fGetSize = false; \
const bool fWrite = true; \
const bool fRead = false; \
unsigned int nSerSize = 0; \
assert(fGetSize||fWrite||fRead); /* suppress warning */ \
{statements} \
} \ } \
template<typename Stream> \ template<typename Stream> \
void Unserialize(Stream& s, int nType, int nVersion) \ void Unserialize(Stream& s, int nType, int nVersion) { \
{ \ SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \
CSerActionUnserialize ser_action; \
const bool fGetSize = false; \
const bool fWrite = false; \
const bool fRead = true; \
unsigned int nSerSize = 0; \
assert(fGetSize||fWrite||fRead); /* suppress warning */ \
{statements} \
} }
#define READWRITE(obj) (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))
// //
@ -823,37 +809,26 @@ void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion)
// //
// Support for IMPLEMENT_SERIALIZE and READWRITE macro // Support for IMPLEMENT_SERIALIZE and READWRITE macro
// //
class CSerActionGetSerializeSize { }; struct CSerActionSerialize
class CSerActionSerialize { };
class CSerActionUnserialize { };
template<typename Stream, typename T>
inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
{ {
return ::GetSerializeSize(obj, nType, nVersion); bool ForRead() const { return false; }
} };
struct CSerActionUnserialize
template<typename Stream, typename T>
inline unsigned int SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
{ {
::Serialize(s, obj, nType, nVersion); bool ForRead() const { return true; }
return 0;
}
template<typename Stream, typename T>
inline unsigned int SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
{
::Unserialize(s, obj, nType, nVersion);
return 0;
}
struct ser_streamplaceholder
{
int nType;
int nVersion;
}; };
template<typename Stream, typename T>
inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action)
{
::Serialize(s, obj, nType, nVersion);
}
template<typename Stream, typename T>
inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action)
{
::Unserialize(s, obj, nType, nVersion);
}

View file

@ -64,13 +64,15 @@ public:
CKeyPool(); CKeyPool();
CKeyPool(const CPubKey& vchPubKeyIn); CKeyPool(const CPubKey& vchPubKeyIn);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(nTime); READWRITE(nTime);
READWRITE(vchPubKey); READWRITE(vchPubKey);
) }
}; };
/** Address book data */ /** Address book data */
@ -490,16 +492,16 @@ public:
fMerkleVerified = false; fMerkleVerified = false;
} }
IMPLEMENT_SERIALIZE;
IMPLEMENT_SERIALIZE template <typename Stream, typename Operation>
( inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
nSerSize += SerReadWrite(s, *(CTransaction*)this, nType, nVersion, ser_action); READWRITE(*(CTransaction*)this);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(hashBlock); READWRITE(hashBlock);
READWRITE(vMerkleBranch); READWRITE(vMerkleBranch);
READWRITE(nIndex); READWRITE(nIndex);
) }
int SetMerkleBranch(const CBlock* pblock=NULL); int SetMerkleBranch(const CBlock* pblock=NULL);
@ -514,7 +516,6 @@ public:
bool AcceptToMemoryPool(bool fLimitFree=true, bool fRejectInsaneFee=true); bool AcceptToMemoryPool(bool fLimitFree=true, bool fRejectInsaneFee=true);
}; };
/** A transaction with a bunch of additional info that only the owner cares about. /** A transaction with a bunch of additional info that only the owner cares about.
* It includes any unrecorded transactions needed to link it back to the block chain. * It includes any unrecorded transactions needed to link it back to the block chain.
*/ */
@ -604,8 +605,12 @@ public:
nOrderPos = -1; nOrderPos = -1;
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
CWalletTx* pthis = const_cast<CWalletTx*>(this); CWalletTx* pthis = const_cast<CWalletTx*>(this);
if (fRead) if (fRead)
pthis->Init(NULL); pthis->Init(NULL);
@ -621,7 +626,7 @@ public:
pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart); pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart);
} }
nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action); READWRITE(*(CMerkleTx*)this);
std::vector<CMerkleTx> vUnused; // Used to be vtxPrev std::vector<CMerkleTx> vUnused; // Used to be vtxPrev
READWRITE(vUnused); READWRITE(vUnused);
READWRITE(mapValue); READWRITE(mapValue);
@ -640,12 +645,12 @@ public:
pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0; pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0;
} }
pthis->mapValue.erase("fromaccount"); mapValue.erase("fromaccount");
pthis->mapValue.erase("version"); mapValue.erase("version");
pthis->mapValue.erase("spent"); mapValue.erase("spent");
pthis->mapValue.erase("n"); mapValue.erase("n");
pthis->mapValue.erase("timesmart"); mapValue.erase("timesmart");
) }
// make sure balances are recalculated // make sure balances are recalculated
void MarkDirty() void MarkDirty()
@ -891,15 +896,17 @@ public:
CWalletKey(int64_t nExpires=0); CWalletKey(int64_t nExpires=0);
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(vchPrivKey); READWRITE(vchPrivKey);
READWRITE(nTimeCreated); READWRITE(nTimeCreated);
READWRITE(nTimeExpires); READWRITE(nTimeExpires);
READWRITE(LIMITED_STRING(strComment, 65536)); READWRITE(LIMITED_STRING(strComment, 65536));
) }
}; };
@ -925,12 +932,14 @@ public:
vchPubKey = CPubKey(); vchPubKey = CPubKey();
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
READWRITE(vchPubKey); READWRITE(vchPubKey);
) }
}; };
@ -966,8 +975,12 @@ public:
nEntryNo = 0; nEntryNo = 0;
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
bool fRead = ser_action.ForRead();
CAccountingEntry& me = *const_cast<CAccountingEntry*>(this); CAccountingEntry& me = *const_cast<CAccountingEntry*>(this);
if (!(nType & SER_GETHASH)) if (!(nType & SER_GETHASH))
READWRITE(nVersion); READWRITE(nVersion);
@ -1007,8 +1020,8 @@ public:
if (std::string::npos != nSepPos) if (std::string::npos != nSepPos)
me.strComment.erase(nSepPos); me.strComment.erase(nSepPos);
me.mapValue.erase("n"); mapValue.erase("n");
) }
private: private:
std::vector<char> _ssExtra; std::vector<char> _ssExtra;

View file

@ -55,12 +55,14 @@ public:
nCreateTime = nCreateTime_; nCreateTime = nCreateTime_;
} }
IMPLEMENT_SERIALIZE IMPLEMENT_SERIALIZE;
(
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(this->nVersion); READWRITE(this->nVersion);
nVersion = this->nVersion; nVersion = this->nVersion;
READWRITE(nCreateTime); READWRITE(nCreateTime);
) }
void SetNull() void SetNull()
{ {