Skip to content

Commit b8bc9e9

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#15138: Drop IsLimited in favor of IsReachable
d6b076c Drop IsLimited in favor of IsReachable (Ben Woosley) Pull request description: These two methods have had the same meaning, but inverted, since 110b62f. Having one name for a single concept simplifies the code. This is a follow-up to bitcoin#15051. /cc bitcoin#7553 Tree-SHA512: 347ceb9e2a55ea06f4c01226411c7bbcade09dd82130e4c59d0824ecefd960875938022edbe5d4bfdf12b0552c9b4cb78b09a688284d707119571daf4eb371b4
1 parent 886024b commit b8bc9e9

File tree

6 files changed

+44
-65
lines changed

6 files changed

+44
-65
lines changed

src/init.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ bool AppInitMain()
19201920
for (int n = 0; n < NET_MAX; n++) {
19211921
enum Network net = (enum Network)n;
19221922
if (!nets.count(net))
1923-
SetLimited(net);
1923+
SetReachable(net, false);
19241924
}
19251925
}
19261926

@@ -1931,7 +1931,7 @@ bool AppInitMain()
19311931
// -proxy sets a proxy for all outgoing network traffic
19321932
// -noproxy (or -proxy=0) as well as the empty string can be used to not set a proxy, this is the default
19331933
std::string proxyArg = gArgs.GetArg("-proxy", "");
1934-
SetLimited(NET_ONION);
1934+
SetReachable(NET_ONION, false);
19351935
if (proxyArg != "" && proxyArg != "0") {
19361936
CService proxyAddr;
19371937
if (!Lookup(proxyArg.c_str(), proxyAddr, 9050, fNameLookup)) {
@@ -1946,7 +1946,7 @@ bool AppInitMain()
19461946
SetProxy(NET_IPV6, addrProxy);
19471947
SetProxy(NET_ONION, addrProxy);
19481948
SetNameProxy(addrProxy);
1949-
SetLimited(NET_ONION, false); // by default, -proxy sets onion as reachable, unless -noonion later
1949+
SetReachable(NET_ONION, true); // by default, -proxy sets onion as reachable, unless -noonion later
19501950
}
19511951

19521952
// -onion can be used to set only a proxy for .onion, or override normal proxy for .onion addresses
@@ -1955,7 +1955,7 @@ bool AppInitMain()
19551955
std::string onionArg = gArgs.GetArg("-onion", "");
19561956
if (onionArg != "") {
19571957
if (onionArg == "0") { // Handle -noonion/-onion=0
1958-
SetLimited(NET_ONION); // set onions as unreachable
1958+
SetReachable(NET_ONION, false);
19591959
} else {
19601960
CService onionProxy;
19611961
if (!Lookup(onionArg.c_str(), onionProxy, 9050, fNameLookup)) {
@@ -1965,7 +1965,7 @@ bool AppInitMain()
19651965
if (!addrOnion.IsValid())
19661966
return InitError(strprintf(_("Invalid -onion address or hostname: '%s'"), onionArg));
19671967
SetProxy(NET_ONION, addrOnion);
1968-
SetLimited(NET_ONION, false);
1968+
SetReachable(NET_ONION, true);
19691969
}
19701970
}
19711971

src/net.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ bool IsPeerAddrLocalGood(CNode *pnode)
213213
{
214214
CService addrLocal = pnode->GetAddrLocal();
215215
return fDiscover && pnode->addr.IsRoutable() && addrLocal.IsRoutable() &&
216-
!IsLimited(addrLocal.GetNetwork());
216+
IsReachable(addrLocal.GetNetwork());
217217
}
218218

219219
// pushes our own address to a peer
@@ -252,7 +252,7 @@ bool AddLocal(const CService& addr, int nScore)
252252
if (!fDiscover && nScore < LOCAL_MANUAL)
253253
return false;
254254

255-
if (IsLimited(addr))
255+
if (!IsReachable(addr))
256256
return false;
257257

258258
LogPrintf("AddLocal(%s,%i)\n", addr.ToString(), nScore);
@@ -282,24 +282,23 @@ void RemoveLocal(const CService& addr)
282282
mapLocalHost.erase(addr);
283283
}
284284

285-
/** Make a particular network entirely off-limits (no automatic connects to it) */
286-
void SetLimited(enum Network net, bool fLimited)
285+
void SetReachable(enum Network net, bool reachable)
287286
{
288287
if (net == NET_UNROUTABLE || net == NET_INTERNAL)
289288
return;
290289
LOCK(cs_mapLocalHost);
291-
vfLimited[net] = fLimited;
290+
vfLimited[net] = !reachable;
292291
}
293292

294-
bool IsLimited(enum Network net)
293+
bool IsReachable(enum Network net)
295294
{
296295
LOCK(cs_mapLocalHost);
297-
return vfLimited[net];
296+
return !vfLimited[net];
298297
}
299298

300-
bool IsLimited(const CNetAddr &addr)
299+
bool IsReachable(const CNetAddr &addr)
301300
{
302-
return IsLimited(addr.GetNetwork());
301+
return IsReachable(addr.GetNetwork());
303302
}
304303

305304
/** vote for a local address */
@@ -322,19 +321,6 @@ bool IsLocal(const CService& addr)
322321
return mapLocalHost.count(addr) > 0;
323322
}
324323

