Skip to content

Commit ffff0d0

Browse files
author
MarcoFalke
committed
refactor: Switch serialize to uint8_t (1/n)
1 parent 8c6df2b commit ffff0d0

8 files changed

+45
-49
lines changed

src/addrman.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std:
3636

3737
int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
3838
{
39-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash();
39+
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << nBucket << GetKey()).GetCheapHash();
4040
return hash1 % ADDRMAN_BUCKET_SIZE;
4141
}
4242

@@ -652,7 +652,7 @@ std::vector<bool> CAddrMan::DecodeAsmap(fs::path path)
652652
int length = ftell(filestr);
653653
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length);
654654
fseek(filestr, 0, SEEK_SET);
655-
char cur_byte;
655+
uint8_t cur_byte;
656656
for (int i = 0; i < length; ++i) {
657657
file >> cur_byte;
658658
for (int bit = 0; bit < 8; ++bit) {

src/net.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3048,7 +3048,7 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa
30483048
ser_writedata64(f, now.count());
30493049
f.write(msg_type.data(), msg_type.length());
30503050
for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) {
3051-
f << '\0';
3051+
f << uint8_t{'\0'};
30523052
}
30533053
uint32_t size = data.size();
30543054
ser_writedata32(f, size);

src/serialize.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,8 @@ template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]
226226
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); }
227227
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); }
228228

229-
template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); }
230-
template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; }
231-
232-
233-
234-
229+
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
230+
template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
235231

236232

