Skip to content

Commit fae0c79

Browse files
author
MarcoFalke
committed
refactor: Mark CAddrMan::GetAddr const
1 parent fa02934 commit fae0c79

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/addrman.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, in
138138
return &mapInfo[nId];
139139
}
140140

141-
void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2)
141+
void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
142142
{
143143
AssertLockHeld(cs);
144144

@@ -150,11 +150,13 @@ void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2)
150150
int nId1 = vRandom[nRndPos1];
151151
int nId2 = vRandom[nRndPos2];
152152

153-
assert(mapInfo.count(nId1) == 1);
154-
assert(mapInfo.count(nId2) == 1);
153+
const auto it_1{mapInfo.find(nId1)};
154+
const auto it_2{mapInfo.find(nId2)};
155+
assert(it_1 != mapInfo.end());
156+
assert(it_2 != mapInfo.end());
155157

156-
mapInfo[nId1].nRandomPos = nRndPos2;
157-
mapInfo[nId2].nRandomPos = nRndPos1;
158+
it_1->second.nRandomPos = nRndPos2;
159+
it_2->second.nRandomPos = nRndPos1;
158160

159161
vRandom[nRndPos1] = nId2;
160162
vRandom[nRndPos2] = nId1;
@@ -541,7 +543,7 @@ int CAddrMan::Check_()
541543
}
542544
#endif
543545

544-
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network)
546+
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const
545547
{
546548
AssertLockHeld(cs);
547549

@@ -561,9 +563,10 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size
561563

562564
int nRndPos = insecure_rand.randrange(vRandom.size() - n) + n;
563565
SwapRandom(n, nRndPos);
564-
assert(mapInfo.count(vRandom[n]) == 1);
566+
const auto it{mapInfo.find(vRandom[n])};
567+
assert(it != mapInfo.end());
565568

566-
const CAddrInfo& ai = mapInfo[vRandom[n]];
569+
const CAddrInfo& ai{it->second};
567570

568571
// Filter by network (optional)
569572
if (network != std::nullopt && ai.GetNetClass() != network) continue;

src/addrman.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CAddrInfo : public CAddress
5555
bool fInTried{false};
5656

5757
//! position in vRandom
58-
int nRandomPos{-1};
58+
mutable int nRandomPos{-1};
5959

6060
friend class CAddrMan;
6161

@@ -596,7 +596,7 @@ class CAddrMan
596596
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
597597
* @param[in] network Select only addresses of this network (nullopt = all).
598598
*/
599-
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network)
599+
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
600600
EXCLUSIVE_LOCKS_REQUIRED(!cs)
601601
{
602602
LOCK(cs);
@@ -669,7 +669,9 @@ class CAddrMan
669669
std::unordered_map<CNetAddr, int, CNetAddrHash> mapAddr GUARDED_BY(cs);
670670

671671
//! randomly-ordered vector of all nIds
672-
std::vector<int> vRandom GUARDED_BY(cs);
672+
//! This is mutable because it is unobservable outside the class, so any
673+
//! changes to it (even in const methods) are also unobservable.
674+
mutable std::vector<int> vRandom GUARDED_BY(cs);
673675

674676
// number of "tried" entries
675677
int nTried GUARDED_BY(cs);
@@ -697,7 +699,7 @@ class CAddrMan
697699
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
698700

699701
//! Swap two elements in vRandom.
700-
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
702+
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
701703

702704
//! Move an entry from the "new" table(s) to the "tried" table
703705
void MakeTried(CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -752,7 +754,7 @@ class CAddrMan
752754
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
753755
* @param[in] network Select only addresses of this network (nullopt = all).
754756
*/
755-
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);
757+
void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
756758

757759
/** We have successfully connected to this peer. Calling this function
758760
* updates the CAddress's nTime, which is used in our IsTerrible()

0 commit comments

Comments
 (0)