Skip to content

Commit fa579f3

Browse files
author
MacroFake
committed
refactor: Pass reference to last header, not pointer
It is never a nullptr, otherwise an assertion would fire in UpdatePeerStateForReceivedHeaders. Passing a reference makes the code easier to read and less brittle.
1 parent 3db23fd commit fa579f3

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
@@ -657,9 +657,9 @@ class PeerManagerImpl final : public PeerManager
657657
*/
658658
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
659659
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
660-
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast);
660+
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
661661
/** Update peer state based on received headers message */
662-
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers);
662+
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers);
663663

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

@@ -2605,22 +2605,21 @@ bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& loc
26052605
}
26062606

26072607
/*
2608-
* Given a new headers tip ending in pindexLast, potentially request blocks towards that tip.
2608+
* Given a new headers tip ending in last_header, potentially request blocks towards that tip.
26092609
* We require that the given tip have at least as much work as our tip, and for
26102610
* our current tip to be "close to synced" (see CanDirectFetch()).
26112611
*/
2612-
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast)
2612+
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header)
26132613
{
26142614
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
26152615

26162616
LOCK(cs_main);
26172617
CNodeState *nodestate = State(pfrom.GetId());
26182618

2619-
if (CanDirectFetch() && pindexLast->IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= pindexLast->nChainWork) {
2620-
2619+
if (CanDirectFetch() && last_header.IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= last_header.nChainWork) {
26212620
std::vector<const CBlockIndex*> vToFetch;
2622-
const CBlockIndex *pindexWalk = pindexLast;
2623-
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
2621+
const CBlockIndex* pindexWalk{&last_header};
2622+
// Calculate all the blocks we'd need to switch to last_header, up to a limit.
26242623
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
26252624
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
26262625
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
@@ -2636,8 +2635,8 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
26362635
// direct fetch and rely on parallel download instead.
26372636
if (!m_chainman.ActiveChain().Contains(pindexWalk)) {
26382637
LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
2639-
pindexLast->GetBlockHash().ToString(),
2640-
pindexLast->nHeight);
2638+
last_header.GetBlockHash().ToString(),
2639+
last_header.nHeight);
26412640
} else {
26422641
std::vector<CInv> vGetData;
26432642
// Download as much as possible, from earliest to latest.
@@ -2654,14 +2653,15 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
26542653
}
26552654
if (vGetData.size() > 1) {
26562655
LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
2657-
pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
2656+
last_header.GetBlockHash().ToString(),
2657+
last_header.nHeight);
26582658
}
26592659
if (vGetData.size() > 0) {
26602660
if (!m_ignore_incoming_txs &&
26612661
nodestate->m_provides_cmpctblocks &&
26622662
vGetData.size() == 1 &&
26632663
mapBlocksInFlight.size() == 1 &&
2664-
pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
2664+
last_header.pprev->IsValid(BLOCK_VALID_CHAIN)) {
26652665
// In any case, we want to download using a compact block, not a regular one
26662666
vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
26672667
}
@@ -2672,12 +2672,12 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
26722672
}
26732673

26742674
/**
2675-
* Given receipt of headers from a peer ending in pindexLast, along with
2675+
* Given receipt of headers from a peer ending in last_header, along with
26762676
* whether that header was new and whether the headers message was full,
26772677
* update the state we keep for the peer.
26782678
*/
26792679
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
2680-
const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers)
2680+
const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
26812681
{
26822682
LOCK(cs_main);
26832683
CNodeState *nodestate = State(pfrom.GetId());
@@ -2686,14 +2686,13 @@ void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
26862686
}
26872687
nodestate->nUnconnectingHeaders = 0;
26882688

2689-
assert(pindexLast);
2690-
UpdateBlockAvailability(pfrom.GetId(), pindexLast->GetBlockHash());
2689+
UpdateBlockAvailability(pfrom.GetId(), last_header.GetBlockHash());
26912690

26922691
// From here, pindexBestKnownBlock should be guaranteed to be non-null,
26932692
// because it is set in UpdateBlockAvailability. Some nullptr checks
26942693
// are still present, however, as belt-and-suspenders.
26952694

2696-
if (received_new_header && pindexLast->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
2695+
if (received_new_header && last_header.nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
26972696
nodestate->m_last_block_announcement = GetTime();
26982697
}
26992698

@@ -2859,7 +2858,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
28592858
return;
28602859
}
28612860
}
2862-
Assume(pindexLast);
2861+
assert(pindexLast);
28632862

28642863
// Consider fetching more headers if we are not using our headers-sync mechanism.
28652864
if (nCount == MAX_HEADERS_RESULTS && !have_headers_sync) {
@@ -2870,10 +2869,10 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
28702869
}
28712870
}
28722871

2873-
UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
2872+
UpdatePeerStateForReceivedHeaders(pfrom, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
28742873

28752874
// Consider immediately downloading blocks.
2876-
HeadersDirectFetchBlocks(pfrom, peer, pindexLast);
2875+
HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
28772876

28782877
return;
28792878
}

0 commit comments

Comments
 (0)