Skip to content

Commit 66b3fc5

Browse files
committed
Skip stale tip checking if outbound connections are off or if reindexing.
1 parent 271b379 commit 66b3fc5

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/net.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class CConnman
149149
nLocalServices = connOptions.nLocalServices;
150150
nMaxConnections = connOptions.nMaxConnections;
151151
nMaxOutbound = std::min(connOptions.nMaxOutbound, connOptions.nMaxConnections);
152+
m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing;
152153
nMaxAddnode = connOptions.nMaxAddnode;
153154
nMaxFeeler = connOptions.nMaxFeeler;
154155
nBestHeight = connOptions.nBestHeight;
@@ -174,6 +175,7 @@ class CConnman
174175
void Stop();
175176
void Interrupt();
176177
bool GetNetworkActive() const { return fNetworkActive; };
178+
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
177179
void SetNetworkActive(bool active);
178180
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool fOneShot = false, bool fFeeler = false, bool manual_connection = false);
179181
bool CheckIncomingNonce(uint64_t nonce);
@@ -416,6 +418,7 @@ class CConnman
416418
int nMaxOutbound;
417419
int nMaxAddnode;
418420
int nMaxFeeler;
421+
bool m_use_addrman_outgoing;
419422
std::atomic<int> nBestHeight;
420423
CClientUIInterface* clientInterface;
421424
NetEventsInterface* m_msgproc;

src/net_processing.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3166,8 +3166,6 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
31663166
NodeId worst_peer = -1;
31673167
int64_t oldest_block_announcement = std::numeric_limits<int64_t>::max();
31683168

3169-
LOCK(cs_main);
3170-
31713169
connman->ForEachNode([&](CNode* pnode) {
31723170
AssertLockHeld(cs_main);
31733171

@@ -3215,17 +3213,18 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
32153213

32163214
void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams)
32173215
{
3216+
LOCK(cs_main);
3217+
32183218
if (connman == nullptr) return;
32193219

32203220
int64_t time_in_seconds = GetTime();
32213221

32223222
EvictExtraOutboundPeers(time_in_seconds);
32233223

32243224
if (time_in_seconds > m_stale_tip_check_time) {
3225-
LOCK(cs_main);
32263225
// Check whether our tip is stale, and if so, allow using an extra
32273226
// outbound peer
3228-
if (TipMayBeStale(consensusParams)) {
3227+
if (!fImporting && !fReindex && connman->GetNetworkActive() && connman->GetUseAddrmanOutgoing() && TipMayBeStale(consensusParams)) {
32293228
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);
32303229
connman->SetTryNewOutboundPeer(true);
32313230
} else if (connman->GetTryNewOutboundPeer()) {

src/net_processing.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <net.h>
1010
#include <validationinterface.h>
1111
#include <consensus/params.h>
12+
#include <sync.h>
13+
14+
extern CCriticalSection cs_main;
1215

1316
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
1417
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
@@ -65,7 +68,7 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
6568
/** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */
6669
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
6770
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
68-
void EvictExtraOutboundPeers(int64_t time_in_seconds);
71+
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
6972

7073
private:
7174
int64_t m_stale_tip_check_time; //! Next time to check for stale tip

0 commit comments

Comments
 (0)