aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPieter Wuille <[email protected]>2016-11-07 14:12:26 -0800
committerPieter Wuille <[email protected]>2016-11-07 14:19:44 -0800
commit9f554e03ebe5701c1b75ff03b3d6152095c0cad3 (patch)
tree1b71c507fc134b726c5bf39ec48e31396c936568 /src
parentMerge #9077: [qa] Increase wallet-dump RPC timeout (diff)
parentHash P2P messages as they are received instead of at process-time (diff)
downloaddiscoin-9f554e03ebe5701c1b75ff03b3d6152095c0cad3.tar.xz
discoin-9f554e03ebe5701c1b75ff03b3d6152095c0cad3.zip
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)
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp2
-rw-r--r--src/net.cpp9
-rw-r--r--src/net.h5
3 files changed, 15 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp
index e0c614b73..2fb143e6a 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6399,7 +6399,7 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman)
// Checksum
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)
{
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
diff --git a/src/net.cpp b/src/net.cpp
index 4ab8ef98a..e47a8bb16 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -742,12 +742,21 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes)
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
}
+ hasher.Write((const unsigned char*)pch, nCopy);
memcpy(&vRecv[nDataPos], pch, nCopy);
nDataPos += nCopy;
return nCopy;
}
+const uint256& CNetMessage::GetMessageHash() const
+{
+ assert(complete());
+ if (data_hash.IsNull())
+ hasher.Finalize(data_hash.begin());
+ return data_hash;
+}
+
diff --git a/src/net.h b/src/net.h
index 22b80fc50..e712953be 100644
--- a/src/net.h
+++ b/src/net.h
@@ -543,6 +543,9 @@ public:
class CNetMessage {
+private:
+ mutable CHash256 hasher;
+ mutable uint256 data_hash;
public:
bool in_data; // parsing header (false) or data (true)
@@ -570,6 +573,8 @@ public:
return (hdr.nMessageSize == nDataPos);
}
+ const uint256& GetMessageHash() const;
+
void SetVersion(int nVersionIn)
{
hdrbuf.SetVersion(nVersionIn);