Browse Source

Make GetSerializeSize a wrapper on top of CSizeComputer

Given that in default GetSerializeSize implementations created by
ADD_SERIALIZE_METHODS we're already using CSizeComputer(), get rid
of the specialized GetSerializeSize methods everywhere, and just use
CSizeComputer. This removes a lot of code which isn't actually used
anywhere.

For CCompactSize and CVarInt this actually removes a more efficient
size computing algorithm, which is brought back in a later commit.
0.14
Pieter Wuille 8 years ago
parent
commit
657e05ab2e
  1. 5
      src/addrman.h
  2. 2
      src/blockencodings.cpp
  3. 23
      src/coins.h
  4. 8
      src/compressor.h
  5. 12
      src/dbwrapper.h
  6. 4
      src/main.cpp
  7. 2
      src/primitives/transaction.h
  8. 8
      src/pubkey.h
  9. 2
      src/script/bitcoinconsensus.cpp
  10. 138
      src/serialize.h
  11. 14
      src/streams.h
  12. 8
      src/test/uint256_tests.cpp
  13. 5
      src/uint256.h
  14. 6
      src/undo.h

5
src/addrman.h

@ -448,11 +448,6 @@ public:
Check(); Check();
} }
unsigned int GetSerializeSize(int nType, int nVersion) const
{
return (CSizeComputer(nType, nVersion) << *this).size();
}
void Clear() void Clear()
{ {
std::vector<int>().swap(vRandom); std::vector<int>().swap(vRandom);

2
src/blockencodings.cpp

@ -131,7 +131,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
break; 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; return READ_STATUS_OK;
} }

23
src/coins.h

