Merge bitcoin/bitcoin#34464: Change BlockRequestAllowed() to take ref (minor refactor)

1f8f7d477a Change BlockRequestAllowed() to take ref (optout)

Pull request description:

  As [suggested here](https://github.com/bitcoin/bitcoin/pull/34416#discussion_r2745302958), a minor refactor of `PeerManagerImpl::BlockRequestAllowed()` to take reference parameter (instead of pointer). The motivation is to make the code safer, by minimizing the risk of null-dereference, and to be more consistent.
  The change is local to the `PeerManagerImpl::BlockRequestAllowed()` class.
  Related to #34440.

ACKs for top commit:
  maflcko:
    review ACK 1f8f7d477a 🎐
  l0rinc:
    tested ACK 1f8f7d477a
  stickies-v:
    ACK 1f8f7d477a

Tree-SHA512: 9c2de2d3e7d067e018db7ec54c79f512ccc1da54574d4fb362f6697ee6e235783779d7094cf20856cd34e08a1dbc74609d8351fe7b2287cd8ec0c836ef07be19
This commit is contained in:
merge-script
2026-02-05 10:56:37 +00:00

View File

@@ -1017,7 +1017,7 @@ private:
* and in best equivalent proof of work) than the best header chain we know
* about and we fully-validated them at some point.
*/
bool BlockRequestAllowed(const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool BlockRequestAllowed(const CBlockIndex& block_index) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& inv)
EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex, !m_most_recent_block_mutex);
@@ -1922,13 +1922,13 @@ void PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidati
}
}
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex& block_index)
{
AssertLockHeld(cs_main);
if (m_chainman.ActiveChain().Contains(pindex)) return true;
return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (m_chainman.m_best_header != nullptr) &&
(m_chainman.m_best_header->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
(GetBlockProofEquivalentTime(*m_chainman.m_best_header, *pindex, *m_chainman.m_best_header, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
if (m_chainman.ActiveChain().Contains(&block_index)) return true;
return block_index.IsValid(BLOCK_VALID_SCRIPTS) && (m_chainman.m_best_header != nullptr) &&
(m_chainman.m_best_header->GetBlockTime() - block_index.GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
(GetBlockProofEquivalentTime(*m_chainman.m_best_header, block_index, *m_chainman.m_best_header, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
}
util::Expected<void, std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
@@ -2340,7 +2340,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
if (!pindex) {
return;
}
if (!BlockRequestAllowed(pindex)) {
if (!BlockRequestAllowed(*pindex)) {
LogDebug(BCLog::NET, "%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom.GetId());
return;
}
@@ -3255,7 +3255,7 @@ bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& node, Peer& peer,
stop_index = m_chainman.m_blockman.LookupBlockIndex(stop_hash);
// Check that the stop block exists and the peer would be allowed to fetch it.
if (!stop_index || !BlockRequestAllowed(stop_index)) {
if (!stop_index || !BlockRequestAllowed(*stop_index)) {
LogDebug(BCLog::NET, "peer requested invalid block hash: %s, %s\n",
stop_hash.ToString(), node.DisconnectMsg(fLogIPs));
node.fDisconnect = true;
@@ -4404,8 +4404,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
if (!pindex) {
return;
}
if (!BlockRequestAllowed(pindex)) {
if (!BlockRequestAllowed(*pindex)) {
LogDebug(BCLog::NET, "%s: ignoring request from peer=%i for old block header that isn't in the main chain\n", __func__, pfrom.GetId());
return;
}