From a8e5ae92bae9760df78b4c405e2a0ab2300f3fa1 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 9 Mar 2018 12:32:02 +0000 Subject: [PATCH] GetSerializeSize changes in Zcash-specific code --- src/dbwrapper.h | 2 +- src/main.cpp | 2 +- src/paymentdisclosuredb.cpp | 2 +- src/serialize.h | 32 -------------------------------- src/wallet/rpcwallet.cpp | 10 +++++----- 5 files changed, 8 insertions(+), 40 deletions(-) diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 40ed9e583..1d62d2b72 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -99,7 +99,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); diff --git a/src/main.cpp b/src/main.cpp index 7a401e673..f6d3f3992 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -575,7 +575,7 @@ bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(c // have been mined or received. // 10,000 orphans, each of which is at most 5,000 bytes big is // at most 500 megabytes of orphans: - unsigned int sz = tx.GetSerializeSize(SER_NETWORK, tx.nVersion); + unsigned int sz = GetSerializeSize(tx, SER_NETWORK, tx.nVersion); if (sz > 5000) { LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString()); diff --git a/src/paymentdisclosuredb.cpp b/src/paymentdisclosuredb.cpp index 1851e77ec..8840dcda0 100644 --- a/src/paymentdisclosuredb.cpp +++ b/src/paymentdisclosuredb.cpp @@ -57,7 +57,7 @@ bool PaymentDisclosureDB::Put(const PaymentDisclosureKey& key, const PaymentDisc std::lock_guard guard(lock_); CDataStream ssValue(SER_DISK, CLIENT_VERSION); - ssValue.reserve(ssValue.GetSerializeSize(info)); + ssValue.reserve(GetSerializeSize(ssValue, info)); ssValue << info; leveldb::Slice slice(&ssValue[0], ssValue.size()); diff --git a/src/serialize.h b/src/serialize.h index da80b7630..124169c67 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -506,14 +506,12 @@ template inline void Unserialize(Stream /** * optional */ -template unsigned int GetSerializeSize(const boost::optional &item, int nType, int nVersion); template void Serialize(Stream& os, const boost::optional& item, int nType, int nVersion); template void Unserialize(Stream& is, boost::optional& item, int nType, int nVersion); /** * array */ -template unsigned int GetSerializeSize(const boost::array &item, int nType, int nVersion); template void Serialize(Stream& os, const boost::array& item, int nType, int nVersion); template void Unserialize(Stream& is, boost::array& item, int nType, int nVersion); @@ -538,7 +536,6 @@ template void Unserializ /** * list */ -template unsigned int GetSerializeSize(const std::list& m, int nType, int nVersion); template void Serialize(Stream& os, const std::list& m, int nType, int nVersion); template void Unserialize(Stream& is, std::list& m, int nType, int nVersion); @@ -729,16 +726,6 @@ inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersio /** * optional */ -template -unsigned int GetSerializeSize(const boost::optional &item, int nType, int nVersion) -{ - if (item) { - return 1 + GetSerializeSize(*item, nType, nVersion); - } else { - return 1; - } -} - template void Serialize(Stream& os, const boost::optional& item, int nType, int nVersion) { @@ -776,16 +763,6 @@ void Unserialize(Stream& is, boost::optional& item, int nType, int nVersion) /** * array */ -template -unsigned int GetSerializeSize(const boost::array &item, int nType, int nVersion) -{ - unsigned int size = 0; - for (size_t i = 0; i < N; i++) { - size += GetSerializeSize(item[0], nType, nVersion); - } - return size; -} - template void Serialize(Stream& os, const boost::array& item, int nType, int nVersion) { @@ -879,15 +856,6 @@ 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) { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c03194e54..e86f0e2de 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -1341,7 +1341,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe entry.push_back(Pair("fee", ValueFromAmount(-nFee))); if (fLong) WalletTxToJSON(wtx, entry); - entry.push_back(Pair("size", static_cast(static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION)))); + entry.push_back(Pair("size", static_cast(GetSerializeSize(static_cast(wtx), SER_NETWORK, PROTOCOL_VERSION)))); ret.push_back(entry); } } @@ -1378,7 +1378,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe entry.push_back(Pair("vout", r.vout)); if (fLong) WalletTxToJSON(wtx, entry); - entry.push_back(Pair("size", static_cast(static_cast(wtx).GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION)))); + entry.push_back(Pair("size", static_cast(GetSerializeSize(static_cast(wtx), SER_NETWORK, PROTOCOL_VERSION)))); ret.push_back(entry); } } @@ -3482,7 +3482,7 @@ UniValue z_getoperationstatus_IMPL(const UniValue& params, bool fRemoveFinishedO // If input notes are small, we might actually require more than one joinsplit per zaddr output. // For now though, we assume we use one joinsplit per zaddr output (and the second output note is change). // We reduce the result by 1 to ensure there is room for non-joinsplit CTransaction data. -#define Z_SENDMANY_MAX_ZADDR_OUTPUTS ((MAX_TX_SIZE / JSDescription().GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION)) - 1) +#define Z_SENDMANY_MAX_ZADDR_OUTPUTS ((MAX_TX_SIZE / GetSerializeSize(JSDescription(), SER_NETWORK, PROTOCOL_VERSION)) - 1) // transaction.h comment: spending taddr output requires CTxIn >= 148 bytes and typical taddr txout is 34 bytes #define CTXIN_SPEND_DUST_SIZE 148 @@ -3627,7 +3627,7 @@ UniValue z_sendmany(const UniValue& params, bool fHelp) mtx.vjoinsplit.push_back(JSDescription()); } CTransaction tx(mtx); - txsize += tx.GetSerializeSize(SER_NETWORK, tx.nVersion); + txsize += GetSerializeSize(tx, SER_NETWORK, tx.nVersion); if (fromTaddr) { txsize += CTXIN_SPEND_DUST_SIZE; txsize += CTXOUT_REGULAR_SIZE; // There will probably be taddr change @@ -3898,7 +3898,7 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp) #define MERGE_TO_ADDRESS_DEFAULT_TRANSPARENT_LIMIT 50 #define MERGE_TO_ADDRESS_DEFAULT_SHIELDED_LIMIT 10 -#define JOINSPLIT_SIZE JSDescription().GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) +#define JOINSPLIT_SIZE GetSerializeSize(JSDescription(), SER_NETWORK, PROTOCOL_VERSION) UniValue z_mergetoaddress(const UniValue& params, bool fHelp) {