mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-23 07:15:29 +01:00
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.
This commit is contained in:
parent
3db23fd821
commit
fa579f3063
1 changed files with 19 additions and 20 deletions
|
@ -657,9 +657,9 @@ private:
|
||||||
*/
|
*/
|
||||||
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
|
bool MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& locator, Peer& peer) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
|
||||||
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
|
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
|
||||||
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast);
|
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
|
||||||
/** Update peer state based on received headers message */
|
/** Update peer state based on received headers message */
|
||||||
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers);
|
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers);
|
||||||
|
|
||||||
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
|
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
|
||||||
|
|
||||||
|
@ -2605,22 +2605,21 @@ bool PeerManagerImpl::MaybeSendGetHeaders(CNode& pfrom, const CBlockLocator& loc
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Given a new headers tip ending in pindexLast, potentially request blocks towards that tip.
|
* Given a new headers tip ending in last_header, potentially request blocks towards that tip.
|
||||||
* We require that the given tip have at least as much work as our tip, and for
|
* We require that the given tip have at least as much work as our tip, and for
|
||||||
* our current tip to be "close to synced" (see CanDirectFetch()).
|
* our current tip to be "close to synced" (see CanDirectFetch()).
|
||||||
*/
|
*/
|
||||||
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex* pindexLast)
|
void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header)
|
||||||
{
|
{
|
||||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||||
|
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
CNodeState *nodestate = State(pfrom.GetId());
|
CNodeState *nodestate = State(pfrom.GetId());
|
||||||
|
|
||||||
if (CanDirectFetch() && pindexLast->IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= pindexLast->nChainWork) {
|
if (CanDirectFetch() && last_header.IsValid(BLOCK_VALID_TREE) && m_chainman.ActiveChain().Tip()->nChainWork <= last_header.nChainWork) {
|
||||||
|
|
||||||
std::vector<const CBlockIndex*> vToFetch;
|
std::vector<const CBlockIndex*> vToFetch;
|
||||||
const CBlockIndex *pindexWalk = pindexLast;
|
const CBlockIndex* pindexWalk{&last_header};
|
||||||
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
|
// Calculate all the blocks we'd need to switch to last_header, up to a limit.
|
||||||
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
|
while (pindexWalk && !m_chainman.ActiveChain().Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
|
||||||
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
|
if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
|
||||||
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
|
!IsBlockRequested(pindexWalk->GetBlockHash()) &&
|
||||||
|
@ -2636,8 +2635,8 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
|
||||||
// direct fetch and rely on parallel download instead.
|
// direct fetch and rely on parallel download instead.
|
||||||
if (!m_chainman.ActiveChain().Contains(pindexWalk)) {
|
if (!m_chainman.ActiveChain().Contains(pindexWalk)) {
|
||||||
LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
|
LogPrint(BCLog::NET, "Large reorg, won't direct fetch to %s (%d)\n",
|
||||||
pindexLast->GetBlockHash().ToString(),
|
last_header.GetBlockHash().ToString(),
|
||||||
pindexLast->nHeight);
|
last_header.nHeight);
|
||||||
} else {
|
} else {
|
||||||
std::vector<CInv> vGetData;
|
std::vector<CInv> vGetData;
|
||||||
// Download as much as possible, from earliest to latest.
|
// Download as much as possible, from earliest to latest.
|
||||||
|
@ -2654,14 +2653,15 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
|
||||||
}
|
}
|
||||||
if (vGetData.size() > 1) {
|
if (vGetData.size() > 1) {
|
||||||
LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
|
LogPrint(BCLog::NET, "Downloading blocks toward %s (%d) via headers direct fetch\n",
|
||||||
pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
|
last_header.GetBlockHash().ToString(),
|
||||||
|
last_header.nHeight);
|
||||||
}
|
}
|
||||||
if (vGetData.size() > 0) {
|
if (vGetData.size() > 0) {
|
||||||
if (!m_ignore_incoming_txs &&
|
if (!m_ignore_incoming_txs &&
|
||||||
nodestate->m_provides_cmpctblocks &&
|
nodestate->m_provides_cmpctblocks &&
|
||||||
vGetData.size() == 1 &&
|
vGetData.size() == 1 &&
|
||||||
mapBlocksInFlight.size() == 1 &&
|
mapBlocksInFlight.size() == 1 &&
|
||||||
pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
|
last_header.pprev->IsValid(BLOCK_VALID_CHAIN)) {
|
||||||
// In any case, we want to download using a compact block, not a regular one
|
// In any case, we want to download using a compact block, not a regular one
|
||||||
vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
|
vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
|
||||||
}
|
}
|
||||||
|
@ -2672,12 +2672,12 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given receipt of headers from a peer ending in pindexLast, along with
|
* Given receipt of headers from a peer ending in last_header, along with
|
||||||
* whether that header was new and whether the headers message was full,
|
* whether that header was new and whether the headers message was full,
|
||||||
* update the state we keep for the peer.
|
* update the state we keep for the peer.
|
||||||
*/
|
*/
|
||||||
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
|
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
|
||||||
const CBlockIndex *pindexLast, bool received_new_header, bool may_have_more_headers)
|
const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
CNodeState *nodestate = State(pfrom.GetId());
|
CNodeState *nodestate = State(pfrom.GetId());
|
||||||
|
@ -2686,14 +2686,13 @@ void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
|
||||||
}
|
}
|
||||||
nodestate->nUnconnectingHeaders = 0;
|
nodestate->nUnconnectingHeaders = 0;
|
||||||
|
|
||||||
assert(pindexLast);
|
UpdateBlockAvailability(pfrom.GetId(), last_header.GetBlockHash());
|
||||||
UpdateBlockAvailability(pfrom.GetId(), pindexLast->GetBlockHash());
|
|
||||||
|
|
||||||
// From here, pindexBestKnownBlock should be guaranteed to be non-null,
|
// From here, pindexBestKnownBlock should be guaranteed to be non-null,
|
||||||
// because it is set in UpdateBlockAvailability. Some nullptr checks
|
// because it is set in UpdateBlockAvailability. Some nullptr checks
|
||||||
// are still present, however, as belt-and-suspenders.
|
// are still present, however, as belt-and-suspenders.
|
||||||
|
|
||||||
if (received_new_header && pindexLast->nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
|
if (received_new_header && last_header.nChainWork > m_chainman.ActiveChain().Tip()->nChainWork) {
|
||||||
nodestate->m_last_block_announcement = GetTime();
|
nodestate->m_last_block_announcement = GetTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2859,7 +2858,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assume(pindexLast);
|
assert(pindexLast);
|
||||||
|
|
||||||
// Consider fetching more headers if we are not using our headers-sync mechanism.
|
// Consider fetching more headers if we are not using our headers-sync mechanism.
|
||||||
if (nCount == MAX_HEADERS_RESULTS && !have_headers_sync) {
|
if (nCount == MAX_HEADERS_RESULTS && !have_headers_sync) {
|
||||||
|
@ -2870,10 +2869,10 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdatePeerStateForReceivedHeaders(pfrom, pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
|
UpdatePeerStateForReceivedHeaders(pfrom, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
|
||||||
|
|
||||||
// Consider immediately downloading blocks.
|
// Consider immediately downloading blocks.
|
||||||
HeadersDirectFetchBlocks(pfrom, peer, pindexLast);
|
HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue