Skip to content

Commit 326ffed

Browse files
committed
Merge bitcoin#7212: Adds unittests for CAddrMan and CAddrinfo, removes source of non-determinism.
40c87b6 Increase test coverage for addrman and addrinfo (Ethan Heilman)
2 parents 1e06bab + 40c87b6 commit 326ffed

File tree

3 files changed

+391
-49
lines changed

3 files changed

+391
-49
lines changed

src/addrman.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void CAddrMan::Good_(const CService& addr, int64_t nTime)
221221
return;
222222

223223
// find a bucket it is in now
224-
int nRnd = GetRandInt(ADDRMAN_NEW_BUCKET_COUNT);
224+
int nRnd = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
225225
int nUBucket = -1;
226226
for (unsigned int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
227227
int nB = (n + nRnd) % ADDRMAN_NEW_BUCKET_COUNT;
@@ -278,7 +278,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
278278
int nFactor = 1;
279279
for (int n = 0; n < pinfo->nRefCount; n++)
280280
nFactor *= 2;
281-
if (nFactor > 1 && (GetRandInt(nFactor) != 0))
281+
if (nFactor > 1 && (RandomInt(nFactor) != 0))
282282
return false;
283283
} else {
284284
pinfo = Create(addr, source, &nId);
@@ -340,37 +340,37 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
340340

341341
// Use a 50% chance for choosing between tried and new table entries.
342342
if (!newOnly &&
343-
(nTried > 0 && (nNew == 0 || GetRandInt(2) == 0))) {
343+
(nTried > 0 && (nNew == 0 || RandomInt(2) == 0))) {
344344
// use a tried node
345345
double fChanceFactor = 1.0;
346346
while (1) {
347-
int nKBucket = GetRandInt(ADDRMAN_TRIED_BUCKET_COUNT);
348-
int nKBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE);
347+
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
348+
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
349349
while (vvTried[nKBucket][nKBucketPos] == -1) {
350350
nKBucket = (nKBucket + insecure_rand()) % ADDRMAN_TRIED_BUCKET_COUNT;
351351
nKBucketPos = (nKBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
352352
}
353353
int nId = vvTried[nKBucket][nKBucketPos];
354354
assert(mapInfo.count(nId) == 1);
355355
CAddrInfo& info = mapInfo[nId];
356-
if (GetRandInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
356+
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
357357
return info;
358358
fChanceFactor *= 1.2;
359359
}
360360
} else {
361361
// use a new node
362362
double fChanceFactor = 1.0;
363363
while (1) {
364-
int nUBucket = GetRandInt(ADDRMAN_NEW_BUCKET_COUNT);
365-
int nUBucketPos = GetRandInt(ADDRMAN_BUCKET_SIZE);
364+
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
365+
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
366366
while (vvNew[nUBucket][nUBucketPos] == -1) {
367367
nUBucket = (nUBucket + insecure_rand()) % ADDRMAN_NEW_BUCKET_COUNT;
368368
nUBucketPos = (nUBucketPos + insecure_rand()) % ADDRMAN_BUCKET_SIZE;
369369
}
370370
int nId = vvNew[nUBucket][nUBucketPos];
371371
assert(mapInfo.count(nId) == 1);
372372
CAddrInfo& info = mapInfo[nId];
373-
if (GetRandInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
373+
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
374374
return info;
375375
fChanceFactor *= 1.2;
376376
}
@@ -466,7 +466,7 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr)
466466
if (vAddr.size() >= nNodes)
467467
break;
468468

469-
int nRndPos = GetRandInt(vRandom.size() - n) + n;
469+
int nRndPos = RandomInt(vRandom.size() - n) + n;
470470
SwapRandom(n, nRndPos);
471471
assert(mapInfo.count(vRandom[n]) == 1);
472472

@@ -495,3 +495,7 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
495495
if (nTime - info.nTime > nUpdateInterval)
496496
info.nTime = nTime;
497497
}
498+
499+
int CAddrMan::RandomInt(int nMax){
500+
return GetRandInt(nMax);
501+
}

src/addrman.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@ class CAddrMan
176176
//! critical section to protect the inner data structures
177177
mutable CCriticalSection cs;
178178

179-
//! secret key to randomize bucket select with
180-
uint256 nKey;
181-
182179
//! last used nId
183180
int nIdCount;
184181

@@ -204,6 +201,8 @@ class CAddrMan
204201
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
205202

206203
protected:
204+
//! secret key to randomize bucket select with
205+
uint256 nKey;
207206

208207
//! Find an entry.
209208
CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
@@ -236,6 +235,9 @@ class CAddrMan
236235
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
237236
CAddrInfo Select_(bool newOnly);
238237

238+
//! Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
239+
virtual int RandomInt(int nMax);
240+
239241
#ifdef DEBUG_ADDRMAN
240242
//! Perform consistency check. Returns an error code or zero.
241243
int Check_();
@@ -570,11 +572,6 @@ class CAddrMan
570572
Check();
571573
}
572574
}
573-
574-
//! Ensure that bucket placement is always the same for testing purposes.
575-
void MakeDeterministic(){
576-
nKey.SetNull(); //Do not use outside of tests.
577-
}
578575

579576
};
580577

0 commit comments

Comments
 (0)