mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
Merge bitcoin/bitcoin#27213: p2p: Diversify automatic outbound connections with respect to networks
1b52d16d07p2p: network-specific management of outbound connections (Martin Zumsande)65cff00ceetest: Add test for outbound protection by network (Martin Zumsande)034f61f83bp2p: Protect extra full outbound peers by network (Martin Zumsande)654d9bc276p2p: Introduce data struct to track connection counts by network (Amiti Uttarwar) Pull request description: This is joint work with mzumsande. This is a proposal to diversify outbound connections with respect to reachable networks. The existing logic evaluates peers for connection based purely on the frequency of available addresses in `AddrMan`. This PR adds logic to automatically connect to alternate reachable networks and adds eviction logic that protects one existing connection to each network. For instance, if `AddrMan` is populated primarily with IPv4 and IPv6 addresses and only a handful of onion addresses, it is likely that we won't establish any automatic outbound connections to Tor, even if we're capable of doing so. For smaller networks like CJDNS, this is even more of an issue and often requires adding manual peers to ensure regularly being connected to the network. Connecting to multiple networks improves resistance to eclipse attacks for individual nodes. It also benefits the entire p2p network by increasing partition resistance and privacy in general. The automatic connections to alternate networks is done defensively, by first filling all outbound slots with random addresses (as in the status quo) and then adding additional peers from reachable networks the node is currently not connected to. This approach ensures that outbound slots are not left unfilled while attempting to connect to a network that may be unavailable due to a technical issue or misconfiguration that bitcoind cannot detect. Once an additional peer is added and we have one more outbound connection than we want, outbound eviction ensures that peers are protected if they are the only ones for their network. Manual connections are also taken into account: If a user already establishes manual connections to a trusted peer from a network, there is no longer a need to make extra efforts to ensure we also have an automatic connection to it (although this may of course happen by random selection). ACKs for top commit: naumenkogs: ACK1b52d16d07vasild: ACK1b52d16d07Tree-SHA512: 5616c038a5fbb868d4c46c5963cfd53e4599feee25db04b0e18da426d77d22e0994dc4e1da0b810f5b457f424ebbed3db1704f371aa6cad002b3565b20170ec0
This commit is contained in:
52
src/net.cpp
52
src/net.cpp
@@ -83,6 +83,9 @@ static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24};
|
||||
// A random time period (0 to 1 seconds) is added to feeler connections to prevent synchronization.
|
||||
static constexpr auto FEELER_SLEEP_WINDOW{1s};
|
||||
|
||||
/** Frequency to attempt extra connections to reachable networks we're not connected to yet **/
|
||||
static constexpr auto EXTRA_NETWORK_PEER_INTERVAL{5min};
|
||||
|
||||
/** Used to pass flags to the Bind() function */
|
||||
enum BindFlags {
|
||||
BF_NONE = 0,
|
||||
@@ -1138,6 +1141,9 @@ void CConnman::DisconnectNodes()
|
||||
// close socket and cleanup
|
||||
pnode->CloseSocketDisconnect();
|
||||
|
||||
// update connection count by network
|
||||
if (pnode->IsManualOrFullOutboundConn()) --m_network_conn_counts[pnode->addr.GetNetwork()];
|
||||
|
||||
// hold in disconnected pool until all refs are released
|
||||
pnode->Release();
|
||||
m_nodes_disconnected.push_back(pnode);
|
||||
@@ -1605,6 +1611,28 @@ std::unordered_set<Network> CConnman::GetReachableEmptyNetworks() const
|
||||
return networks;
|
||||
}
|
||||
|
||||
bool CConnman::MultipleManualOrFullOutboundConns(Network net) const
|
||||
{
|
||||
AssertLockHeld(m_nodes_mutex);
|
||||
return m_network_conn_counts[net] > 1;
|
||||
}
|
||||
|
||||
bool CConnman::MaybePickPreferredNetwork(std::optional<Network>& network)
|
||||
{
|
||||
std::array<Network, 5> nets{NET_IPV4, NET_IPV6, NET_ONION, NET_I2P, NET_CJDNS};
|
||||
Shuffle(nets.begin(), nets.end(), FastRandomContext());
|
||||
|
||||
LOCK(m_nodes_mutex);
|
||||
for (const auto net : nets) {
|
||||
if (IsReachable(net) && m_network_conn_counts[net] == 0 && addrman.Size(net) != 0) {
|
||||
network = net;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
{
|
||||
AssertLockNotHeld(m_unused_i2p_sessions_mutex);
|
||||
@@ -1635,6 +1663,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
// Minimum time before next feeler connection (in microseconds).
|
||||
auto next_feeler = GetExponentialRand(start, FEELER_INTERVAL);
|
||||
auto next_extra_block_relay = GetExponentialRand(start, EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
|
||||
auto next_extra_network_peer{GetExponentialRand(start, EXTRA_NETWORK_PEER_INTERVAL)};
|
||||
const bool dnsseed = gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED);
|
||||
bool add_fixed_seeds = gArgs.GetBoolArg("-fixedseeds", DEFAULT_FIXEDSEEDS);
|
||||
const bool use_seednodes{gArgs.IsArgSet("-seednode")};
|
||||
@@ -1747,6 +1776,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
auto now = GetTime<std::chrono::microseconds>();
|
||||
bool anchor = false;
|
||||
bool fFeeler = false;
|
||||
std::optional<Network> preferred_net;
|
||||
|
||||
// Determine what type of connection to open. Opening
|
||||
// BLOCK_RELAY connections to addresses from anchors.dat gets the highest
|
||||
@@ -1796,6 +1826,17 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
next_feeler = GetExponentialRand(now, FEELER_INTERVAL);
|
||||
conn_type = ConnectionType::FEELER;
|
||||
fFeeler = true;
|
||||
} else if (nOutboundFullRelay == m_max_outbound_full_relay &&
|
||||
m_max_outbound_full_relay == MAX_OUTBOUND_FULL_RELAY_CONNECTIONS &&
|
||||
now > next_extra_network_peer &&
|
||||
MaybePickPreferredNetwork(preferred_net)) {
|
||||
// Full outbound connection management: Attempt to get at least one
|
||||
// outbound peer from each reachable network by making extra connections
|
||||
// and then protecting "only" peers from a network during outbound eviction.
|
||||
// This is not attempted if the user changed -maxconnections to a value
|
||||
// so low that less than MAX_OUTBOUND_FULL_RELAY_CONNECTIONS are made,
|
||||
// to prevent interactions with otherwise protected outbound peers.
|
||||
next_extra_network_peer = GetExponentialRand(now, EXTRA_NETWORK_PEER_INTERVAL);
|
||||
} else {
|
||||
// skip to next iteration of while loop
|
||||
continue;
|
||||
@@ -1849,7 +1890,10 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
}
|
||||
} else {
|
||||
// Not a feeler
|
||||
std::tie(addr, addr_last_try) = addrman.Select();
|
||||
// If preferred_net has a value set, pick an extra outbound
|
||||
// peer from that network. The eviction logic in net_processing
|
||||
// ensures that a peer from another network will be evicted.
|
||||
std::tie(addr, addr_last_try) = addrman.Select(false, preferred_net);
|
||||
}
|
||||
|
||||
// Require outbound IPv4/IPv6 connections, other than feelers, to be to distinct network groups
|
||||
@@ -1896,6 +1940,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
||||
}
|
||||
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToStringAddrPort());
|
||||
}
|
||||
|
||||
if (preferred_net != std::nullopt) LogPrint(BCLog::NET, "Making network specific connection to %s on %s.\n", addrConnect.ToStringAddrPort(), GetNetworkName(preferred_net.value()));
|
||||
|
||||
// Record addrman failure attempts when node has at least 2 persistent outbound connections to peers with
|
||||
// different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
|
||||
// Don't record addrman failure attempts when node is offline. This can be identified since all local
|
||||
@@ -2035,6 +2082,9 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
|
||||
{
|
||||
LOCK(m_nodes_mutex);
|
||||
m_nodes.push_back(pnode);
|
||||
|
||||
// update connection count by network
|
||||
if (pnode->IsManualOrFullOutboundConn()) ++m_network_conn_counts[pnode->addr.GetNetwork()];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user