@@ -385,6 +385,9 @@ struct Peer {
385
385
/* * Whether we've sent our peer a sendheaders message. **/
386
386
std::atomic<bool > m_sent_sendheaders{false };
387
387
388
+ /* * Length of current-streak of unconnecting headers announcements */
389
+ int nUnconnectingHeaders GUARDED_BY (NetEventsInterface::g_msgproc_mutex){0 };
390
+
388
391
explicit Peer (NodeId id, ServiceFlags our_services)
389
392
: m_id{id}
390
393
, m_our_services{our_services}
@@ -414,8 +417,6 @@ struct CNodeState {
414
417
const CBlockIndex* pindexLastCommonBlock{nullptr };
415
418
// ! The best header we have sent our peer.
416
419
const CBlockIndex* pindexBestHeaderSent{nullptr };
417
- // ! Length of current-streak of unconnecting headers announcements
418
- int nUnconnectingHeaders GUARDED_BY (NetEventsInterface::g_msgproc_mutex){0 };
419
420
// ! Whether we've started headers synchronization with this peer.
420
421
bool fSyncStarted {false };
421
422
// ! When to potentially disconnect peer for stalling headers download
@@ -666,7 +667,7 @@ class PeerManagerImpl final : public PeerManager
666
667
/* * Potentially fetch blocks from this peer upon receipt of a new headers tip */
667
668
void HeadersDirectFetchBlocks (CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
668
669
/* * Update peer state based on received headers message */
669
- void UpdatePeerStateForReceivedHeaders (CNode& pfrom, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
670
+ void UpdatePeerStateForReceivedHeaders (CNode& pfrom, Peer& peer, const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
670
671
EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
671
672
672
673
void SendBlockTransactions (CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
@@ -2440,27 +2441,26 @@ arith_uint256 PeerManagerImpl::GetAntiDoSWorkThreshold()
2440
2441
void PeerManagerImpl::HandleFewUnconnectingHeaders (CNode& pfrom, Peer& peer,
2441
2442
const std::vector<CBlockHeader>& headers)
2442
2443
{
2443
- LOCK (cs_main);
2444
- CNodeState *nodestate = State (pfrom.GetId ());
2445
-
2446
- nodestate->nUnconnectingHeaders ++;
2444
+ peer.nUnconnectingHeaders ++;
2447
2445
// Try to fill in the missing headers.
2448
- if (MaybeSendGetHeaders (pfrom, GetLocator (m_chainman.m_best_header ), peer)) {
2446
+ const CBlockIndex* best_header{WITH_LOCK (cs_main, return m_chainman.m_best_header )};
2447
+ if (MaybeSendGetHeaders (pfrom, GetLocator (best_header), peer)) {
2449
2448
LogPrint (BCLog::NET, " received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n " ,
2450
2449
headers[0 ].GetHash ().ToString (),
2451
2450
headers[0 ].hashPrevBlock .ToString (),
2452
- m_chainman. m_best_header ->nHeight ,
2453
- pfrom.GetId (), nodestate-> nUnconnectingHeaders );
2451
+ best_header ->nHeight ,
2452
+ pfrom.GetId (), peer. nUnconnectingHeaders );
2454
2453
}
2454
+
2455
2455
// Set hashLastUnknownBlock for this peer, so that if we
2456
2456
// eventually get the headers - even from a different peer -
2457
2457
// we can use this peer to download.
2458
- UpdateBlockAvailability (pfrom.GetId (), headers.back ().GetHash ());
2458
+ WITH_LOCK (cs_main, UpdateBlockAvailability (pfrom.GetId (), headers.back ().GetHash () ));
2459
2459
2460
2460
// The peer may just be broken, so periodically assign DoS points if this
2461
2461
// condition persists.
2462
- if (nodestate-> nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0 ) {
2463
- Misbehaving (peer, 20 , strprintf (" %d non-connecting headers" , nodestate-> nUnconnectingHeaders ));
2462
+ if (peer. nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0 ) {
2463
+ Misbehaving (peer, 20 , strprintf (" %d non-connecting headers" , peer. nUnconnectingHeaders ));
2464
2464
}
2465
2465
}
2466
2466
@@ -2708,15 +2708,16 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
2708
2708
* whether that header was new and whether the headers message was full,
2709
2709
* update the state we keep for the peer.
2710
2710
*/
2711
- void PeerManagerImpl::UpdatePeerStateForReceivedHeaders (CNode& pfrom,
2711
+ void PeerManagerImpl::UpdatePeerStateForReceivedHeaders (CNode& pfrom, Peer& peer,
2712
2712
const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
2713
2713
{
2714
+ if (peer.nUnconnectingHeaders > 0 ) {
2715
+ LogPrint (BCLog::NET, " peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n " , pfrom.GetId (), peer.nUnconnectingHeaders );
2716
+ }
2717
+ peer.nUnconnectingHeaders = 0 ;
2718
+
2714
2719
LOCK (cs_main);
2715
2720
CNodeState *nodestate = State (pfrom.GetId ());
2716
- if (nodestate->nUnconnectingHeaders > 0 ) {
2717
- LogPrint (BCLog::NET, " peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n " , pfrom.GetId (), nodestate->nUnconnectingHeaders );
2718
- }
2719
- nodestate->nUnconnectingHeaders = 0 ;
2720
2721
2721
2722
UpdateBlockAvailability (pfrom.GetId (), last_header.GetBlockHash ());
2722
2723
@@ -2901,7 +2902,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
2901
2902
}
2902
2903
}
2903
2904
2904
- UpdatePeerStateForReceivedHeaders (pfrom, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
2905
+ UpdatePeerStateForReceivedHeaders (pfrom, peer, *pindexLast, received_new_header, nCount == MAX_HEADERS_RESULTS);
2905
2906
2906
2907
// Consider immediately downloading blocks.
2907
2908
HeadersDirectFetchBlocks (pfrom, peer, *pindexLast);
0 commit comments