Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
Show First 20 Lines • Show All 4,101 Lines • ▼ Show 20 Lines | void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds) { | ||||
} | } | ||||
// If we have more outbound peers than we target, disconnect one. | // If we have more outbound peers than we target, disconnect one. | ||||
// Pick the outbound peer that least recently announced us a new block, with | // Pick the outbound peer that least recently announced us a new block, with | ||||
// ties broken by choosing the more recent connection (higher node id) | // ties broken by choosing the more recent connection (higher node id) | ||||
NodeId worst_peer = -1; | NodeId worst_peer = -1; | ||||
int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max(); | int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max(); | ||||
LOCK(cs_main); | |||||
connman->ForEachNode([&](CNode *pnode) { | connman->ForEachNode([&](CNode *pnode) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
// Ignore non-outbound peers, or nodes marked for disconnect already | // Ignore non-outbound peers, or nodes marked for disconnect already | ||||
if (!IsOutboundDisconnectionCandidate(pnode) || pnode->fDisconnect) { | if (!IsOutboundDisconnectionCandidate(pnode) || pnode->fDisconnect) { | ||||
return; | return; | ||||
} | } | ||||
CNodeState *state = State(pnode->GetId()); | CNodeState *state = State(pnode->GetId()); | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | if (disconnected) { | ||||
// stale tip, to limit the load we put on the network from these extra | // stale tip, to limit the load we put on the network from these extra | ||||
// connections. | // connections. | ||||
connman->SetTryNewOutboundPeer(false); | connman->SetTryNewOutboundPeer(false); | ||||
} | } | ||||
} | } | ||||
void PeerLogicValidation::CheckForStaleTipAndEvictPeers( | void PeerLogicValidation::CheckForStaleTipAndEvictPeers( | ||||
const Consensus::Params &consensusParams) { | const Consensus::Params &consensusParams) { | ||||
LOCK(cs_main); | |||||
if (connman == nullptr) { | if (connman == nullptr) { | ||||
return; | return; | ||||
} | } | ||||
int64_t time_in_seconds = GetTime(); | int64_t time_in_seconds = GetTime(); | ||||
EvictExtraOutboundPeers(time_in_seconds); | EvictExtraOutboundPeers(time_in_seconds); | ||||
if (time_in_seconds <= m_stale_tip_check_time) { | if (time_in_seconds <= m_stale_tip_check_time) { | ||||
return; | return; | ||||
} | } | ||||
LOCK(cs_main); | |||||
// Check whether our tip is stale, and if so, allow using an extra outbound | // Check whether our tip is stale, and if so, allow using an extra outbound | ||||
// peer. | // peer. | ||||
if (TipMayBeStale(consensusParams)) { | if (!fImporting && !fReindex && connman->GetNetworkActive() && | ||||
connman->GetUseAddrmanOutgoing() && TipMayBeStale(consensusParams)) { | |||||
LogPrintf("Potential stale tip detected, will try using extra outbound " | LogPrintf("Potential stale tip detected, will try using extra outbound " | ||||
"peer (last tip update: %d seconds ago)\n", | "peer (last tip update: %d seconds ago)\n", | ||||
time_in_seconds - g_last_tip_update); | time_in_seconds - g_last_tip_update); | ||||
connman->SetTryNewOutboundPeer(true); | connman->SetTryNewOutboundPeer(true); | ||||
} else if (connman->GetTryNewOutboundPeer()) { | } else if (connman->GetTryNewOutboundPeer()) { | ||||
connman->SetTryNewOutboundPeer(false); | connman->SetTryNewOutboundPeer(false); | ||||
} | } | ||||
m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL; | m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL; | ||||
▲ Show 20 Lines • Show All 725 Lines • Show Last 20 Lines |