diff --git a/src/addrman.h b/src/addrman.h index e9e137c97..03b380165 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -448,11 +448,6 @@ public: 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/blockencodings.cpp b/src/blockencodings.cpp index 93d3fa372..bde8463b3 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -131,7 +131,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c break; } - LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), cmpctblock.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION)); + LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION)); return READ_STATUS_OK; } diff --git a/src/coins.h b/src/coins.h index 033651a43..3e134cf67 100644 --- a/src/coins.h +++ b/src/coins.h @@ -153,29 +153,6 @@ public: return fCoinBase; } - unsigned int GetSerializeSize(int nType, int nVersion) const { - unsigned int nSize = 0; - unsigned int nMaskSize = 0, nMaskCode = 0; - CalcMaskSize(nMaskSize, nMaskCode); - bool fFirst = vout.size() > 0 && !vout[0].IsNull(); - bool fSecond = vout.size() > 1 && !vout[1].IsNull(); - assert(fFirst || fSecond || nMaskCode); - unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0); - // version - nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion); - // size of header code - nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion); - // spentness bitmask - nSize += nMaskSize; - // txouts themself - for (unsigned int i = 0; i < vout.size(); i++) - if (!vout[i].IsNull()) - nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion); - // height - nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion); - return nSize; - } - template void Serialize(Stream &s, int nType, int nVersion) const { unsigned int nMaskSize = 0, nMaskCode = 0; diff --git a/src/compressor.h b/src/compressor.h index fa702f0df..90749499d 100644 --- a/src/compressor.h +++ b/src/compressor.h @@ -55,14 +55,6 @@ protected: 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 { std::vector compr; diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 47bdb31b5..7728b49d9 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -60,12 +60,12 @@ public: void Write(const K& key, const V& value) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); - ssKey.reserve(ssKey.GetSerializeSize(key)); + ssKey.reserve(GetSerializeSize(ssKey, key)); ssKey << key; leveldb::Slice slKey(&ssKey[0], ssKey.size()); CDataStream ssValue(SER_DISK, CLIENT_VERSION); - ssValue.reserve(ssValue.GetSerializeSize(value)); + ssValue.reserve(GetSerializeSize(ssValue, value)); ssValue << value; ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); leveldb::Slice slValue(&ssValue[0], ssValue.size()); @@ -77,7 +77,7 @@ public: void Erase(const K& key) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); - ssKey.reserve(ssKey.GetSerializeSize(key)); + ssKey.reserve(GetSerializeSize(ssKey, key)); ssKey << key; leveldb::Slice slKey(&ssKey[0], ssKey.size()); @@ -107,7 +107,7 @@ public: template void Seek(const K& key) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); - ssKey.reserve(ssKey.GetSerializeSize(key)); + ssKey.reserve(GetSerializeSize(ssKey, key)); ssKey << key; leveldb::Slice slKey(&ssKey[0], ssKey.size()); piter->Seek(slKey); @@ -200,7 +200,7 @@ public: bool Read(const K& key, V& value) const { CDataStream ssKey(SER_DISK, CLIENT_VERSION); - ssKey.reserve(ssKey.GetSerializeSize(key)); + ssKey.reserve(GetSerializeSize(ssKey, key)); ssKey << key; leveldb::Slice slKey(&ssKey[0], ssKey.size()); @@ -234,7 +234,7 @@ public: bool Exists(const K& key) const { CDataStream ssKey(SER_DISK, CLIENT_VERSION); - ssKey.reserve(ssKey.GetSerializeSize(key)); + ssKey.reserve(GetSerializeSize(ssKey, key)); ssKey << key; leveldb::Slice slKey(&ssKey[0], ssKey.size()); diff --git a/src/main.cpp b/src/main.cpp index e0c614b73..13872c4bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1710,7 +1710,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 @@ -2100,7 +2100,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/primitives/transaction.h b/src/primitives/transaction.h index 1afeb8703..714ab3454 100644 --- a/src/primitives/transaction.h +++ b/src/primitives/transaction.h @@ -177,7 +177,7 @@ public: if (scriptPubKey.IsUnspendable()) return 0; - size_t nSize = GetSerializeSize(SER_DISK, 0); + size_t nSize = GetSerializeSize(*this, SER_DISK, 0); int witnessversion = 0; std::vector witnessprogram; diff --git a/src/pubkey.h b/src/pubkey.h index 3a554877f..3277218bb 100644 --- a/src/pubkey.h +++ b/src/pubkey.h @@ -116,10 +116,6 @@ public: } //! 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 { @@ -214,10 +210,6 @@ 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 - { - return BIP32_EXTKEY_SIZE+1; //add one byte for the size (compact int) - } template void Serialize(Stream& s, int nType, int nVersion) const { diff --git a/src/script/bitcoinconsensus.cpp b/src/script/bitcoinconsensus.cpp index 4e2358d9a..ec573c318 100644 --- a/src/script/bitcoinconsensus.cpp +++ b/src/script/bitcoinconsensus.cpp @@ -87,7 +87,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP stream >> tx; if (nIn >= tx.vin.size()) return set_error(err, bitcoinconsensus_ERR_TX_INDEX); - if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) != txToLen) + if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH); // Regardless of the verification result, the tx did not error. diff --git a/src/serialize.h b/src/serialize.h index 68a8e2847..80289a365 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -169,11 +169,6 @@ enum * 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);\ @@ -186,18 +181,6 @@ enum /* * 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); } @@ -222,7 +205,6 @@ template inline void Unserialize(Stream& s, uint64_t& a, int, 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; } @@ -403,11 +385,6 @@ public: 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 { @@ -429,10 +406,6 @@ protected: public: CVarInt(I& nIn) : n(nIn) { } - unsigned int GetSerializeSize(int, int) const { - return GetSizeOfVarInt(n); - } - template void Serialize(Stream &s, int, int) const { WriteVarInt(s, n); @@ -451,10 +424,6 @@ protected: public: CCompactSize(uint64_t& nIn) : n(nIn) { } - unsigned int GetSerializeSize(int, int) const { - return GetSizeOfCompactSize(n); - } - template void Serialize(Stream &s, int, int) const { WriteCompactSize(s, n); @@ -493,11 +462,6 @@ public: if (!string.empty()) s.write((char*)&string[0], string.size()); } - - unsigned int GetSerializeSize(int, int=0) const - { - return GetSizeOfCompactSize(string.size()) + string.size(); - } }; template @@ -510,7 +474,6 @@ 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); @@ -518,9 +481,6 @@ template void Unserialize(Stream& is, std::basic_st * 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); @@ -532,9 +492,6 @@ template inline void Unserialize(St * 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); @@ -545,21 +502,18 @@ template inline void Unserialize(Stream /** * 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); /** * 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); /** * 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); @@ -573,12 +527,6 @@ template void Unserializ * 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) { @@ -598,12 +546,6 @@ 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) { @@ -626,28 +568,6 @@ 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&) { @@ -716,28 +636,6 @@ 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&) { @@ -806,12 +704,6 @@ 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) { @@ -831,15 +723,6 @@ 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) { @@ -867,15 +750,6 @@ 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) { @@ -1008,4 +882,16 @@ inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionUnser ::UnserializeMany(s, nType, nVersion, args...); } +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) +{ + return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size(); +} + #endif // BITCOIN_SERIALIZE_H diff --git a/src/streams.h b/src/streams.h index 12a36a4a7..d9965b534 100644 --- a/src/streams.h +++ b/src/streams.h @@ -308,13 +308,6 @@ public: 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) { @@ -457,13 +450,6 @@ public: throw std::ios_base::failure("CAutoFile::write: write failed"); } - template - unsigned int GetSerializeSize(const T& obj) - { - // Tells the size of the object if serialized to this stream - return ::GetSerializeSize(obj, nType, nVersion); - } - template CAutoFile& operator<<(const T& obj) { diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp index da0a3d73e..ba8eb16ba 100644 --- a/src/test/uint256_tests.cpp +++ b/src/test/uint256_tests.cpp @@ -184,8 +184,8 @@ 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); @@ -230,8 +230,8 @@ 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); BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); diff --git a/src/uint256.h b/src/uint256.h index dd8432d74..94fe66d26 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -78,11 +78,6 @@ public: 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; diff --git a/src/undo.h b/src/undo.h index d4fc84c90..9d6f9bd0d 100644 --- a/src/undo.h +++ b/src/undo.h @@ -27,12 +27,6 @@ public: CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {} CTxInUndo(const CTxOut &txoutIn, bool fCoinBaseIn = false, unsigned int nHeightIn = 0, int nVersionIn = 0) : txout(txoutIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn), nVersion(nVersionIn) { } - unsigned int GetSerializeSize(int nType, int nVersion) const { - return ::GetSerializeSize(VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion) + - (nHeight > 0 ? ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion) : 0) + - ::GetSerializeSize(CTxOutCompressor(REF(txout)), nType, nVersion); - } - template void Serialize(Stream &s, int nType, int nVersion) const { ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);