Skip stale tip checking if outbound connections are off or if reindexing.

This commit is contained in:
Gregory Maxwell 2018-08-22 23:29:01 +00:00
parent 271b379e63
commit 66b3fc5437
3 changed files with 10 additions and 5 deletions

View File

@ -149,6 +149,7 @@ public:
nLocalServices = connOptions.nLocalServices; nLocalServices = connOptions.nLocalServices;
nMaxConnections = connOptions.nMaxConnections; nMaxConnections = connOptions.nMaxConnections;
nMaxOutbound = std::min(connOptions.nMaxOutbound, connOptions.nMaxConnections); nMaxOutbound = std::min(connOptions.nMaxOutbound, connOptions.nMaxConnections);
m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing;
nMaxAddnode = connOptions.nMaxAddnode; nMaxAddnode = connOptions.nMaxAddnode;
nMaxFeeler = connOptions.nMaxFeeler; nMaxFeeler = connOptions.nMaxFeeler;
nBestHeight = connOptions.nBestHeight; nBestHeight = connOptions.nBestHeight;
@ -174,6 +175,7 @@ public:
void Stop(); void Stop();
void Interrupt(); void Interrupt();
bool GetNetworkActive() const { return fNetworkActive; }; bool GetNetworkActive() const { return fNetworkActive; };
bool GetUseAddrmanOutgoing() const { return m_use_addrman_outgoing; };
void SetNetworkActive(bool active); void SetNetworkActive(bool active);
void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool fOneShot = false, bool fFeeler = false, bool manual_connection = false); void OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = nullptr, const char *strDest = nullptr, bool fOneShot = false, bool fFeeler = false, bool manual_connection = false);
bool CheckIncomingNonce(uint64_t nonce); bool CheckIncomingNonce(uint64_t nonce);
@ -416,6 +418,7 @@ private:
int nMaxOutbound; int nMaxOutbound;
int nMaxAddnode; int nMaxAddnode;
int nMaxFeeler; int nMaxFeeler;
bool m_use_addrman_outgoing;
std::atomic<int> nBestHeight; std::atomic<int> nBestHeight;
CClientUIInterface* clientInterface; CClientUIInterface* clientInterface;
NetEventsInterface* m_msgproc; NetEventsInterface* m_msgproc;

View File

@ -3166,8 +3166,6 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
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);
@ -3215,6 +3213,8 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams) void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams)
{ {
LOCK(cs_main);
if (connman == nullptr) return; if (connman == nullptr) return;
int64_t time_in_seconds = GetTime(); int64_t time_in_seconds = GetTime();
@ -3222,10 +3222,9 @@ void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params
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) {
LOCK(cs_main);
// Check whether our tip is stale, and if so, allow using an extra // Check whether our tip is stale, and if so, allow using an extra
// outbound peer // 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); 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);
connman->SetTryNewOutboundPeer(true); connman->SetTryNewOutboundPeer(true);
} else if (connman->GetTryNewOutboundPeer()) { } else if (connman->GetTryNewOutboundPeer()) {

View File

@ -9,6 +9,9 @@
#include <net.h> #include <net.h>
#include <validationinterface.h> #include <validationinterface.h>
#include <consensus/params.h> #include <consensus/params.h>
#include <sync.h>
extern CCriticalSection cs_main;
/** Default for -maxorphantx, maximum number of orphan transactions kept in memory */ /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100; static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
@ -65,7 +68,7 @@ public:
/** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */ /** Evict extra outbound peers. If we think our tip may be stale, connect to an extra outbound */
void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams); void CheckForStaleTipAndEvictPeers(const Consensus::Params &consensusParams);
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */ /** 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: private:
int64_t m_stale_tip_check_time; //! Next time to check for stale tip int64_t m_stale_tip_check_time; //! Next time to check for stale tip