From a6bbaacfaa50a88f4f9d5a58097b6f5fe7c9ee55 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Tue, 10 Sep 2024 20:31:20 +0300 Subject: [PATCH] fix: GetHeadersLimit is used for getheaders(2) and headers(2), refactor it to accept `compressed` instead of `msg_type` --- src/net_processing.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index d38b972fdc..d410f23248 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1039,9 +1039,9 @@ static bool IsLimitedPeer(const Peer& peer) } /** Get maximum number of headers that can be included in one batch */ -static uint16_t GetHeadersLimit(const CNode& pfrom, const std::string& msg_type) +static uint16_t GetHeadersLimit(const CNode& pfrom, bool compressed) { - if (pfrom.GetCommonVersion() >= INCREASE_MAX_HEADERS2_VERSION && msg_type == NetMsgType::GETHEADERS2) { + if (pfrom.GetCommonVersion() >= INCREASE_MAX_HEADERS2_VERSION && compressed) { return MAX_HEADERS_COMPRESSED_RESULT; } return MAX_HEADERS_UNCOMPRESSED_RESULT; @@ -2961,8 +2961,9 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer, } // Consider fetching more headers. - std::string msg_type = UsesCompressedHeaders(peer) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS; - if (nCount == GetHeadersLimit(pfrom, msg_type)) { + const bool uses_compressed = UsesCompressedHeaders(peer); + const std::string msg_type = uses_compressed ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS; + if (nCount == GetHeadersLimit(pfrom, uses_compressed)) { // Headers message had its maximum size; the peer may have more headers. if (MaybeSendGetHeaders(pfrom, msg_type, m_chainman.ActiveChain().GetLocator(pindexLast), peer)) { LogPrint(BCLog::NET, "more %s (%d) to end to peer=%d (startheight:%d)\n", @@ -2970,7 +2971,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer, } } - UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == GetHeadersLimit(pfrom, msg_type)); + UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == GetHeadersLimit(pfrom, uses_compressed)); // Consider immediately downloading blocks. HeadersDirectFetchBlocks(pfrom, peer, pindexLast); @@ -4064,8 +4065,8 @@ void PeerManagerImpl::ProcessMessage( pindex = m_chainman.ActiveChain().Next(pindex); } - const auto send_headers = [this /* for m_connman */, &hashStop, &pindex, &nodestate, &pfrom, &msgMaker](auto msg_type, auto& v_headers, auto callback) { - int nLimit = GetHeadersLimit(pfrom, msg_type); + const auto send_headers = [this /* for m_connman */, &hashStop, &pindex, &nodestate, &pfrom, &msgMaker](auto msg_type_internal, auto& v_headers, auto callback) { + int nLimit = GetHeadersLimit(pfrom, msg_type_internal == NetMsgType::HEADERS2); for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex)) { v_headers.push_back(callback(pindex)); @@ -4085,7 +4086,7 @@ void PeerManagerImpl::ProcessMessage( // will re-announce the new block via headers (or compact blocks again) // in the SendMessages logic. nodestate->pindexBestHeaderSent = pindex ? pindex : m_chainman.ActiveChain().Tip(); - m_connman.PushMessage(&pfrom, msgMaker.Make(msg_type, v_headers)); + m_connman.PushMessage(&pfrom, msgMaker.Make(msg_type_internal, v_headers)); }; LogPrint(BCLog::NET, "%s %d to %s from peer=%d\n", msg_type, (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom.GetId()); @@ -4573,7 +4574,7 @@ void PeerManagerImpl::ProcessMessage( // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks. unsigned int nCount = ReadCompactSize(vRecv); - if (nCount > GetHeadersLimit(pfrom, msg_type)) { + if (nCount > GetHeadersLimit(pfrom, msg_type == NetMsgType::HEADERS2)) { Misbehaving(pfrom.GetId(), 20, strprintf("headers message size = %u", nCount)); return; }