Merge #15138: Drop IsLimited in favor of IsReachable

d6b076c17b Drop IsLimited in favor of IsReachable (Ben Woosley)

Pull request description:

  These two methods have had the same meaning, but inverted, since
  110b62f069. Having one name for a single
  concept simplifies the code.

  This is a follow-up to #15051.
  /cc #7553

Tree-SHA512: 347ceb9e2a55ea06f4c01226411c7bbcade09dd82130e4c59d0824ecefd960875938022edbe5d4bfdf12b0552c9b4cb78b09a688284d707119571daf4eb371b4
This commit is contained in:
Wladimir J. van der Laan 2019-01-14 14:22:47 +01:00
commit 43a79d22c1
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
6 changed files with 44 additions and 65 deletions

View file

@ -1321,7 +1321,7 @@ bool AppInitMain(InitInterfaces& interfaces)
for (int n = 0; n < NET_MAX; n++) { for (int n = 0; n < NET_MAX; n++) {
enum Network net = (enum Network)n; enum Network net = (enum Network)n;
if (!nets.count(net)) if (!nets.count(net))
SetLimited(net); SetReachable(net, false);
} }
} }
@ -1332,7 +1332,7 @@ bool AppInitMain(InitInterfaces& interfaces)
// -proxy sets a proxy for all outgoing network traffic // -proxy sets a proxy for all outgoing network traffic
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default // -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
std::string proxyArg = gArgs.GetArg("-proxy", ""); std::string proxyArg = gArgs.GetArg("-proxy", "");
SetLimited(NET_ONION); SetReachable(NET_ONION, false);
if (proxyArg != "" && proxyArg != "0") { if (proxyArg != "" && proxyArg != "0") {
CService proxyAddr; CService proxyAddr;
if (!Lookup(proxyArg.c_str(), proxyAddr, 9050, fNameLookup)) { if (!Lookup(proxyArg.c_str(), proxyAddr, 9050, fNameLookup)) {
@ -1347,7 +1347,7 @@ bool AppInitMain(InitInterfaces& interfaces)
SetProxy(NET_IPV6, addrProxy); SetProxy(NET_IPV6, addrProxy);
SetProxy(NET_ONION, addrProxy); SetProxy(NET_ONION, addrProxy);
SetNameProxy(addrProxy); SetNameProxy(addrProxy);
SetLimited(NET_ONION, false); // by default, -proxy sets onion as reachable, unless -noonion later SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
} }
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses // -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
@ -1356,7 +1356,7 @@ bool AppInitMain(InitInterfaces& interfaces)
std::string onionArg = gArgs.GetArg("-onion", ""); std::string onionArg = gArgs.GetArg("-onion", "");
if (onionArg != "") { if (onionArg != "") {
if (onionArg == "0") { // Handle -noonion/-onion=0 if (onionArg == "0") { // Handle -noonion/-onion=0
SetLimited(NET_ONION); // set onions as unreachable SetReachable(NET_ONION, false);
} else { } else {
CService onionProxy; CService onionProxy;
if (!Lookup(onionArg.c_str(), onionProxy, 9050, fNameLookup)) { if (!Lookup(onionArg.c_str(), onionProxy, 9050, fNameLookup)) {
@ -1366,7 +1366,7 @@ bool AppInitMain(InitInterfaces& interfaces)
if (!addrOnion.IsValid()) if (!addrOnion.IsValid())
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg)); return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
SetProxy(NET_ONION, addrOnion); SetProxy(NET_ONION, addrOnion);
SetLimited(NET_ONION, false); SetReachable(NET_ONION, true);
} }
} }

View file

@ -183,7 +183,7 @@ bool IsPeerAddrLocalGood(CNode *pnode)
{ {
CService addrLocal = pnode->GetAddrLocal(); CService addrLocal = pnode->GetAddrLocal();
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() && return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
!IsLimited(addrLocal.GetNetwork()); IsReachable(addrLocal.GetNetwork());
} }
// pushes our own address to a peer // pushes our own address to a peer
@ -222,7 +222,7 @@ bool AddLocal(const CService& addr, int nScore)
if (!fDiscover && nScore < LOCAL_MANUAL) if (!fDiscover && nScore < LOCAL_MANUAL)
return false; return false;
if (IsLimited(addr)) if (!IsReachable(addr))
return false; return false;
LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore); LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
@ -252,24 +252,23 @@ void RemoveLocal(const CService& addr)
mapLocalHost.erase(addr); mapLocalHost.erase(addr);
} }
/** Make a particular network entirely off-limits (no automatic connects to it) */ void SetReachable(enum Network net, bool reachable)
void SetLimited(enum Network net, bool fLimited)
{ {
if (net == NET_UNROUTABLE || net == NET_INTERNAL) if (net == NET_UNROUTABLE || net == NET_INTERNAL)
return; return;
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
vfLimited[net] = fLimited; vfLimited[net] = !reachable;
} }
bool IsLimited(enum Network net) bool IsReachable(enum Network net)
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
return vfLimited[net]; return !vfLimited[net];
} }
bool IsLimited(const CNetAddr &addr) bool IsReachable(const CNetAddr &addr)
{ {
return IsLimited(addr.GetNetwork()); return IsReachable(addr.GetNetwork());
} }
/** vote for a local address */ /** vote for a local address */
@ -292,19 +291,6 @@ bool IsLocal(const CService& addr)
return mapLocalHost.count(addr) > 0; return mapLocalHost.count(addr) > 0;
} }
/** check whether a given network is one we can probably connect to */
bool IsReachable(enum Network net)
{
return !IsLimited(net);
}
/** check whether a given address is in a network we can probably connect to */
bool IsReachable(const CNetAddr& addr)
{
return IsReachable(addr.GetNetwork());
}
CNode* CConnman::FindNode(const CNetAddr& ip) CNode* CConnman::FindNode(const CNetAddr& ip)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
@ -1965,7 +1951,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
if (nTries > 100) if (nTries > 100)
break; break;
if (IsLimited(addr)) if (!IsReachable(addr))
continue; continue;
// only consider very recently tried nodes after 30 failed attempts // only consider very recently tried nodes after 30 failed attempts
@ -2327,7 +2313,7 @@ NodeId CConnman::GetNewNodeId()
bool CConnman::Bind(const CService &addr, unsigned int flags) { bool CConnman::Bind(const CService &addr, unsigned int flags) {
if (!(flags & BF_EXPLICIT) && IsLimited(addr)) if (!(flags & BF_EXPLICIT) && !IsReachable(addr))
return false; return false;
std::string strError; std::string strError;
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) { if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {

View file

@ -520,17 +520,23 @@ enum
bool IsPeerAddrLocalGood(CNode *pnode); bool IsPeerAddrLocalGood(CNode *pnode);
void AdvertiseLocal(CNode *pnode); void AdvertiseLocal(CNode *pnode);
void SetLimited(enum Network net, bool fLimited = true);
bool IsLimited(enum Network net); /**
bool IsLimited(const CNetAddr& addr); * Mark a network as reachable or unreachable (no automatic connects to it)
* @note Networks are reachable by default
*/
void SetReachable(enum Network net, bool reachable);
/** @returns true if the network is reachable, false otherwise */
bool IsReachable(enum Network net);
/** @returns true if the address is in a reachable network, false otherwise */
bool IsReachable(const CNetAddr& addr);
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE); bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE); bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
void RemoveLocal(const CService& addr); void RemoveLocal(const CService& addr);
bool SeenLocal(const CService& addr); bool SeenLocal(const CService& addr);
bool IsLocal(const CService& addr); bool IsLocal(const CService& addr);
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr); bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr);
bool IsReachable(enum Network net);
bool IsReachable(const CNetAddr &addr);
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices); CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices);

