From 2f51951d03cc1f8917e0fc931dce674f9bfedaf5 Mon Sep 17 00:00:00 2001 From: Martin Zumsande Date: Mon, 6 Oct 2025 16:07:19 -0400 Subject: [PATCH] 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. --- src/net_processing.cpp | 7 +++++++ src/validation.cpp | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 9cab2461763..c539f4a5180 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -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; } diff --git a/src/validation.cpp b/src/validation.cpp index 73b3e91dd20..e1d6babfd0e 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -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; }