325-
/** check whether a given network is one we can probably connect to */
326-
bool IsReachable(enum Network net)
327-
{
328-
return !IsLimited(net);
329-
}
330-
331-
/** check whether a given address is in a network we can probably connect to */
332-
bool IsReachable(const CNetAddr& addr)
333-
{
334-
return IsReachable(addr.GetNetwork());
335-
}
336-
337-
338324
CNode* CConnman::FindNode(const CNetAddr& ip, bool fExcludeDisconnecting)
339325
{
340326
LOCK(cs_vNodes);
@@ -2482,7 +2468,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24822468
if (nTries > 100)
24832469
break;
24842470

2485-
if (IsLimited(addr))
2471+
if (!IsReachable(addr))
24862472
continue;
24872473

24882474
// only consider very recently tried nodes after 30 failed attempts
@@ -3066,7 +3052,7 @@ NodeId CConnman::GetNewNodeId()
30663052

30673053

30683054
bool CConnman::Bind(const CService &addr, unsigned int flags) {
3069-
if (!(flags & BF_EXPLICIT) && IsLimited(addr))
3055+
if (!(flags & BF_EXPLICIT) && !IsReachable(addr))
30703056
return false;
30713057
std::string strError;
30723058
if (!BindListenPort(addr, strError, (flags & BF_WHITELIST) != 0)) {

src/net.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,17 +718,23 @@ enum
718718

719719
bool IsPeerAddrLocalGood(CNode *pnode);
720720
void AdvertiseLocal(CNode *pnode);
721-
void SetLimited(enum Network net, bool fLimited = true);
722-
bool IsLimited(enum Network net);
723-
bool IsLimited(const CNetAddr& addr);
721+
722+
/**
723+
* Mark a network as reachable or unreachable (no automatic connects to it)
724+
* @note Networks are reachable by default
725+
*/
726+
void SetReachable(enum Network net, bool reachable);
727+
/** @returns true if the network is reachable, false otherwise */
728+
bool IsReachable(enum Network net);
729+
/** @returns true if the address is in a reachable network, false otherwise */
730+
bool IsReachable(const CNetAddr& addr);
731+
724732
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
725733
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
726734
void RemoveLocal(const CService& addr);
727735
bool SeenLocal(const CService& addr);
728736
bool IsLocal(const CService& addr);
729737
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr);
730-
bool IsReachable(enum Network net);
731-
bool IsReachable(const CNetAddr &addr);
732738
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices);
733739

734740

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static UniValue GetNetworksInfo()
424424
UniValue obj(UniValue::VOBJ);
425425
GetProxy(network, proxy);
426426
obj.pushKV("name", GetNetworkName(network));
427-
obj.pushKV("limited", IsLimited(network));
427+
obj.pushKV("limited", !IsReachable(network));
428428
obj.pushKV("reachable", IsReachable(network));
429429
obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringIPPort() : std::string());
430430
obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);

src/test/net_tests.cpp

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -639,26 +639,21 @@ BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
639639

