Merge #9045: Hash P2P messages as they are received instead of at process-time

fe1dc62 Hash P2P messages as they are received instead of at process-time (Matt Corallo)
This commit is contained in:
Pieter Wuille 2016-11-07 14:12:26 -08:00 committed by Alexander Block
parent 3d7bb9cf55
commit 9b8cc0b1ce
3 changed files with 15 additions and 1 deletions

View File

@ -793,12 +793,21 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes)
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024)); vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
} }
hasher.Write((const unsigned char*)pch, nCopy);
memcpy(&vRecv[nDataPos], pch, nCopy); memcpy(&vRecv[nDataPos], pch, nCopy);
nDataPos += nCopy; nDataPos += nCopy;
return nCopy; return nCopy;
} }
const uint256& CNetMessage::GetMessageHash() const
{
assert(complete());
if (data_hash.IsNull())
hasher.Finalize(data_hash.begin());
return data_hash;
}

View File

@ -629,6 +629,9 @@ public:
class CNetMessage { class CNetMessage {
private:
mutable CHash256 hasher;
mutable uint256 data_hash;
public: public:
bool in_data; // parsing header (false) or data (true) bool in_data; // parsing header (false) or data (true)
@ -656,6 +659,8 @@ public:
return (hdr.nMessageSize == nDataPos); return (hdr.nMessageSize == nDataPos);
} }
const uint256& GetMessageHash() const;
void SetVersion(int nVersionIn) void SetVersion(int nVersionIn)
{ {
hdrbuf.SetVersion(nVersionIn); hdrbuf.SetVersion(nVersionIn);

View File

@ -2347,7 +2347,7 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman, std::atomic<bool>& interru
// Checksum // Checksum
CDataStream& vRecv = msg.vRecv; CDataStream& vRecv = msg.vRecv;
uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize); const uint256& hash = msg.GetMessageHash();
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
{ {
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__, LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,