mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-04 13:55:23 -05:00
[refactor] Define MessageStartChars as std::array
This commit is contained in:
parent
37e2b01113
commit
9be330b654
10 changed files with 19 additions and 19 deletions
|
@ -90,10 +90,10 @@ void DeserializeDB(Stream& stream, Data&& data, bool fCheckSum = true)
|
||||||
{
|
{
|
||||||
HashVerifier verifier{stream};
|
HashVerifier verifier{stream};
|
||||||
// de-serialize file header (network specific magic number) and ..
|
// de-serialize file header (network specific magic number) and ..
|
||||||
unsigned char pchMsgTmp[4];
|
CMessageHeader::MessageStartChars pchMsgTmp;
|
||||||
verifier >> pchMsgTmp;
|
verifier >> pchMsgTmp;
|
||||||
// ... verify the network matches ours
|
// ... verify the network matches ours
|
||||||
if (memcmp(pchMsgTmp, Params().MessageStart(), sizeof(pchMsgTmp))) {
|
if (pchMsgTmp != Params().MessageStart()) {
|
||||||
throw std::runtime_error{"Invalid network magic number"};
|
throw std::runtime_error{"Invalid network magic number"};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -359,7 +359,7 @@ public:
|
||||||
HashWriter h{};
|
HashWriter h{};
|
||||||
h << consensus.signet_challenge;
|
h << consensus.signet_challenge;
|
||||||
uint256 hash = h.GetHash();
|
uint256 hash = h.GetHash();
|
||||||
memcpy(pchMessageStart, hash.begin(), 4);
|
std::copy_n(hash.begin(), 4, pchMessageStart.begin());
|
||||||
|
|
||||||
nDefaultPort = 38333;
|
nDefaultPort = 38333;
|
||||||
nPruneAfterHeight = 1000;
|
nPruneAfterHeight = 1000;
|
||||||
|
|
|
@ -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)
|
m_node_id(node_id), hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
|
||||||
{
|
{
|
||||||
assert(std::size(Params().MessageStart()) == std::size(m_magic_bytes));
|
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);
|
LOCK(m_recv_mutex);
|
||||||
Reset();
|
Reset();
|
||||||
}
|
}
|
||||||
|
@ -759,7 +759,7 @@ int V1Transport::readHeader(Span<const uint8_t> msg_bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check start string, network magic
|
// 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);
|
LogPrint(BCLog::NET, "Header error: Wrong MessageStart %s received, peer=%d\n", HexStr(hdr.pchMessageStart), m_node_id);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -1144,7 +1144,7 @@ bool V2Transport::ProcessReceivedKeyBytes() noexcept
|
||||||
// they receive our uniformly random key and garbage, but detecting this case specially
|
// they receive our uniformly random key and garbage, but detecting this case specially
|
||||||
// means we can log it.
|
// 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 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 (!m_initiating && m_recv_buffer.size() >= OFFSET + MATCH.size()) {
|
||||||
if (std::equal(MATCH.begin(), MATCH.end(), m_recv_buffer.begin() + OFFSET)) {
|
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",
|
LogPrint(BCLog::NET, "V2 transport error: V1 peer with wrong MessageStart %s\n",
|
||||||
|
|
|
@ -932,7 +932,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
|
||||||
|
|
||||||
filein >> blk_start >> blk_size;
|
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(),
|
return error("%s: Block magic mismatch for %s: %s versus expected %s", __func__, pos.ToString(),
|
||||||
HexStr(blk_start),
|
HexStr(blk_start),
|
||||||
HexStr(GetParams().MessageStart()));
|
HexStr(GetParams().MessageStart()));
|
||||||
|
|
|
@ -73,7 +73,7 @@ static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
|
||||||
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
|
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
|
||||||
|
|
||||||
/** Size of header written by WriteBlockToDisk before a serialized CBlock */
|
/** 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;
|
extern std::atomic_bool fReindex;
|
||||||
|
|
||||||
|
|
|
@ -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)
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
|
||||||
{
|
{
|
||||||
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
pchMessageStart = pchMessageStartIn;
|
||||||
|
|
||||||
// Copy the command name
|
// Copy the command name
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
#include <util/time.h>
|
#include <util/time.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -26,14 +27,13 @@
|
||||||
class CMessageHeader
|
class CMessageHeader
|
||||||
{
|
{
|
||||||
public:
|
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 COMMAND_SIZE = 12;
|
||||||
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
|
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
|
||||||
static constexpr size_t CHECKSUM_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 CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE;
|
||||||
static constexpr size_t HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
|
static constexpr size_t HEADER_SIZE = std::tuple_size_v<MessageStartChars> + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE;
|
||||||
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
|
|
||||||
|
|
||||||
explicit CMessageHeader() = default;
|
explicit CMessageHeader() = default;
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ public:
|
||||||
|
|
||||||
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
|
SERIALIZE_METHODS(CMessageHeader, obj) { READWRITE(obj.pchMessageStart, obj.pchCommand, obj.nMessageSize, obj.pchChecksum); }
|
||||||
|
|
||||||
char pchMessageStart[MESSAGE_START_SIZE]{};
|
MessageStartChars pchMessageStart{};
|
||||||
char pchCommand[COMMAND_SIZE]{};
|
char pchCommand[COMMAND_SIZE]{};
|
||||||
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
|
uint32_t nMessageSize{std::numeric_limits<uint32_t>::max()};
|
||||||
uint8_t pchChecksum[CHECKSUM_SIZE]{};
|
uint8_t pchChecksum[CHECKSUM_SIZE]{};
|
||||||
|
|
|
@ -4533,11 +4533,11 @@ void ChainstateManager::LoadExternalBlockFile(
|
||||||
unsigned int nSize = 0;
|
unsigned int nSize = 0;
|
||||||
try {
|
try {
|
||||||
// locate a header
|
// locate a header
|
||||||
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
|
CMessageHeader::MessageStartChars buf;
|
||||||
blkdat.FindByte(std::byte(params.MessageStart()[0]));
|
blkdat.FindByte(std::byte(params.MessageStart()[0]));
|
||||||
nRewind = blkdat.GetPos() + 1;
|
nRewind = blkdat.GetPos() + 1;
|
||||||
blkdat >> buf;
|
blkdat >> buf;
|
||||||
if (memcmp(buf, params.MessageStart(), CMessageHeader::MESSAGE_START_SIZE)) {
|
if (buf != params.MessageStart()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// read size
|
// read size
|
||||||
|
|
|
@ -136,7 +136,7 @@ bool IsSQLiteFile(const fs::path& path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the application id matches our network magic
|
// 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)
|
void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options)
|
||||||
|
|
|
@ -193,7 +193,7 @@ bool SQLiteDatabase::Verify(bilingual_str& error)
|
||||||
auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
|
auto read_result = ReadPragmaInteger(m_db, "application_id", "the application id", error);
|
||||||
if (!read_result.has_value()) return false;
|
if (!read_result.has_value()) return false;
|
||||||
uint32_t app_id = static_cast<uint32_t>(read_result.value());
|
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) {
|
if (app_id != net_magic) {
|
||||||
error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
|
error = strprintf(_("SQLiteDatabase: Unexpected application id. Expected %u, got %u"), net_magic, app_id);
|
||||||
return false;
|
return false;
|
||||||
|
@ -324,7 +324,7 @@ void SQLiteDatabase::Open()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the application id
|
// 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)),
|
SetPragma(m_db, "application_id", strprintf("%d", static_cast<int32_t>(app_id)),
|
||||||
"Failed to set the application id");
|
"Failed to set the application id");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue