From 5185fb954ababbf8e8d78d5345c4c518000e7bdf Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Wed, 9 Nov 2016 12:32:57 +0100 Subject: [PATCH] =?UTF-8?q?Merge=20bitcoin#9039:=20Various=20serialization?= =?UTF-8?q?=20simplifcations=20and=20optimiza=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …tions d59a518 Use fixed preallocation instead of costly GetSerializeSize (Pieter Wuille) 25a211a Add optimized CSizeComputer serializers (Pieter Wuille) a2929a2 Make CSerAction's ForRead() constexpr (Pieter Wuille) a603925 Avoid -Wshadow errors (Pieter Wuille) 5284721 Get rid of nType and nVersion (Pieter Wuille) 657e05a Make GetSerializeSize a wrapper on top of CSizeComputer (Pieter Wuille) fad9b66 Make nType and nVersion private and sometimes const (Pieter Wuille) c2c5d42 Make streams' read and write return void (Pieter Wuille) 50e8a9c Remove unused ReadVersion and WriteVersion (Pieter Wuille) --- src/addrdb.h | 2 +- src/addrman.h | 11 +- src/amount.h | 2 +- src/bloom.h | 2 +- src/chain.h | 9 +- src/coins.h | 12 +- src/compressor.h | 14 +- src/hash.h | 14 +- src/key.h | 4 +- src/merkleblock.h | 4 +- src/net.h | 2 +- src/netaddress.h | 6 +- src/primitives/block.h | 9 +- src/primitives/transaction.h | 12 +- src/protocol.h | 13 +- src/pubkey.h | 17 +- src/qt/recentrequeststablemodel.h | 2 +- src/qt/walletmodel.h | 2 +- src/rest.cpp | 2 +- src/script/absoluteconsensus.cpp | 9 +- src/script/interpreter.cpp | 30 +- src/serialize.h | 463 ++++++++++++------------------ src/streams.h | 93 +++--- src/test/bloom_tests.cpp | 6 +- src/test/dbwrapper_tests.cpp | 2 +- src/test/net_tests.cpp | 8 +- src/test/serialize_tests.cpp | 4 +- src/test/uint256_tests.cpp | 46 +-- src/txdb.cpp | 16 +- src/txdb.h | 2 +- src/uint256.h | 9 +- src/undo.h | 30 +- src/validation.cpp | 4 +- src/wallet/crypter.h | 2 +- src/wallet/wallet.h | 28 +- src/wallet/walletdb.h | 2 +- 36 files changed, 383 insertions(+), 510 deletions(-) diff --git a/src/addrdb.h b/src/addrdb.h index 2a1864fbcbe4d..7129d24a115f7 100644 --- a/src/addrdb.h +++ b/src/addrdb.h @@ -46,7 +46,7 @@ class CBanEntry ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(this->nVersion); READWRITE(nCreateTime); diff --git a/src/addrman.h b/src/addrman.h index e36bee2d251de..64484dc3c4ae8 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -56,7 +56,7 @@ class CAddrInfo : public CAddress ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(*(CAddress*)this); READWRITE(source); READWRITE(nLastSuccess); @@ -289,7 +289,7 @@ class CAddrMan * very little in common. */ template - void Serialize(Stream &s, int nType, int nVersionDummy) const + void Serialize(Stream &s) const { LOCK(cs); @@ -339,7 +339,7 @@ class CAddrMan } template - void Unserialize(Stream& s, int nType, int nVersionDummy) + void Unserialize(Stream& s) { LOCK(cs); @@ -444,11 +444,6 @@ class CAddrMan Check(); } - unsigned int GetSerializeSize(int nType, int nVersion) const - { - return (CSizeComputer(nType, nVersion) << *this).size(); - } - void Clear() { std::vector().swap(vRandom); diff --git a/src/amount.h b/src/amount.h index 5e52f37f2369d..ba0c86040f18c 100644 --- a/src/amount.h +++ b/src/amount.h @@ -64,7 +64,7 @@ class CFeeRate ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(nSatoshisPerK); } }; diff --git a/src/bloom.h b/src/bloom.h index ad6de625d8f8a..d3a017371f30a 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -73,7 +73,7 @@ class CBloomFilter ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(vData); READWRITE(nHashFuncs); READWRITE(nTweak); diff --git a/src/chain.h b/src/chain.h index 7db367b554608..08fd213e1f90e 100644 --- a/src/chain.h +++ b/src/chain.h @@ -28,7 +28,7 @@ class CBlockFileInfo ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(VARINT(nBlocks)); READWRITE(VARINT(nSize)); READWRITE(VARINT(nUndoSize)); @@ -76,7 +76,7 @@ struct CDiskBlockPos ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(VARINT(nFile)); READWRITE(VARINT(nPos)); } @@ -358,8 +358,9 @@ class CDiskBlockIndex : public CBlockIndex ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!(nType & SER_GETHASH)) + inline void SerializationOp(Stream& s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT(nVersion)); READWRITE(VARINT(nHeight)); diff --git a/src/coins.h b/src/coins.h index 735c0c6b980e3..b56342ca74774 100644 --- a/src/coins.h +++ b/src/coins.h @@ -56,20 +56,20 @@ class Coin } template - void Serialize(Stream &s, int nType, int nVersion) const { + void Serialize(Stream &s) const { assert(!IsSpent()); uint32_t code = nHeight * 2 + fCoinBase; - ::Serialize(s, VARINT(code), nType, nVersion); - ::Serialize(s, CTxOutCompressor(REF(out)), nType, nVersion); + ::Serialize(s, VARINT(code)); + ::Serialize(s, CTxOutCompressor(REF(out))); } template - void Unserialize(Stream &s, int nType, int nVersion) { + void Unserialize(Stream &s) { uint32_t code = 0; - ::Unserialize(s, VARINT(code), nType, nVersion); + ::Unserialize(s, VARINT(code)); nHeight = code >> 1; fCoinBase = code & 1; - ::Unserialize(s, REF(CTxOutCompressor(out)), nType, nVersion); + ::Unserialize(s, REF(CTxOutCompressor(out))); } bool IsSpent() const { diff --git a/src/compressor.h b/src/compressor.h index fa702f0dfa900..961365d2618a4 100644 --- a/src/compressor.h +++ b/src/compressor.h @@ -55,16 +55,8 @@ class CScriptCompressor public: CScriptCompressor(CScript &scriptIn) : script(scriptIn) { } - unsigned int GetSerializeSize(int nType, int nVersion) const { - std::vector compr; - if (Compress(compr)) - return compr.size(); - unsigned int nSize = script.size() + nSpecialScripts; - return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion); - } - template - void Serialize(Stream &s, int nType, int nVersion) const { + void Serialize(Stream &s) const { std::vector compr; if (Compress(compr)) { s << CFlatData(compr); @@ -76,7 +68,7 @@ class CScriptCompressor } template - void Unserialize(Stream &s, int nType, int nVersion) { + void Unserialize(Stream &s) { unsigned int nSize = 0; s >> VARINT(nSize); if (nSize < nSpecialScripts) { @@ -112,7 +104,7 @@ class CTxOutCompressor ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { if (!ser_action.ForRead()) { uint64_t nVal = CompressAmount(txout.nValue); READWRITE(VARINT(nVal)); diff --git a/src/hash.h b/src/hash.h index fb8250b16803c..b0222771a7ffa 100644 --- a/src/hash.h +++ b/src/hash.h @@ -240,15 +240,17 @@ class CHashWriter private: CHash256 ctx; + const int nType; + const int nVersion; public: - int nType; - int nVersion; CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {} - CHashWriter& write(const char *pch, size_t size) { + int GetType() const { return nType; } + int GetVersion() const { return nVersion; } + + void write(const char *pch, size_t size) { ctx.Write((const unsigned char*)pch, size); - return (*this); } // invalidates the object @@ -261,7 +263,7 @@ class CHashWriter template CHashWriter& operator<<(const T& obj) { // Serialize to this stream - ::Serialize(*this, obj, nType, nVersion); + ::Serialize(*this, obj); return (*this); } }; @@ -296,7 +298,7 @@ class CHashVerifier : public CHashWriter CHashVerifier& operator>>(T& obj) { // Unserialize from this stream - ::Unserialize(*this, obj, nType, nVersion); + ::Unserialize(*this, obj); return (*this); } }; diff --git a/src/key.h b/src/key.h index ad0356e83cfde..bf20eb28231b7 100644 --- a/src/key.h +++ b/src/key.h @@ -163,7 +163,7 @@ struct CExtKey { CExtPubKey Neuter() const; void SetMaster(const unsigned char* seed, unsigned int nSeedLen); template - void Serialize(Stream& s, int nType, int nVersion) const + void Serialize(Stream& s) const { unsigned int len = BIP32_EXTKEY_SIZE; ::WriteCompactSize(s, len); @@ -172,7 +172,7 @@ struct CExtKey { s.write((const char *)&code[0], len); } template - void Unserialize(Stream& s, int nType, int nVersion) + void Unserialize(Stream& s) { unsigned int len = ::ReadCompactSize(s); unsigned char code[BIP32_EXTKEY_SIZE]; diff --git a/src/merkleblock.h b/src/merkleblock.h index 835cbcce5550d..17c33194a95b1 100644 --- a/src/merkleblock.h +++ b/src/merkleblock.h @@ -85,7 +85,7 @@ class CPartialMerkleTree ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(nTransactions); READWRITE(vHash); std::vector vBytes; @@ -148,7 +148,7 @@ class CMerkleBlock ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(header); READWRITE(txn); } diff --git a/src/net.h b/src/net.h index dd723fc1ead89..a34a94944ccb1 100644 --- a/src/net.h +++ b/src/net.h @@ -184,7 +184,7 @@ class CConnman void PushMessageWithVersionAndFlag(CNode* pnode, int nVersion, int flag, const std::string& sCommand, Args&&... args) { auto msg(BeginMessage(pnode, nVersion, flag, sCommand)); - ::SerializeMany(msg, msg.nType, msg.nVersion, std::forward(args)...); + ::SerializeMany(msg, std::forward(args)...); EndMessage(msg); PushMessage(pnode, msg, sCommand); } diff --git a/src/netaddress.h b/src/netaddress.h index d92309a6f43d3..aa11e15732ef0 100644 --- a/src/netaddress.h +++ b/src/netaddress.h @@ -87,7 +87,7 @@ class CNetAddr ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(FLATDATA(ip)); } @@ -124,7 +124,7 @@ class CSubNet ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(network); READWRITE(FLATDATA(netmask)); READWRITE(FLATDATA(valid)); @@ -161,7 +161,7 @@ class CService : public CNetAddr ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(FLATDATA(ip)); unsigned short portN = htons(port); READWRITE(FLATDATA(portN)); diff --git a/src/primitives/block.h b/src/primitives/block.h index 09f45147691c7..1ad8338646aa3 100644 --- a/src/primitives/block.h +++ b/src/primitives/block.h @@ -38,7 +38,7 @@ class CBlockHeader ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(this->nVersion); READWRITE(hashPrevBlock); @@ -97,7 +97,7 @@ class CBlock : public CBlockHeader ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(*(CBlockHeader*)this); READWRITE(vtx); } @@ -145,8 +145,9 @@ struct CBlockLocator ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!(nType & SER_GETHASH)) + inline void SerializationOp(Stream& s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); READWRITE(vHave); } diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h index 82fa0ee35d032..49c6dcb512dab 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -24,7 +24,7 @@ class COutPoint ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(hash); READWRITE(n); } @@ -101,7 +101,7 @@ class CTxIn ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(prevout); READWRITE(*(CScriptBase*)(&scriptSig)); READWRITE(nSequence); @@ -147,7 +147,7 @@ class CTxOut ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(nValue); READWRITE(*(CScriptBase*)(&scriptPubKey)); } @@ -176,7 +176,7 @@ class CTxOut if (scriptPubKey.IsUnspendable()) return 0; - size_t nSize = GetSerializeSize(SER_DISK,0)+148u; + size_t nSize = GetSerializeSize(*this, SER_DISK, 0)+148u; return 3*minRelayTxFee.GetFee(nSize); } @@ -243,7 +243,7 @@ class CTransaction ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(*const_cast(&this->nVersion)); nVersion = this->nVersion; READWRITE(*const_cast*>(&vin)); @@ -311,7 +311,7 @@ struct CMutableTransaction ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(this->nVersion); nVersion = this->nVersion; READWRITE(vin); diff --git a/src/protocol.h b/src/protocol.h index f70554ad2dd35..d8dc2ce6e89f1 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -49,7 +49,7 @@ class CMessageHeader ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(FLATDATA(pchMessageStart)); READWRITE(FLATDATA(pchCommand)); @@ -294,14 +294,15 @@ class CAddress : public CService ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) + inline void SerializationOp(Stream& s, Operation ser_action) { if (ser_action.ForRead()) Init(); - if (nType & SER_DISK) + int nVersion = s.GetVersion(); + if (s.GetType() & SER_DISK) READWRITE(nVersion); - if ((nType & SER_DISK) || - (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) + if ((s.GetType() & SER_DISK) || + (nVersion >= CADDR_TIME_VERSION && !(s.GetType() & SER_GETHASH))) READWRITE(nTime); uint64_t nServicesInt = nServices; READWRITE(nServicesInt); @@ -358,7 +359,7 @@ class CInv ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(type); READWRITE(hash); diff --git a/src/pubkey.h b/src/pubkey.h index 3a554877f8000..9499862210ae5 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -116,19 +116,15 @@ class CPubKey } //! Implement serialization, as if this was a byte vector. - unsigned int GetSerializeSize(int nType, int nVersion) const - { - return size() + 1; - } template - void Serialize(Stream& s, int nType, int nVersion) const + void Serialize(Stream& s) const { unsigned int len = size(); ::WriteCompactSize(s, len); s.write((char*)vch, len); } template - void Unserialize(Stream& s, int nType, int nVersion) + void Unserialize(Stream& s) { unsigned int len = ::ReadCompactSize(s); if (len <= 65) { @@ -214,12 +210,13 @@ struct CExtPubKey { void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]); bool Derive(CExtPubKey& out, unsigned int nChild) const; - unsigned int GetSerializeSize(int nType, int nVersion) const + void Serialize(CSizeComputer& s) const { - return BIP32_EXTKEY_SIZE+1; //add one byte for the size (compact int) + // Optimized implementation for ::GetSerializeSize that avoids copying. + s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int) } template - void Serialize(Stream& s, int nType, int nVersion) const + void Serialize(Stream& s) const { unsigned int len = BIP32_EXTKEY_SIZE; ::WriteCompactSize(s, len); @@ -228,7 +225,7 @@ struct CExtPubKey { s.write((const char *)&code[0], len); } template - void Unserialize(Stream& s, int nType, int nVersion) + void Unserialize(Stream& s) { unsigned int len = ::ReadCompactSize(s); unsigned char code[BIP32_EXTKEY_SIZE]; diff --git a/src/qt/recentrequeststablemodel.h b/src/qt/recentrequeststablemodel.h index cb7c42c408bc3..2aa9c2a2caec4 100644 --- a/src/qt/recentrequeststablemodel.h +++ b/src/qt/recentrequeststablemodel.h @@ -27,7 +27,7 @@ class RecentRequestEntry ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { unsigned int nDate = date.toTime_t(); READWRITE(this->nVersion); diff --git a/src/qt/walletmodel.h b/src/qt/walletmodel.h index 75f1b9c23ddd6..9ad471ce53f75 100644 --- a/src/qt/walletmodel.h +++ b/src/qt/walletmodel.h @@ -72,7 +72,7 @@ class SendCoinsRecipient ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { std::string sAddress = address.toStdString(); std::string sLabel = label.toStdString(); std::string sMessage = message.toStdString(); diff --git a/src/rest.cpp b/src/rest.cpp index 9221a5f739954..f055ac76dad19 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -52,7 +52,7 @@ struct CCoin { CCoin(Coin&& in) : nHeight(in.nHeight), out(std::move(in.out)) {} template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) + inline void SerializationOp(Stream& s, Operation ser_action) { uint32_t nTxVerDummy = 0; READWRITE(nTxVerDummy); diff --git a/src/script/absoluteconsensus.cpp b/src/script/absoluteconsensus.cpp index 49ac0a36d75c4..ef82c1ad366c5 100644 --- a/src/script/absoluteconsensus.cpp +++ b/src/script/absoluteconsensus.cpp @@ -23,7 +23,7 @@ class TxInputStream m_remaining(txToLen) {} - TxInputStream& read(char* pch, size_t nSize) + void read(char* pch, size_t nSize) { if (nSize > m_remaining) throw std::ios_base::failure(std::string(__func__) + ": end of data"); @@ -37,16 +37,17 @@ class TxInputStream memcpy(pch, m_data, nSize); m_remaining -= nSize; m_data += nSize; - return *this; } template TxInputStream& operator>>(T& obj) { - ::Unserialize(*this, obj, m_type, m_version); + ::Unserialize(*this, obj); return *this; } + int GetVersion() const { return m_version; } + int GetType() const { return m_type; } private: const int m_type; const int m_version; @@ -79,7 +80,7 @@ int absoluteconsensus_verify_script(const unsigned char *scriptPubKey, unsigned stream >> tx; if (nIn >= tx.vin.size()) return set_error(err, absoluteconsensus_ERR_TX_INDEX); - if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) != txToLen) + if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) return set_error(err, absoluteconsensus_ERR_TX_SIZE_MISMATCH); // Regardless of the verification result, the tx did not error. diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp index 59fb291af714c..96a2a263bc0a8 100644 --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1042,7 +1042,7 @@ class CTransactionSignatureSerializer { /** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */ template - void SerializeScriptCode(S &s, int nType, int nVersion) const { + void SerializeScriptCode(S &s) const { CScript::const_iterator it = scriptCode.begin(); CScript::const_iterator itBegin = it; opcodetype opcode; @@ -1065,53 +1065,53 @@ class CTransactionSignatureSerializer { /** Serialize an input of txTo */ template - void SerializeInput(S &s, unsigned int nInput, int nType, int nVersion) const { + void SerializeInput(S &s, unsigned int nInput) const { // In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized if (fAnyoneCanPay) nInput = nIn; // Serialize the prevout - ::Serialize(s, txTo.vin[nInput].prevout, nType, nVersion); + ::Serialize(s, txTo.vin[nInput].prevout); // Serialize the script if (nInput != nIn) // Blank out other inputs' signatures - ::Serialize(s, CScriptBase(), nType, nVersion); + ::Serialize(s, CScriptBase()); else - SerializeScriptCode(s, nType, nVersion); + SerializeScriptCode(s); // Serialize the nSequence if (nInput != nIn && (fHashSingle || fHashNone)) // let the others update at will - ::Serialize(s, (int)0, nType, nVersion); + ::Serialize(s, (int)0); else - ::Serialize(s, txTo.vin[nInput].nSequence, nType, nVersion); + ::Serialize(s, txTo.vin[nInput].nSequence); } /** Serialize an output of txTo */ template - void SerializeOutput(S &s, unsigned int nOutput, int nType, int nVersion) const { + void SerializeOutput(S &s, unsigned int nOutput) const { if (fHashSingle && nOutput != nIn) // Do not lock-in the txout payee at other indices as txin - ::Serialize(s, CTxOut(), nType, nVersion); + ::Serialize(s, CTxOut()); else - ::Serialize(s, txTo.vout[nOutput], nType, nVersion); + ::Serialize(s, txTo.vout[nOutput]); } /** Serialize txTo */ template - void Serialize(S &s, int nType, int nVersion) const { + void Serialize(S &s) const { // Serialize nVersion - ::Serialize(s, txTo.nVersion, nType, nVersion); + ::Serialize(s, txTo.nVersion); // Serialize vin unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size(); ::WriteCompactSize(s, nInputs); for (unsigned int nInput = 0; nInput < nInputs; nInput++) - SerializeInput(s, nInput, nType, nVersion); + SerializeInput(s, nInput); // Serialize vout unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size()); ::WriteCompactSize(s, nOutputs); for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) - SerializeOutput(s, nOutput, nType, nVersion); + SerializeOutput(s, nOutput); // Serialize nLockTime - ::Serialize(s, txTo.nLockTime, nType, nVersion); + ::Serialize(s, txTo.nLockTime); } }; diff --git a/src/serialize.h b/src/serialize.h index 8e36eaf6ce8ce..572ab8f72841c 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -163,6 +163,8 @@ inline float ser_uint32_to_float(uint32_t y) // i.e. anything that supports .read(char*, size_t) and .write(char*, size_t) // +class CSizeComputer; + enum { // primary actions @@ -171,8 +173,8 @@ enum SER_GETHASH = (1 << 2), }; -#define READWRITE(obj) (::SerReadWrite(s, (obj), nType, nVersion, ser_action)) -#define READWRITEMANY(...) (::SerReadWriteMany(s, nType, nVersion, ser_action, __VA_ARGS__)) +#define READWRITE(obj) (::SerReadWrite(s, (obj), ser_action)) +#define READWRITEMANY(...) (::SerReadWriteMany(s, ser_action, __VA_ARGS__)) /** * Implement three methods for serializable objects. These are actually wrappers over @@ -180,63 +182,42 @@ enum * code. Adding "ADD_SERIALIZE_METHODS" in the body of the class causes these wrappers to be * added as members. */ -#define ADD_SERIALIZE_METHODS \ - size_t GetSerializeSize(int nType, int nVersion) const { \ - CSizeComputer s(nType, nVersion); \ - NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\ - return s.size(); \ - } \ - template \ - void Serialize(Stream& s, int nType, int nVersion) const { \ - NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\ - } \ - template \ - void Unserialize(Stream& s, int nType, int nVersion) { \ - SerializationOp(s, CSerActionUnserialize(), nType, nVersion); \ +#define ADD_SERIALIZE_METHODS \ + template \ + void Serialize(Stream& s) const { \ + NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize()); \ + } \ + template \ + void Unserialize(Stream& s) { \ + SerializationOp(s, CSerActionUnserialize()); \ } -/* - * Basic Types - */ -inline unsigned int GetSerializeSize(char a, int, int=0) { return 1; } -inline unsigned int GetSerializeSize(int8_t a, int, int=0) { return 1; } -inline unsigned int GetSerializeSize(uint8_t a, int, int=0) { return 1; } -inline unsigned int GetSerializeSize(int16_t a, int, int=0) { return 2; } -inline unsigned int GetSerializeSize(uint16_t a, int, int=0) { return 2; } -inline unsigned int GetSerializeSize(int32_t a, int, int=0) { return 4; } -inline unsigned int GetSerializeSize(uint32_t a, int, int=0) { return 4; } -inline unsigned int GetSerializeSize(int64_t a, int, int=0) { return 8; } -inline unsigned int GetSerializeSize(uint64_t a, int, int=0) { return 8; } -inline unsigned int GetSerializeSize(float a, int, int=0) { return 4; } -inline unsigned int GetSerializeSize(double a, int, int=0) { return 8; } - -template inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char -template inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); } -template inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); } -template inline void Serialize(Stream& s, int16_t a, int, int=0) { ser_writedata16(s, a); } -template inline void Serialize(Stream& s, uint16_t a, int, int=0) { ser_writedata16(s, a); } -template inline void Serialize(Stream& s, int32_t a, int, int=0) { ser_writedata32(s, a); } -template inline void Serialize(Stream& s, uint32_t a, int, int=0) { ser_writedata32(s, a); } -template inline void Serialize(Stream& s, int64_t a, int, int=0) { ser_writedata64(s, a); } -template inline void Serialize(Stream& s, uint64_t a, int, int=0) { ser_writedata64(s, a); } -template inline void Serialize(Stream& s, float a, int, int=0) { ser_writedata32(s, ser_float_to_uint32(a)); } -template inline void Serialize(Stream& s, double a, int, int=0) { ser_writedata64(s, ser_double_to_uint64(a)); } - -template inline void Unserialize(Stream& s, char& a, int, int=0) { a = ser_readdata8(s); } // TODO Get rid of bare char -template inline void Unserialize(Stream& s, int8_t& a, int, int=0) { a = ser_readdata8(s); } -template inline void Unserialize(Stream& s, uint8_t& a, int, int=0) { a = ser_readdata8(s); } -template inline void Unserialize(Stream& s, int16_t& a, int, int=0) { a = ser_readdata16(s); } -template inline void Unserialize(Stream& s, uint16_t& a, int, int=0) { a = ser_readdata16(s); } -template inline void Unserialize(Stream& s, int32_t& a, int, int=0) { a = ser_readdata32(s); } -template inline void Unserialize(Stream& s, uint32_t& a, int, int=0) { a = ser_readdata32(s); } -template inline void Unserialize(Stream& s, int64_t& a, int, int=0) { a = ser_readdata64(s); } -template inline void Unserialize(Stream& s, uint64_t& a, int, int=0) { a = ser_readdata64(s); } -template inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); } -template inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); } - -inline unsigned int GetSerializeSize(bool a, int, int=0) { return sizeof(char); } -template inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); } -template inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; } +template inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char +template inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); } +template inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); } +template inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); } +template inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); } +template inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); } +template inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); } +template inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); } +template inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); } +template inline void Serialize(Stream& s, float a ) { ser_writedata32(s, ser_float_to_uint32(a)); } +template inline void Serialize(Stream& s, double a ) { ser_writedata64(s, ser_double_to_uint64(a)); } + +template inline void Unserialize(Stream& s, char& a ) { a = ser_readdata8(s); } // TODO Get rid of bare char +template inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); } +template inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); } +template inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); } +template inline void Unserialize(Stream& s, uint16_t& a) { a = ser_readdata16(s); } +template inline void Unserialize(Stream& s, int32_t& a ) { a = ser_readdata32(s); } +template inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); } +template inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); } +template inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); } +template inline void Unserialize(Stream& s, float& a ) { a = ser_uint32_to_float(ser_readdata32(s)); } +template inline void Unserialize(Stream& s, double& a ) { a = ser_uint64_to_double(ser_readdata64(s)); } + +template inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); } +template inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; } @@ -258,6 +239,8 @@ inline unsigned int GetSizeOfCompactSize(uint64_t nSize) else return sizeof(unsigned char) + sizeof(uint64_t); } +inline void WriteCompactSize(CSizeComputer& os, uint64_t nSize); + template void WriteCompactSize(Stream& os, uint64_t nSize) { @@ -352,6 +335,9 @@ inline unsigned int GetSizeOfVarInt(I n) return nRet; } +template +inline void WriteVarInt(CSizeComputer& os, I n); + template void WriteVarInt(Stream& os, I n) { @@ -415,19 +401,14 @@ class CFlatData char* end() { return pend; } const char* end() const { return pend; } - unsigned int GetSerializeSize(int, int=0) const - { - return pend - pbegin; - } - template - void Serialize(Stream& s, int, int=0) const + void Serialize(Stream& s) const { s.write(pbegin, pend - pbegin); } template - void Unserialize(Stream& s, int, int=0) + void Unserialize(Stream& s) { s.read(pbegin, pend - pbegin); } @@ -441,17 +422,13 @@ class CVarInt public: CVarInt(I& nIn) : n(nIn) { } - unsigned int GetSerializeSize(int, int) const { - return GetSizeOfVarInt(n); - } - template - void Serialize(Stream &s, int, int) const { + void Serialize(Stream &s) const { WriteVarInt(s, n); } template - void Unserialize(Stream& s, int, int) { + void Unserialize(Stream& s) { n = ReadVarInt(s); } }; @@ -463,17 +440,17 @@ class CCompactSize public: CCompactSize(uint64_t& nIn) : n(nIn) { } - unsigned int GetSerializeSize(int, int) const { + unsigned int GetSerializeSize() const { return GetSizeOfCompactSize(n); } template - void Serialize(Stream &s, int, int) const { + void Serialize(Stream &s) const { WriteCompactSize(s, n); } template - void Unserialize(Stream& s, int, int) { + void Unserialize(Stream& s) { n = ReadCompactSize(s); } }; @@ -484,10 +461,10 @@ class LimitedString protected: std::string& string; public: - LimitedString(std::string& string) : string(string) {} + LimitedString(std::string& _string) : string(_string) {} template - void Unserialize(Stream& s, int, int=0) + void Unserialize(Stream& s) { size_t size = ReadCompactSize(s); if (size > Limit) { @@ -499,17 +476,12 @@ class LimitedString } template - void Serialize(Stream& s, int, int=0) const + void Serialize(Stream& s) const { WriteCompactSize(s, string.size()); if (!string.empty()) s.write((char*)&string[0], string.size()); } - - unsigned int GetSerializeSize(int, int=0) const - { - return GetSizeOfCompactSize(string.size()) + string.size(); - } }; template @@ -522,58 +494,48 @@ CVarInt WrapVarInt(I& n) { return CVarInt(n); } /** * string */ -template unsigned int GetSerializeSize(const std::basic_string& str, int, int=0); -template void Serialize(Stream& os, const std::basic_string& str, int, int=0); -template void Unserialize(Stream& is, std::basic_string& str, int, int=0); +template void Serialize(Stream& os, const std::basic_string& str); +template void Unserialize(Stream& is, std::basic_string& str); /** * prevector * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ -template unsigned int GetSerializeSize_impl(const prevector& v, int nType, int nVersion, const unsigned char&); -template unsigned int GetSerializeSize_impl(const prevector& v, int nType, int nVersion, const V&); -template inline unsigned int GetSerializeSize(const prevector& v, int nType, int nVersion); -template void Serialize_impl(Stream& os, const prevector& v, int nType, int nVersion, const unsigned char&); -template void Serialize_impl(Stream& os, const prevector& v, int nType, int nVersion, const V&); -template inline void Serialize(Stream& os, const prevector& v, int nType, int nVersion); -template void Unserialize_impl(Stream& is, prevector& v, int nType, int nVersion, const unsigned char&); -template void Unserialize_impl(Stream& is, prevector& v, int nType, int nVersion, const V&); -template inline void Unserialize(Stream& is, prevector& v, int nType, int nVersion); +template void Serialize_impl(Stream& os, const prevector& v, const unsigned char&); +template void Serialize_impl(Stream& os, const prevector& v, const V&); +template inline void Serialize(Stream& os, const prevector& v); +template void Unserialize_impl(Stream& is, prevector& v, const unsigned char&); +template void Unserialize_impl(Stream& is, prevector& v, const V&); +template inline void Unserialize(Stream& is, prevector& v); /** * vector * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. */ -template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const unsigned char&); -template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const V&); -template inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion); -template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const unsigned char&); -template void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const V&); -template inline void Serialize(Stream& os, const std::vector& v, int nType, int nVersion); -template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const unsigned char&); -template void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const V&); -template inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion); +template void Serialize_impl(Stream& os, const std::vector& v, const unsigned char&); +template void Serialize_impl(Stream& os, const std::vector& v, const V&); +template inline void Serialize(Stream& os, const std::vector& v); +template void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&); +template void Unserialize_impl(Stream& is, std::vector& v, const V&); +template inline void Unserialize(Stream& is, std::vector& v); /** * pair */ -template unsigned int GetSerializeSize(const std::pair& item, int nType, int nVersion); -template void Serialize(Stream& os, const std::pair& item, int nType, int nVersion); -template void Unserialize(Stream& is, std::pair& item, int nType, int nVersion); +template void Serialize(Stream& os, const std::pair& item); +template void Unserialize(Stream& is, std::pair& item); /** * map */ -template unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion); -template void Serialize(Stream& os, const std::map& m, int nType, int nVersion); -template void Unserialize(Stream& is, std::map& m, int nType, int nVersion); +template void Serialize(Stream& os, const std::map& m); +template void Unserialize(Stream& is, std::map& m); /** * set */ -template unsigned int GetSerializeSize(const std::set& m, int nType, int nVersion); -template void Serialize(Stream& os, const std::set& m, int nType, int nVersion); -template void Unserialize(Stream& is, std::set& m, int nType, int nVersion); +template void Serialize(Stream& os, const std::set& m); +template void Unserialize(Stream& is, std::set& m); @@ -581,26 +543,17 @@ template void Unserializ /** * If none of the specialized versions above matched, default to calling member function. - * "int nType" is changed to "long nType" to keep from getting an ambiguous overload error. - * The compiler will only cast int to long if none of the other templates matched. - * Thanks to Boost serialization for this idea. */ -template -inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion) -{ - return a.GetSerializeSize((int)nType, nVersion); -} - template -inline void Serialize(Stream& os, const T& a, long nType, int nVersion) +inline void Serialize(Stream& os, const T& a) { - a.Serialize(os, (int)nType, nVersion); + a.Serialize(os); } template -inline void Unserialize(Stream& is, T& a, long nType, int nVersion) +inline void Unserialize(Stream& is, T& a) { - a.Unserialize(is, (int)nType, nVersion); + a.Unserialize(is); } @@ -610,14 +563,8 @@ inline void Unserialize(Stream& is, T& a, long nType, int nVersion) /** * string */ -template -unsigned int GetSerializeSize(const std::basic_string& str, int, int) -{ - return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]); -} - template -void Serialize(Stream& os, const std::basic_string& str, int, int) +void Serialize(Stream& os, const std::basic_string& str) { WriteCompactSize(os, str.size()); if (!str.empty()) @@ -625,7 +572,7 @@ void Serialize(Stream& os, const std::basic_string& str, int, int) } template -void Unserialize(Stream& is, std::basic_string& str, int, int) +void Unserialize(Stream& is, std::basic_string& str) { unsigned int nSize = ReadCompactSize(is); str.resize(nSize); @@ -638,30 +585,8 @@ void Unserialize(Stream& is, std::basic_string& str, int, int) /** * prevector */ -template -unsigned int GetSerializeSize_impl(const prevector& v, int nType, int nVersion, const unsigned char&) -{ - return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T)); -} - -template -unsigned int GetSerializeSize_impl(const prevector& v, int nType, int nVersion, const V&) -{ - unsigned int nSize = GetSizeOfCompactSize(v.size()); - for (typename prevector::const_iterator vi = v.begin(); vi != v.end(); ++vi) - nSize += GetSerializeSize((*vi), nType, nVersion); - return nSize; -} - -template -inline unsigned int GetSerializeSize(const prevector& v, int nType, int nVersion) -{ - return GetSerializeSize_impl(v, nType, nVersion, T()); -} - - template -void Serialize_impl(Stream& os, const prevector& v, int nType, int nVersion, const unsigned char&) +void Serialize_impl(Stream& os, const prevector& v, const unsigned char&) { WriteCompactSize(os, v.size()); if (!v.empty()) @@ -669,22 +594,22 @@ void Serialize_impl(Stream& os, const prevector& v, int nType, int nVersio } template -void Serialize_impl(Stream& os, const prevector& v, int nType, int nVersion, const V&) +void Serialize_impl(Stream& os, const prevector& v, const V&) { WriteCompactSize(os, v.size()); for (typename prevector::const_iterator vi = v.begin(); vi != v.end(); ++vi) - ::Serialize(os, (*vi), nType, nVersion); + ::Serialize(os, (*vi)); } template -inline void Serialize(Stream& os, const prevector& v, int nType, int nVersion) +inline void Serialize(Stream& os, const prevector& v) { - Serialize_impl(os, v, nType, nVersion, T()); + Serialize_impl(os, v, T()); } template -void Unserialize_impl(Stream& is, prevector& v, int nType, int nVersion, const unsigned char&) +void Unserialize_impl(Stream& is, prevector& v, const unsigned char&) { // Limit size per read so bogus size value won't cause out of memory v.clear(); @@ -700,7 +625,7 @@ void Unserialize_impl(Stream& is, prevector& v, int nType, int nVersion, c } template -void Unserialize_impl(Stream& is, prevector& v, int nType, int nVersion, const V&) +void Unserialize_impl(Stream& is, prevector& v, const V&) { v.clear(); unsigned int nSize = ReadCompactSize(is); @@ -713,14 +638,14 @@ void Unserialize_impl(Stream& is, prevector& v, int nType, int nVersion, c nMid = nSize; v.resize(nMid); for (; i < nMid; i++) - Unserialize(is, v[i], nType, nVersion); + Unserialize(is, v[i]); } } template -inline void Unserialize(Stream& is, prevector& v, int nType, int nVersion) +inline void Unserialize(Stream& is, prevector& v) { - Unserialize_impl(is, v, nType, nVersion, T()); + Unserialize_impl(is, v, T()); } @@ -728,30 +653,8 @@ inline void Unserialize(Stream& is, prevector& v, int nType, int nVersion) /** * vector */ -template -unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const unsigned char&) -{ - return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T)); -} - -template -unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const V&) -{ - unsigned int nSize = GetSizeOfCompactSize(v.size()); - for (typename std::vector::const_iterator vi = v.begin(); vi != v.end(); ++vi) - nSize += GetSerializeSize((*vi), nType, nVersion); - return nSize; -} - -template -inline unsigned int GetSerializeSize(const std::vector& v, int nType, int nVersion) -{ - return GetSerializeSize_impl(v, nType, nVersion, T()); -} - - template -void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const unsigned char&) +void Serialize_impl(Stream& os, const std::vector& v, const unsigned char&) { WriteCompactSize(os, v.size()); if (!v.empty()) @@ -759,22 +662,22 @@ void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVers } template -void Serialize_impl(Stream& os, const std::vector& v, int nType, int nVersion, const V&) +void Serialize_impl(Stream& os, const std::vector& v, const V&) { WriteCompactSize(os, v.size()); for (typename std::vector::const_iterator vi = v.begin(); vi != v.end(); ++vi) - ::Serialize(os, (*vi), nType, nVersion); + ::Serialize(os, (*vi)); } template -inline void Serialize(Stream& os, const std::vector& v, int nType, int nVersion) +inline void Serialize(Stream& os, const std::vector& v) { - Serialize_impl(os, v, nType, nVersion, T()); + Serialize_impl(os, v, T()); } template -void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const unsigned char&) +void Unserialize_impl(Stream& is, std::vector& v, const unsigned char&) { // Limit size per read so bogus size value won't cause out of memory v.clear(); @@ -790,7 +693,7 @@ void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, } template -void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, const V&) +void Unserialize_impl(Stream& is, std::vector& v, const V&) { v.clear(); unsigned int nSize = ReadCompactSize(is); @@ -803,14 +706,14 @@ void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, nMid = nSize; v.resize(nMid); for (; i < nMid; i++) - Unserialize(is, v[i], nType, nVersion); + Unserialize(is, v[i]); } } template -inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersion) +inline void Unserialize(Stream& is, std::vector& v) { - Unserialize_impl(is, v, nType, nVersion, T()); + Unserialize_impl(is, v, T()); } @@ -818,24 +721,18 @@ inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersio /** * pair */ -template -unsigned int GetSerializeSize(const std::pair& item, int nType, int nVersion) -{ - return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion); -} - template -void Serialize(Stream& os, const std::pair& item, int nType, int nVersion) +void Serialize(Stream& os, const std::pair& item) { - Serialize(os, item.first, nType, nVersion); - Serialize(os, item.second, nType, nVersion); + Serialize(os, item.first); + Serialize(os, item.second); } template -void Unserialize(Stream& is, std::pair& item, int nType, int nVersion) +void Unserialize(Stream& is, std::pair& item) { - Unserialize(is, item.first, nType, nVersion); - Unserialize(is, item.second, nType, nVersion); + Unserialize(is, item.first); + Unserialize(is, item.second); } @@ -843,25 +740,16 @@ void Unserialize(Stream& is, std::pair& item, int nType, int nVersion) /** * map */ -template -unsigned int GetSerializeSize(const std::map& m, int nType, int nVersion) -{ - unsigned int nSize = GetSizeOfCompactSize(m.size()); - for (typename std::map::const_iterator mi = m.begin(); mi != m.end(); ++mi) - nSize += GetSerializeSize((*mi), nType, nVersion); - return nSize; -} - template -void Serialize(Stream& os, const std::map& m, int nType, int nVersion) +void Serialize(Stream& os, const std::map& m) { WriteCompactSize(os, m.size()); for (typename std::map::const_iterator mi = m.begin(); mi != m.end(); ++mi) - Serialize(os, (*mi), nType, nVersion); + Serialize(os, (*mi)); } template -void Unserialize(Stream& is, std::map& m, int nType, int nVersion) +void Unserialize(Stream& is, std::map& m) { m.clear(); unsigned int nSize = ReadCompactSize(is); @@ -869,7 +757,7 @@ void Unserialize(Stream& is, std::map& m, int nType, int nVersion for (unsigned int i = 0; i < nSize; i++) { std::pair item; - Unserialize(is, item, nType, nVersion); + Unserialize(is, item); mi = m.insert(mi, item); } } @@ -879,25 +767,16 @@ void Unserialize(Stream& is, std::map& m, int nType, int nVersion /** * set */ -template -unsigned int GetSerializeSize(const std::set& m, int nType, int nVersion) -{ - unsigned int nSize = GetSizeOfCompactSize(m.size()); - for (typename std::set::const_iterator it = m.begin(); it != m.end(); ++it) - nSize += GetSerializeSize((*it), nType, nVersion); - return nSize; -} - template -void Serialize(Stream& os, const std::set& m, int nType, int nVersion) +void Serialize(Stream& os, const std::set& m) { WriteCompactSize(os, m.size()); for (typename std::set::const_iterator it = m.begin(); it != m.end(); ++it) - Serialize(os, (*it), nType, nVersion); + Serialize(os, (*it)); } template -void Unserialize(Stream& is, std::set& m, int nType, int nVersion) +void Unserialize(Stream& is, std::set& m) { m.clear(); unsigned int nSize = ReadCompactSize(is); @@ -905,7 +784,7 @@ void Unserialize(Stream& is, std::set& m, int nType, int nVersion) for (unsigned int i = 0; i < nSize; i++) { K key; - Unserialize(is, key, nType, nVersion); + Unserialize(is, key); it = m.insert(it, key); } } @@ -913,32 +792,23 @@ void Unserialize(Stream& is, std::set& m, int nType, int nVersion) /** * list */ -template -unsigned int GetSerializeSize(const std::list& l, int nType, int nVersion) -{ - unsigned int nSize = GetSizeOfCompactSize(l.size()); - for (typename std::list::const_iterator it = l.begin(); it != l.end(); ++it) - nSize += GetSerializeSize((*it), nType, nVersion); - return nSize; -} - template -void Serialize(Stream& os, const std::list& l, int nType, int nVersion) +void Serialize(Stream& os, const std::list& l) { WriteCompactSize(os, l.size()); for (typename std::list::const_iterator it = l.begin(); it != l.end(); ++it) - Serialize(os, (*it), nType, nVersion); + Serialize(os, (*it)); } template -void Unserialize(Stream& is, std::list& l, int nType, int nVersion) +void Unserialize(Stream& is, std::list& l) { l.clear(); unsigned int nSize = ReadCompactSize(is); for (unsigned int i = 0; i < nSize; i++) { T val; - Unserialize(is, val, nType, nVersion); + Unserialize(is, val); l.push_back(val); } } @@ -950,23 +820,23 @@ void Unserialize(Stream& is, std::list& l, int nType, int nVersion) */ struct CSerActionSerialize { - bool ForRead() const { return false; } + constexpr bool ForRead() const { return false; } }; struct CSerActionUnserialize { - bool ForRead() const { return true; } + constexpr bool ForRead() const { return true; } }; template -inline void SerReadWrite(Stream& s, const T& obj, int nType, int nVersion, CSerActionSerialize ser_action) +inline void SerReadWrite(Stream& s, const T& obj, CSerActionSerialize ser_action) { - ::Serialize(s, obj, nType, nVersion); + ::Serialize(s, obj); } template -inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionUnserialize ser_action) +inline void SerReadWrite(Stream& s, T& obj, CSerActionUnserialize ser_action) { - ::Unserialize(s, obj, nType, nVersion); + ::Unserialize(s, obj); } @@ -977,81 +847,122 @@ inline void SerReadWrite(Stream& s, T& obj, int nType, int nVersion, CSerActionU +/* ::GetSerializeSize implementations + * + * Computing the serialized size of objects is done through a special stream + * object of type CSizeComputer, which only records the number of bytes written + * to it. + * + * If your Serialize or SerializationOp method has non-trivial overhead for + * serialization, it may be worthwhile to implement a specialized version for + * CSizeComputer, which uses the s.seek() method to record bytes that would + * be written instead. + */ class CSizeComputer { protected: size_t nSize; + const int nType; + const int nVersion; public: - int nType; - int nVersion; - CSizeComputer(int nTypeIn, int nVersionIn) : nSize(0), nType(nTypeIn), nVersion(nVersionIn) {} - CSizeComputer& write(const char *psz, size_t nSize) + void write(const char *psz, size_t _nSize) + { + this->nSize += _nSize; + } + + /** Pretend _nSize bytes are written, without specifying them. */ + void seek(size_t _nSize) { - this->nSize += nSize; - return *this; + this->nSize += _nSize; } template CSizeComputer& operator<<(const T& obj) { - ::Serialize(*this, obj, nType, nVersion); + ::Serialize(*this, obj); return (*this); } size_t size() const { return nSize; } + + int GetVersion() const { return nVersion; } + int GetType() const { return nType; } }; template -void SerializeMany(Stream& s, int nType, int nVersion) +void SerializeMany(Stream& s) { } template -void SerializeMany(Stream& s, int nType, int nVersion, Arg&& arg) +void SerializeMany(Stream& s, Arg&& arg) { - ::Serialize(s, std::forward(arg), nType, nVersion); + ::Serialize(s, std::forward(arg)); } template -void SerializeMany(Stream& s, int nType, int nVersion, Arg&& arg, Args&&... args) +void SerializeMany(Stream& s, Arg&& arg, Args&&... args) { - ::Serialize(s, std::forward(arg), nType, nVersion); - ::SerializeMany(s, nType, nVersion, std::forward(args)...); + ::Serialize(s, std::forward(arg)); + ::SerializeMany(s, std::forward(args)...); } template -inline void UnserializeMany(Stream& s, int nType, int nVersion) +inline void UnserializeMany(Stream& s) { } template -inline void UnserializeMany(Stream& s, int nType, int nVersion, Arg& arg) +inline void UnserializeMany(Stream& s, Arg& arg) { - ::Unserialize(s, arg, nType, nVersion); + ::Unserialize(s, arg); } template -inline void UnserializeMany(Stream& s, int nType, int nVersion, Arg& arg, Args&... args) +inline void UnserializeMany(Stream& s, Arg& arg, Args&... args) { - ::Unserialize(s, arg, nType, nVersion); - ::UnserializeMany(s, nType, nVersion, args...); + ::Unserialize(s, arg); + ::UnserializeMany(s, args...); } template -inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionSerialize ser_action, Args&&... args) +inline void SerReadWriteMany(Stream& s, CSerActionSerialize ser_action, Args&&... args) { - ::SerializeMany(s, nType, nVersion, std::forward(args)...); + ::SerializeMany(s, std::forward(args)...); } template -inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionUnserialize ser_action, Args&... args) +inline void SerReadWriteMany(Stream& s, CSerActionUnserialize ser_action, Args&... args) +{ + ::UnserializeMany(s, args...); +} + +template +inline void WriteVarInt(CSizeComputer &s, I n) +{ + s.seek(GetSizeOfVarInt(n)); +} + +inline void WriteCompactSize(CSizeComputer &s, uint64_t nSize) +{ + s.seek(GetSizeOfCompactSize(nSize)); +} + +template +size_t GetSerializeSize(const T& t, int nType, int nVersion = 0) +{ + return (CSizeComputer(nType, nVersion) << t).size(); +} + +template +size_t GetSerializeSize(const S& s, const T& t) { - ::UnserializeMany(s, nType, nVersion, args...); + return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size(); } #endif // BITCOIN_SERIALIZE_H diff --git a/src/streams.h b/src/streams.h index 44b29fb32872d..e9314223303b6 100644 --- a/src/streams.h +++ b/src/streams.h @@ -33,9 +33,10 @@ class CDataStream typedef CSerializeData vector_type; vector_type vch; unsigned int nReadPos; -public: + int nType; int nVersion; +public: typedef vector_type::allocator_type allocator_type; typedef vector_type::size_type size_type; @@ -81,7 +82,7 @@ class CDataStream CDataStream(int nTypeIn, int nVersionIn, Args&&... args) { Init(nTypeIn, nVersionIn); - ::SerializeMany(*this, nType, nVersion, std::forward(args)...); + ::SerializeMany(*this, std::forward(args)...); } void Init(int nTypeIn, int nVersionIn) @@ -218,15 +219,13 @@ class CDataStream int in_avail() { return size(); } void SetType(int n) { nType = n; } - int GetType() { return nType; } + int GetType() const { return nType; } void SetVersion(int n) { nVersion = n; } - int GetVersion() { return nVersion; } - void ReadVersion() { *this >> nVersion; } - void WriteVersion() { *this << nVersion; } + int GetVersion() const { return nVersion; } - CDataStream& read(char* pch, size_t nSize) + void read(char* pch, size_t nSize) { - if (nSize == 0) return (*this); + if (nSize == 0) return; // Read from the beginning of the buffer unsigned int nReadPosNext = nReadPos + nSize; @@ -239,14 +238,13 @@ class CDataStream memcpy(pch, &vch[nReadPos], nSize); nReadPos = 0; vch.clear(); - return (*this); + return; } memcpy(pch, &vch[nReadPos], nSize); nReadPos = nReadPosNext; - return (*this); } - CDataStream& ignore(int nSize) + void ignore(int nSize) { // Ignore from the beginning of the buffer if (nSize < 0) { @@ -259,39 +257,30 @@ class CDataStream throw std::ios_base::failure("CDataStream::ignore(): end of data"); nReadPos = 0; vch.clear(); - return (*this); + return; } nReadPos = nReadPosNext; - return (*this); } - CDataStream& write(const char* pch, size_t nSize) + void write(const char* pch, size_t nSize) { // Write to the end of the buffer vch.insert(vch.end(), pch, pch + nSize); - return (*this); } template - void Serialize(Stream& s, int nType, int nVersion) const + void Serialize(Stream& s) const { // Special case: stream << stream concatenates like stream += stream if (!vch.empty()) s.write((char*)&vch[0], vch.size() * sizeof(vch[0])); } - template - unsigned int GetSerializeSize(const T& obj) - { - // Tells the size of the object if serialized to this stream - return ::GetSerializeSize(obj, nType, nVersion); - } - template CDataStream& operator<<(const T& obj) { // Serialize to this stream - ::Serialize(*this, obj, nType, nVersion); + ::Serialize(*this, obj); return (*this); } @@ -299,7 +288,7 @@ class CDataStream CDataStream& operator>>(T& obj) { // Unserialize from this stream - ::Unserialize(*this, obj, nType, nVersion); + ::Unserialize(*this, obj); return (*this); } @@ -354,17 +343,15 @@ class CAutoFile CAutoFile(const CAutoFile&); CAutoFile& operator=(const CAutoFile&); - int nType; - int nVersion; - + const int nType; + const int nVersion; + FILE* file; public: - CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) + CAutoFile(FILE* filenew, int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) { file = filenew; - nType = nTypeIn; - nVersion = nVersionIn; } ~CAutoFile() @@ -399,23 +386,18 @@ class CAutoFile // // Stream subset // - void SetType(int n) { nType = n; } - int GetType() { return nType; } - void SetVersion(int n) { nVersion = n; } - int GetVersion() { return nVersion; } - void ReadVersion() { *this >> nVersion; } - void WriteVersion() { *this << nVersion; } + int GetType() const { return nType; } + int GetVersion() const { return nVersion; } - CAutoFile& read(char* pch, size_t nSize) + void read(char* pch, size_t nSize) { if (!file) throw std::ios_base::failure("CAutoFile::read: file handle is NULL"); if (fread(pch, 1, nSize, file) != nSize) throw std::ios_base::failure(feof(file) ? "CAutoFile::read: end of file" : "CAutoFile::read: fread failed"); - return (*this); } - CAutoFile& ignore(size_t nSize) + void ignore(size_t nSize) { if (!file) throw std::ios_base::failure("CAutoFile::ignore: file handle is NULL"); @@ -426,23 +408,14 @@ class CAutoFile throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed"); nSize -= nNow; } - return (*this); } - CAutoFile& write(const char* pch, size_t nSize) + void write(const char* pch, size_t nSize) { if (!file) throw std::ios_base::failure("CAutoFile::write: file handle is NULL"); if (fwrite(pch, 1, nSize, file) != nSize) throw std::ios_base::failure("CAutoFile::write: write failed"); - return (*this); - } - - template - unsigned int GetSerializeSize(const T& obj) - { - // Tells the size of the object if serialized to this stream - return ::GetSerializeSize(obj, nType, nVersion); } template @@ -451,7 +424,7 @@ class CAutoFile // Serialize to this stream if (!file) throw std::ios_base::failure("CAutoFile::operator<<: file handle is NULL"); - ::Serialize(*this, obj, nType, nVersion); + ::Serialize(*this, obj); return (*this); } @@ -461,7 +434,7 @@ class CAutoFile // Unserialize from this stream if (!file) throw std::ios_base::failure("CAutoFile::operator>>: file handle is NULL"); - ::Unserialize(*this, obj, nType, nVersion); + ::Unserialize(*this, obj); return (*this); } }; @@ -479,8 +452,8 @@ class CBufferedFile CBufferedFile(const CBufferedFile&); CBufferedFile& operator=(const CBufferedFile&); - int nType; - int nVersion; + const int nType; + const int nVersion; FILE *src; // source file uint64_t nSrcPos; // how many bytes have been read from source @@ -510,11 +483,9 @@ class CBufferedFile public: CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) : - nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0) + nType(nTypeIn), nVersion(nVersionIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0) { src = fileIn; - nType = nTypeIn; - nVersion = nVersionIn; } ~CBufferedFile() @@ -522,6 +493,9 @@ class CBufferedFile fclose(); } + int GetVersion() const { return nVersion; } + int GetType() const { return nType; } + void fclose() { if (src) { @@ -536,7 +510,7 @@ class CBufferedFile } // read a number of bytes - CBufferedFile& read(char *pch, size_t nSize) { + void read(char *pch, size_t nSize) { if (nSize + nReadPos > nReadLimit) throw std::ios_base::failure("Read attempted past buffer limit"); if (nSize + nRewind > vchBuf.size()) @@ -555,7 +529,6 @@ class CBufferedFile pch += nNow; nSize -= nNow; } - return (*this); } // return the current reading position @@ -601,7 +574,7 @@ class CBufferedFile template CBufferedFile& operator>>(T& obj) { // Unserialize from this stream - ::Unserialize(*this, obj, nType, nVersion); + ::Unserialize(*this, obj); return (*this); } diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp index 8d3b7dbe9f684..1dbd45d446fa2 100644 --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize) BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "BloomFilter doesn't contain just-inserted object (3)!"); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); - filter.Serialize(stream, SER_NETWORK, PROTOCOL_VERSION); + stream << filter; vector vch = ParseHex("03614e9b050000000000000001"); vector expected(vch.size()); @@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_serialize_with_tweak) BOOST_CHECK_MESSAGE(filter.contains(ParseHex("b9300670b4c5366e95b2699e8b18bc75e5f729c5")), "BloomFilter doesn't contain just-inserted object (3)!"); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); - filter.Serialize(stream, SER_NETWORK, PROTOCOL_VERSION); + stream << filter; vector vch = ParseHex("03ce4299050000000100008001"); vector expected(vch.size()); @@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_key) filter.insert(vector(hash.begin(), hash.end())); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); - filter.Serialize(stream, SER_NETWORK, PROTOCOL_VERSION); + stream << filter; vector vch = ParseHex("038fc16b080000000000000001"); vector expected(vch.size()); diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index 34f0fb2aa960f..db83ddb11022a 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -254,7 +254,7 @@ struct StringContentsSerializer { ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { if (ser_action.ForRead()) { str.clear(); char c = 0; diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 5f40ddbe18d01..92ddd798f4256 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -17,7 +17,7 @@ using namespace std; class CAddrManSerializationMock : public CAddrMan { public: - virtual void Serialize(CDataStream& s, int nType, int nVersionDummy) const = 0; + virtual void Serialize(CDataStream& s) const = 0; //! Ensure that bucket placement is always the same for testing purposes. void MakeDeterministic() @@ -30,16 +30,16 @@ class CAddrManSerializationMock : public CAddrMan class CAddrManUncorrupted : public CAddrManSerializationMock { public: - void Serialize(CDataStream& s, int nType, int nVersionDummy) const + void Serialize(CDataStream& s) const { - CAddrMan::Serialize(s, nType, nVersionDummy); + CAddrMan::Serialize(s); } }; class CAddrManCorrupted : public CAddrManSerializationMock { public: - void Serialize(CDataStream& s, int nType, int nVersionDummy) const + void Serialize(CDataStream& s) const { // Produces corrupt output that claims addrman has 20 addrs when it only has one addr. unsigned char nVersion = 1; diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index 3ae7bb28deeec..a34447404a0d2 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -28,7 +28,7 @@ class CSerializeMethodsTestSingle ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(intval); READWRITE(boolval); READWRITE(stringval); @@ -53,7 +53,7 @@ class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITEMANY(intval, boolval, stringval, FLATDATA(charstrval), txval); } }; diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index bb7e684f702f7..abf9cc4e65152 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -184,25 +184,25 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(OneL.begin() + 32 == OneL.end()); BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); - BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32); - BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32); + BOOST_CHECK(GetSerializeSize(R1L, 0, PROTOCOL_VERSION) == 32); + BOOST_CHECK(GetSerializeSize(ZeroL, 0, PROTOCOL_VERSION) == 32); - std::stringstream ss; - R1L.Serialize(ss,0,PROTOCOL_VERSION); + CDataStream ss(0, PROTOCOL_VERSION); + ss << R1L; BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+32)); - TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + ss >> TmpL; BOOST_CHECK(R1L == TmpL); - ss.str(""); - ZeroL.Serialize(ss,0,PROTOCOL_VERSION); + ss.clear(); + ss << ZeroL; BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+32)); - TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + ss >> TmpL; BOOST_CHECK(ZeroL == TmpL); - ss.str(""); - MaxL.Serialize(ss,0,PROTOCOL_VERSION); + ss.clear(); + ss << MaxL; BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+32)); - TmpL.Unserialize(ss,0,PROTOCOL_VERSION); + ss >> TmpL; BOOST_CHECK(MaxL == TmpL); - ss.str(""); + ss.clear(); BOOST_CHECK(R1S.GetHex() == R1S.ToString()); BOOST_CHECK(R2S.GetHex() == R2S.ToString()); @@ -230,24 +230,24 @@ BOOST_AUTO_TEST_CASE( methods ) // GetHex SetHex begin() end() size() GetLow64 G BOOST_CHECK(OneS.begin() + 20 == OneS.end()); BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); - BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20); - BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20); + BOOST_CHECK(GetSerializeSize(R1S, 0, PROTOCOL_VERSION) == 20); + BOOST_CHECK(GetSerializeSize(ZeroS, 0, PROTOCOL_VERSION) == 20); - R1S.Serialize(ss,0,PROTOCOL_VERSION); + ss << R1S; BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); - TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + ss >> TmpS; BOOST_CHECK(R1S == TmpS); - ss.str(""); - ZeroS.Serialize(ss,0,PROTOCOL_VERSION); + ss.clear(); + ss << ZeroS; BOOST_CHECK(ss.str() == std::string(ZeroArray,ZeroArray+20)); - TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + ss >> TmpS; BOOST_CHECK(ZeroS == TmpS); - ss.str(""); - MaxS.Serialize(ss,0,PROTOCOL_VERSION); + ss.clear(); + ss << MaxS; BOOST_CHECK(ss.str() == std::string(MaxArray,MaxArray+20)); - TmpS.Unserialize(ss,0,PROTOCOL_VERSION); + ss >> TmpS; BOOST_CHECK(MaxS == TmpS); - ss.str(""); + ss.clear(); } BOOST_AUTO_TEST_CASE( conversion ) diff --git a/src/txdb.cpp b/src/txdb.cpp index 8753a4833fc8e..bc45661d82adf 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -41,14 +41,14 @@ struct CoinEntry { CoinEntry(const COutPoint* ptr) : outpoint(const_cast(ptr)), key(DB_COIN) {} template - void Serialize(Stream &s, int nType, int nVersion) const { + void Serialize(Stream &s) const { s << key; s << outpoint->hash; s << VARINT(outpoint->n); } template - void Unserialize(Stream& s, int nType, int nVersion) { + void Unserialize(Stream& s) { s >> key; s >> outpoint->hash; s >> VARINT(outpoint->n); @@ -408,13 +408,13 @@ class CCoins CCoins() : fCoinBase(false), vout(0), nHeight(0) { } template - void Unserialize(Stream &s, int nType, int nVersion) { + void Unserialize(Stream &s) { unsigned int nCode = 0; // version int nVersionDummy; - ::Unserialize(s, VARINT(nVersionDummy), nType, nVersion); + ::Unserialize(s, VARINT(nVersionDummy)); // header code - ::Unserialize(s, VARINT(nCode), nType, nVersion); + ::Unserialize(s, VARINT(nCode)); fCoinBase = nCode & 1; std::vector vAvail(2, false); vAvail[0] = (nCode & 2) != 0; @@ -423,7 +423,7 @@ class CCoins // spentness bitmask while (nMaskCode > 0) { unsigned char chAvail = 0; - ::Unserialize(s, chAvail, nType, nVersion); + ::Unserialize(s, chAvail); for (unsigned int p = 0; p < 8; p++) { bool f = (chAvail & (1 << p)) != 0; vAvail.push_back(f); @@ -435,10 +435,10 @@ class CCoins vout.assign(vAvail.size(), CTxOut()); for (unsigned int i = 0; i < vAvail.size(); i++) { if (vAvail[i]) - ::Unserialize(s, REF(CTxOutCompressor(vout[i])), nType, nVersion); + ::Unserialize(s, REF(CTxOutCompressor(vout[i]))); } // coinbase height - ::Unserialize(s, VARINT(nHeight), nType, nVersion); + ::Unserialize(s, VARINT(nHeight)); } }; diff --git a/src/txdb.h b/src/txdb.h index fccaa0740f22c..c709fd0ca8bf4 100644 --- a/src/txdb.h +++ b/src/txdb.h @@ -48,7 +48,7 @@ struct CDiskTxPos : public CDiskBlockPos ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(*(CDiskBlockPos*)this); READWRITE(VARINT(nTxOffset)); } diff --git a/src/uint256.h b/src/uint256.h index 9e141d3e5085c..851b9ce0c24da 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -80,11 +80,6 @@ class base_blob return sizeof(data); } - unsigned int GetSerializeSize(int nType, int nVersion) const - { - return sizeof(data); - } - uint64_t GetUint64(int pos) const { const uint8_t* ptr = data + pos * 8; @@ -99,13 +94,13 @@ class base_blob } template - void Serialize(Stream& s, int nType, int nVersion) const + void Serialize(Stream& s) const { s.write((char*)data, sizeof(data)); } template - void Unserialize(Stream& s, int nType, int nVersion) + void Unserialize(Stream& s) { s.read((char*)data, sizeof(data)); } diff --git a/src/undo.h b/src/undo.h index ca4c5be54fcaf..a83b701485d5a 100644 --- a/src/undo.h +++ b/src/undo.h @@ -24,13 +24,13 @@ class TxInUndoSerializer public: template - void Serialize(Stream &s, int nType, int nVersion) const { - ::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1 : 0)), nType, nVersion); + void Serialize(Stream &s) const { + ::Serialize(s, VARINT(txout->nHeight * 2 + (txout->fCoinBase ? 1 : 0))); if (txout->nHeight > 0) { // Required to maintain compatibility with older undo format. - ::Serialize(s, (unsigned char)0, nType, nVersion); + ::Serialize(s, (unsigned char)0); } - ::Serialize(s, CTxOutCompressor(REF(txout->out)), nType, nVersion); + ::Serialize(s, CTxOutCompressor(REF(txout->out))); } TxInUndoSerializer(const Coin* coin) : txout(coin) {} @@ -42,9 +42,9 @@ class TxInUndoDeserializer public: template - void Unserialize(Stream &s, int nType, int nVersion) { + void Unserialize(Stream &s) { unsigned int nCode = 0; - ::Unserialize(s, VARINT(nCode), nType, nVersion); + ::Unserialize(s, VARINT(nCode)); txout->nHeight = nCode / 2; txout->fCoinBase = nCode & 1; if (txout->nHeight > 0) { @@ -52,9 +52,9 @@ class TxInUndoDeserializer // a transaction's outputs. Non-final spends were indicated with // height = 0. int nVersionDummy; - ::Unserialize(s, VARINT(nVersionDummy), nType, nVersion); + ::Unserialize(s, VARINT(nVersionDummy)); } - ::Unserialize(s, REF(CTxOutCompressor(REF(txout->out))), nType, nVersion); + ::Unserialize(s, REF(CTxOutCompressor(REF(txout->out)))); } TxInUndoDeserializer(Coin* coin) : txout(coin) {} @@ -70,26 +70,26 @@ class CTxUndo std::vector vprevout; template - void Serialize(Stream& s, int nType, int nVersion) const { + void Serialize(Stream& s) const { // TODO: avoid reimplementing vector serializer uint64_t count = vprevout.size(); - ::Serialize(s, COMPACTSIZE(REF(count)), nType, nVersion); + ::Serialize(s, COMPACTSIZE(REF(count))); for (const auto& prevout : vprevout) { - ::Serialize(s, REF(TxInUndoSerializer(&prevout)), nType, nVersion); + ::Serialize(s, REF(TxInUndoSerializer(&prevout))); } } template - void Unserialize(Stream& s, int nType, int nVersion) { + void Unserialize(Stream& s) { // TODO: avoid reimplementing vector deserializer uint64_t count = 0; - ::Unserialize(s, COMPACTSIZE(count), nType, nVersion); + ::Unserialize(s, COMPACTSIZE(count)); if (count > MAX_INPUTS_PER_BLOCK) { throw std::ios_base::failure("Too many input undo records"); } vprevout.resize(count); for (auto& prevout : vprevout) { - ::Unserialize(s, REF(TxInUndoDeserializer(&prevout)), nType, nVersion); + ::Unserialize(s, REF(TxInUndoDeserializer(&prevout))); } } }; @@ -103,7 +103,7 @@ class CBlockUndo ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(vtxundo); } }; diff --git a/src/validation.cpp b/src/validation.cpp index ae91ee390190f..61028f2910cae 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1203,7 +1203,7 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHea return error("WriteBlockToDisk: OpenBlockFile failed"); // Write index header - unsigned int nSize = fileout.GetSerializeSize(block); + unsigned int nSize = GetSerializeSize(fileout, block); fileout << FLATDATA(messageStart) << nSize; // Write block @@ -1617,7 +1617,7 @@ bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint return error("%s: OpenUndoFile failed", __func__); // Write index header - unsigned int nSize = fileout.GetSerializeSize(blockundo); + unsigned int nSize = GetSerializeSize(fileout, blockundo); fileout << FLATDATA(messageStart) << nSize; // Write undo data diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h index 3d8ac0a64f3a6..ef85572d425e3 100644 --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -47,7 +47,7 @@ class CMasterKey ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(vchCryptedKey); READWRITE(vchSalt); READWRITE(nDerivationMethod); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1c37b08a1835f..2032fd7631154 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -127,8 +127,9 @@ class CKeyPool ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!(nType & SER_GETHASH)) + inline void SerializationOp(Stream& s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); READWRITE(nTime); READWRITE(vchPubKey); @@ -235,7 +236,7 @@ class CMerkleTx : public CTransaction ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { std::vector vMerkleBranch; // For compatibility with older versions. READWRITE(*(CTransaction*)this); READWRITE(hashBlock); @@ -367,7 +368,7 @@ class CWalletTx : public CMerkleTx ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { if (ser_action.ForRead()) Init(NULL); char fSpent = false; @@ -510,8 +511,9 @@ class CWalletKey ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!(nType & SER_GETHASH)) + inline void SerializationOp(Stream& s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); READWRITE(vchPrivKey); READWRITE(nTimeCreated); @@ -555,8 +557,9 @@ class CAccountingEntry ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!(nType & SER_GETHASH)) + inline void SerializationOp(Stream& s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); //! Note: strAccount is serialized as part of the key, not here. READWRITE(nCreditDebit); @@ -569,7 +572,7 @@ class CAccountingEntry if (!(mapValue.empty() && _ssExtra.empty())) { - CDataStream ss(nType, nVersion); + CDataStream ss(s.GetType(), s.GetVersion()); ss.insert(ss.begin(), '\0'); ss << mapValue; ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end()); @@ -585,7 +588,7 @@ class CAccountingEntry mapValue.clear(); if (std::string::npos != nSepPos) { - CDataStream ss(std::vector(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion); + CDataStream ss(std::vector(strComment.begin() + nSepPos + 1, strComment.end()), s.GetType(), s.GetVersion()); ss >> mapValue; _ssExtra = std::vector(ss.begin(), ss.end()); } @@ -1115,8 +1118,9 @@ class CAccount ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { - if (!(nType & SER_GETHASH)) + inline void SerializationOp(Stream& s, Operation ser_action) { + int nVersion = s.GetVersion(); + if (!(s.GetType() & SER_GETHASH)) READWRITE(nVersion); READWRITE(vchPubKey); } diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 66bc69cfa95b5..a8aa9b3d03306 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -61,7 +61,7 @@ class CKeyMetadata ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { + inline void SerializationOp(Stream& s, Operation ser_action) { READWRITE(this->nVersion); READWRITE(nCreateTime);