From ffff0d04425a616c14fc4a562e8ef93d286705f8 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Mon, 31 May 2021 14:57:32 +0200 Subject: refactor: Switch serialize to uint8_t (1/n) --- src/addrman.cpp | 4 ++-- src/net.cpp | 2 +- src/serialize.h | 8 ++------ src/test/dbwrapper_tests.cpp | 28 ++++++++++++++-------------- src/test/serialize_tests.cpp | 8 ++++---- src/txdb.cpp | 32 ++++++++++++++++---------------- src/wallet/transaction.h | 4 ++-- src/wallet/walletdb.cpp | 8 ++++---- 8 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index ceab1689d7..14b412a038 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -36,7 +36,7 @@ int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std: int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const { - uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash(); + uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << nBucket << GetKey()).GetCheapHash(); return hash1 % ADDRMAN_BUCKET_SIZE; } @@ -652,7 +652,7 @@ std::vector CAddrMan::DecodeAsmap(fs::path path) int length = ftell(filestr); LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length); fseek(filestr, 0, SEEK_SET); - char cur_byte; + uint8_t cur_byte; for (int i = 0; i < length; ++i) { file >> cur_byte; for (int bit = 0; bit < 8; ++bit) { diff --git a/src/net.cpp b/src/net.cpp index 1b6f04dead..52936508ba 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -3048,7 +3048,7 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa ser_writedata64(f, now.count()); f.write(msg_type.data(), msg_type.length()); for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) { - f << '\0'; + f << uint8_t{'\0'}; } uint32_t size = data.size(); ser_writedata32(f, size); diff --git a/src/serialize.h b/src/serialize.h index 5ef846b9e9..edf10440c6 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -226,12 +226,8 @@ template inline void Unserialize(Stream& s, char (&a)[N] template inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); } template inline void Unserialize(Stream& s, Span& span) { s.read(CharCast(span.data()), span.size()); } -template inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); } -template inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; } - - - - +template inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); } +template inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; } /** diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index e2e7644dfa..b6f5938892 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper) for (const bool obfuscate : {false, true}) { fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false"); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); - char key = 'k'; + uint8_t key{'k'}; uint256 in = InsecureRand256(); uint256 res; @@ -88,21 +88,21 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data) BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString()); //Simulate last block file number - "l" - char key_last_blockfile_number = 'l'; + uint8_t key_last_blockfile_number{'l'}; uint32_t lastblockfilenumber = InsecureRand32(); BOOST_CHECK(dbw.Write(key_last_blockfile_number, lastblockfilenumber)); BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32)); BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32); //Simulate Is Reindexing - "R" - char key_IsReindexing = 'R'; + uint8_t key_IsReindexing{'R'}; bool isInReindexing = InsecureRandBool(); BOOST_CHECK(dbw.Write(key_IsReindexing, isInReindexing)); BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool)); BOOST_CHECK_EQUAL(isInReindexing, res_bool); //Simulate last block hash up to which UXTO covers - 'B' - char key_lastblockhash_uxto = 'B'; + uint8_t key_lastblockhash_uxto{'B'}; uint256 lastblock_hash = InsecureRand256(); BOOST_CHECK(dbw.Write(key_lastblockhash_uxto, lastblock_hash)); BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res)); @@ -129,11 +129,11 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch) fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false"); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); - char key = 'i'; + uint8_t key{'i'}; uint256 in = InsecureRand256(); - char key2 = 'j'; + uint8_t key2{'j'}; uint256 in2 = InsecureRand256(); - char key3 = 'k'; + uint8_t key3{'k'}; uint256 in3 = InsecureRand256(); uint256 res; @@ -166,10 +166,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator) CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); // The two keys are intentionally chosen for ordering - char key = 'j'; + uint8_t key{'j'}; uint256 in = InsecureRand256(); BOOST_CHECK(dbw.Write(key, in)); - char key2 = 'k'; + uint8_t key2{'k'}; uint256 in2 = InsecureRand256(); BOOST_CHECK(dbw.Write(key2, in2)); @@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator) // Be sure to seek past the obfuscation key (if it exists) it->Seek(key); - char key_res; + uint8_t key_res; uint256 val_res; BOOST_REQUIRE(it->GetKey(key_res)); @@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) // Set up a non-obfuscated wrapper to write some initial data. std::unique_ptr dbw = std::make_unique(ph, (1 << 10), false, false, false); - char key = 'k'; + uint8_t key{'k'}; uint256 in = InsecureRand256(); uint256 res; @@ -248,7 +248,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex) // Set up a non-obfuscated wrapper to write some initial data. std::unique_ptr dbw = std::make_unique(ph, (1 << 10), false, false, false); - char key = 'k'; + uint8_t key{'k'}; uint256 in = InsecureRand256(); uint256 res; @@ -334,7 +334,7 @@ struct StringContentsSerializer { void Serialize(Stream& s) const { for (size_t i = 0; i < str.size(); i++) { - s << str[i]; + s << uint8_t(str[i]); } } @@ -342,7 +342,7 @@ struct StringContentsSerializer { void Unserialize(Stream& s) { str.clear(); - char c = 0; + uint8_t c{0}; while (true) { try { s >> c; diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index 58709178a4..4b55e3bc26 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -24,7 +24,7 @@ protected: CTransactionRef txval; public: CSerializeMethodsTestSingle() = default; - CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const char* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin) + CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin) { memcpy(charstrval, charstrvalin, sizeof(charstrval)); } @@ -70,8 +70,8 @@ BOOST_AUTO_TEST_CASE(sizes) BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0)); BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0)); BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0)); - // Bool is serialized as char - BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0)); + // Bool is serialized as uint8_t + BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0), 0)); // Sanity-check GetSerializeSize and c++ type matching BOOST_CHECK_EQUAL(GetSerializeSize(char(0), 0), 1U); @@ -263,7 +263,7 @@ BOOST_AUTO_TEST_CASE(class_methods) int intval(100); bool boolval(true); std::string stringval("testing"); - const char charstrval[16] = "testing charstr"; + const uint8_t charstrval[16]{"testing charstr"}; CMutableTransaction txval; CTransactionRef tx_ref{MakeTransactionRef(txval)}; CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref); diff --git a/src/txdb.cpp b/src/txdb.cpp index c11d46cf88..762f71feb1 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -16,22 +16,22 @@ #include -static const char DB_COIN = 'C'; -static const char DB_COINS = 'c'; -static const char DB_BLOCK_FILES = 'f'; -static const char DB_BLOCK_INDEX = 'b'; +static constexpr uint8_t DB_COIN{'C'}; +static constexpr uint8_t DB_COINS{'c'}; +static constexpr uint8_t DB_BLOCK_FILES{'f'}; +static constexpr uint8_t DB_BLOCK_INDEX{'b'}; -static const char DB_BEST_BLOCK = 'B'; -static const char DB_HEAD_BLOCKS = 'H'; -static const char DB_FLAG = 'F'; -static const char DB_REINDEX_FLAG = 'R'; -static const char DB_LAST_BLOCK = 'l'; +static constexpr uint8_t DB_BEST_BLOCK{'B'}; +static constexpr uint8_t DB_HEAD_BLOCKS{'H'}; +static constexpr uint8_t DB_FLAG{'F'}; +static constexpr uint8_t DB_REINDEX_FLAG{'R'}; +static constexpr uint8_t DB_LAST_BLOCK{'l'}; namespace { struct CoinEntry { COutPoint* outpoint; - char key; + uint8_t key; explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast(ptr)), key(DB_COIN) {} SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); } @@ -143,7 +143,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { size_t CCoinsViewDB::EstimateSize() const { - return m_db->EstimateSize(DB_COIN, (char)(DB_COIN+1)); + return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1)); } CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(gArgs.GetDataDirNet() / "blocks" / "index", nCacheSize, fMemory, fWipe) { @@ -155,7 +155,7 @@ bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) { bool CBlockTreeDB::WriteReindexing(bool fReindexing) { if (fReindexing) - return Write(DB_REINDEX_FLAG, '1'); + return Write(DB_REINDEX_FLAG, uint8_t{'1'}); else return Erase(DB_REINDEX_FLAG); } @@ -235,14 +235,14 @@ bool CBlockTreeDB::WriteBatchSync(const std::vectorValid()) { if (ShutdownRequested()) return false; - std::pair key; + std::pair key; if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) { CDiskBlockIndex diskindex; if (pcursor->GetValue(diskindex)) { diff --git a/src/wallet/transaction.h b/src/wallet/transaction.h index b57ab3db48..131faefe0b 100644 --- a/src/wallet/transaction.h +++ b/src/wallet/transaction.h @@ -204,8 +204,8 @@ public: mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart); } - std::vector dummy_vector1; //!< Used to be vMerkleBranch - std::vector dummy_vector2; //!< Used to be vtxPrev + std::vector dummy_vector1; //!< Used to be vMerkleBranch + std::vector dummy_vector2; //!< Used to be vtxPrev bool dummy_bool = false; //!< Used to be fSpent uint256 serializedHash = isAbandoned() ? ABANDON_HASH : m_confirm.hashBlock; int serializedIndex = isAbandoned() || isConflicted() ? -1 : m_confirm.nIndex; diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 3d9248009f..c06b319b0b 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -155,7 +155,7 @@ bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMet if (!WriteIC(std::make_pair(DBKeys::WATCHMETA, dest), keyMeta)) { return false; } - return WriteIC(std::make_pair(DBKeys::WATCHS, dest), '1'); + return WriteIC(std::make_pair(DBKeys::WATCHS, dest), uint8_t{'1'}); } bool WalletBatch::EraseWatchOnly(const CScript &dest) @@ -308,8 +308,8 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { if (!ssValue.empty()) { - char fTmp; - char fUnused; + uint8_t fTmp; + uint8_t fUnused; std::string unused_string; ssValue >> fTmp >> fUnused >> unused_string; strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s", @@ -336,7 +336,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, wss.nWatchKeys++; CScript script; ssKey >> script; - char fYes; + uint8_t fYes; ssValue >> fYes; if (fYes == '1') { pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadWatchOnly(script); -- cgit v1.2.3