Skip to content

Commit 5faa7dd

Browse files
committed
[move-only] Move CAddrMan function definitions to cpp
In preparation for introducing the pimpl pattern to addrman, move all function bodies out of the header file. Review hint: use git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent efa227f commit 5faa7dd

File tree

2 files changed

+111
-87
lines changed

2 files changed

+111
-87
lines changed

src/addrman.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ CAddrMan::CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consiste
119119
}
120120
}
121121

122+
CAddrMan::~CAddrMan()
123+
{
124+
nKey.SetNull();
125+
}
126+
122127
template <typename Stream>
123128
void CAddrMan::Serialize(Stream& s_) const
124129
{
@@ -1017,3 +1022,97 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
10171022

10181023
return mapInfo[id_old];
10191024
}
1025+
1026+
size_t CAddrMan::size() const
1027+
{
1028+
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
1029+
return vRandom.size();
1030+
}
1031+
1032+
bool CAddrMan::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
1033+
{
1034+
LOCK(cs);
1035+
int nAdd = 0;
1036+
Check();
1037+
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
1038+
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
1039+
Check();
1040+
if (nAdd) {
1041+
LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
1042+
}
1043+
return nAdd > 0;
1044+
}
1045+
1046+
void CAddrMan::Good(const CService &addr, int64_t nTime)
1047+
{
1048+
LOCK(cs);
1049+
Check();
1050+
Good_(addr, /* test_before_evict */ true, nTime);
1051+
Check();
1052+
}
1053+
1054+
void CAddrMan::Attempt(const CService &addr, bool fCountFailure, int64_t nTime)
1055+
{
1056+
LOCK(cs);
1057+
Check();
1058+
Attempt_(addr, fCountFailure, nTime);
1059+
Check();
1060+
}
1061+
1062+
1063+
void CAddrMan::ResolveCollisions()
1064+
{
1065+
LOCK(cs);
1066+
Check();
1067+
ResolveCollisions_();
1068+
Check();
1069+
}
1070+
1071+
CAddrInfo CAddrMan::SelectTriedCollision()
1072+
{
1073+
LOCK(cs);
1074+
Check();
1075+
const CAddrInfo ret = SelectTriedCollision_();
1076+
Check();
1077+
return ret;
1078+
}
1079+
1080+
CAddrInfo CAddrMan::Select(bool newOnly) const
1081+
{
1082+
LOCK(cs);
1083+
Check();
1084+
const CAddrInfo addrRet = Select_(newOnly);
1085+
Check();
1086+
return addrRet;
1087+
}
1088+
1089+
std::vector<CAddress> CAddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1090+
{
1091+
LOCK(cs);
1092+
Check();
1093+
std::vector<CAddress> vAddr;
1094+
GetAddr_(vAddr, max_addresses, max_pct, network);
1095+
Check();
1096+
return vAddr;
1097+
}
1098+
1099+
void CAddrMan::Connected(const CService &addr, int64_t nTime)
1100+
{
1101+
LOCK(cs);
1102+
Check();
1103+
Connected_(addr, nTime);
1104+
Check();
1105+
}
1106+
1107+
void CAddrMan::SetServices(const CService &addr, ServiceFlags nServices)
1108+
{
1109+
LOCK(cs);
1110+
Check();
1111+
SetServices_(addr, nServices);
1112+
Check();
1113+
}
1114+
1115+
const std::vector<bool>& CAddrMan::GetAsmap() const
1116+
{
1117+
return m_asmap;
1118+
}

src/addrman.h

Lines changed: 12 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -150,88 +150,33 @@ class CAddrMan
150150

151151
explicit CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio);
152152

153-
~CAddrMan()
154-
{
155-
nKey.SetNull();
156-
}
153+
~CAddrMan();
157154

158155
//! Return the number of (unique) addresses in all tables.
159-
size_t size() const
160-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
161-
{
162-
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
163-
return vRandom.size();
164-
}
156+
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
165157

166158
//! Add addresses to addrman's new table.
167159
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
168-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
169-
{
170-
LOCK(cs);
171-
int nAdd = 0;
172-
Check();
173-
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
174-
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
175-
Check();
176-
if (nAdd) {
177-
LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
178-
}
179-
return nAdd > 0;
180-
}
160+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
181161

182162
//! Mark an entry as accessible.
183163
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
184-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
185-
{
186-
LOCK(cs);
187-
Check();
188-
Good_(addr, /* test_before_evict */ true, nTime);
189-
Check();
190-
}
164+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
191165

192166
//! Mark an entry as connection attempted to.
193167
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
194-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
195-
{
196-
LOCK(cs);
197-
Check();
198-
Attempt_(addr, fCountFailure, nTime);
199-
Check();
200-
}
168+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
201169

202170
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
203-
void ResolveCollisions()
204-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
205-
{
206-
LOCK(cs);
207-
Check();
208-
ResolveCollisions_();
209-
Check();
210-
}
171+
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs);
211172

212173
//! Randomly select an address in tried that another address is attempting to evict.
213-
CAddrInfo SelectTriedCollision()
214-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
215-
{
216-
LOCK(cs);
217-
Check();
218-
const CAddrInfo ret = SelectTriedCollision_();
219-
Check();
220-
return ret;
221-
}
174+
CAddrInfo SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
222175

223176
/**
224177
* Choose an address to connect to.
225178
*/
226-
CAddrInfo Select(bool newOnly = false) const
227-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
228-
{
229-
LOCK(cs);
230-
Check();
231-
const CAddrInfo addrRet = Select_(newOnly);
232-
Check();
233-
return addrRet;
234-
}
179+
CAddrInfo Select(bool newOnly = false) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
235180

236181
/**
237182
* Return all or many randomly selected addresses, optionally by network.
@@ -241,36 +186,16 @@ class CAddrMan
241186
* @param[in] network Select only addresses of this network (nullopt = all).
242187
*/
243188
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
244-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
245-
{
246-
LOCK(cs);
247-
Check();
248-
std::vector<CAddress> vAddr;
249-
GetAddr_(vAddr, max_addresses, max_pct, network);
250-
Check();
251-
return vAddr;
252-
}
189+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
253190

254191
//! Outer function for Connected_()
255192
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
256-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
257-
{
258-
LOCK(cs);
259-
Check();
260-
Connected_(addr, nTime);
261-
Check();
262-
}
193+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
263194

264195
void SetServices(const CService &addr, ServiceFlags nServices)
265-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
266-
{
267-
LOCK(cs);
268-
Check();
269-
SetServices_(addr, nServices);
270-
Check();
271-
}
196+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
272197

273-
const std::vector<bool>& GetAsmap() const { return m_asmap; }
198+
const std::vector<bool>& GetAsmap() const;
274199

275200
private:
276201
//! A mutex to protect the inner data structures.

0 commit comments

Comments
 (0)