Skip to content

Commit 657e05a

Browse files
committed
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.
1 parent fad9b66 commit 657e05a

File tree

14 files changed

+27
-210
lines changed

14 files changed

+27
-210
lines changed

src/addrman.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,6 @@ class CAddrMan
448448
Check();
449449
}
450450

451-
unsigned int GetSerializeSize(int nType, int nVersion) const
452-
{
453-
return (CSizeComputer(nType, nVersion) << *this).size();
454-
}
455-
456451
void Clear()
457452
{
458453
std::vector<int>().swap(vRandom);

src/blockencodings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
131131
break;
132132
}
133133

134-
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), cmpctblock.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION));
134+
LogPrint("cmpctblock", "Initialized PartiallyDownloadedBlock for block %s using a cmpctblock of size %lu\n", cmpctblock.header.GetHash().ToString(), GetSerializeSize(cmpctblock, SER_NETWORK, PROTOCOL_VERSION));
135135

136136
return READ_STATUS_OK;
137137
}

src/coins.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,6 @@ class CCoins
153153
return fCoinBase;
154154
}
155155

156-
unsigned int GetSerializeSize(int nType, int nVersion) const {
157-
unsigned int nSize = 0;
158-
unsigned int nMaskSize = 0, nMaskCode = 0;
159-
CalcMaskSize(nMaskSize, nMaskCode);
160-
bool fFirst = vout.size() > 0 && !vout[0].IsNull();
161-
bool fSecond = vout.size() > 1 && !vout[1].IsNull();
162-
assert(fFirst || fSecond || nMaskCode);
163-
unsigned int nCode = 8*(nMaskCode - (fFirst || fSecond ? 0 : 1)) + (fCoinBase ? 1 : 0) + (fFirst ? 2 : 0) + (fSecond ? 4 : 0);
164-
// version
165-
nSize += ::GetSerializeSize(VARINT(this->nVersion), nType, nVersion);
166-
// size of header code
167-
nSize += ::GetSerializeSize(VARINT(nCode), nType, nVersion);
168-
// spentness bitmask
169-
nSize += nMaskSize;
170-
// txouts themself
171-
for (unsigned int i = 0; i < vout.size(); i++)
172-
if (!vout[i].IsNull())
173-
nSize += ::GetSerializeSize(CTxOutCompressor(REF(vout[i])), nType, nVersion);
174-
// height
175-
nSize += ::GetSerializeSize(VARINT(nHeight), nType, nVersion);
176-
return nSize;
177-
}
178-
179156
template<typename Stream>
180157
void Serialize(Stream &s, int nType, int nVersion) const {
181158
unsigned int nMaskSize = 0, nMaskCode = 0;

src/compressor.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,6 @@ class CScriptCompressor
5555
public:
5656
CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
5757

58-
unsigned int GetSerializeSize(int nType, int nVersion) const {
59-
std::vector<unsigned char> compr;
60-
if (Compress(compr))
61-
return compr.size();
62-
unsigned int nSize = script.size() + nSpecialScripts;
63-
return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
64-
}
65-
6658
template<typename Stream>
6759
void Serialize(Stream &s, int nType, int nVersion) const {
6860
std::vector<unsigned char> compr;

src/dbwrapper.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ class CDBBatch
6060
void Write(const K& key, const V& value)
6161
{
6262
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
63-
ssKey.reserve(ssKey.GetSerializeSize(key));
63+
ssKey.reserve(GetSerializeSize(ssKey, key));
6464
ssKey << key;
6565
leveldb::Slice slKey(&ssKey[0], ssKey.size());
6666

6767
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
68-
ssValue.reserve(ssValue.GetSerializeSize(value));
68+
ssValue.reserve(GetSerializeSize(ssValue, value));
6969
ssValue << value;
7070
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
7171
leveldb::Slice slValue(&ssValue[0], ssValue.size());
@@ -77,7 +77,7 @@ class CDBBatch
7777
void Erase(const K& key)
7878
{
7979
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
80-
ssKey.reserve(ssKey.GetSerializeSize(key));
80+
ssKey.reserve(GetSerializeSize(ssKey, key));
8181
ssKey << key;
8282
leveldb::Slice slKey(&ssKey[0], ssKey.size());
8383

@@ -107,7 +107,7 @@ class CDBIterator
107107

108108
template<typename K> void Seek(const K& key) {
109109
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
110-
ssKey.reserve(ssKey.GetSerializeSize(key));
110+
ssKey.reserve(GetSerializeSize(ssKey, key));
111111
ssKey << key;
112112
leveldb::Slice slKey(&ssKey[0], ssKey.size());
113113
piter->Seek(slKey);
@@ -200,7 +200,7 @@ class CDBWrapper
200200
bool Read(const K& key, V& value) const
201201
{
202202
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
203-
ssKey.reserve(ssKey.GetSerializeSize(key));
203+
ssKey.reserve(GetSerializeSize(ssKey, key));
204204
ssKey << key;
205205
leveldb::Slice slKey(&ssKey[0], ssKey.size());
206206

@@ -234,7 +234,7 @@ class CDBWrapper
234234
bool Exists(const K& key) const
235235
{
236236
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
237-
ssKey.reserve(ssKey.GetSerializeSize(key));
237+
ssKey.reserve(GetSerializeSize(ssKey, key));
238238
ssKey << key;
239239
leveldb::Slice slKey(&ssKey[0], ssKey.size());
240240

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHea
17101710
return error("WriteBlockToDisk: OpenBlockFile failed");
17111711

17121712
// Write index header
1713-
unsigned int nSize = fileout.GetSerializeSize(block);
1713+
unsigned int nSize = GetSerializeSize(fileout, block);
17141714
fileout << FLATDATA(messageStart) << nSize;
17151715

17161716
// Write block
@@ -2100,7 +2100,7 @@ bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint
21002100
return error("%s: OpenUndoFile failed", __func__);
21012101

21022102
// Write index header
2103-
unsigned int nSize = fileout.GetSerializeSize(blockundo);
2103+
unsigned int nSize = GetSerializeSize(fileout, blockundo);
21042104
fileout << FLATDATA(messageStart) << nSize;
21052105

21062106
// Write undo data

src/primitives/transaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class CTxOut
177177
if (scriptPubKey.IsUnspendable())
178178
return 0;
179179

180-
size_t nSize = GetSerializeSize(SER_DISK, 0);
180+
size_t nSize = GetSerializeSize(*this, SER_DISK, 0);
181181
int witnessversion = 0;
182182
std::vector<unsigned char> witnessprogram;
183183

src/pubkey.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ class CPubKey
116116
}
117117

118118
//! Implement serialization, as if this was a byte vector.
119-
unsigned int GetSerializeSize(int nType, int nVersion) const
120-
{
121-
return size() + 1;
122-
}
123119
template <typename Stream>
124120
void Serialize(Stream& s, int nType, int nVersion) const
125121
{
@@ -214,10 +210,6 @@ struct CExtPubKey {
214210
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
215211
bool Derive(CExtPubKey& out, unsigned int nChild) const;
216212

217-
unsigned int GetSerializeSize(int nType, int nVersion) const
218-
{
219-
return BIP32_EXTKEY_SIZE+1; //add one byte for the size (compact int)
220-
}
221213
template <typename Stream>
222214
void Serialize(Stream& s, int nType, int nVersion) const
223215
{

src/script/bitcoinconsensus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
8787
stream >> tx;
8888
if (nIn >= tx.vin.size())
8989
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
90-
if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) != txToLen)
90+
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen)
9191
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
9292

9393
// Regardless of the verification result, the tx did not error.

0 commit comments

Comments
 (0)