make CMessageHeader a dumb storage class

It shouldn't know or care about bitcoind's chain param selection
This commit is contained in:
Cory Fields 2014-10-27 20:24:31 -04:00
parent 8b298ca7d7
commit eec37136fd
6 changed files with 17 additions and 18 deletions

View file

@ -14,8 +14,6 @@
#include <vector> #include <vector>
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
struct CDNSSeedData { struct CDNSSeedData {
std::string name, host; std::string name, host;
CDNSSeedData(const std::string &strName, const std::string &strHost) : name(strName), host(strHost) {} CDNSSeedData(const std::string &strName, const std::string &strHost) : name(strName), host(strHost) {}
@ -42,7 +40,7 @@ public:
}; };
const uint256& HashGenesisBlock() const { return hashGenesisBlock; } const uint256& HashGenesisBlock() const { return hashGenesisBlock; }
const MessageStartChars& MessageStart() const { return pchMessageStart; } const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; } const std::vector<unsigned char>& AlertKey() const { return vAlertPubKey; }
int GetDefaultPort() const { return nDefaultPort; } int GetDefaultPort() const { return nDefaultPort; }
const arith_uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; } const arith_uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; }
@ -83,7 +81,7 @@ protected:
CChainParams() {} CChainParams() {}
uint256 hashGenesisBlock; uint256 hashGenesisBlock;
MessageStartChars pchMessageStart; CMessageHeader::MessageStartChars pchMessageStart;
//! Raw pub key bytes for the broadcast alert signing key. //! Raw pub key bytes for the broadcast alert signing key.
std::vector<unsigned char> vAlertPubKey; std::vector<unsigned char> vAlertPubKey;
int nDefaultPort; int nDefaultPort;

View file

@ -4307,7 +4307,7 @@ bool ProcessMessages(CNode* pfrom)
// Read header // Read header
CMessageHeader& hdr = msg.hdr; CMessageHeader& hdr = msg.hdr;
if (!hdr.IsValid()) if (!hdr.IsValid(Params().MessageStart()))
{ {
LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id); LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
continue; continue;

View file

@ -509,7 +509,7 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes)
// get current incomplete message, or create a new one // get current incomplete message, or create a new one
if (vRecvMsg.empty() || if (vRecvMsg.empty() ||
vRecvMsg.back().complete()) vRecvMsg.back().complete())
vRecvMsg.push_back(CNetMessage(SER_NETWORK, nRecvVersion)); vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, nRecvVersion));
CNetMessage& msg = vRecvMsg.back(); CNetMessage& msg = vRecvMsg.back();
@ -1970,7 +1970,7 @@ void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSen
{ {
ENTER_CRITICAL_SECTION(cs_vSend); ENTER_CRITICAL_SECTION(cs_vSend);
assert(ssSend.size() == 0); assert(ssSend.size() == 0);
ssSend << CMessageHeader(pszCommand, 0); ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0);
LogPrint("net", "sending: %s ", SanitizeString(pszCommand)); LogPrint("net", "sending: %s ", SanitizeString(pszCommand));
} }

View file

@ -183,7 +183,7 @@ public:
int64_t nTime; // time (in microseconds) of message receipt. int64_t nTime; // time (in microseconds) of message receipt.
CNetMessage(int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn) { CNetMessage(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
hdrbuf.resize(24); hdrbuf.resize(24);
in_data = false; in_data = false;
nHdrPos = 0; nHdrPos = 0;

View file

@ -5,7 +5,6 @@
#include "protocol.h" #include "protocol.h"
#include "chainparams.h"
#include "util.h" #include "util.h"
#include "utilstrencodings.h" #include "utilstrencodings.h"
@ -21,17 +20,17 @@ static const char* ppszTypeName[] =
"filtered block" "filtered block"
}; };
CMessageHeader::CMessageHeader() CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
{ {
memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE); memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
memset(pchCommand, 0, sizeof(pchCommand)); memset(pchCommand, 0, sizeof(pchCommand));
nMessageSize = -1; nMessageSize = -1;
nChecksum = 0; nChecksum = 0;
} }
CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
{ {
memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE); memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
memset(pchCommand, 0, sizeof(pchCommand)); memset(pchCommand, 0, sizeof(pchCommand));
strncpy(pchCommand, pszCommand, COMMAND_SIZE); strncpy(pchCommand, pszCommand, COMMAND_SIZE);
nMessageSize = nMessageSizeIn; nMessageSize = nMessageSizeIn;
@ -43,10 +42,10 @@ std::string CMessageHeader::GetCommand() const
return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE)); return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
} }
bool CMessageHeader::IsValid() const bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
{ {
// Check start string // Check start string
if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
return false; return false;
// Check the command string for errors // Check the command string for errors

View file

@ -29,11 +29,13 @@
class CMessageHeader class CMessageHeader
{ {
public: public:
CMessageHeader(); typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
CMessageHeader(const MessageStartChars& pchMessageStartIn);
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
std::string GetCommand() const; std::string GetCommand() const;
bool IsValid() const; bool IsValid(const MessageStartChars& messageStart) const;
ADD_SERIALIZE_METHODS; ADD_SERIALIZE_METHODS;