Skip to content

Commit

Permalink
fix: GetHeadersLimit is used for getheaders(2) and headers(2), refact…
Browse files Browse the repository at this point in the history
…or it to accept `compressed` instead of `msg_type`
  • Loading branch information
UdjinM6 authored and PastaPastaPasta committed Sep 10, 2024
1 parent b224f3f commit a6bbaac
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2961,16 +2961,17 @@ 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",
msg_type, pindexLast->nHeight, pfrom.GetId(), peer.m_starting_height);
}
}

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);
Expand Down Expand Up @@ -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));

Expand All @@ -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());
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit a6bbaac

Please sign in to comment.