aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheCharlatan <seb.kung@gmail.com>2023-09-12 13:01:07 +0200
committerTheCharlatan <seb.kung@gmail.com>2023-09-12 22:49:49 +0200
commit9be330b654cfbd792620295f3867f592059d6a7a (patch)
treed1596eed457af928c181b2332c292d1c7a982b85
parent37e2b011136ca1cf00dfb9e575d12f0d035a6a2c (diff)
downloadbitcoin-9be330b654cfbd792620295f3867f592059d6a7a.tar.xz
[refactor] Define MessageStartChars as std::array
-rw-r--r--src/addrdb.cpp4
-rw-r--r--src/kernel/chainparams.cpp2
-rw-r--r--src/net.cpp6
-rw-r--r--src/node/blockstorage.cpp2
-rw-r--r--src/node/blockstorage.h2
-rw-r--r--src/protocol.cpp2
-rw-r--r--src/protocol.h10
-rw-r--r--src/validation.cpp4
-rw-r--r--src/wallet/db.cpp2
-rw-r--r--src/wallet/sqlite.cpp4
10 files changed, 19 insertions, 19 deletions
diff --git a/src/addrdb.cpp b/src/addrdb.cpp
index c5b474339b..6b21d3a1ed 100644
--- a/src/addrdb.cpp
+++ b/src/addrdb.cpp
@@ -90,10 +90,10 @@ void DeserializeDB(Stream& stream, Data&& data, bool fCheckSum = true)
{
HashVerifier verifier{stream};
// de-serialize file header (network specific magic number) and ..
- unsigned char pchMsgTmp[4];
+ CMessageHeader::MessageStartChars pchMsgTmp;
verifier >> pchMsgTmp;
// ... verify the network matches ours
- if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
+ if (pchMsgTmp != Params().MessageStart()) {
throw std::runtime_error{"Invalid network magic number"};
}
diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp
index 733a3339b3..c41559ad98 100644
--- a/src/kernel/chainparams.cpp
+++ b/src/kernel/chainparams.cpp
@@ -359,7 +359,7 @@ public:
HashWriter h{};
h << consensus.signet_challenge;
uint256 hash = h.GetHash();
- memcpy(pchMessageStart, hash.begin(), 4);
+ std::copy_n(hash.begin(), 4, pchMessageStart.begin());
nDefaultPort = 38333;
nPruneAfterHeight = 1000;
diff --git a/src/net.cpp b/src/net.cpp
index af2855932d..d091db74c4 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -730,7 +730,7 @@ V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noex
m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
{
assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes));
- std::copy(std::begin(Params().MessageStart()), std::end(Params().MessageStart()), m_magic_bytes);
+ m_magic_bytes = Params().MessageStart();
LOCK(m_recv_mutex);
Reset();
}
@@ -759,7 +759,7 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
}
// Check start string, network magic
- if (memcmp(hdr.pchMessageStart, m_magic_bytes, CMessageHeader::MESSAGE_START_SIZE) != 0) {
+ if (hdr.pchMessageStart != m_magic_bytes) {
LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
return -1;
}
@@ -1144,7 +1144,7 @@ bool V2Transport::ProcessReceivedKeyBytes() noexcept
// they receive our uniformly random key and garbage, but detecting this case specially
// means we can log it.
static constexpr std::array<uint8_t, 12> MATCH = {'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0};
- static constexpr size_t OFFSET = sizeof(CMessageHeader::MessageStartChars);
+ static constexpr size_t OFFSET = std::tuple_size_v<CMessageHeader::MessageStartChars>;
if (!m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) {
if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) {
LogPrint(BCLog::NET, "V2 transport error: V1 peer with wrong MessageStart %s\n",
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index 70f11be586..2e4e077f18 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -932,7 +932,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
filein >> blk_start >> blk_size;
- if (memcmp(blk_start, GetParams().MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
+ if (blk_start != GetParams().MessageStart()) {
return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
HexStr(blk_start),
HexStr(GetParams().MessageStart()));
diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h
index c79fd2c6a1..4b57637427 100644
--- a/src/node/blockstorage.h
+++ b/src/node/blockstorage.h
@@ -73,7 +73,7 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
/** Size of header written by WriteBlockToDisk before a serialized CBlock */
-static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = CMessageHeader::MESSAGE_START_SIZE + sizeof(unsigned int);
+static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v<CMessageHeader::MessageStartChars> + sizeof(unsigned int);
extern std::atomic_bool fReindex;
diff --git a/src/protocol.cpp b/src/protocol.cpp
index 2105480c72..cb956191e4 100644
--- a/src/protocol.cpp
+++ b/src/protocol.cpp
@@ -92,7 +92,7 @@ const static std::vector<std::string> g_all_net_message_types{
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
{
- memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
+ pchMessageStart = pchMessageStartIn;
// Copy the command name
size_t i = 0;
diff --git a/src/protocol.h b/src/protocol.h
index a7ca0c6f3e..cb7f9666e1 100644
--- a/src/protocol.h
+++ b/src/protocol.h
@@ -13,6 +13,7 @@
#include <uint256.h>
#include <util/time.h>
+#include <array>
#include <cstdint>
#include <limits>
#include <string>
@@ -26,14 +27,13 @@
class CMessageHeader
{
public:
- static constexpr size_t MESSAGE_START_SIZE = 4;
+ using MessageStartChars = std::array<uint8_t, 4>;
static constexpr size_t COMMAND_SIZE = 12;
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
static constexpr size_t CHECKSUM_SIZE = 4;
- static constexpr size_t MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE;
+ static constexpr size_t MESSAGE_SIZE_OFFSET = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE;
static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
- static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
- typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
+ static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
explicit CMessageHeader() = default;
@@ -47,7 +47,7 @@ public:
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
- char pchMessageStart[MESSAGE_START_SIZE]{};
+ MessageStartChars pchMessageStart{};
char pchCommand[COMMAND_SIZE]{};
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
uint8_t pchChecksum[CHECKSUM_SIZE]{};
diff --git a/src/validation.cpp b/src/validation.cpp
index e3a00e4241..4bb648d1e5 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4533,11 +4533,11 @@ void ChainstateManager::LoadExternalBlockFile(
unsigned int nSize = 0;
try {
// locate a header
- unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
+ CMessageHeader::MessageStartChars buf;
blkdat.FindByte(std::byte(params.MessageStart()[0]));
nRewind = blkdat.GetPos() + 1;
blkdat >> buf;
- if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
+ if (buf != params.MessageStart()) {
continue;
}
// read size
diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp
index 0c24920516..0ee39d2b5a 100644
--- a/src/wallet/db.cpp
+++ b/src/wallet/db.cpp
@@ -136,7 +136,7 @@ bool IsSQLiteFile(const fs::path& path)
}
// Check the application id matches our network magic
- return memcmp(Params().MessageStart(), app_id, 4) == 0;
+ return memcmp(Params().MessageStart().data(), app_id, 4) == 0;
}
void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options)
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp
index ecd34bb96a..db9989163d 100644
--- a/src/wallet/sqlite.cpp
+++ b/src/wallet/sqlite.cpp
@@ -193,7 +193,7 @@ bool SQLiteDatabase::Verify(bilingual_str& error)
auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
if (!read_result.has_value()) return false;
uint32_t app_id = static_cast<uint32_t>(read_result.value());
- uint32_t net_magic = ReadBE32(Params().MessageStart());
+ uint32_t net_magic = ReadBE32(Params().MessageStart().data());
if (app_id != net_magic) {
error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
return false;
@@ -324,7 +324,7 @@ void SQLiteDatabase::Open()
}
// Set the application id
- uint32_t app_id = ReadBE32(Params().MessageStart());
+ uint32_t app_id = ReadBE32(Params().MessageStart().data());
SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
"Failed to set the application id");