From eec37136fd0f6ab80772380d8f047d98743f31f5 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Mon, 27 Oct 2014 20:24:31 -0400 Subject: [PATCH] make CMessageHeader a dumb storage class It shouldn't know or care about bitcoind's chain param selection --- src/chainparams.h | 6 ++---- src/main.cpp | 2 +- src/net.cpp | 4 ++-- src/net.h | 2 +- src/protocol.cpp | 13 ++++++------- src/protocol.h | 8 +++++--- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/chainparams.h b/src/chainparams.h index 86b84df6677..78b575d8f37 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -14,8 +14,6 @@ #include -typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; - struct CDNSSeedData { std::string name, host; 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 MessageStartChars& MessageStart() const { return pchMessageStart; } + const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; } const std::vector& AlertKey() const { return vAlertPubKey; } int GetDefaultPort() const { return nDefaultPort; } const arith_uint256& ProofOfWorkLimit() const { return bnProofOfWorkLimit; } @@ -83,7 +81,7 @@ protected: CChainParams() {} uint256 hashGenesisBlock; - MessageStartChars pchMessageStart; + CMessageHeader::MessageStartChars pchMessageStart; //! Raw pub key bytes for the broadcast alert signing key. std::vector vAlertPubKey; int nDefaultPort; diff --git a/src/main.cpp b/src/main.cpp index 34b4c51d5f8..c9a2f34a4d4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4307,7 +4307,7 @@ bool ProcessMessages(CNode* pfrom) // Read header 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); continue; diff --git a/src/net.cpp b/src/net.cpp index 3c3666615e1..36a5b6fb6b3 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -509,7 +509,7 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes) // get current incomplete message, or create a new one if (vRecvMsg.empty() || vRecvMsg.back().complete()) - vRecvMsg.push_back(CNetMessage(SER_NETWORK, nRecvVersion)); + vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, nRecvVersion)); CNetMessage& msg = vRecvMsg.back(); @@ -1970,7 +1970,7 @@ void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSen { ENTER_CRITICAL_SECTION(cs_vSend); assert(ssSend.size() == 0); - ssSend << CMessageHeader(pszCommand, 0); + ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0); LogPrint("net", "sending: %s ", SanitizeString(pszCommand)); } diff --git a/src/net.h b/src/net.h index b78665b5c62..0a9fbb38c0c 100644 --- a/src/net.h +++ b/src/net.h @@ -183,7 +183,7 @@ public: 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); in_data = false; nHdrPos = 0; diff --git a/src/protocol.cpp b/src/protocol.cpp index 74ac706d60c..568580a595a 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -5,7 +5,6 @@ #include "protocol.h" -#include "chainparams.h" #include "util.h" #include "utilstrencodings.h" @@ -21,17 +20,17 @@ static const char* ppszTypeName[] = "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)); nMessageSize = -1; 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)); strncpy(pchCommand, pszCommand, COMMAND_SIZE); nMessageSize = nMessageSizeIn; @@ -43,10 +42,10 @@ std::string CMessageHeader::GetCommand() const return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE)); } -bool CMessageHeader::IsValid() const +bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const { // Check start string - if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) + if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0) return false; // Check the command string for errors diff --git a/src/protocol.h b/src/protocol.h index f8394ce52fd..e838c0d3638 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -29,11 +29,13 @@ class CMessageHeader { public: - CMessageHeader(); - CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn); + typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; + + CMessageHeader(const MessageStartChars& pchMessageStartIn); + CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn); std::string GetCommand() const; - bool IsValid() const; + bool IsValid(const MessageStartChars& messageStart) const; ADD_SERIALIZE_METHODS;