Skip to content

Commit 3788a84

Browse files
committed
Do not send (potentially) invalid headers in response to getheaders
Nowhere else in the protocol do we send headers which are for blocks we have not fully validated except in response to getheaders messages with a null locator. On my public node I have not seen any such request (whether for an invalid block or not) in at least two years of debug.log output, indicating that this should have minimal impact.
1 parent bb9ab0f commit 3788a84

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/net_processing.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,13 @@ void Misbehaving(NodeId pnode, int howmuch)
755755

756756
// To prevent fingerprinting attacks, only send blocks/headers outside of the
757757
// active chain if they are no more than a month older (both in time, and in
758-
// best equivalent proof of work) than the best header chain we know about.
759-
static bool StaleBlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams)
758+
// best equivalent proof of work) than the best header chain we know about and
759+
// we fully-validated them at some point.
760+
static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams)
760761
{
761762
AssertLockHeld(cs_main);
762-
return (pindexBestHeader != nullptr) &&
763+
if (chainActive.Contains(pindex)) return true;
764+
return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != nullptr) &&
763765
(pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
764766
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, consensusParams) < STALE_RELAY_AGE_LIMIT);
765767
}
@@ -1038,14 +1040,9 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
10381040
CValidationState dummy;
10391041
ActivateBestChain(dummy, Params(), a_recent_block);
10401042
}
1041-
if (chainActive.Contains(mi->second)) {
1042-
send = true;
1043-
} else {
1044-
send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) &&
1045-
StaleBlockRequestAllowed(mi->second, consensusParams);
1046-
if (!send) {
1047-
LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
1048-
}
1043+
send = BlockRequestAllowed(mi->second, consensusParams);
1044+
if (!send) {
1045+
LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
10491046
}
10501047
}
10511048
// disconnect node in case we have reached the outbound limit for serving historical blocks
@@ -1986,8 +1983,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19861983
return true;
19871984
pindex = (*mi).second;
19881985

1989-
if (!chainActive.Contains(pindex) &&
1990-
!StaleBlockRequestAllowed(pindex, chainparams.GetConsensus())) {
1986+
if (!BlockRequestAllowed(pindex, chainparams.GetConsensus())) {
19911987
LogPrintf("%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom->GetId());
19921988
return true;
19931989
}

0 commit comments

Comments
 (0)