640640
BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
641641
{
642-
SetLimited(NET_IPV4, true);
643-
SetLimited(NET_IPV6, true);
644-
SetLimited(NET_ONION, true);
642+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
643+
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
644+
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), true);
645645

646-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), true);
647-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), true);
648-
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), true);
646+
SetReachable(NET_IPV4, false);
647+
SetReachable(NET_IPV6, false);
648+
SetReachable(NET_ONION, false);
649649

650650
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), false);
651651
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), false);
652652
BOOST_CHECK_EQUAL(IsReachable(NET_ONION), false);
653653

654-
655-
SetLimited(NET_IPV4, false);
656-
SetLimited(NET_IPV6, false);
657-
SetLimited(NET_ONION, false);
658-
659-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV4), false);
660-
BOOST_CHECK_EQUAL(IsLimited(NET_IPV6), false);
661-
BOOST_CHECK_EQUAL(IsLimited(NET_ONION), false);
654+
SetReachable(NET_IPV4, true);
655+
SetReachable(NET_IPV6, true);
656+
SetReachable(NET_ONION, true);
662657

663658
BOOST_CHECK_EQUAL(IsReachable(NET_IPV4), true);
664659
BOOST_CHECK_EQUAL(IsReachable(NET_IPV6), true);
@@ -667,19 +662,13 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_Network)
667662

668663
BOOST_AUTO_TEST_CASE(LimitedAndReachable_NetworkCaseUnroutableAndInternal)
669664
{
670-
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false);
671-
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
672-
673665
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
674666
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
675667

676-
SetLimited(NET_UNROUTABLE, true);
677-
SetLimited(NET_INTERNAL, true);
668+
SetReachable(NET_UNROUTABLE, false);
669+
SetReachable(NET_INTERNAL, false);
678670

679-
BOOST_CHECK_EQUAL(IsLimited(NET_UNROUTABLE), false); // Ignored for both networks
680-
BOOST_CHECK_EQUAL(IsLimited(NET_INTERNAL), false);
681-
682-
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true);
671+
BOOST_CHECK_EQUAL(IsReachable(NET_UNROUTABLE), true); // Ignored for both networks
683672
BOOST_CHECK_EQUAL(IsReachable(NET_INTERNAL), true);
684673
}
685674

@@ -698,23 +687,21 @@ BOOST_AUTO_TEST_CASE(LimitedAndReachable_CNetAddr)
698687
{
699688
CNetAddr addr = UtilBuildAddress(0x001, 0x001, 0x001, 0x001); // 1.1.1.1
700689

701-
SetLimited(NET_IPV4, false);
702-
BOOST_CHECK_EQUAL(IsLimited(addr), false);
690+
SetReachable(NET_IPV4, true);
703691
BOOST_CHECK_EQUAL(IsReachable(addr), true);
704692

705-
SetLimited(NET_IPV4, true);
706-
BOOST_CHECK_EQUAL(IsLimited(addr), true);
693+
SetReachable(NET_IPV4, false);
707694
BOOST_CHECK_EQUAL(IsReachable(addr), false);
708695

709-
SetLimited(NET_IPV4, false); // have to reset this, because this is stateful.
696+
SetReachable(NET_IPV4, true); // have to reset this, because this is stateful.
710697
}
711698

712699

713700
BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
714701
{
715702
CService addr = CService(UtilBuildAddress(0x002, 0x001, 0x001, 0x001), 1000); // 2.1.1.1:1000
716703

717-
SetLimited(NET_IPV4, false);
704+
SetReachable(NET_IPV4, true);
718705

719706
BOOST_CHECK_EQUAL(IsLocal(addr), false);
720707
BOOST_CHECK_EQUAL(AddLocal(addr, 1000), true);

src/torcontrol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ void TorController::auth_cb(TorControlConnection& _conn, const TorControlReply&
529529
CService resolved(LookupNumeric("127.0.0.1", 9050));
530530
proxyType addrOnion = proxyType(resolved, true);
531531
SetProxy(NET_ONION, addrOnion);
532-
SetLimited(NET_ONION, false);
532+
SetReachable(NET_ONION, true);
533533
}
534534

535535
// Finally - now create the service

0 commit comments

Comments
 (0)