diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -174,14 +174,18 @@ void Init(const Options &connOptions) { nLocalServices = connOptions.nLocalServices; nMaxConnections = connOptions.nMaxConnections; - m_max_outbound_full_relay = std::min( - connOptions.m_max_outbound_full_relay, connOptions.nMaxConnections); - m_max_outbound_block_relay = connOptions.m_max_outbound_block_relay; m_use_addrman_outgoing = connOptions.m_use_addrman_outgoing; nMaxAddnode = connOptions.nMaxAddnode; nMaxFeeler = connOptions.nMaxFeeler; - m_max_outbound = - m_max_outbound_full_relay + m_max_outbound_block_relay + nMaxFeeler; + { + LOCK(cs_maxOutboundPeers); + m_max_outbound_full_relay = + std::min(connOptions.m_max_outbound_full_relay, + connOptions.nMaxConnections); + m_max_outbound_block_relay = connOptions.m_max_outbound_block_relay; + m_max_outbound = m_max_outbound_full_relay + + m_max_outbound_block_relay + nMaxFeeler; + } nBestHeight = connOptions.nBestHeight; clientInterface = connOptions.uiInterface; m_banman = connOptions.m_banman; @@ -475,12 +479,12 @@ std::unique_ptr semAddnode; int nMaxConnections; + RecursiveMutex cs_maxOutboundPeers; // How many full-relay (tx, block, addr) outbound peers we want - int m_max_outbound_full_relay; - + int m_max_outbound_full_relay GUARDED_BY(cs_maxOutboundPeers); // How many block-relay only outbound peers we want // We do not relay tx or addr messages with these peers - int m_max_outbound_block_relay; + int m_max_outbound_block_relay GUARDED_BY(cs_maxOutboundPeers); int nMaxAddnode; int nMaxFeeler; diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -1918,6 +1918,7 @@ } } } + LOCK(cs_maxOutboundPeers); return std::max( nOutbound - m_max_outbound_full_relay - m_max_outbound_block_relay, 0); } @@ -2023,16 +2024,19 @@ // bool fFeeler = false; - if (nOutboundFullRelay >= m_max_outbound_full_relay && - nOutboundBlockRelay >= m_max_outbound_block_relay && - !GetTryNewOutboundPeer()) { - // The current time right now (in microseconds). - int64_t nTime = GetTimeMicros(); - if (nTime > nNextFeeler) { - nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL); - fFeeler = true; - } else { - continue; + { + LOCK(cs_maxOutboundPeers); + if (nOutboundFullRelay >= m_max_outbound_full_relay && + nOutboundBlockRelay >= m_max_outbound_block_relay && + !GetTryNewOutboundPeer()) { + // The current time right now (in microseconds). + int64_t nTime = GetTimeMicros(); + if (nTime > nNextFeeler) { + nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL); + fFeeler = true; + } else { + continue; + } } } @@ -2119,9 +2123,13 @@ // (It should not be possible for fFeeler to be set if we're not // also at our block-relay peer limit, but check against that as // well for sanity.) - bool block_relay_only = - nOutboundBlockRelay < m_max_outbound_block_relay && !fFeeler && - nOutboundFullRelay >= m_max_outbound_full_relay; + bool block_relay_only; + { + LOCK(cs_maxOutboundPeers); + block_relay_only = + nOutboundBlockRelay < m_max_outbound_block_relay && + !fFeeler && nOutboundFullRelay >= m_max_outbound_full_relay; + } OpenNetworkConnection( addrConnect,