Skip to content

Commit d405145

Browse files
committed
p2p: Try to connect to anchors once
1 parent 2dcabcb commit d405145

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/net.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17921792
}
17931793
}
17941794

1795+
bool anchor{false};
1796+
17951797
addrman.ResolveCollisions();
17961798

17971799
int64_t nANow = GetAdjustedTime();
@@ -1805,11 +1807,24 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18051807
if (nTries > 100)
18061808
break;
18071809

1808-
CAddrInfo addr = addrman.SelectTriedCollision();
1810+
CAddress addr;
1811+
if (!m_anchors.empty() && (nOutboundBlockRelay < m_max_outbound_block_relay) && !fFeeler) {
1812+
anchor = true;
1813+
addr = m_anchors.back();
1814+
m_anchors.pop_back();
1815+
} else {
1816+
CAddrInfo addr_info = addrman.SelectTriedCollision();
1817+
1818+
// SelectTriedCollision returns an invalid address if it is empty.
1819+
if (!fFeeler || !addr_info.IsValid()) {
1820+
addr_info = addrman.Select(fFeeler);
1821+
}
1822+
1823+
// only consider very recently tried nodes after 30 failed attempts
1824+
if (nANow - addr_info.nLastTry < 600 && nTries < 30)
1825+
continue;
18091826

1810-
// SelectTriedCollision returns an invalid address if it is empty.
1811-
if (!fFeeler || !addr.IsValid()) {
1812-
addr = addrman.Select(fFeeler);
1827+
addr = static_cast<CAddress>(addr_info);
18131828
}
18141829

18151830
// Require outbound connections, other than feelers, to be to distinct network groups
@@ -1825,10 +1840,6 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18251840
if (!IsReachable(addr))
18261841
continue;
18271842

1828-
// only consider very recently tried nodes after 30 failed attempts
1829-
if (nANow - addr.nLastTry < 600 && nTries < 30)
1830-
continue;
1831-
18321843
// for non-feelers, require all the services we'll want,
18331844
// for feelers, only require they be a full node (only because most
18341845
// SPV clients don't have a good address DB available)
@@ -1863,6 +1874,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18631874
// well for sanity.)
18641875
bool block_relay_only = nOutboundBlockRelay < m_max_outbound_block_relay && !fFeeler && nOutboundFullRelay >= m_max_outbound_full_relay;
18651876

1877+
if (anchor) {
1878+
block_relay_only = true;
1879+
LogPrintf("Making anchor connection to %s\n", addrConnect.ToString());
1880+
}
1881+
18661882
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, false, fFeeler, false, block_relay_only);
18671883
}
18681884
}

0 commit comments

Comments
 (0)