mirror of
https://github.com/dashpay/dash.git
synced 2024-12-24 19:42:46 +01:00
merge bitcoin#23140: Make CAddrman::Select_ select buckets, not positions, first
This commit is contained in:
parent
19b0145379
commit
d56702aa0c
@ -732,40 +732,56 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
|
||||
// use a tried node
|
||||
double fChanceFactor = 1.0;
|
||||
while (1) {
|
||||
// Pick a tried bucket, and an initial position in that bucket.
|
||||
int nKBucket = insecure_rand.randrange(ADDRMAN_TRIED_BUCKET_COUNT);
|
||||
int nKBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
|
||||
while (vvTried[nKBucket][nKBucketPos] == -1) {
|
||||
nKBucket = (nKBucket + insecure_rand.randbits(ADDRMAN_TRIED_BUCKET_COUNT_LOG2)) % ADDRMAN_TRIED_BUCKET_COUNT;
|
||||
nKBucketPos = (nKBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
|
||||
// Iterate over the positions of that bucket, starting at the initial one,
|
||||
// and looping around.
|
||||
int i;
|
||||
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
|
||||
if (vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break;
|
||||
}
|
||||
int nId = vvTried[nKBucket][nKBucketPos];
|
||||
// If the bucket is entirely empty, start over with a (likely) different one.
|
||||
if (i == ADDRMAN_BUCKET_SIZE) continue;
|
||||
// Find the entry to return.
|
||||
int nId = vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE];
|
||||
const auto it_found{mapInfo.find(nId)};
|
||||
assert(it_found != mapInfo.end());
|
||||
const AddrInfo& info{it_found->second};
|
||||
// With probability GetChance() * fChanceFactor, return the entry.
|
||||
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
|
||||
LogPrint(BCLog::ADDRMAN, "Selected %s from tried\n", info.ToString());
|
||||
return {info, info.nLastTry};
|
||||
}
|
||||
// Otherwise start over with a (likely) different bucket, and increased chance factor.
|
||||
fChanceFactor *= 1.2;
|
||||
}
|
||||
} else {
|
||||
// use a new node
|
||||
double fChanceFactor = 1.0;
|
||||
while (1) {
|
||||
// Pick a new bucket, and an initial position in that bucket.
|
||||
int nUBucket = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
|
||||
int nUBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
|
||||
while (vvNew[nUBucket][nUBucketPos] == -1) {
|
||||
nUBucket = (nUBucket + insecure_rand.randbits(ADDRMAN_NEW_BUCKET_COUNT_LOG2)) % ADDRMAN_NEW_BUCKET_COUNT;
|
||||
nUBucketPos = (nUBucketPos + insecure_rand.randbits(ADDRMAN_BUCKET_SIZE_LOG2)) % ADDRMAN_BUCKET_SIZE;
|
||||
// Iterate over the positions of that bucket, starting at the initial one,
|
||||
// and looping around.
|
||||
int i;
|
||||
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
|
||||
if (vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break;
|
||||
}
|
||||
int nId = vvNew[nUBucket][nUBucketPos];
|
||||
// If the bucket is entirely empty, start over with a (likely) different one.
|
||||
if (i == ADDRMAN_BUCKET_SIZE) continue;
|
||||
// Find the entry to return.
|
||||
int nId = vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE];
|
||||
const auto it_found{mapInfo.find(nId)};
|
||||
assert(it_found != mapInfo.end());
|
||||
const AddrInfo& info{it_found->second};
|
||||
// With probability GetChance() * fChanceFactor, return the entry.
|
||||
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
|
||||
LogPrint(BCLog::ADDRMAN, "Selected %s from new\n", info.ToString());
|
||||
return {info, info.nLastTry};
|
||||
}
|
||||
// Otherwise start over with a (likely) different bucket, and increased chance factor.
|
||||
fChanceFactor *= 1.2;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user