237233
/**

src/test/dbwrapper_tests.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
2828
for (const bool obfuscate : {false, true}) {
2929
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false");
3030
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
31-
char key = 'k';
31+
uint8_t key{'k'};
3232
uint256 in = InsecureRand256();
3333
uint256 res;
3434

@@ -88,21 +88,21 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
8888
BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString());
8989

9090
//Simulate last block file number - "l"
91-
char key_last_blockfile_number = 'l';
91+
uint8_t key_last_blockfile_number{'l'};
9292
uint32_t lastblockfilenumber = InsecureRand32();
9393
BOOST_CHECK(dbw.Write(key_last_blockfile_number, lastblockfilenumber));
9494
BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
9595
BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32);
9696

9797
//Simulate Is Reindexing - "R"
98-
char key_IsReindexing = 'R';
98+
uint8_t key_IsReindexing{'R'};
9999
bool isInReindexing = InsecureRandBool();
100100
BOOST_CHECK(dbw.Write(key_IsReindexing, isInReindexing));
101101
BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool));
102102
BOOST_CHECK_EQUAL(isInReindexing, res_bool);
103103

104104
//Simulate last block hash up to which UXTO covers - 'B'
105-
char key_lastblockhash_uxto = 'B';
105+
uint8_t key_lastblockhash_uxto{'B'};
106106
uint256 lastblock_hash = InsecureRand256();
107107
BOOST_CHECK(dbw.Write(key_lastblockhash_uxto, lastblock_hash));
108108
BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
@@ -129,11 +129,11 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
129129
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
130130
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
131131

132-
char key = 'i';
132+
uint8_t key{'i'};
133133
uint256 in = InsecureRand256();
134-
char key2 = 'j';
134+
uint8_t key2{'j'};
135135
uint256 in2 = InsecureRand256();
136-
char key3 = 'k';
136+
uint8_t key3{'k'};
137137
uint256 in3 = InsecureRand256();
138138

139139
uint256 res;
@@ -166,10 +166,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
166166
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
167167

168168
// The two keys are intentionally chosen for ordering
169-
char key = 'j';
169+
uint8_t key{'j'};
170170
uint256 in = InsecureRand256();
171171
BOOST_CHECK(dbw.Write(key, in));
172-
char key2 = 'k';
172+
uint8_t key2{'k'};
173173
uint256 in2 = InsecureRand256();
174174
BOOST_CHECK(dbw.Write(key2, in2));
175175

@@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
178178
// Be sure to seek past the obfuscation key (if it exists)
179179
it->Seek(key);
180180

181-
char key_res;
181+
uint8_t key_res;
182182
uint256 val_res;
183183

184184
BOOST_REQUIRE(it->GetKey(key_res));
@@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
207207

208208
// Set up a non-obfuscated wrapper to write some initial data.
209209
std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false);
210-
char key = 'k';
210+
uint8_t key{'k'};
211211
uint256 in = InsecureRand256();
212212
uint256 res;
213213

@@ -248,7 +248,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
248248

249249
// Set up a non-obfuscated wrapper to write some initial data.
250250
std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false);
251-
char key = 'k';
251+
uint8_t key{'k'};
252252
uint256 in = InsecureRand256();
253253
uint256 res;
254254

@@ -334,15 +334,15 @@ struct StringContentsSerializer {
334334
void Serialize(Stream& s) const
335335
{
336336
for (size_t i = 0; i < str.size(); i++) {
337-
s << str[i];
337+
s << uint8_t(str[i]);
338338
}
339339
}
340340

341341
template<typename Stream>
342342
void Unserialize(Stream& s)
343343
{
344344
str.clear();
345-
char c = 0;
345+
uint8_t c{0};
346346
while (true) {
347347
try {
348348
s >> c;

src/test/serialize_tests.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class CSerializeMethodsTestSingle
2424
CTransactionRef txval;
2525
public:
2626
CSerializeMethodsTestSingle() = default;
27-
CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const char* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin)
27+
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)
2828
{
2929
memcpy(charstrval, charstrvalin, sizeof(charstrval));
3030
}
@@ -70,8 +70,8 @@ BOOST_AUTO_TEST_CASE(sizes)
7070
BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
7171
BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
7272
BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
73-
// Bool is serialized as char
74-
BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0));
73+
// Bool is serialized as uint8_t
74+
BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0), 0));
7575

7676
// Sanity-check GetSerializeSize and c++ type matching
7777
BOOST_CHECK_EQUAL(GetSerializeSize(char(0), 0), 1U);
@@ -263,7 +263,7 @@ BOOST_AUTO_TEST_CASE(class_methods)
263263
int intval(100);
264264
bool boolval(true);
265265
std::string stringval("testing");
266-
const char charstrval[16] = "testing charstr";
266+
const uint8_t charstrval[16]{"testing charstr"};
267267
CMutableTransaction txval;
268268
CTransactionRef tx_ref{MakeTransactionRef(txval)};
269269
CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);

src/txdb.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@
1616

1717
#include <stdint.h>
1818

19-
static const char DB_COIN = 'C';
20-
static const char DB_COINS = 'c';
21-
static const char DB_BLOCK_FILES = 'f';
22-
static const char DB_BLOCK_INDEX = 'b';
19+
static constexpr uint8_t DB_COIN{'C'};
20+
static constexpr uint8_t DB_COINS{'c'};
21+
static constexpr uint8_t DB_BLOCK_FILES{'f'};
22+
static constexpr uint8_t DB_BLOCK_INDEX{'b'};
2323

24-
static const char DB_BEST_BLOCK = 'B';
25-
static const char DB_HEAD_BLOCKS = 'H';
26-
static const char DB_FLAG = 'F';
27-
static const char DB_REINDEX_FLAG = 'R';
28-
static const char DB_LAST_BLOCK = 'l';
24+
static constexpr uint8_t DB_BEST_BLOCK{'B'};
25+
static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
26+
static constexpr uint8_t DB_FLAG{'F'};
27+
static constexpr uint8_t DB_REINDEX_FLAG{'R'};
28+
static constexpr uint8_t DB_LAST_BLOCK{'l'};
2929

3030
namespace {
3131

3232
struct CoinEntry {
3333
COutPoint* outpoint;
34-
char key;
34+
uint8_t key;
3535
explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)), key(DB_COIN) {}
3636

3737
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) {
143143

144144
size_t CCoinsViewDB::EstimateSize() const
145145
{
146-
return m_db->EstimateSize(DB_COIN, (char)(DB_COIN+1));
146+
return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
147147
}
148148

149149
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) {
155155

156156
bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
157157
if (fReindexing)
158-
return Write(DB_REINDEX_FLAG, '1');
158+
return Write(DB_REINDEX_FLAG, uint8_t{'1'});
159159
else
160160
return Erase(DB_REINDEX_FLAG);
161161
}
@@ -235,14 +235,14 @@ bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockF
235235
}
236236

237237
bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
238-
return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
238+
return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
239239
}
240240

241241
bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
242-
char ch;
242+
uint8_t ch;
243243
if (!Read(std::make_pair(DB_FLAG, name), ch))
244244
return false;
245-
fValue = ch == '1';
245+
fValue = ch == uint8_t{'1'};
246246
return true;
247247
}
248248

@@ -255,7 +255,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
255255
// Load m_block_index
256256
while (pcursor->Valid()) {
257257
if (ShutdownRequested()) return false;
258-
std::pair<char, uint256> key;
258+
std::pair<uint8_t, uint256> key;
259259
if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
260260
CDiskBlockIndex diskindex;
261261
if (pcursor->GetValue(diskindex)) {

src/wallet/transaction.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ class CWalletTx
204204
mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart);
205205
}
206206

207-
std::vector<char> dummy_vector1; //!< Used to be vMerkleBranch
208-
std::vector<char> dummy_vector2; //!< Used to be vtxPrev
207+
std::vector<uint8_t> dummy_vector1; //!< Used to be vMerkleBranch
208+
std::vector<uint8_t> dummy_vector2; //!< Used to be vtxPrev
209209
bool dummy_bool = false; //!< Used to be fSpent
210210
uint256 serializedHash = isAbandoned() ? ABANDON_HASH : m_confirm.hashBlock;
211211
int serializedIndex = isAbandoned() || isConflicted() ? -1 : m_confirm.nIndex;

src/wallet/walletdb.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMet
155155
if (!WriteIC(std::make_pair(DBKeys::WATCHMETA, dest), keyMeta)) {
156156
return false;
157157
}
158-
return WriteIC(std::make_pair(DBKeys::WATCHS, dest), '1');
158+
return WriteIC(std::make_pair(DBKeys::WATCHS, dest), uint8_t{'1'});
159159
}
160160

161161
bool WalletBatch::EraseWatchOnly(const CScript &dest)
@@ -308,8 +308,8 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
308308
{
309309
if (!ssValue.empty())
310310
{
311-
char fTmp;
312-
char fUnused;
311+
uint8_t fTmp;
312+
uint8_t fUnused;
313313
std::string unused_string;
314314
ssValue >> fTmp >> fUnused >> unused_string;
315315
strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s",
@@ -336,7 +336,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
336336
wss.nWatchKeys++;
337337
CScript script;
338338
ssKey >> script;
339-
char fYes;
339+
uint8_t fYes;
340340
ssValue >> fYes;
341341
if (fYes == '1') {
342342
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadWatchOnly(script);

0 commit comments

Comments
 (0)