@ -153,29 +153,6 @@ public:
return fCoinBase; 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<typename Stream> template<typename Stream>
void Serialize(Stream &s, int nType, int nVersion) const { void Serialize(Stream &s, int nType, int nVersion) const {
unsigned int nMaskSize = 0, nMaskCode = 0; unsigned int nMaskSize = 0, nMaskCode = 0;

8
src/compressor.h

@ -55,14 +55,6 @@ protected:
public: public:
CScriptCompressor(CScript &scriptIn) : script(scriptIn) { } CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
unsigned int GetSerializeSize(int nType, int nVersion) const {
std::vector<unsigned char> compr;
if (Compress(compr))
return compr.size();
unsigned int nSize = script.size() + nSpecialScripts;
return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
}
template<typename Stream> template<typename Stream>
void Serialize(Stream &s, int nType, int nVersion) const { void Serialize(Stream &s, int nType, int nVersion) const {
std::vector<unsigned char> compr; std::vector<unsigned char> compr;

12
src/dbwrapper.h

@ -60,12 +60,12 @@ public:
void Write(const K& key, const V& value) void Write(const K& key, const V& value)
{ {
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key)); ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());
CDataStream ssValue(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION);
ssValue.reserve(ssValue.GetSerializeSize(value)); ssValue.reserve(GetSerializeSize(ssValue, value));
ssValue << value; ssValue << value;
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue(&ssValue[0], ssValue.size()); leveldb::Slice slValue(&ssValue[0], ssValue.size());
@ -77,7 +77,7 @@ public:
void Erase(const K& key) void Erase(const K& key)
{ {
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key)); ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());
@ -107,7 +107,7 @@ public:
template<typename K> void Seek(const K& key) { template<typename K> void Seek(const K& key) {
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key)); ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());
piter->Seek(slKey); piter->Seek(slKey);
@ -200,7 +200,7 @@ public:
bool Read(const K& key, V& value) const bool Read(const K& key, V& value) const
{ {
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key)); ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());
@ -234,7 +234,7 @@ public:
bool Exists(const K& key) const bool Exists(const K& key) const
{ {
CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssKey(SER_DISK, CLIENT_VERSION);
ssKey.reserve(ssKey.GetSerializeSize(key)); ssKey.reserve(GetSerializeSize(ssKey, key));
ssKey << key; ssKey << key;
leveldb::Slice slKey(&ssKey[0], ssKey.size()); leveldb::Slice slKey(&ssKey[0], ssKey.size());

4
src/main.cpp

@ -1710,7 +1710,7 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHea
return error("WriteBlockToDisk: OpenBlockFile failed"); return error("WriteBlockToDisk: OpenBlockFile failed");
// Write index header // Write index header
unsigned int nSize = fileout.GetSerializeSize(block); unsigned int nSize = GetSerializeSize(fileout, block);
fileout << FLATDATA(messageStart) << nSize; fileout << FLATDATA(messageStart) << nSize;
// Write block // Write block
@ -2100,7 +2100,7 @@ bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint
return error("%s: OpenUndoFile failed", __func__); return error("%s: OpenUndoFile failed", __func__);
// Write index header // Write index header
unsigned int nSize = fileout.GetSerializeSize(blockundo); unsigned int nSize = GetSerializeSize(fileout, blockundo);
fileout << FLATDATA(messageStart) << nSize; fileout << FLATDATA(messageStart) << nSize;
// Write undo data // Write undo data

2
src/primitives/transaction.h

@ -177,7 +177,7 @@ public:
if (scriptPubKey.IsUnspendable()) if (scriptPubKey.IsUnspendable())
return 0; return 0;
size_t nSize = GetSerializeSize(SER_DISK, 0); size_t nSize = GetSerializeSize(*this, SER_DISK, 0);
int witnessversion = 0; int witnessversion = 0;
std::vector<unsigned char> witnessprogram; std::vector<unsigned char> witnessprogram;

8
src/pubkey.h

@ -116,10 +116,6 @@ public:
} }
//! Implement serialization, as if this was a byte vector. //! Implement serialization, as if this was a byte vector.
unsigned int GetSerializeSize(int nType, int nVersion) const
{
return size() + 1;
}
template <typename Stream> template <typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const void Serialize(Stream& s, int nType, int nVersion) const
{ {
@ -214,10 +210,6 @@ struct CExtPubKey {
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]); void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
bool Derive(CExtPubKey& out, unsigned int nChild) const; 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 <typename Stream> template <typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const void Serialize(Stream& s, int nType, int nVersion) const
{ {

2
src/script/bitcoinconsensus.cpp

@ -87,7 +87,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
stream >> tx; stream >> tx;
if (nIn >= tx.vin.size()) if (nIn >= tx.vin.size())
return set_error(err, bitcoinconsensus_ERR_TX_INDEX); 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); return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
// Regardless of the verification result, the tx did not error. // Regardless of the verification result, the tx did not error.

138
src/serialize.h

@ -169,11 +169,6 @@ enum
* added as members. * added as members.
*/ */
#define ADD_SERIALIZE_METHODS \ #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<typename Stream> \ template<typename Stream> \
void Serialize(Stream& s, int nType, int nVersion) const { \ void Serialize(Stream& s, int nType, int nVersion) const { \
NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\ NCONST_PTR(this)->SerializationOp(s, CSerActionSerialize(), nType, nVersion);\
@ -186,18 +181,6 @@ enum
/* /*
* Basic Types * 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<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char template<typename Stream> inline void Serialize(Stream& s, char a, int, int=0) { ser_writedata8(s, a); } // TODO Get rid of bare char
template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); } template<typename Stream> inline void Serialize(Stream& s, int8_t a, int, int=0) { ser_writedata8(s, a); }
template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); } template<typename Stream> inline void Serialize(Stream& s, uint8_t a, int, int=0) { ser_writedata8(s, a); }
@ -222,7 +205,6 @@ template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a, int,
template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); } template<typename Stream> inline void Unserialize(Stream& s, float& a, int, int=0) { a = ser_uint32_to_float(ser_readdata32(s)); }
template<typename Stream> inline void Unserialize(Stream& s, double& a, int, int=0) { a = ser_uint64_to_double(ser_readdata64(s)); } template<typename Stream> 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<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); } template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0) { char f=a; ser_writedata8(s, f); }
template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f=ser_readdata8(s); a=f; } template<typename Stream> 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; } char* end() { return pend; }
const char* end() const { return pend; } const char* end() const { return pend; }
unsigned int GetSerializeSize(int, int=0) const
{
return pend - pbegin;
}
template<typename Stream> template<typename Stream>
void Serialize(Stream& s, int, int=0) const void Serialize(Stream& s, int, int=0) const
{ {
@ -429,10 +406,6 @@ protected:
public: public:
CVarInt(I& nIn) : n(nIn) { } CVarInt(I& nIn) : n(nIn) { }
unsigned int GetSerializeSize(int, int) const {
return GetSizeOfVarInt<I>(n);
}
template<typename Stream> template<typename Stream>
void Serialize(Stream &s, int, int) const { void Serialize(Stream &s, int, int) const {
WriteVarInt<Stream,I>(s, n); WriteVarInt<Stream,I>(s, n);
@ -451,10 +424,6 @@ protected:
public: public:
CCompactSize(uint64_t& nIn) : n(nIn) { } CCompactSize(uint64_t& nIn) : n(nIn) { }
unsigned int GetSerializeSize(int, int) const {
return GetSizeOfCompactSize(n);
}
template<typename Stream> template<typename Stream>
void Serialize(Stream &s, int, int) const { void Serialize(Stream &s, int, int) const {
WriteCompactSize<Stream>(s, n); WriteCompactSize<Stream>(s, n);
@ -493,11 +462,6 @@ public:
if (!string.empty()) if (!string.empty())
s.write((char*)&string[0], string.size()); s.write((char*)&string[0], string.size());
} }
unsigned int GetSerializeSize(int, int=0) const
{
return GetSizeOfCompactSize(string.size()) + string.size();
}
}; };
template<typename I> template<typename I>
@ -510,7 +474,6 @@ CVarInt<I> WrapVarInt(I& n) { return CVarInt<I>(n); }
/** /**
* string * string
*/ */
template<typename C> unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int=0);
template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0); template<typename Stream, typename C> void Serialize(Stream& os, const std::basic_string<C>& str, int, int=0);
template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0); template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_string<C>& str, int, int=0);
@ -518,9 +481,6 @@ template<typename Stream, typename C> void Unserialize(Stream& is, std::basic_st
* prevector * prevector
* prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. * prevectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
*/ */
template<unsigned int N, typename T> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
template<unsigned int N, typename T, typename V> unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&);
template<unsigned int N, typename T> inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion);
template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&); template<typename Stream, unsigned int N, typename T> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&);
template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&); template<typename Stream, unsigned int N, typename T, typename V> void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const V&);
template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion); template<typename Stream, unsigned int N, typename T> inline void Serialize(Stream& os, const prevector<N, T>& v, int nType, int nVersion);
@ -532,9 +492,6 @@ template<typename Stream, unsigned int N, typename T> inline void Unserialize(St
* vector * vector
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob. * vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
*/ */
template<typename T, typename A> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
template<typename T, typename A, typename V> unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&);
template<typename T, typename A> inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion);
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&); template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&);
template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&); template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const V&);
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion); template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v, int nType, int nVersion);
@ -545,21 +502,18 @@ template<typename Stream, typename T, typename A> inline void Unserialize(Stream
/** /**
* pair * pair
*/ */
template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion);
template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion); template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion);
template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion); template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion);
/** /**
* map * map
*/ */
template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion);
template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion); template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion);
template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion); template<typename Stream, typename K, typename T, typename Pred, typename A> void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion);
/** /**
* set * set
*/ */
template<typename K, typename Pred, typename A> unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion);
template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion); template<typename Stream, typename K, typename Pred, typename A> void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion);
template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion); template<typename Stream, typename K, typename Pred, typename A> void Unserialize(Stream& is, std::set<K, Pred, A>& m, int nType, int nVersion);
@ -573,12 +527,6 @@ template<typename Stream, typename K, typename Pred, typename A> void Unserializ
* The compiler will only cast int to long if none of the other templates matched. * The compiler will only cast int to long if none of the other templates matched.
* Thanks to Boost serialization for this idea. * Thanks to Boost serialization for this idea.
*/ */
template<typename T>
inline unsigned int GetSerializeSize(const T& a, long nType, int nVersion)
{
return a.GetSerializeSize((int)nType, nVersion);
}
template<typename Stream, typename T> template<typename Stream, typename T>
inline void Serialize(Stream& os, const T& a, long nType, int nVersion) 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 * string
*/ */
template<typename C>
unsigned int GetSerializeSize(const std::basic_string<C>& str, int, int)
{
return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]);
}
template<typename Stream, typename C> template<typename Stream, typename C>
void Serialize(Stream& os, const std::basic_string<C>& str, int, int) void Serialize(Stream& os, const std::basic_string<C>& str, int, int)
{ {
@ -626,28 +568,6 @@ void Unserialize(Stream& is, std::basic_string<C>& str, int, int)
/** /**
* prevector * prevector
*/ */
template<unsigned int N, typename T>
unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
{
return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
}
template<unsigned int N, typename T, typename V>
unsigned int GetSerializeSize_impl(const prevector<N, T>& v, int nType, int nVersion, const V&)
{
unsigned int nSize = GetSizeOfCompactSize(v.size());
for (typename prevector<N, T>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
nSize += GetSerializeSize((*vi), nType, nVersion);
return nSize;
}
template<unsigned int N, typename T>
inline unsigned int GetSerializeSize(const prevector<N, T>& v, int nType, int nVersion)
{
return GetSerializeSize_impl(v, nType, nVersion, T());
}
template<typename Stream, unsigned int N, typename T> template<typename Stream, unsigned int N, typename T>
void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&) void Serialize_impl(Stream& os, const prevector<N, T>& v, int nType, int nVersion, const unsigned char&)
{ {
@ -716,28 +636,6 @@ inline void Unserialize(Stream& is, prevector<N, T>& v, int nType, int nVersion)
/** /**
* vector * vector
*/ */
template<typename T, typename A>
unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
{
return (GetSizeOfCompactSize(v.size()) + v.size() * sizeof(T));
}
template<typename T, typename A, typename V>
unsigned int GetSerializeSize_impl(const std::vector<T, A>& v, int nType, int nVersion, const V&)
{
unsigned int nSize = GetSizeOfCompactSize(v.size());
for (typename std::vector<T, A>::const_iterator vi = v.begin(); vi != v.end(); ++vi)
nSize += GetSerializeSize((*vi), nType, nVersion);
return nSize;
}
template<typename T, typename A>
inline unsigned int GetSerializeSize(const std::vector<T, A>& v, int nType, int nVersion)
{
return GetSerializeSize_impl(v, nType, nVersion, T());
}
template<typename Stream, typename T, typename A> template<typename Stream, typename T, typename A>
void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&) void Serialize_impl(Stream& os, const std::vector<T, A>& v, int nType, int nVersion, const unsigned char&)
{ {
@ -806,12 +704,6 @@ inline void Unserialize(Stream& is, std::vector<T, A>& v, int nType, int nVersio
/** /**
* pair * pair
*/ */
template<typename K, typename T>
unsigned int GetSerializeSize(const std::pair<K, T>& item, int nType, int nVersion)
{
return GetSerializeSize(item.first, nType, nVersion) + GetSerializeSize(item.second, nType, nVersion);
}
template<typename Stream, typename K, typename T> template<typename Stream, typename K, typename T>
void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion) void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion)
{ {
@ -831,15 +723,6 @@ void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
/** /**
* map * map
*/ */
template<typename K, typename T, typename Pred, typename A>
unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion)
{
unsigned int nSize = GetSizeOfCompactSize(m.size());
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
nSize += GetSerializeSize((*mi), nType, nVersion);
return nSize;
}
template<typename Stream, typename K, typename T, typename Pred, typename A> template<typename Stream, typename K, typename T, typename Pred, typename A>
void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion) void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion)
{ {
@ -867,15 +750,6 @@ void Unserialize(Stream& is, std::map<K, T, Pred, A>& m, int nType, int nVersion
/** /**
* set * set
*/ */
template<typename K, typename Pred, typename A>
unsigned int GetSerializeSize(const std::set<K, Pred, A>& m, int nType, int nVersion)
{
unsigned int nSize = GetSizeOfCompactSize(m.size());
for (typename std::set<K, Pred, A>::const_iterator it = m.begin(); it != m.end(); ++it)
nSize += GetSerializeSize((*it), nType, nVersion);
return nSize;
}
template<typename Stream, typename K, typename Pred, typename A> template<typename Stream, typename K, typename Pred, typename A>
void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion) void Serialize(Stream& os, const std::set<K, Pred, A>& m, int nType, int nVersion)
{ {
@ -1008,4 +882,16 @@ inline void SerReadWriteMany(Stream& s, int nType, int nVersion, CSerActionUnser
::UnserializeMany(s, nType, nVersion, args...); ::UnserializeMany(s, nType, nVersion, args...);
} }
template <typename T>
size_t GetSerializeSize(const T& t, int nType, int nVersion = 0)
{
return (CSizeComputer(nType, nVersion) << t).size();
}
template <typename S, typename T>
size_t GetSerializeSize(const S& s, const T& t)
{
return (CSizeComputer(s.GetType(), s.GetVersion()) << t).size();
}
#endif // BITCOIN_SERIALIZE_H #endif // BITCOIN_SERIALIZE_H

14
src/streams.h

@ -308,13 +308,6 @@ public:
s.write((char*)&vch[0], vch.size() * sizeof(vch[0])); s.write((char*)&vch[0], vch.size() * sizeof(vch[0]));
} }
template<typename T>
unsigned int GetSerializeSize(const T& obj)
{
// Tells the size of the object if serialized to this stream
return ::GetSerializeSize(obj, nType, nVersion);
}
template<typename T> template<typename T>
CDataStream& operator<<(const T& obj) CDataStream& operator<<(const T& obj)
{ {
@ -457,13 +450,6 @@ public:
throw std::ios_base::failure("CAutoFile::write: write failed"); throw std::ios_base::failure("CAutoFile::write: write failed");
} }
template<typename T>
unsigned int GetSerializeSize(const T& obj)
{
// Tells the size of the object if serialized to this stream
return ::GetSerializeSize(obj, nType, nVersion);
}
template<typename T> template<typename T>
CAutoFile& operator<<(const T& obj) CAutoFile& operator<<(const T& obj)
{ {

8
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(OneL.begin() + 32 == OneL.end());
BOOST_CHECK(MaxL.begin() + 32 == MaxL.end()); BOOST_CHECK(MaxL.begin() + 32 == MaxL.end());
BOOST_CHECK(TmpL.begin() + 32 == TmpL.end()); BOOST_CHECK(TmpL.begin() + 32 == TmpL.end());
BOOST_CHECK(R1L.GetSerializeSize(0,PROTOCOL_VERSION) == 32); BOOST_CHECK(GetSerializeSize(R1L, 0, PROTOCOL_VERSION) == 32);
BOOST_CHECK(ZeroL.GetSerializeSize(0,PROTOCOL_VERSION) == 32); BOOST_CHECK(GetSerializeSize(ZeroL, 0, PROTOCOL_VERSION) == 32);
std::stringstream ss; std::stringstream ss;
R1L.Serialize(ss,0,PROTOCOL_VERSION); 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(OneS.begin() + 20 == OneS.end());
BOOST_CHECK(MaxS.begin() + 20 == MaxS.end()); BOOST_CHECK(MaxS.begin() + 20 == MaxS.end());
BOOST_CHECK(TmpS.begin() + 20 == TmpS.end()); BOOST_CHECK(TmpS.begin() + 20 == TmpS.end());
BOOST_CHECK(R1S.GetSerializeSize(0,PROTOCOL_VERSION) == 20); BOOST_CHECK(GetSerializeSize(R1S, 0, PROTOCOL_VERSION) == 20);
BOOST_CHECK(ZeroS.GetSerializeSize(0,PROTOCOL_VERSION) == 20); BOOST_CHECK(GetSerializeSize(ZeroS, 0, PROTOCOL_VERSION) == 20);
R1S.Serialize(ss,0,PROTOCOL_VERSION); R1S.Serialize(ss,0,PROTOCOL_VERSION);
BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20)); BOOST_CHECK(ss.str() == std::string(R1Array,R1Array+20));

5
src/uint256.h

@ -78,11 +78,6 @@ public:
return sizeof(data); return sizeof(data);
} }
unsigned int GetSerializeSize(int nType, int nVersion) const
{
return sizeof(data);
}
uint64_t GetUint64(int pos) const uint64_t GetUint64(int pos) const
{ {
const uint8_t* ptr = data + pos * 8; const uint8_t* ptr = data + pos * 8;

6
src/undo.h

@ -27,12 +27,6 @@ public:
CTxInUndo() : txout(), fCoinBase(false), nHeight(0), nVersion(0) {} 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) { } 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<typename Stream> template<typename Stream>
void Serialize(Stream &s, int nType, int nVersion) const { void Serialize(Stream &s, int nType, int nVersion) const {
::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion); ::Serialize(s, VARINT(nHeight*2+(fCoinBase ? 1 : 0)), nType, nVersion);

Loading…
Cancel
Save