diff options
author | dergoegge <n.goeggi@gmail.com> | 2022-09-20 14:30:29 +0100 |
---|---|---|
committer | dergoegge <n.goeggi@gmail.com> | 2023-03-30 14:56:17 +0200 |
commit | d8c0d1c345546c6066274edbf3bf4fb5d6f75419 (patch) | |
tree | 2aa426158aaea4ed05c36cb5c2799edf44de7a38 /src/net_processing.cpp | |
parent | 5f80d8d1eec14253407ceed1a813619d3e5a0fe4 (diff) |
[net processing] Move nUnconnectingHeaders from CNodeState to Peer
Diffstat (limited to 'src/net_processing.cpp')
-rw-r--r-- | src/net_processing.cpp | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 14eac13aa4..ddb735f00a 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -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); |