Change BlockRequestAllowed() to take ref
What changed, and why it matters
This is a small internal code cleanup in Bitcoin Core. It changes one function so it accepts a direct reference to a block index object instead of a pointer. The behavior of the program is unchanged; the only difference is that callers now dereference their pointers before passing them in. There is no security issue here.
No action required. This is a benign refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors PeerManagerImpl::BlockRequestAllowed() to take const CBlockIndex& instead of const CBlockIndex*. All call sites are updated to pass *pindex or *stop_index. The function body is adjusted to use the reference directly. This is a type-safety/cleanup change with no functional or security impact. Callers already validate non-null pointers before invoking the function.
Changed components
src/net_processing.cppPeerManagerImpl::BlockRequestAllowed()Inspect captured patch +9 / −10
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 7354eb57..7da26174 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -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;
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.