Skip to content

Commit 5126e62

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26378: refactor: Pass reference to last header, not pointer
fa579f3 refactor: Pass reference to last header, not pointer (MacroFake) Pull request description: It is never a nullptr, otherwise an assertion would fire in UpdatePeerStateForReceivedHeaders. Passing a reference makes the code easier to read and less brittle. ACKs for top commit: john-moffett: ACK fa579f3 aureleoules: ACK fa579f3 Tree-SHA512: 9725195663a31df57ae46bb7b11211cc4963a8f3d100f60332bfd4a3f3327a73ac978b3172e3007793cfc508dfc7c3a81aab57a275a6963a5ab662ce85743fd0
2 parents 07ac7a2 + fa579f3 commit 5126e62

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/net_processing.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,9 @@ class PeerManagerImpl final : public PeerManager
661661
*/
662662
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
663663
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
664-
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast);
664+
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
665665
/** Update peer state based on received headers message */
666-
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers);
666+
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers);
667667

668668
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
669669

@@ -2622,22 +2622,21 @@ bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& loc
26222622
}
26232623

26242624
/*
2625-
* Given a new headers tip ending in pindexLast, potentially request blocks towards that tip.
2625+
* Given a new headers tip ending in last_header, potentially request blocks towards that tip.
26262626
* We require that the given tip have at least as much work as our tip, and for
26272627
* our current tip to be "close to synced" (see CanDirectFetch()).
26282628
*/
2629-
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast)
2629+
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header)
26302630
{
26312631
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
26322632

26332633
LOCK(cs_main);
26342634
CNodeState *nodestate = State(pfrom.GetId());
26352635

2636-
if (CanDirectFetch() && pindexLast->IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= pindexLast->nChainWork) {
2637-
2636+
if (CanDirectFetch() && last_header.IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= last_header.nChainWork) {
26382637
std::vector<const CBlockIndex*> vToFetch;
2639-
const CBlockIndex *pindexWalk = pindexLast;
2640-
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2638+
const CBlockIndex* pindexWalk{&last_header};
2639+
// Calculate all the blocks we'd need to switch to last_header, up to a limit.
26412640
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
26422641
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
26432642
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
@@ -2653,8 +2652,8 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
26532652
// direct fetch and rely on parallel download instead.
26542653
if (!m_chainman.ActiveChain().Contains(pindexWalk)) {
26552654
LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
2656-
pindexLast->GetBlockHash().ToString(),
2657-
pindexLast->nHeight);
2655+
last_header.GetBlockHash().ToString(),
2656+
last_header.nHeight);
26582657
} else {
26592658
std::vector<CInv> vGetData;
26602659
// Download as much as possible, from earliest to latest.
@@ -2671,14 +2670,15 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
26712670
}
26722671
if (vGetData.size() > 1) {
26732672
LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
2674-
pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
2673+
last_header.GetBlockHash().ToString(),
2674+
last_header.nHeight);
26752675
}
26762676
if (vGetData.size() > 0) {
26772677
if (!m_ignore_incoming_txs &&
26782678
nodestate->m_provides_cmpctblocks &&
26792679
vGetData.size() == 1 &&
26802680
mapBlocksInFlight.size() == 1 &&
2681-
pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
2681+
last_header.pprev->IsValid(BLOCK_VALID_CHAIN)) {
26822682
// In any case, we want to download using a compact block, not a regular one
26832683
vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
26842684
}
@@ -2689,12 +2689,12 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
26892689
}
26902690

26912691
/**
2692-
* Given receipt of headers from a peer ending in pindexLast, along with
2692+
* Given receipt of headers from a peer ending in last_header, along with
26932693
* whether that header was new and whether the headers message was full,
26942694
* update the state we keep for the peer.
26952695
*/
26962696
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
2697-
const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers)
2697+
const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
26982698
{
26992699
LOCK(cs_main);
27002700
CNodeState *nodestate = State(pfrom.GetId());
@@ -2703,14 +2703,13 @@ void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
27032703
}
27042704
nodestate->nUnconnectingHeaders = 0;
27052705

2706-
assert(pindexLast);
2707-
UpdateBlockAvailability(pfrom.GetId(), pindexLast->GetBlockHash());
2706+
UpdateBlockAvailability(pfrom.GetId(), last_header.GetBlockHash());
27082707

27092708
// From here, pindexBestKnownBlock should be guaranteed to be non-null,
27102709
// because it is set in UpdateBlockAvailability. Some nullptr checks
27112710
// are still present, however, as belt-and-suspenders.
27122711

2713-
if (received_new_header && pindexLast->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
2712+
if (received_new_header && last_header.nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
27142713
nodestate->m_last_block_announcement = GetTime();
27152714
}
27162715

@@ -2876,7 +2875,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
28762875
return;
28772876
}
28782877
}
2879-
Assume(pindexLast);
2878+
assert(pindexLast);
28802879

28812880
// Consider fetching more headers if we are not using our headers-sync mechanism.
28822881
if (nCount == MAX_HEADERS_RESULTS && !have_headers_sync) {
@@ -2887,10 +2886,10 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
28872886
}
28882887
}
28892888

2890-
UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
2889+
UpdatePeerStateForReceivedHeaders(pfrom, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
28912890

28922891
// Consider immediately downloading blocks.
2893-
HeadersDirectFetchBlocks(pfrom, peer, pindexLast);
2892+
HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
28942893

28952894
return;
28962895
}

0 commit comments

Comments
 (0)