mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-19 02:13:37 +02:00
Merge bitcoin/bitcoin#34741: refactor: Return std::optional from GetNameProxy/GetProxy
fa73ed467crefactor: Fix redundant conversion to std::string and then to std::string_view [performance-string-view-conversions] (MarcoFalke)fa270fdacfrefactor: Return std::optional from GetProxy (MarcoFalke)faeac1a931refactor: Return std::optional from GetNameProxy (MarcoFalke) Pull request description: Currently the getters have a mutable reference as inout param and return a bool to indicate success. This is confusing, because the success bool is redundant with the `IsValid()` state on the proxy object. So in theory, the functions could reset the mutable proxy object to an invalid state and return `void`. However, this would also be confusing, because devs can forget to check `IsValid()`. Fix all issues by using `std::optional<Proxy>`, where devs no longer have to check `IsValid()` manually, or a separate bool. Note that new code in the repo is already using `std::optional<Proxy>`, see `git grep 'std::optional<Proxy>' bitcoin-core/master`. Also, `std::optional<Proxy>` will enforce checking at compile time, whereas calling `Proxy::IsValid` is not enforced. ACKs for top commit: achow101: ACKfa73ed467csedited: ACKfa73ed467cViniciusCestarii: ACKfa73ed467cfrankomosh: Code Review ACKfa73ed467c. Good refactor, correctly replaces the bool + mutable reference output parameter pattern with `std::optional<Proxy>` across `GetProxy` and `GetNameProxy`. Semantics are preserved. Tree-SHA512: c6a1e1d1691958d2e6507e32e3484f96703fba03ccc710145ae2fb84b1254fb0e6e1d8d78e9b572daf5ea485247b73568704881762379b50bcf939a35494dd13
This commit is contained in:
49
src/net.cpp
49
src/net.cpp
@@ -439,20 +439,15 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
|
||||
|
||||
// Connect
|
||||
std::unique_ptr<Sock> sock;
|
||||
Proxy proxy;
|
||||
CService addr_bind;
|
||||
assert(!addr_bind.IsValid());
|
||||
std::unique_ptr<i2p::sam::Session> i2p_transient_session;
|
||||
|
||||
for (auto& target_addr: connect_to) {
|
||||
for (auto& target_addr : connect_to) {
|
||||
if (target_addr.IsValid()) {
|
||||
bool use_proxy;
|
||||
if (proxy_override.has_value()) {
|
||||
use_proxy = true;
|
||||
proxy = proxy_override.value();
|
||||
} else {
|
||||
use_proxy = GetProxy(target_addr.GetNetwork(), proxy);
|
||||
}
|
||||
const std::optional<Proxy> use_proxy{
|
||||
proxy_override.has_value() ? proxy_override : GetProxy(target_addr.GetNetwork()),
|
||||
};
|
||||
bool proxyConnectionFailed = false;
|
||||
|
||||
if (target_addr.IsI2P() && use_proxy) {
|
||||
@@ -469,7 +464,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
|
||||
LOCK(m_unused_i2p_sessions_mutex);
|
||||
if (m_unused_i2p_sessions.empty()) {
|
||||
i2p_transient_session =
|
||||
std::make_unique<i2p::sam::Session>(proxy, m_interrupt_net);
|
||||
std::make_unique<i2p::sam::Session>(*use_proxy, m_interrupt_net);
|
||||
} else {
|
||||
i2p_transient_session.swap(m_unused_i2p_sessions.front());
|
||||
m_unused_i2p_sessions.pop();
|
||||
@@ -489,8 +484,8 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
|
||||
addr_bind = conn.me;
|
||||
}
|
||||
} else if (use_proxy) {
|
||||
LogDebug(BCLog::PROXY, "Using proxy: %s to connect to %s\n", proxy.ToString(), target_addr.ToStringAddrPort());
|
||||
sock = ConnectThroughProxy(proxy, target_addr.ToStringAddr(), target_addr.GetPort(), proxyConnectionFailed);
|
||||
LogDebug(BCLog::PROXY, "Using proxy: %s to connect to %s\n", use_proxy->ToString(), target_addr.ToStringAddrPort());
|
||||
sock = ConnectThroughProxy(*use_proxy, target_addr.ToStringAddr(), target_addr.GetPort(), proxyConnectionFailed);
|
||||
} else {
|
||||
// no proxy needed (none set for target network)
|
||||
sock = ConnectDirectly(target_addr, conn_type == ConnectionType::MANUAL);
|
||||
@@ -500,12 +495,14 @@ CNode* CConnman::ConnectNode(CAddress addrConnect,
|
||||
// the proxy, mark this as an attempt.
|
||||
addrman.get().Attempt(target_addr, fCountFailure);
|
||||
}
|
||||
} else if (pszDest && GetNameProxy(proxy)) {
|
||||
std::string host;
|
||||
uint16_t port{default_port};
|
||||
SplitHostPort(std::string(pszDest), port, host);
|
||||
bool proxyConnectionFailed;
|
||||
sock = ConnectThroughProxy(proxy, host, port, proxyConnectionFailed);
|
||||
} else if (pszDest) {
|
||||
if (const auto name_proxy = GetNameProxy()) {
|
||||
std::string host;
|
||||
uint16_t port{default_port};
|
||||
SplitHostPort(pszDest, port, host);
|
||||
bool proxyConnectionFailed;
|
||||
sock = ConnectThroughProxy(*name_proxy, host, port, proxyConnectionFailed);
|
||||
}
|
||||
}
|
||||
// Check any other resolved address (if any) if we fail to connect
|
||||
if (!sock) {
|
||||
@@ -3120,9 +3117,10 @@ void CConnman::PrivateBroadcast::NumToOpenWait() const
|
||||
|
||||
std::optional<Proxy> CConnman::PrivateBroadcast::ProxyForIPv4or6() const
|
||||
{
|
||||
Proxy tor_proxy;
|
||||
if (m_outbound_tor_ok_at_least_once.load() && GetProxy(NET_ONION, tor_proxy)) {
|
||||
return tor_proxy;
|
||||
if (m_outbound_tor_ok_at_least_once.load()) {
|
||||
if (const auto tor_proxy = GetProxy(NET_ONION)) {
|
||||
return tor_proxy;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -3479,10 +3477,11 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
|
||||
return false;
|
||||
}
|
||||
|
||||
Proxy i2p_sam;
|
||||
if (GetProxy(NET_I2P, i2p_sam) && connOptions.m_i2p_accept_incoming) {
|
||||
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(gArgs.GetDataDirNet() / "i2p_private_key",
|
||||
i2p_sam, m_interrupt_net);
|
||||
if (connOptions.m_i2p_accept_incoming) {
|
||||
if (const auto i2p_sam = GetProxy(NET_I2P)) {
|
||||
m_i2p_sam_session = std::make_unique<i2p::sam::Session>(gArgs.GetDataDirNet() / "i2p_private_key",
|
||||
*i2p_sam, m_interrupt_net);
|
||||
}
|
||||
}
|
||||
|
||||
// Randomize the order in which we may query seednode to potentially prevent connecting to the same one every restart (and signal that we have restarted)
|
||||
|
||||
Reference in New Issue
Block a user