diff -uNr a/bitcoin/manifest b/bitcoin/manifest --- a/bitcoin/manifest 8e13fc151b01d30d5551cf8b587081009982dd47302aa33b98bdd65d75f9ab46a9f414cca26bdaa031db3283c907c0e19bfb83e233217f8e1b0ef88a05cfef91 +++ b/bitcoin/manifest 5c6656b37a48c56e035188ad4ed3770cf897bb7457c79ef0646dd29cafa19745e684f2d2930636a78529a127c6f0c46032654764b394035564cc1319d96262f6 @@ -46,3 +46,4 @@ 735223 bitcoin_response_size_limits_1 jfw Simple adjustments to prepare the code for more significant ones to improve response size limiting at the source. CBlockLocator::GetBlockIndex never returns NULL unless something is very wrong. Don't log when the getblocks response stops at the requested hash: it's uninteresting since that hash was already logged just before, and this allows simplifying loop bounds. Differentiate getheaders implementation between the null locator (single block) case and the normal loop. Always log starting heights for getblocks/getheaders rather than punting to -1 just because a non-null pointer wasn't immediately available. 735223 bitcoin_response_size_limits_2 jfw getblocks: switch from nonsensical block count based limit (which didn't really limit at all) and heavy-weight yet still inexact bytes based limit to a simple conservative bound. getheaders: switch from nonsensical limit (which didn't really limit at all) to a sending buffer size derived limit to prevent flooding or at least give peer the chance to do so. 735324 bitcoin_pushmessage_cleanup_1 jfw Tame the PushMessage template explosion by pushing CDataStream serialization out to callers. Permanently disable undocumented -allowreceivebyip and otherwise dead code turned up when auditing PushMessage callers, namely generic network request tracking and publish/subscribe infrastructure. Simplify initialization of vSend/vRecv fields: type (since SER_NETWORK is the default) and version (since the indicated flag day is long past; it wasn't quite clear to remove the field altogether). Add unit test for assumption noted in one of the PushMessage calls. +735336 bitcoin_pushmessage_cleanup_2 jfw With PushMessage deduplicated, we can integrate BeginMessage, AbortMessage and EndMessage for major simplification. While we're here, fix those infernal torn send/receive debug messages with their redundant timestamps and no clue as to sender or recipient. diff -uNr a/bitcoin/src/main.cpp b/bitcoin/src/main.cpp --- a/bitcoin/src/main.cpp 5b796065e6bdfce53478af230763776d8342f0da923744cc8f76b9802ad8d01f6bdde0149acc784491130d3033662c9d1602bde39ec213c958cd788cdc3ffef6 +++ b/bitcoin/src/main.cpp 9f194a61889c5bbd661aa84cb485ade9fc6705752e928b9230e9293851a7449d03d9859ca226f3f2c485ec8b506f11436d60123038d20551c63acf1546436284 @@ -1681,8 +1681,7 @@ { RandAddSeedPerfmon(); if (fDebug) { - printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); - printf("received: %s (%d bytes)\n", strCommand.c_str(), vRecv.size()); + printf("recv %s %s (%d B)\n", pfrom->addr.ToString().c_str(), strCommand.c_str(), vRecv.size()); } if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { diff -uNr a/bitcoin/src/net.cpp b/bitcoin/src/net.cpp --- a/bitcoin/src/net.cpp b5ef3c960b24f41ac4cac25eef4dd17f6da3b10d7d404b8048dd30519e994908b11c64f94115cd55a732164c15c4b62ff5f454fe771a5843a7474f239d6f87ee +++ b/bitcoin/src/net.cpp 2a3a5df4dbdaa976df3229923e229c0c1ffbc2d654b16b3953c5fcc4d8330ae2df5b66a3431d42891ec214710bbdede093c57ac0988b255ba092cafbcae94882 @@ -400,8 +400,6 @@ fDisconnect = true; if (hSocket != INVALID_SOCKET) { - if (fDebug) - printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); printf("disconnecting node %s\n", addr.ToString().c_str()); closesocket(hSocket); hSocket = INVALID_SOCKET; diff -uNr a/bitcoin/src/net.h b/bitcoin/src/net.h --- a/bitcoin/src/net.h 446bb5f05b776039fd25680c8b46d8aada9447be55d56d882dbf52a5c1d527b02241a2fdf9985836e344128dbcf6fb3f118afac37b80109867cb05856906a598 +++ b/bitcoin/src/net.h 24f6a340b277bf9b2cc6e13e3d5d65d6324ddd538e121a5fa05dd4c31aeb5bca41c6eaf114a483e1943c7b6f9448afdd174806e81740ad9fa92b8515fd26bbac @@ -80,8 +80,6 @@ int64 nLastRecv; int64 nLastSendEmpty; int64 nTimeConnected; - unsigned int nHeaderStart; - unsigned int nMessageStart; CAddress addr; int nVersion; std::string strSubVer; @@ -127,8 +125,6 @@ nLastRecv = 0; nLastSendEmpty = GetTime(); nTimeConnected = GetTime(); - nHeaderStart = -1; - nMessageStart = -1; addr = addrIn; nVersion = 0; strSubVer = ""; @@ -238,81 +234,6 @@ return CDataStream(SER_NETWORK, vSend.GetVersion()); } - void BeginMessage(const char* pszCommand) - { - ENTER_CRITICAL_SECTION(cs_vSend); - if (nHeaderStart != -1) - AbortMessage(); - nHeaderStart = vSend.size(); - vSend << CMessageHeader(pszCommand, 0); - nMessageStart = vSend.size(); - if (fDebug) { - printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); - printf("sending: %s ", pszCommand); - } - } - - void AbortMessage() - { - if (nHeaderStart == -1) - return; - vSend.resize(nHeaderStart); - nHeaderStart = -1; - nMessageStart = -1; - LEAVE_CRITICAL_SECTION(cs_vSend); - - if (fDebug) - printf("(aborted)\n"); - } - - void EndMessage() - { - if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) - { - printf("dropmessages DROPPING SEND MESSAGE\n"); - AbortMessage(); - return; - } - - if (nHeaderStart == -1) - return; - - // Set the size - unsigned int nSize = vSend.size() - nMessageStart; - memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nMessageSize), &nSize, sizeof(nSize)); - - // Set the checksum - if (vSend.GetVersion() >= 209) - { - uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end()); - unsigned int nChecksum = 0; - memcpy(&nChecksum, &hash, sizeof(nChecksum)); - assert(nMessageStart - nHeaderStart >= offsetof(CMessageHeader, nChecksum) + sizeof(nChecksum)); - memcpy((char*)&vSend[nHeaderStart] + offsetof(CMessageHeader, nChecksum), &nChecksum, sizeof(nChecksum)); - } - - if (fDebug) { - printf("(%d bytes)\n", nSize); - } - - nHeaderStart = -1; - nMessageStart = -1; - LEAVE_CRITICAL_SECTION(cs_vSend); - } - - void EndMessageAbortIfEmpty() - { - if (nHeaderStart == -1) - return; - int nSize = vSend.size() - nMessageStart; - if (nSize > 0) - EndMessage(); - else - AbortMessage(); - } - - - void PushVersion() { /// when NTP implemented, change to just nTime = GetAdjustedTime() @@ -331,9 +252,6 @@ << nBestHeight); } - - - void PushMessage(const char* pszCommand) { PushMessage(pszCommand, SendingStream()); @@ -341,15 +259,28 @@ void PushMessage(const char* pszCommand, const CDataStream& payload) { + if (fDebug) + printf("send %s %s (%d B)\n", addr.ToString().c_str(), pszCommand, payload.size()); + if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) + { + printf("dropmessages DROPPING SEND MESSAGE %s %s\n", addr.ToString().c_str(), pszCommand); + return; + } + SCOPED_LOCK(cs_vSend); + unsigned int nHeaderStart = vSend.size(); try { - BeginMessage(pszCommand); + CMessageHeader header(pszCommand, payload.size()); + uint256 hash = Hash(payload.begin(), payload.end()); + memcpy(&(header.nChecksum), &hash, sizeof header.nChecksum); + // If the peer speaks an old protocol version (<209) as indicated by vSend.nVersion, header.nChecksum is excluded from serialization (see CMessageHeader); no need for special handling here as in prior revisions. + vSend << header; vSend += payload; - EndMessage(); } catch (...) { - AbortMessage(); + vSend.resize(nHeaderStart); + printf("aborted send %s %s\n", addr.ToString().c_str(), pszCommand); throw; } }