Skip to content

Commit

Permalink
Make GetSerializeSize a wrapper on top of CSizeComputer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sipa committed Nov 7, 2016
1 parent fad9b66 commit 657e05a
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 210 deletions.
5 changes: 0 additions & 5 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,6 @@ class CAddrMan
Check();
}

unsigned int GetSerializeSize(int nType, int nVersion) const
{
return (CSizeComputer(nType, nVersion) << *this).size();
}

void Clear()
{
std::vector<int>().swap(vRandom);
Expand Down
2 changes: 1 addition & 1 deletion src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 0 additions & 23 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,6 @@ class CCoins
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>
void Serialize(Stream &s, int nType, int nVersion) const {
unsigned int nMaskSize = 0, nMaskCode = 0;
Expand Down
8 changes: 0 additions & 8 deletions src/compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,6 @@ class CScriptCompressor
public:
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>
void Serialize(Stream &s, int nType, int nVersion) const {
std::vector<unsigned char> compr;
Expand Down
12 changes: 6 additions & 6 deletions src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class CDBBatch
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());
Expand All @@ -77,7 +77,7 @@ class CDBBatch
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());

Expand Down Expand Up @@ -107,7 +107,7 @@ class CDBIterator

template<typename K> 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);
Expand Down Expand Up @@ -200,7 +200,7 @@ class CDBWrapper
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());

Expand Down Expand Up @@ -234,7 +234,7 @@ class CDBWrapper
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());

Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class CTxOut
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<unsigned char> witnessprogram;

Expand Down
8 changes: 0 additions & 8 deletions src/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ class CPubKey
}

//! Implement serialization, as if this was a byte vector.
unsigned int GetSerializeSize(int nType, int nVersion) const
{
return size() + 1;
}
template <typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const
{
Expand Down Expand Up @@ -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 <typename Stream>
void Serialize(Stream& s, int nType, int nVersion) const
{
Expand Down
2 changes: 1 addition & 1 deletion src/script/bitcoinconsensus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 657e05a

Please sign in to comment.