Skip to content

Commit 0a85e5a

Browse files
committed
p2p: Try to connect to anchors once
1 parent 5543c7a commit 0a85e5a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/net.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,18 +1930,23 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19301930

19311931
ConnectionType conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
19321932
int64_t nTime = GetTimeMicros();
1933+
bool anchor = false;
19331934
bool fFeeler = false;
19341935

19351936
// Determine what type of connection to open. Opening
1936-
// OUTBOUND_FULL_RELAY connections gets the highest priority until we
1937+
// BLOCK_RELAY connections to addresses from anchors.dat gets the highest
1938+
// priority. Then we open OUTBOUND_FULL_RELAY priority until we
19371939
// meet our full-relay capacity. Then we open BLOCK_RELAY connection
19381940
// until we hit our block-relay-only peer limit.
19391941
// GetTryNewOutboundPeer() gets set when a stale tip is detected, so we
19401942
// try opening an additional OUTBOUND_FULL_RELAY connection. If none of
19411943
// these conditions are met, check the nNextFeeler timer to decide if
19421944
// we should open a FEELER.
19431945

1944-
if (nOutboundFullRelay < m_max_outbound_full_relay) {
1946+
if (!m_anchors.empty() && (nOutboundBlockRelay < m_max_outbound_block_relay)) {
1947+
conn_type = ConnectionType::BLOCK_RELAY;
1948+
anchor = true;
1949+
} else if (nOutboundFullRelay < m_max_outbound_full_relay) {
19451950
// OUTBOUND_FULL_RELAY
19461951
} else if (nOutboundBlockRelay < m_max_outbound_block_relay) {
19471952
conn_type = ConnectionType::BLOCK_RELAY;
@@ -1962,6 +1967,17 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19621967
int nTries = 0;
19631968
while (!interruptNet)
19641969
{
1970+
if (anchor && !m_anchors.empty()) {
1971+
const CAddress addr = m_anchors.back();
1972+
m_anchors.pop_back();
1973+
if (!addr.IsValid() || IsLocal(addr) || !IsReachable(addr) ||
1974+
!HasAllDesirableServiceFlags(addr.nServices) ||
1975+
setConnected.count(addr.GetGroup(addrman.m_asmap))) continue;
1976+
addrConnect = addr;
1977+
LogPrint(BCLog::NET, "Trying to make an anchor connection to %s\n", addrConnect.ToString());
1978+
break;
1979+
}
1980+
19651981
// If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
19661982
// stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
19671983
// already-connected network ranges, ...) before trying new addrman addresses.

src/net.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ enum class ConnectionType {
174174
* attacks. By not relaying transactions or addresses, these connections
175175
* are harder to detect by a third party, thus helping obfuscate the
176176
* network topology. We automatically attempt to open
177-
* MAX_BLOCK_RELAY_ONLY_CONNECTIONS using addresses from our AddrMan.
177+
* MAX_BLOCK_RELAY_ONLY_ANCHORS using addresses from our anchors.dat. Then
178+
* addresses from our AddrMan if MAX_BLOCK_RELAY_ONLY_CONNECTIONS
179+
* isn't reached yet.
178180
*/
179181
BLOCK_RELAY,
180182

0 commit comments

Comments
 (0)