Skip to content

Commit

Permalink
Move MESSAGE_START_SIZE into CMessageHeader
Browse files Browse the repository at this point in the history
Also move the enum to the top, and remove a deceptive TODO
comment.

Adaptation of btc@2c09a5209ab00573a2422e1e65c437a6e2f59624
  • Loading branch information
furszy committed Dec 11, 2021
1 parent 3296b9e commit 800ed43
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter

msg.SetVersion(pfrom->GetRecvVersion());
// Scan for message start
if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
LogPrint(BCLog::NET, "PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->GetId());
pfrom->fDisconnect = true;
return false;
Expand Down
5 changes: 1 addition & 4 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include <stdint.h>
#include <string>

#define MESSAGE_START_SIZE 4

/** Message header.
* (4) message start.
* (12) command.
Expand All @@ -32,6 +30,7 @@
class CMessageHeader
{
public:
static constexpr size_t MESSAGE_START_SIZE = 4;
static constexpr size_t COMMAND_SIZE = 12;
static constexpr size_t MESSAGE_SIZE_SIZE = 4;
static constexpr size_t CHECKSUM_SIZE = 4;
Expand All @@ -52,8 +51,6 @@ class CMessageHeader

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

// TODO: make private (improves encapsulation)
public:
char pchMessageStart[MESSAGE_START_SIZE];
char pchCommand[COMMAND_SIZE];
uint32_t nMessageSize;
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3918,11 +3918,11 @@ bool LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
unsigned int nSize = 0;
try {
// locate a header
unsigned char buf[MESSAGE_START_SIZE];
unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
blkdat.FindByte(Params().MessageStart()[0]);
nRewind = blkdat.GetPos()+1;
blkdat >> buf;
if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
if (memcmp(buf, Params().MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
continue;
// read size
blkdat >> nSize;
Expand Down

0 comments on commit 800ed43

Please sign in to comment.