[net processing] Move nUnconnectingHeaders from CNodeState to Peer
This commit is contained in:
1 parent
5f80d8d1ee
commit
d8c0d1c345
1 file changed
+20
-19
+20
-19
@@ -385,6 +385,9 @@ struct Peer {
|
|||||||
/** Whether we've sent our peer a sendheaders message. **/
|
/** Whether we've sent our peer a sendheaders message. **/
|
||||||
std::atomic<bool> m_sent_sendheaders{false};
|
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)
|
explicit Peer(NodeId id, ServiceFlags our_services)
|
||||||
: m_id{id}
|
: m_id{id}
|
||||||
, m_our_services{our_services}
|
, m_our_services{our_services}
|
||||||
@@ -414,8 +417,6 @@ struct CNodeState {
|
|||||||
const CBlockIndex* pindexLastCommonBlock{nullptr};
|
const CBlockIndex* pindexLastCommonBlock{nullptr};
|
||||||
//! The best header we have sent our peer.
|
//! The best header we have sent our peer.
|
||||||
const CBlockIndex* pindexBestHeaderSent{nullptr};
|
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.
|
//! Whether we've started headers synchronization with this peer.
|
||||||
bool fSyncStarted{false};
|
bool fSyncStarted{false};
|
||||||
//! When to potentially disconnect peer for stalling headers download
|
//! 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 */
|
/** Potentially fetch blocks from this peer upon receipt of a new headers tip */
|
||||||
void HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, const CBlockIndex& last_header);
|
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& 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);
|
EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
|
||||||
|
|
||||||
void SendBlockTransactions(CNode& pfrom, Peer& peer, const CBlock& block, const BlockTransactionsRequest& req);
|
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,
|
void PeerManagerImpl::HandleFewUnconnectingHeaders(CNode& pfrom, Peer& peer,
|
||||||
const std::vector<CBlockHeader>& headers)
|
const std::vector<CBlockHeader>& headers)
|
||||||
{
|
{
|
||||||
LOCK(cs_main);
|
peer.nUnconnectingHeaders++;
|
||||||
CNodeState *nodestate = State(pfrom.GetId());
|
|
||||||
|
|
||||||
nodestate->nUnconnectingHeaders++;
|
|
||||||
// Try to fill in the missing headers.
|
// 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",
|
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].GetHash().ToString(),
|
||||||
headers[0].hashPrevBlock.ToString(),
|
headers[0].hashPrevBlock.ToString(),
|
||||||
m_chainman.m_best_header->nHeight,
|
best_header->nHeight,
|
||||||
pfrom.GetId(), nodestate->nUnconnectingHeaders);
|
pfrom.GetId(), peer.nUnconnectingHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set hashLastUnknownBlock for this peer, so that if we
|
// Set hashLastUnknownBlock for this peer, so that if we
|
||||||
// eventually get the headers - even from a different peer -
|
// eventually get the headers - even from a different peer -
|
||||||
// we can use this peer to download.
|
// 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
|
// The peer may just be broken, so periodically assign DoS points if this
|
||||||
// condition persists.
|
// condition persists.
|
||||||
if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
|
if (peer.nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
|
||||||
Misbehaving(peer, 20, strprintf("%d non-connecting headers", nodestate->nUnconnectingHeaders));
|
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,
|
* 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, Peer& peer,
|
||||||
const CBlockIndex& last_header, bool received_new_header, bool may_have_more_headers)
|
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);
|
LOCK(cs_main);
|
||||||
CNodeState *nodestate = State(pfrom.GetId());
|
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());
|
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.
|
// Consider immediately downloading blocks.
|
||||||
HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
|
HeadersDirectFetchBlocks(pfrom, peer, *pindexLast);
|
||||||
|
|||||||
Reference in new issue
Block a user