Skip to content

Commit 9be330b

Browse files
committed
[refactor] Define MessageStartChars as std::array
1 parent 37e2b01 commit 9be330b

File tree

10 files changed

+19
-19
lines changed

10 files changed

+19
-19
lines changed

src/addrdb.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ void DeserializeDB(Stream& stream, Data&& data, bool fCheckSum = true)
9090
{
9191
HashVerifier verifier{stream};
9292
// de-serialize file header (network specific magic number) and ..
93-
unsigned char pchMsgTmp[4];
93+
CMessageHeader::MessageStartChars pchMsgTmp;
9494
verifier >> pchMsgTmp;
9595
// ... verify the network matches ours
96-
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
96+
if (pchMsgTmp != Params().MessageStart()) {
9797
throw std::runtime_error{"Invalid network magic number"};
9898
}
9999

src/kernel/chainparams.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class SigNetParams : public CChainParams {
359359
HashWriter h{};
360360
h << consensus.signet_challenge;
361361
uint256 hash = h.GetHash();
362-
memcpy(pchMessageStart, hash.begin(), 4);
362+
std::copy_n(hash.begin(), 4, pchMessageStart.begin());
363363

364364
nDefaultPort = 38333;
365365
nPruneAfterHeight = 1000;

src/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ V1Transport::V1Transport(const NodeId node_id, int nTypeIn, int nVersionIn) noex
730730
m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
731731
{
732732
assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes));
733-
std::copy(std::begin(Params().MessageStart()), std::end(Params().MessageStart()), m_magic_bytes);
733+
m_magic_bytes = Params().MessageStart();
734734
LOCK(m_recv_mutex);
735735
Reset();
736736
}
@@ -759,7 +759,7 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
759759
}
760760

761761
// Check start string, network magic
762-
if (memcmp(hdr.pchMessageStart, m_magic_bytes, CMessageHeader::MESSAGE_START_SIZE) != 0) {
762+
if (hdr.pchMessageStart != m_magic_bytes) {
763763
LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
764764
return -1;
765765
}
@@ -1144,7 +1144,7 @@ bool V2Transport::ProcessReceivedKeyBytes() noexcept
11441144
// they receive our uniformly random key and garbage, but detecting this case specially
11451145
// means we can log it.
11461146
static constexpr std::array<uint8_t, 12> MATCH = {'v', 'e', 'r', 's', 'i', 'o', 'n', 0, 0, 0, 0, 0};
1147-
static constexpr size_t OFFSET = sizeof(CMessageHeader::MessageStartChars);
1147+
static constexpr size_t OFFSET = std::tuple_size_v<CMessageHeader::MessageStartChars>;
11481148
if (!m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) {
11491149
if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) {
11501150
LogPrint(BCLog::NET, "V2 transport error: V1 peer with wrong MessageStart %s\n",

src/node/blockstorage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
932932

933933
filein >> blk_start >> blk_size;
934934

935-
if (memcmp(blk_start, GetParams().MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
935+
if (blk_start != GetParams().MessageStart()) {
936936
return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
937937
HexStr(blk_start),
938938
HexStr(GetParams().MessageStart()));

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
7373
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
7474

7575
/** Size of header written by WriteBlockToDisk before a serialized CBlock */
76-
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = CMessageHeader::MESSAGE_START_SIZE + sizeof(unsigned int);
76+
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = std::tuple_size_v<CMessageHeader::MessageStartChars> + sizeof(unsigned int);
7777

7878
extern std::atomic_bool fReindex;
7979

src/protocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const static std::vector<std::string> g_all_net_message_types{
9292

9393
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
9494
{
95-
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
95+
pchMessageStart = pchMessageStartIn;
9696

9797
// Copy the command name
9898
size_t i = 0;

src/protocol.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <uint256.h>
1414
#include <util/time.h>
1515

16+
#include <array>
1617
#include <cstdint>
1718
#include <limits>
1819
#include <string>
@@ -26,14 +27,13 @@
2627
class CMessageHeader
2728
{
2829
public:
29-
static constexpr size_t MESSAGE_START_SIZE = 4;
30+
using MessageStartChars = std::array<uint8_t, 4>;
3031
static constexpr size_t COMMAND_SIZE = 12;
3132
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
3233
static constexpr size_t CHECKSUM_SIZE = 4;
33-
static constexpr size_t MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE;
34+
static constexpr size_t MESSAGE_SIZE_OFFSET = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE;
3435
static constexpr size_t CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
35-
static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
36-
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
36+
static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
3737

3838
explicit CMessageHeader() = default;
3939

@@ -47,7 +47,7 @@ class CMessageHeader
4747

4848
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
4949

50-
char pchMessageStart[MESSAGE_START_SIZE]{};
50+
MessageStartChars pchMessageStart{};
5151
char pchCommand[COMMAND_SIZE]{};
5252
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
5353
uint8_t pchChecksum[CHECKSUM_SIZE]{};

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,11 +4533,11 @@ void ChainstateManager::LoadExternalBlockFile(
45334533
unsigned int nSize = 0;
45344534
try {
45354535
// locate a header
4536-
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4536+
CMessageHeader::MessageStartChars buf;
45374537
blkdat.FindByte(std::byte(params.MessageStart()[0]));
45384538
nRewind = blkdat.GetPos() + 1;
45394539
blkdat >> buf;
4540-
if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
4540+
if (buf != params.MessageStart()) {
45414541
continue;
45424542
}
45434543
// read size

src/wallet/db.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ bool IsSQLiteFile(const fs::path& path)
136136
}
137137

138138
// Check the application id matches our network magic
139-
return memcmp(Params().MessageStart(), app_id, 4) == 0;
139+
return memcmp(Params().MessageStart().data(), app_id, 4) == 0;
140140
}
141141

142142
void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options)

src/wallet/sqlite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ bool SQLiteDatabase::Verify(bilingual_str& error)
193193
auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
194194
if (!read_result.has_value()) return false;
195195
uint32_t app_id = static_cast<uint32_t>(read_result.value());
196-
uint32_t net_magic = ReadBE32(Params().MessageStart());
196+
uint32_t net_magic = ReadBE32(Params().MessageStart().data());
197197
if (app_id != net_magic) {
198198
error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
199199
return false;
@@ -324,7 +324,7 @@ void SQLiteDatabase::Open()
324324
}
325325

326326
// Set the application id
327-
uint32_t app_id = ReadBE32(Params().MessageStart());
327+
uint32_t app_id = ReadBE32(Params().MessageStart().data());
328328
SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
329329
"Failed to set the application id");
330330

0 commit comments

Comments
 (0)