mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
[net processing] Move nUnconnectingHeaders from CNodeState to Peer
This commit is contained in:
parent
5f80d8d1ee
commit
d8c0d1c345
1 changed files with 20 additions and 19 deletions
|
@ -385,6 +385,9 @@ struct Peer {
|
|||
/** Whether we've sent our peer a sendheaders message. **/
|
||||
std::atomic<bool> m_sent_sendheaders{false};
|
||||
|
||||
/** Length of current-streak of unconnecting headers announcements */
|
||||
int nUnconnectingHeaders GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
|
||||
|
||||
explicit Peer(NodeId id, ServiceFlags our_services)
|
||||
: m_id{id}
|
||||
, m_our_services{our_services}
|
||||
|
@ -414,8 +417,6 @@ struct CNodeState {
|
|||
const CBlockIndex* pindexLastCommonBlock{nullptr};
|
||||
//! The best header we have sent our peer.
|
||||
const CBlockIndex* pindexBestHeaderSent{nullptr};
|
||||
//! Length of current-streak of unconnecting headers announcements
|
||||
int nUnconnectingHeaders GUARDED_BY(NetEventsInterface::g_msgproc_mutex){0};
|
||||
//! Whether we've started headers synchronization with this peer.
|
||||
bool fSyncStarted{false};
|
||||
//! When to potentially disconnect peer for stalling headers download
|
||||
|
@ -666,7 +667,7 @@ private:
|
|||
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
|
||||
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
|
||||
/** Update peer state based on received headers message */
|
||||
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
|
||||
void UpdatePeerStateForReceivedHeaders(CNode& pfrom, Peer& peer, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
|
||||
EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
|
||||
|
||||
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
|
||||
|
@ -2440,27 +2441,26 @@ arith_uint256 PeerManagerImpl::GetAntiDoSWorkThreshold()
|
|||
void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
|
||||
const std::vector<CBlockHeader>& headers)
|
||||
{
|
||||
LOCK(cs_main);
|
||||
CNodeState *nodestate = State(pfrom.GetId());
|
||||
|
||||
nodestate->nUnconnectingHeaders++;
|
||||
peer.nUnconnectingHeaders++;
|
||||
// Try to fill in the missing headers.
|
||||
if (MaybeSendGetHeaders(pfrom, GetLocator(m_chainman.m_best_header), peer)) {
|
||||
const CBlockIndex* best_header{WITH_LOCK(cs_main, return m_chainman.m_best_header)};
|
||||
if (MaybeSendGetHeaders(pfrom, GetLocator(best_header), peer)) {
|
||||
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(),
|
||||
m_chainman.m_best_header->nHeight,
|
||||
pfrom.GetId(), nodestate->nUnconnectingHeaders);
|
||||
best_header->nHeight,
|
||||
pfrom.GetId(), peer.nUnconnectingHeaders);
|
||||
}
|
||||
|
||||
// Set hashLastUnknownBlock for this peer, so that if we
|
||||
// eventually get the headers - even from a different peer -
|
||||
// we can use this peer to download.
|
||||
UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash());
|
||||
WITH_LOCK(cs_main, UpdateBlockAvailability(pfrom.GetId(), headers.back().GetHash()));
|
||||
|
||||
// The peer may just be broken, so periodically assign DoS points if this
|
||||
// condition persists.
|
||||
if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
|
||||
Misbehaving(peer, 20, strprintf("%d non-connecting headers", nodestate->nUnconnectingHeaders));
|
||||
if (peer.nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
|
||||
Misbehaving(peer, 20, strprintf("%d non-connecting headers", peer.nUnconnectingHeaders));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2708,15 +2708,16 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
|
|||
* whether that header was new and whether the headers message was full,
|
||||
* update the state we keep for the peer.
|
||||
*/
|
||||
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom,
|
||||
void PeerManagerImpl::UpdatePeerStateForReceivedHeaders(CNode& pfrom, Peer& peer,
|
||||
const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
|
||||
{
|
||||
if (peer.nUnconnectingHeaders > 0) {
|
||||
LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom.GetId(), peer.nUnconnectingHeaders);
|
||||
}
|
||||
peer.nUnconnectingHeaders = 0;
|
||||
|
||||
LOCK(cs_main);
|
||||
CNodeState *nodestate = State(pfrom.GetId());
|
||||
if (nodestate->nUnconnectingHeaders > 0) {
|
||||
LogPrint(BCLog::NET, "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom.GetId(), nodestate->nUnconnectingHeaders);
|
||||
}
|
||||
nodestate->nUnconnectingHeaders = 0;
|
||||
|
||||
UpdateBlockAvailability(pfrom.GetId(), last_header.GetBlockHash());
|
||||
|
||||
|
@ -2901,7 +2902,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
|
|||
}
|
||||
}
|
||||
|
||||
UpdatePeerStateForReceivedHeaders(pfrom, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
|
||||
UpdatePeerStateForReceivedHeaders(pfrom, peer, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
|
||||
|
||||
// Consider immediately downloading blocks.
|
||||
HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
|
||||
|
|
Loading…
Add table
Reference in a new issue