From 22f4288ff023d68e40acc466875fad511dcdeae0 Mon Sep 17 00:00:00 2001 From: svost Date: Mon, 14 Feb 2022 12:34:30 +0300 Subject: [PATCH] Headers cleanup --- src/bitcoinrpc.cpp | 1 + src/db.cpp | 1 + src/init.cpp | 1 + src/ipcollector.cpp | 5 +- src/irc.cpp | 1 + src/main.cpp | 1 + src/net.cpp | 44 ++++++++++++++++++++ src/net.h | 113 +------------------------------------------------- src/wallet.cpp | 1 + 9 files changed, 56 insertions(+), 112 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index a0342c0..2f38597 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -8,6 +8,7 @@ #include "bitcoinrpc.h" #include "db.h" #include "interface.h" +#include "random.h" #include "sync.h" #include "util.h" #include "wallet.h" diff --git a/src/db.cpp b/src/db.cpp index 78e2495..7ed1d23 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -6,6 +6,7 @@ #include "db.h" #include "util.h" #include "main.h" +#include "random.h" #include #include diff --git a/src/init.cpp b/src/init.cpp index 9ada937..b6d4cdd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -9,6 +9,7 @@ #include "walletdb.h" #include "bitcoinrpc.h" #include "net.h" +#include "random.h" #include "util.h" #include "ipcollector.h" #include "interface.h" diff --git a/src/ipcollector.cpp b/src/ipcollector.cpp index 7d1f061..5d191c2 100644 --- a/src/ipcollector.cpp +++ b/src/ipcollector.cpp @@ -2,14 +2,15 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or https://opensource.org/licenses/mit-license.php. +#include "net.h" +#include "random.h" + #ifdef WIN32 #include #define popen _popen #define pclose _pclose #endif -#include "net.h" - std::string strCollectorCommand; std::string exec(const char* cmd) { diff --git a/src/irc.cpp b/src/irc.cpp index a29874e..822b760 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -6,6 +6,7 @@ #include "irc.h" #include "base58.h" #include "net.h" +#include "random.h" using namespace std; diff --git a/src/main.cpp b/src/main.cpp index 805c043..682eae7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include "interface.h" #include "checkqueue.h" #include "kernel.h" +#include "random.h" #include "wallet.h" #include diff --git a/src/net.cpp b/src/net.cpp index cd89567..f5ef026 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -12,6 +12,7 @@ #include "main.h" #include "miner.h" #include "ntp.h" +#include "random.h" #ifdef WIN32 #include @@ -79,6 +80,16 @@ CCriticalSection cs_vAddedNodes; static CSemaphore *semOutbound = NULL; +inline void RelayInventory(const CInv& inv) +{ + // Put on lists to offer to the other nodes + { + LOCK(cs_vNodes); + for (CNode* pnode : vNodes) + pnode->PushInventory(inv); + } +} + void AddOneShot(string strDest) { LOCK(cs_vOneShots); @@ -503,6 +514,39 @@ void CNode::Cleanup() { } +void CNode::EndMessage() +{ + if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) + { + printf("dropmessages DROPPING SEND MESSAGE\n"); + AbortMessage(); + return; + } + + if (nHeaderStart < 0) { + LEAVE_CRITICAL_SECTION(cs_vSend); + return; + } + + // Set the size + uint32_t nSize = (uint32_t) vSend.size() - nMessageStart; + memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::MESSAGE_SIZE_OFFSET, &nSize, sizeof(nSize)); + + // Set the checksum + uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end()); + uint32_t nChecksum = 0; + memcpy(&nChecksum, &hash, sizeof(nChecksum)); + assert(nMessageStart - nHeaderStart >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum)); + memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::CHECKSUM_OFFSET, &nChecksum, sizeof(nChecksum)); + + if (fDebug) { + printf("(%d bytes)\n", nSize); + } + + nHeaderStart = -1; + nMessageStart = std::numeric_limits::max(); + LEAVE_CRITICAL_SECTION(cs_vSend); +} void CNode::PushVersion() { diff --git a/src/net.h b/src/net.h index fd8174b..8600bed 100644 --- a/src/net.h +++ b/src/net.h @@ -10,7 +10,6 @@ #include "addrman.h" #include "hash.h" #include "streams.h" -#include "random.h" #ifndef WIN32 #include @@ -365,8 +364,6 @@ public: mapAskFor.insert(std::make_pair(nRequestTime, inv)); } - - void BeginMessage(const char* pszCommand) { ENTER_CRITICAL_SECTION(cs_vSend); @@ -392,39 +389,7 @@ public: printf("(aborted)\n"); } - void EndMessage() - { - if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) - { - printf("dropmessages DROPPING SEND MESSAGE\n"); - AbortMessage(); - return; - } - - if (nHeaderStart < 0) { - LEAVE_CRITICAL_SECTION(cs_vSend); - return; - } - - // Set the size - uint32_t nSize = (uint32_t) vSend.size() - nMessageStart; - memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::MESSAGE_SIZE_OFFSET, &nSize, sizeof(nSize)); - - // Set the checksum - uint256 hash = Hash(vSend.begin() + nMessageStart, vSend.end()); - uint32_t nChecksum = 0; - memcpy(&nChecksum, &hash, sizeof(nChecksum)); - assert(nMessageStart - nHeaderStart >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum)); - memcpy((char*)&vSend[nHeaderStart] + CMessageHeader::CHECKSUM_OFFSET, &nChecksum, sizeof(nChecksum)); - - if (fDebug) { - printf("(%d bytes)\n", nSize); - } - - nHeaderStart = -1; - nMessageStart = std::numeric_limits::max(); - LEAVE_CRITICAL_SECTION(cs_vSend); - } + void EndMessage(); void EndMessageAbortIfEmpty() { @@ -437,32 +402,16 @@ public: AbortMessage(); } - - void PushVersion(); - - void PushMessage(const char* pszCommand) - { - try - { - BeginMessage(pszCommand); - EndMessage(); - } - catch (...) - { - AbortMessage(); - throw; - } - } - template void PushMessage(const char* pszCommand, const Args&... args) { try { BeginMessage(pszCommand); - (vSend << ... << args); + if constexpr (sizeof...(Args) > 0) + (vSend << ... << args); EndMessage(); } catch (...) @@ -472,52 +421,6 @@ public: } } - void PushRequest(const char* pszCommand, - void (*fn)(void*, CDataStream&), void* param1) - { - uint256 hashReply; - GetRandBytes((unsigned char*)&hashReply, sizeof(hashReply)); - - { - LOCK(cs_mapRequests); - mapRequests[hashReply] = CRequestTracker(fn, param1); - } - - PushMessage(pszCommand, hashReply); - } - - template - void PushRequest(const char* pszCommand, const T1& a1, - void (*fn)(void*, CDataStream&), void* param1) - { - uint256 hashReply; - GetRandBytes((unsigned char*)&hashReply, sizeof(hashReply)); - - { - LOCK(cs_mapRequests); - mapRequests[hashReply] = CRequestTracker(fn, param1); - } - - PushMessage(pszCommand, hashReply, a1); - } - - template - void PushRequest(const char* pszCommand, const T1& a1, const T2& a2, - void (*fn)(void*, CDataStream&), void* param1) - { - uint256 hashReply; - GetRandBytes((unsigned char*)&hashReply, sizeof(hashReply)); - - { - LOCK(cs_mapRequests); - mapRequests[hashReply] = CRequestTracker(fn, param1); - } - - PushMessage(pszCommand, hashReply, a1, a2); - } - - - void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd); bool IsSubscribed(unsigned int nChannel); void Subscribe(unsigned int nChannel, unsigned int nHops=0); @@ -552,16 +455,6 @@ public: static uint64_t GetTotalBytesSent(); }; -inline void RelayInventory(const CInv& inv) -{ - // Put on lists to offer to the other nodes - { - LOCK(cs_vNodes); - for (CNode* pnode : vNodes) - pnode->PushInventory(inv); - } -} - class CTransaction; void RelayTransaction(const CTransaction& tx, const uint256& hash); void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss); diff --git a/src/wallet.cpp b/src/wallet.cpp index 1602479..71a0988 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -9,6 +9,7 @@ #include "coincontrol.h" #include "crypter.h" #include "kernel.h" +#include "random.h" #include "walletdb.h" #include -- 1.7.1