diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -159,6 +159,7 @@ nMaxConnections = connOptions.nMaxConnections; nMaxOutbound = std::min(connOptions.nMaxOutbound, connOptions.nMaxConnections); + m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing; nMaxAddnode = connOptions.nMaxAddnode; nMaxFeeler = connOptions.nMaxFeeler; nBestHeight = connOptions.nBestHeight; @@ -201,6 +202,7 @@ void Interrupt(); bool GetNetworkActive() const { return fNetworkActive; }; + bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; }; void SetNetworkActive(bool active); void OpenNetworkConnection(const CAddress &addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, @@ -436,6 +438,7 @@ int nMaxOutbound; int nMaxAddnode; int nMaxFeeler; + bool m_use_addrman_outgoing; std::atomic nBestHeight; CClientUIInterface *clientInterface; NetEventsInterface *m_msgproc; diff --git a/src/net_processing.h b/src/net_processing.h --- a/src/net_processing.h +++ b/src/net_processing.h @@ -8,8 +8,11 @@ #include #include +#include #include +extern RecursiveMutex cs_main; + class Config; /** @@ -106,7 +109,8 @@ * If we have extra outbound peers, try to disconnect the one with the * oldest block announcement. */ - void EvictExtraOutboundPeers(int64_t time_in_seconds); + void EvictExtraOutboundPeers(int64_t time_in_seconds) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); private: //! Next time to check for stale tip diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4107,8 +4107,6 @@ NodeId worst_peer = -1; int64_t oldest_block_announcement = std::numeric_limits::max(); - LOCK(cs_main); - connman->ForEachNode([&](CNode *pnode) { AssertLockHeld(cs_main); @@ -4175,6 +4173,8 @@ void PeerLogicValidation::CheckForStaleTipAndEvictPeers( const Consensus::Params &consensusParams) { + LOCK(cs_main); + if (connman == nullptr) { return; } @@ -4187,10 +4187,10 @@ return; } - LOCK(cs_main); // Check whether our tip is stale, and if so, allow using an extra outbound // peer. - if (TipMayBeStale(consensusParams)) { + if (!fImporting && !fReindex && connman->GetNetworkActive() && + connman->GetUseAddrmanOutgoing() && TipMayBeStale(consensusParams)) { LogPrintf("Potential stale tip detected, will try using extra outbound " "peer (last tip update: %d seconds ago)\n", time_in_seconds - g_last_tip_update);