From e7fea1d50b9f24fb39fc00be21ef1937f4da0ef5 Mon Sep 17 00:00:00 2001 From: Pablo Greco Date: Wed, 15 Feb 2023 13:12:51 -0300 Subject: [PATCH] Fix underflow when blocks are ahead of headers --- src/net_processing.cpp | 4 ++-- src/node/blockstorage.cpp | 2 +- src/node/blockstorage.h | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index b3eb795d651..c56cfc154a6 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -2015,7 +2015,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer, // the received headers. We can still get ahead by up to a single maximum-sized // headers message here, but never further, so that's fine. if (pindexBestHeader) { - uint64_t headers_ahead = pindexBestHeader->nHeight - m_chainman.ActiveHeight(); + int64_t headers_ahead = pindexBestHeader->nHeight - m_chainman.ActiveHeight(); bool too_far_ahead = fTrimHeaders && (headers_ahead >= nHeaderDownloadBuffer); if (too_far_ahead) { LOCK(cs_main); @@ -4532,7 +4532,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) if (pindexBestHeader == nullptr) pindexBestHeader = m_chainman.ActiveChain().Tip(); bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->IsAddrFetchConn()); // Download if this is a nice peer, or we have no nice peers and this one might do. - uint64_t headers_ahead = pindexBestHeader->nHeight - m_chainman.ActiveHeight(); + int64_t headers_ahead = pindexBestHeader->nHeight - m_chainman.ActiveHeight(); // ELEMENTS: Only download if our headers aren't "too far ahead" of our blocks. bool got_enough_headers = fTrimHeaders && (headers_ahead >= nHeaderDownloadBuffer); if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex && !got_enough_headers) { diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index 5a2744d5537..2e40962f123 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -27,7 +27,7 @@ bool fPruneMode = false; uint64_t nPruneTarget = 0; bool fTrimHeaders = false; uint64_t nMustKeepFullHeaders = std::numeric_limits::max(); -uint64_t nHeaderDownloadBuffer = std::numeric_limits::max(); +int64_t nHeaderDownloadBuffer = std::numeric_limits::max(); // TODO make namespace { RecursiveMutex cs_LastBlockFile; diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index df582b06d1b..0a632eae0fe 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -50,7 +50,7 @@ extern bool fTrimHeaders; extern uint64_t nMustKeepFullHeaders; /** Target number of headers to download beyond the blocks we have. */ // NOTE: this currently only operates when in header trim mode, but it's really independent of that. -extern uint64_t nHeaderDownloadBuffer; +extern int64_t nHeaderDownloadBuffer; //! Check whether the block associated with this index entry is pruned or not. bool IsBlockPruned(const CBlockIndex* pblockindex);