Skip to content

Commit

Permalink
Merge bitcoin#10888: range-based loops and const qualifications in ne…
Browse files Browse the repository at this point in the history
…t.cpp

05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun)

Pull request description:

  Plus a use of std::copy() instead of manual copying.

  (The loop on line 117 is already done in bitcoin#10493).

Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
  • Loading branch information
sipa authored and PastaPastaPasta committed Dec 22, 2019
1 parent f7b611f commit 1eebe9f
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions src/net.cpp
Expand Up @@ -145,11 +145,10 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
const int64_t nOneWeek = 7*24*60*60;
std::vector<CAddress> vSeedsOut;
vSeedsOut.reserve(vSeedsIn.size());
for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i)
{
for (const auto& seed_in : vSeedsIn) {
struct in6_addr ip;
memcpy(&ip, i->addr, sizeof(ip));
CAddress addr(CService(ip, i->port), NODE_NETWORK);
memcpy(&ip, seed_in.addr, sizeof(ip));
CAddress addr(CService(ip, seed_in.port), NODE_NETWORK);
addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
vSeedsOut.push_back(addr);
}
Expand Down Expand Up @@ -309,18 +308,22 @@ bool IsReachable(const CNetAddr& addr)
CNode* CConnman::FindNode(const CNetAddr& ip)
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if ((CNetAddr)pnode->addr == ip)
return (pnode);
for (CNode* pnode : vNodes) {
if ((CNetAddr)pnode->addr == ip) {
return pnode;
}
}
return nullptr;
}

CNode* CConnman::FindNode(const CSubNet& subNet)
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if (subNet.Match((CNetAddr)pnode->addr))
return (pnode);
for (CNode* pnode : vNodes) {
if (subNet.Match((CNetAddr)pnode->addr)) {
return pnode;
}
}
return nullptr;
}

Expand All @@ -329,7 +332,7 @@ CNode* CConnman::FindNode(const std::string& addrName)
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
if (pnode->GetAddrName() == addrName) {
return (pnode);
return pnode;
}
}
return nullptr;
Expand All @@ -338,9 +341,11 @@ CNode* CConnman::FindNode(const std::string& addrName)
CNode* CConnman::FindNode(const CService& addr)
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if((CService)pnode->addr == addr)
return (pnode);
for (CNode* pnode : vNodes) {
if ((CService)pnode->addr == addr) {
return pnode;
}
}
return nullptr;
}

Expand Down Expand Up @@ -491,10 +496,9 @@ void CConnman::ClearBanned()
bool CConnman::IsBanned(CNetAddr ip)
{
LOCK(cs_setBanned);
for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++)
{
CSubNet subNet = (*it).first;
CBanEntry banEntry = (*it).second;
for (const auto& it : setBanned) {
CSubNet subNet = it.first;
CBanEntry banEntry = it.second;

if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
return true;
Expand Down Expand Up @@ -997,7 +1001,7 @@ bool CConnman::AttemptToEvictConnection()
{
LOCK(cs_vNodes);

for (CNode *node : vNodes) {
for (const CNode* node : vNodes) {
if (node->fWhitelisted)
continue;
if (!node->fInbound)
Expand Down Expand Up @@ -1095,9 +1099,9 @@ bool CConnman::AttemptToEvictConnection()
// Disconnect from the network group with the most connections
NodeId evicted = vEvictionCandidates.front().id;
LOCK(cs_vNodes);
for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
if ((*it)->GetId() == evicted) {
(*it)->fDisconnect = true;
for (CNode* pnode : vNodes) {
if (pnode->GetId() == evicted) {
pnode->fDisconnect = true;
return true;
}
}
Expand All @@ -1122,7 +1126,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
for (const CNode* pnode : vNodes) {
if (pnode->fInbound) {
nInbound++;
if (!pnode->verifiedProRegTxHash.IsNull()) {
Expand Down Expand Up @@ -2021,8 +2025,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
{
LOCK(cs_vAddedNodes);
ret.reserve(vAddedNodes.size());
for (const std::string& strAddNode : vAddedNodes)
lAddresses.push_back(strAddNode);
std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses));
}


Expand Down Expand Up @@ -2796,9 +2799,8 @@ std::vector<CAddress> CConnman::GetAddresses()
bool CConnman::AddNode(const std::string& strNode)
{
LOCK(cs_vAddedNodes);
for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
if (strNode == *it)
return false;
for (const std::string& it : vAddedNodes) {
if (strNode == it) return false;
}

vAddedNodes.push_back(strNode);
Expand Down Expand Up @@ -2924,9 +2926,11 @@ size_t CConnman::GetNodeCount(NumConnections flags)
return vNodes.size();

int nNum = 0;
for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
for (const auto& pnode : vNodes) {
if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
nNum++;
}
}

return nNum;
}
Expand All @@ -2936,8 +2940,7 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
vstats.clear();
LOCK(cs_vNodes);
vstats.reserve(vNodes.size());
for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
CNode* pnode = *it;
for (CNode* pnode : vNodes) {
vstats.emplace_back();
pnode->copyStats(vstats.back());
}
Expand Down

0 comments on commit 1eebe9f

Please sign in to comment.