View file

@ -423,7 +423,7 @@ static UniValue GetNetworksInfo()
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
GetProxy(network, proxy); GetProxy(network, proxy);
obj.pushKV("name", GetNetworkName(network)); obj.pushKV("name", GetNetworkName(network));
obj.pushKV("limited", IsLimited(network)); obj.pushKV("limited", !IsReachable(network));
obj.pushKV("reachable", IsReachable(network)); obj.pushKV("reachable", IsReachable(network));
obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string()); obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string());
obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials); obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);

View file

@ -230,26 +230,21 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network) BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
{ {
SetLimited(NET_IPV4, true); BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
SetLimited(NET_IPV6, true); BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
SetLimited(NET_ONION, true); BOOST_CHECK_EQUAL(IsReachable(NET_ONION), true);
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), true); SetReachable(NET_IPV4, false);
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), true); SetReachable(NET_IPV6, false);
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), true); SetReachable(NET_ONION, false);
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), false); BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), false);
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), false); BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), false);
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), false); BOOST_CHECK_EQUAL(IsReachable(NET_ONION), false);
SetReachable(NET_IPV4, true);
SetLimited(NET_IPV4, false); SetReachable(NET_IPV6, true);
SetLimited(NET_IPV6, false); SetReachable(NET_ONION, true);
SetLimited(NET_ONION, false);
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), false);
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), false);
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), false);
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true); BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true); BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
@ -258,19 +253,13 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal) BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
{ {
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false);
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true); BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
SetLimited(NET_UNROUTABLE, true); SetReachable(NET_UNROUTABLE, false);
SetLimited(NET_INTERNAL, true); SetReachable(NET_INTERNAL, false);
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); // Ignored for both networks BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); // Ignored for both networks
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true); BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
} }
@ -289,15 +278,13 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
{ {
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1 CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
SetLimited(NET_IPV4, false); SetReachable(NET_IPV4, true);
BOOST_CHECK_EQUAL(IsLimited(addr), false);
BOOST_CHECK_EQUAL(IsReachable(addr), true); BOOST_CHECK_EQUAL(IsReachable(addr), true);
SetLimited(NET_IPV4, true); SetReachable(NET_IPV4, false);
BOOST_CHECK_EQUAL(IsLimited(addr), true);
BOOST_CHECK_EQUAL(IsReachable(addr), false); BOOST_CHECK_EQUAL(IsReachable(addr), false);
SetLimited(NET_IPV4, false); // have to reset this, because this is stateful. SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
} }
@ -305,7 +292,7 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
{ {
CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000 CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
SetLimited(NET_IPV4, false); SetReachable(NET_IPV4, true);
BOOST_CHECK_EQUAL(IsLocal(addr), false); BOOST_CHECK_EQUAL(IsLocal(addr), false);
BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true); BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true);

View file

@ -527,7 +527,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
CService resolved(LookupNumeric("127.0.0.1", 9050)); CService resolved(LookupNumeric("127.0.0.1", 9050));
proxyType addrOnion = proxyType(resolved, true); proxyType addrOnion = proxyType(resolved, true);
SetProxy(NET_ONION, addrOnion); SetProxy(NET_ONION, addrOnion);
SetLimited(NET_ONION, false); SetReachable(NET_ONION, true);
} }
// Finally - now create the service // Finally - now create the service