Skip to content

Commit 7e6e659

Browse files
committed
[addrman] inline Clear() into CAddrMan ctor
Clear() is now only called from the ctor, so just inline the code into that function. The LOCK(cs) can be removed, since there can be no data races in the ctor. Also move the function definition out of the header and into the cpp file.
1 parent 406be5f commit 7e6e659

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

src/addrman.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ double CAddrInfo::GetChance(int64_t nNow) const
7777
return fChance;
7878
}
7979

80+
CAddrMan::CAddrMan(bool deterministic, int32_t consistency_check_ratio)
81+
: insecure_rand{deterministic}
82+
, m_consistency_check_ratio{consistency_check_ratio}
83+
{
84+
std::vector<int>().swap(vRandom);
85+
nKey = insecure_rand.rand256();
86+
for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
87+
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
88+
vvNew[bucket][entry] = -1;
89+
}
90+
}
91+
for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
92+
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
93+
vvTried[bucket][entry] = -1;
94+
}
95+
}
96+
97+
nIdCount = 0;
98+
nTried = 0;
99+
nNew = 0;
100+
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
101+
mapInfo.clear();
102+
mapAddr.clear();
103+
if (deterministic) nKey = uint256{1};
104+
}
105+
80106
CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
81107
{
82108
AssertLockHeld(cs);

src/addrman.h

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -471,40 +471,7 @@ class CAddrMan
471471
Check();
472472
}
473473

474-
private:
475-
void Clear()
476-
EXCLUSIVE_LOCKS_REQUIRED(!cs)
477-
{
478-
LOCK(cs);
479-
std::vector<int>().swap(vRandom);
480-
nKey = insecure_rand.rand256();
481-
for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
482-
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
483-
vvNew[bucket][entry] = -1;
484-
}
485-
}
486-
for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
487-
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
488-
vvTried[bucket][entry] = -1;
489-
}
490-
}
491-
492-
nIdCount = 0;
493-
nTried = 0;
494-
nNew = 0;
495-
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
496-
mapInfo.clear();
497-
mapAddr.clear();
498-
}
499-
500-
public:
501-
explicit CAddrMan(bool deterministic, int32_t consistency_check_ratio)
502-
: insecure_rand{deterministic},
503-
m_consistency_check_ratio{consistency_check_ratio}
504-
{
505-
Clear();
506-
if (deterministic) nKey = uint256{1};
507-
}
474+
explicit CAddrMan(bool deterministic, int32_t consistency_check_ratio);
508475

509476
~CAddrMan()
510477
{
@@ -626,17 +593,16 @@ class CAddrMan
626593
Check();
627594
}
628595

629-
protected:
630-
//! secret key to randomize bucket select with
631-
uint256 nKey;
632-
596+
private:
633597
//! A mutex to protect the inner data structures.
634598
mutable Mutex cs;
635599

636-
private:
637600
//! Source of random numbers for randomization in inner loops
638601
mutable FastRandomContext insecure_rand GUARDED_BY(cs);
639602

603+
//! secret key to randomize bucket select with
604+
uint256 nKey;
605+
640606
//! Serialization versions.
641607
enum Format : uint8_t {
642608
V0_HISTORICAL = 0, //!< historic format, before commit e6b343d88

src/test/addrman_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CAddrManCorrupted : public CAddrManSerializationMock
4848
unsigned char nVersion = 1;
4949
s << nVersion;
5050
s << ((unsigned char)32);
51-
s << nKey;
51+
s << uint256::ONE;
5252
s << 10; // nNew
5353
s << 10; // nTried
5454

0 commit comments

Comments
 (0)