p2p: Add warning message when receiving headers for blocks cached as invalid

Currently, if database corruption leads to a block being marked as
invalid incorrectly, we can get stuck in an infinite headerssync
loop with no indication what went wrong or how to fix it.
With the added log message, users will receive an explicit warning after each
failed headerssync attempt with an outbound peer.
This commit is contained in:
Martin Zumsande
2025-10-06 16:07:19 -04:00
parent 4c784b25c4
commit 2f51951d03
2 changed files with 9 additions and 1 deletions

View File

@@ -2955,6 +2955,13 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
state, &pindexLast)};
if (!processed) {
if (state.IsInvalid()) {
if (!pfrom.IsInboundConn() && state.GetResult() == BlockValidationResult::BLOCK_CACHED_INVALID) {
// Warn user if outgoing peers send us headers of blocks that we previously marked as invalid.
LogWarning("%s (received from peer=%i). "
"If this happens with all peers, consider database corruption (that -reindex may fix) "
"or a potential consensus incompatibility.",
state.GetDebugMessage(), pfrom.GetId());
}
MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received");
return;
}

View File

@@ -4244,7 +4244,8 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
*ppindex = pindex;
if (pindex->nStatus & BLOCK_FAILED_MASK) {
LogDebug(BCLog::VALIDATION, "%s: block %s is marked invalid\n", __func__, hash.ToString());
return state.Invalid(BlockValidationResult::BLOCK_CACHED_INVALID, "duplicate-invalid");
return state.Invalid(BlockValidationResult::BLOCK_CACHED_INVALID, "duplicate-invalid",
strprintf("block %s was previously marked invalid", hash.ToString()));
}
return true;
}