Make ProcessNextHeaders support empty headers message

Remove the special case in ProcessHeadersMessage dealing with empty headers
messages, and instead let the HeadersSync class deal with it correctly.
This commit is contained in:
Pieter Wuille
2022-08-23 17:46:53 -04:00
parent 4e7ac7b94d
commit ab52fb4e95
2 changed files with 5 additions and 27 deletions

View File

@@ -70,9 +70,6 @@ HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(
{
ProcessingResult ret;
Assume(!headers.empty());
if (headers.empty()) return ret;
Assume(m_download_state != State::FINAL);
if (m_download_state == State::FINAL) return ret;
@@ -139,13 +136,11 @@ HeadersSyncState::ProcessingResult HeadersSyncState::ProcessNextHeaders(
bool HeadersSyncState::ValidateAndStoreHeadersCommitments(const std::vector<CBlockHeader>& headers)
{
// The caller should not give us an empty set of headers.
Assume(headers.size() > 0);
if (headers.size() == 0) return true;
Assume(m_download_state == State::PRESYNC);
if (m_download_state != State::PRESYNC) return false;
if (headers.size() == 0) return true;
if (headers[0].hashPrevBlock != m_last_header_received.GetHash()) {
// Somehow our peer gave us a header that doesn't connect.
// This might be benign -- perhaps our peer reorged away from the chain

View File

@@ -2726,20 +2726,6 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
{
size_t nCount = headers.size();
if (nCount == 0) {
// Nothing interesting. Stop asking this peers for more headers.
// If we were in the middle of headers sync, receiving an empty headers
// message suggests that the peer suddenly has nothing to give us
// (perhaps it reorged to our chain). Clear download state for this peer.
LOCK(peer.m_headers_sync_mutex);
if (peer.m_headers_sync) {
peer.m_headers_sync.reset(nullptr);
LOCK(m_headers_presync_mutex);
m_headers_presync_stats.erase(pfrom.GetId());
}
return;
}
// Before we do any processing, make sure these pass basic sanity checks.
// We'll rely on headers having valid proof-of-work further down, as an
// anti-DoS criteria (note: this check is required before passing any
@@ -2776,15 +2762,12 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
// are still in PRESYNC)
// - replaced with headers that are now ready for validation, such as
// during the REDOWNLOAD phase of a low-work headers sync.
// So just check whether we still have headers that we need to process,
// or not.
if (headers.empty()) {
return;
}
have_headers_sync = !!peer.m_headers_sync;
}
// If there is nothing left to process, stop.
if (headers.empty()) return;
// Do these headers connect to something in our block index?
const CBlockIndex *chain_start_header{WITH_LOCK(::cs_main, return m_chainman.m_blockman.LookupBlockIndex(headers[0].hashPrevBlock))};
bool headers_connect_blockindex{chain_start_header != nullptr};