Skip to content

Commit 9695f31

Browse files
committed
Make addrman use its local RNG exclusively
1 parent 6d0a147 commit 9695f31

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

src/addrman.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void CAddrMan::Good_(const CService& addr, bool test_before_evict, int64_t nTime
217217
return;
218218

219219
// find a bucket it is in now
220-
int nRnd = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
220+
int nRnd = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
221221
int nUBucket = -1;
222222
for (unsigned int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
223223
int nB = (n + nRnd) % ADDRMAN_NEW_BUCKET_COUNT;
@@ -291,7 +291,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
291291
int nFactor = 1;
292292
for (int n = 0; n < pinfo->nRefCount; n++)
293293
nFactor *= 2;
294-
if (nFactor > 1 && (RandomInt(nFactor) != 0))
294+
if (nFactor > 1 && (insecure_rand.randrange(nFactor) != 0))
295295
return false;
296296
} else {
297297
pinfo = Create(addr, source, &nId);
@@ -356,37 +356,37 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
356356

357357
// Use a 50% chance for choosing between tried and new table entries.
358358
if (!newOnly &&
359-
(nTried > 0 && (nNew == 0 || RandomInt(2) == 0))) {
359+
(nTried > 0 && (nNew == 0 || insecure_rand.randbool() == 0))) {
360360
// use a tried node
361361
double fChanceFactor = 1.0;
362362
while (1) {
363-
int nKBucket = RandomInt(ADDRMAN_TRIED_BUCKET_COUNT);
364-
int nKBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
363+
int nKBucket = insecure_rand.randrange(ADDRMAN_TRIED_BUCKET_COUNT);
364+
int nKBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
365365
while (vvTried[nKBucket][nKBucketPos] == -1) {
366366
nKBucket = (nKBucket + insecure_rand.randbits(ADDRMAN_TRIED_BUCKET_COUNT_LOG2)) % ADDRMAN_TRIED_BUCKET_COUNT;
367367
nKBucketPos = (nKBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
368368
}
369369
int nId = vvTried[nKBucket][nKBucketPos];
370370
assert(mapInfo.count(nId) == 1);
371371
CAddrInfo& info = mapInfo[nId];
372-
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
372+
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30))
373373
return info;
374374
fChanceFactor *= 1.2;
375375
}
376376
} else {
377377
// use a new node
378378
double fChanceFactor = 1.0;
379379
while (1) {
380-
int nUBucket = RandomInt(ADDRMAN_NEW_BUCKET_COUNT);
381-
int nUBucketPos = RandomInt(ADDRMAN_BUCKET_SIZE);
380+
int nUBucket = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
381+
int nUBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
382382
while (vvNew[nUBucket][nUBucketPos] == -1) {
383383
nUBucket = (nUBucket + insecure_rand.randbits(ADDRMAN_NEW_BUCKET_COUNT_LOG2)) % ADDRMAN_NEW_BUCKET_COUNT;
384384
nUBucketPos = (nUBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
385385
}
386386
int nId = vvNew[nUBucket][nUBucketPos];
387387
assert(mapInfo.count(nId) == 1);
388388
CAddrInfo& info = mapInfo[nId];
389-
if (RandomInt(1 << 30) < fChanceFactor * info.GetChance() * (1 << 30))
389+
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30))
390390
return info;
391391
fChanceFactor *= 1.2;
392392
}
@@ -482,7 +482,7 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr)
482482
if (vAddr.size() >= nNodes)
483483
break;
484484

485-
int nRndPos = RandomInt(vRandom.size() - n) + n;
485+
int nRndPos = insecure_rand.randrange(vRandom.size() - n) + n;
486486
SwapRandom(n, nRndPos);
487487
assert(mapInfo.count(vRandom[n]) == 1);
488488

@@ -530,10 +530,6 @@ void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
530530
info.nServices = nServices;
531531
}
532532

533-
int CAddrMan::RandomInt(int nMax){
534-
return GetRandInt(nMax);
535-
}
536-
537533
void CAddrMan::ResolveCollisions_()
538534
{
539535
for (std::set<int>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
@@ -593,7 +589,7 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
593589
std::set<int>::iterator it = m_tried_collisions.begin();
594590

595591
// Selects a random element from m_tried_collisions
596-
std::advance(it, GetRandInt(m_tried_collisions.size()));
592+
std::advance(it, insecure_rand.randrange(m_tried_collisions.size()));
597593
int id_new = *it;
598594

599595
// If id_new not found in mapInfo remove it from m_tried_collisions

src/addrman.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,6 @@ class CAddrMan
266266
//! Return a random to-be-evicted tried table address.
267267
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
268268

269-
//! Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
270-
virtual int RandomInt(int nMax);
271-
272269
#ifdef DEBUG_ADDRMAN
273270
//! Perform consistency check. Returns an error code or zero.
274271
int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
@@ -473,7 +470,7 @@ class CAddrMan
473470
{
474471
LOCK(cs);
475472
std::vector<int>().swap(vRandom);
476-
nKey = GetRandHash();
473+
nKey = insecure_rand.rand256();
477474
for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
478475
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
479476
vvNew[bucket][entry] = -1;

src/test/addrman_tests.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ class CAddrManTest : public CAddrMan
3232
insecure_rand = FastRandomContext(true);
3333
}
3434

35-
int RandomInt(int nMax) override
36-
{
37-
state = (CHashWriter(SER_GETHASH, 0) << state).GetCheapHash();
38-
return (unsigned int)(state % nMax);
39-
}
40-
4135
CAddrInfo* Find(const CNetAddr& addr, int* pnId = nullptr)
4236
{
4337
LOCK(cs);

0 commit comments

Comments
 (0)