mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 14:38:29 +01:00
move-mostly: Make pindexBestHeader a ChainMan member
[META] In the next commit, we move the clearing of pindexBestHeader to
ChainstateManager::Unload()
This commit is contained in:
@@ -1499,9 +1499,9 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
if (m_chainman.ActiveChain().Contains(pindex)) return true;
|
||||
return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != nullptr) &&
|
||||
(pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
|
||||
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
|
||||
return pindex->IsValid(BLOCK_VALID_SCRIPTS) && (m_chainman.pindexBestHeader != nullptr) &&
|
||||
(m_chainman.pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() < STALE_RELAY_AGE_LIMIT) &&
|
||||
(GetBlockProofEquivalentTime(*m_chainman.pindexBestHeader, *pindex, *m_chainman.pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
|
||||
}
|
||||
|
||||
std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBlockIndex& block_index)
|
||||
@@ -1896,7 +1896,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
|
||||
const CNetMsgMaker msgMaker(pfrom.GetCommonVersion());
|
||||
// disconnect node in case we have reached the outbound limit for serving historical blocks
|
||||
if (m_connman.OutboundTargetReached(true) &&
|
||||
(((pindexBestHeader != nullptr) && (pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk()) &&
|
||||
(((m_chainman.pindexBestHeader != nullptr) && (m_chainman.pindexBestHeader->GetBlockTime() - pindex->GetBlockTime() > HISTORICAL_BLOCK_AGE)) || inv.IsMsgFilteredBlk()) &&
|
||||
!pfrom.HasPermission(NetPermissionFlags::Download) // nodes with the download permission may exceed target
|
||||
) {
|
||||
LogPrint(BCLog::NET, "historical block serving limit reached, disconnect peer=%d\n", pfrom.GetId());
|
||||
@@ -2173,11 +2173,11 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
|
||||
// nUnconnectingHeaders gets reset back to 0.
|
||||
if (!m_chainman.m_blockman.LookupBlockIndex(headers[0].hashPrevBlock) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
|
||||
nodestate->nUnconnectingHeaders++;
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, m_chainman.ActiveChain().GetLocator(pindexBestHeader), uint256()));
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, m_chainman.ActiveChain().GetLocator(m_chainman.pindexBestHeader), uint256()));
|
||||
LogPrint(BCLog::NET, "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
|
||||
headers[0].GetHash().ToString(),
|
||||
headers[0].hashPrevBlock.ToString(),
|
||||
pindexBestHeader->nHeight,
|
||||
m_chainman.pindexBestHeader->nHeight,
|
||||
pfrom.GetId(), nodestate->nUnconnectingHeaders);
|
||||
// Set hashLastUnknownBlock for this peer, so that if we
|
||||
// eventually get the headers - even from a different peer -
|
||||
@@ -2235,7 +2235,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
|
||||
|
||||
if (nCount == MAX_HEADERS_RESULTS) {
|
||||
// Headers message had its maximum size; the peer may have more headers.
|
||||
// TODO: optimize: if pindexLast is an ancestor of m_chainman.ActiveChain().Tip or pindexBestHeader, continue
|
||||
// TODO: optimize: if pindexLast is an ancestor of m_chainman.ActiveChain().Tip or m_chainman.pindexBestHeader, continue
|
||||
// from there instead.
|
||||
LogPrint(BCLog::NET, "more getheaders (%d) to end to peer=%d (startheight:%d)\n",
|
||||
pindexLast->nHeight, pfrom.GetId(), peer.m_starting_height);
|
||||
@@ -3102,8 +3102,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||
}
|
||||
|
||||
if (best_block != nullptr) {
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, m_chainman.ActiveChain().GetLocator(pindexBestHeader), *best_block));
|
||||
LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, best_block->ToString(), pfrom.GetId());
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, m_chainman.ActiveChain().GetLocator(m_chainman.pindexBestHeader), *best_block));
|
||||
LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", m_chainman.pindexBestHeader->nHeight, best_block->ToString(), pfrom.GetId());
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -3549,7 +3549,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
||||
if (!m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
|
||||
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
|
||||
if (!m_chainman.ActiveChainstate().IsInitialBlockDownload())
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, m_chainman.ActiveChain().GetLocator(pindexBestHeader), uint256()));
|
||||
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETHEADERS, m_chainman.ActiveChain().GetLocator(m_chainman.pindexBestHeader), uint256()));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -4670,28 +4670,29 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
||||
CNodeState &state = *State(pto->GetId());
|
||||
|
||||
// Start block sync
|
||||
if (pindexBestHeader == nullptr)
|
||||
pindexBestHeader = m_chainman.ActiveChain().Tip();
|
||||
if (m_chainman.pindexBestHeader == nullptr) {
|
||||
m_chainman.pindexBestHeader = m_chainman.ActiveChain().Tip();
|
||||
}
|
||||
bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->IsAddrFetchConn()); // Download if this is a nice peer, or we have no nice peers and this one might do.
|
||||
if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
|
||||
// Only actively request headers from a single peer, unless we're close to today.
|
||||
if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
|
||||
if ((nSyncStarted == 0 && fFetch) || m_chainman.pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
|
||||
state.fSyncStarted = true;
|
||||
state.m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
|
||||
(
|
||||
// Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
|
||||
// to maintain precision
|
||||
std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
|
||||
(GetAdjustedTime() - pindexBestHeader->GetBlockTime()) / consensusParams.nPowTargetSpacing
|
||||
(GetAdjustedTime() - m_chainman.pindexBestHeader->GetBlockTime()) / consensusParams.nPowTargetSpacing
|
||||
);
|
||||
nSyncStarted++;
|
||||
const CBlockIndex *pindexStart = pindexBestHeader;
|
||||
const CBlockIndex* pindexStart = m_chainman.pindexBestHeader;
|
||||
/* If possible, start at the block preceding the currently
|
||||
best known header. This ensures that we always get a
|
||||
non-empty list of headers back as long as the peer
|
||||
is up-to-date. With a non-empty response, we can initialise
|
||||
the peer's known best block. This wouldn't be possible
|
||||
if we requested starting at pindexBestHeader and
|
||||
if we requested starting at m_chainman.pindexBestHeader and
|
||||
got back an empty response. */
|
||||
if (pindexStart->pprev)
|
||||
pindexStart = pindexStart->pprev;
|
||||
@@ -5016,7 +5017,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
|
||||
// Check for headers sync timeouts
|
||||
if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds::max()) {
|
||||
// Detect whether this is a stalling initial-headers-sync peer
|
||||
if (pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) {
|
||||
if (m_chainman.pindexBestHeader->GetBlockTime() <= GetAdjustedTime() - 24 * 60 * 60) {
|
||||
if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1)) {
|
||||
// Disconnect a peer (without NetPermissionFlags::NoBan permission) if it is our only sync peer,
|
||||
// and we have others we could be using instead.
|
||||
|
||||
Reference in New Issue
Block a user