@@ -352,7 +352,7 @@ static CAddress GetBindAddress(SOCKET sock)
352
352
return addr_bind;
353
353
}
354
354
355
- CNode* CConnman::ConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure , bool manual_connection)
355
+ CNode* CConnman::ConnectNode (CAddress addrConnect, const char *pszDest, bool fCountFailure , bool manual_connection, bool block_relay_only )
356
356
{
357
357
if (pszDest == nullptr ) {
358
358
if (IsLocal (addrConnect))
@@ -442,7 +442,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
442
442
NodeId id = GetNewNodeId ();
443
443
uint64_t nonce = GetDeterministicRandomizer (RANDOMIZER_ID_LOCALHOSTNONCE).Write (id).Finalize ();
444
444
CAddress addr_bind = GetBindAddress (hSocket);
445
- CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , false );
445
+ CNode* pnode = new CNode (id, nLocalServices, GetBestHeight (), hSocket, addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , false , block_relay_only );
446
446
pnode->AddRef ();
447
447
448
448
return pnode;
@@ -905,7 +905,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
905
905
SOCKET hSocket = accept (hListenSocket.socket , (struct sockaddr *)&sockaddr, &len);
906
906
CAddress addr;
907
907
int nInbound = 0 ;
908
- int nMaxInbound = nMaxConnections - (nMaxOutbound + nMaxFeeler) ;
908
+ int nMaxInbound = nMaxConnections - m_max_outbound ;
909
909
910
910
if (hSocket != INVALID_SOCKET) {
911
911
if (!addr.SetSockAddr ((const struct sockaddr *)&sockaddr)) {
@@ -1666,7 +1666,7 @@ int CConnman::GetExtraOutboundCount()
1666
1666
}
1667
1667
}
1668
1668
}
1669
- return std::max (nOutbound - nMaxOutbound , 0 );
1669
+ return std::max (nOutbound - m_max_outbound_full_relay - m_max_outbound_block_relay , 0 );
1670
1670
}
1671
1671
1672
1672
void CConnman::ThreadOpenConnections (const std::vector<std::string> connect)
@@ -1726,7 +1726,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1726
1726
CAddress addrConnect;
1727
1727
1728
1728
// Only connect out to one peer per network group (/16 for IPv4).
1729
- int nOutbound = 0 ;
1729
+ int nOutboundFullRelay = 0 ;
1730
+ int nOutboundBlockRelay = 0 ;
1730
1731
std::set<std::vector<unsigned char > > setConnected;
1731
1732
{
1732
1733
LOCK (cs_vNodes);
@@ -1738,7 +1739,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1738
1739
// also have the added issue that they're attacker controlled and could be used
1739
1740
// to prevent us from connecting to particular hosts if we used them here.
1740
1741
setConnected.insert (pnode->addr .GetGroup ());
1741
- nOutbound++;
1742
+ if (pnode->m_tx_relay == nullptr ) {
1743
+ nOutboundBlockRelay++;
1744
+ } else if (!pnode->fFeeler ) {
1745
+ nOutboundFullRelay++;
1746
+ }
1742
1747
}
1743
1748
}
1744
1749
}
@@ -1757,7 +1762,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1757
1762
//
1758
1763
bool fFeeler = false ;
1759
1764
1760
- if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer ()) {
1765
+ if (nOutboundFullRelay >= m_max_outbound_full_relay && nOutboundBlockRelay >= m_max_outbound_block_relay && !GetTryNewOutboundPeer ()) {
1761
1766
int64_t nTime = GetTimeMicros (); // The current time right now (in microseconds).
1762
1767
if (nTime > nNextFeeler) {
1763
1768
nNextFeeler = PoissonNextSend (nTime, FEELER_INTERVAL);
@@ -1831,7 +1836,14 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1831
1836
LogPrint (BCLog::NET, " Making feeler connection to %s\n " , addrConnect.ToString ());
1832
1837
}
1833
1838
1834
- OpenNetworkConnection (addrConnect, (int )setConnected.size () >= std::min (nMaxConnections - 1 , 2 ), &grant, nullptr , false , fFeeler );
1839
+ // Open this connection as block-relay-only if we're already at our
1840
+ // full-relay capacity, but not yet at our block-relay peer limit.
1841
+ // (It should not be possible for fFeeler to be set if we're not
1842
+ // also at our block-relay peer limit, but check against that as
1843
+ // well for sanity.)
1844
+ bool block_relay_only = nOutboundBlockRelay < m_max_outbound_block_relay && !fFeeler && nOutboundFullRelay >= m_max_outbound_full_relay;
1845
+
1846
+ OpenNetworkConnection (addrConnect, (int )setConnected.size () >= std::min (nMaxConnections - 1 , 2 ), &grant, nullptr , false , fFeeler , false , block_relay_only);
1835
1847
}
1836
1848
}
1837
1849
}
@@ -1918,7 +1930,7 @@ void CConnman::ThreadOpenAddedConnections()
1918
1930
}
1919
1931
1920
1932
// if successful, this moves the passed grant to the constructed node
1921
- void CConnman::OpenNetworkConnection (const CAddress& addrConnect, bool fCountFailure , CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot , bool fFeeler , bool manual_connection)
1933
+ void CConnman::OpenNetworkConnection (const CAddress& addrConnect, bool fCountFailure , CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot , bool fFeeler , bool manual_connection, bool block_relay_only )
1922
1934
{
1923
1935
//
1924
1936
// Initiate outbound network connection
@@ -1937,7 +1949,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
1937
1949
} else if (FindNode (std::string (pszDest)))
1938
1950
return ;
1939
1951
1940
- CNode* pnode = ConnectNode (addrConnect, pszDest, fCountFailure , manual_connection);
1952
+ CNode* pnode = ConnectNode (addrConnect, pszDest, fCountFailure , manual_connection, block_relay_only );
1941
1953
1942
1954
if (!pnode)
1943
1955
return ;
@@ -2240,7 +2252,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
2240
2252
2241
2253
if (semOutbound == nullptr ) {
2242
2254
// initialize semaphore
2243
- semOutbound = MakeUnique<CSemaphore>(std::min ((nMaxOutbound + nMaxFeeler) , nMaxConnections));
2255
+ semOutbound = MakeUnique<CSemaphore>(std::min (m_max_outbound , nMaxConnections));
2244
2256
}
2245
2257
if (semAddnode == nullptr ) {
2246
2258
// initialize semaphore
@@ -2318,7 +2330,7 @@ void CConnman::Interrupt()
2318
2330
InterruptSocks5 (true );
2319
2331
2320
2332
if (semOutbound) {
2321
- for (int i=0 ; i<(nMaxOutbound + nMaxFeeler) ; i++) {
2333
+ for (int i=0 ; i<m_max_outbound ; i++) {
2322
2334
semOutbound->post ();
2323
2335
}
2324
2336
}
@@ -2628,7 +2640,7 @@ int CConnman::GetBestHeight() const
2628
2640
2629
2641
unsigned int CConnman::GetReceiveFloodSize () const { return nReceiveFloodSize; }
2630
2642
2631
- CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, bool fInboundIn )
2643
+ CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, bool fInboundIn , bool block_relay_only )
2632
2644
: nTimeConnected(GetSystemTimeInSeconds()),
2633
2645
addr(addrIn),
2634
2646
addrBind(addrBindIn),
@@ -2643,7 +2655,9 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
2643
2655
hSocket = hSocketIn;
2644
2656
addrName = addrNameIn == " " ? addr.ToStringIPPort () : addrNameIn;
2645
2657
hashContinue = uint256 ();
2646
- m_tx_relay = MakeUnique<TxRelay>();
2658
+ if (!block_relay_only) {
2659
+ m_tx_relay = MakeUnique<TxRelay>();
2660
+ }
2647
2661
2648
2662
for (const std::string &msg : getAllNetMessageTypes ())
2649
2663
mapRecvBytesPerMsgCmd[msg] = 0 ;
0 commit comments