refactor: consolidate select logic for new and tried tables

Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
This commit is contained in:
Amiti UttarwarandMartin Zumsande committed 2023-03-17 17:59:02 -07:00
1 parent ca2a9c5f8f
commit 48806412e2
1 file changed
+13 -42
+13 -42
View File
@@ -736,69 +736,40 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
bucket_count = ADDRMAN_NEW_BUCKET_COUNT; bucket_count = ADDRMAN_NEW_BUCKET_COUNT;
} }
if (search_tried) {
// use a tried node
double fChanceFactor = 1.0; double fChanceFactor = 1.0;
while (1) { while (1) {
// Pick a tried bucket, and an initial position in that bucket. // Pick a bucket, and an initial position in that bucket.
int nKBucket = insecure_rand.randrange(bucket_count); int nBucket = insecure_rand.randrange(bucket_count);
int nKBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE); int nBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
// Iterate over the positions of that bucket, starting at the initial one, // Iterate over the positions of that bucket, starting at the initial one,
// and looping around. // and looping around.
int i; int i;
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) { for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
int position = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE; int position = (nBucketPos + i) % ADDRMAN_BUCKET_SIZE;
int node_id = GetEntry(search_tried, nKBucket, position); int node_id = GetEntry(search_tried, nBucket, position);
if (node_id != -1) break; if (node_id != -1) break;
} }
// If the bucket is entirely empty, start over with a (likely) different one. // If the bucket is entirely empty, start over with a (likely) different one.
if (i == ADDRMAN_BUCKET_SIZE) continue; if (i == ADDRMAN_BUCKET_SIZE) continue;
// Find the entry to return. // Find the entry to return.
int position = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE; int position = (nBucketPos + i) % ADDRMAN_BUCKET_SIZE;
int nId = GetEntry(search_tried, nKBucket, position); int nId = GetEntry(search_tried, nBucket, position);
const auto it_found{mapInfo.find(nId)}; const auto it_found{mapInfo.find(nId)};
assert(it_found != mapInfo.end()); assert(it_found != mapInfo.end());
const AddrInfo& info{it_found->second}; const AddrInfo& info{it_found->second};
// With probability GetChance() * fChanceFactor, return the entry. // With probability GetChance() * fChanceFactor, return the entry.
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) { if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
LogPrint(BCLog::ADDRMAN, "Selected %s from tried\n", info.ToStringAddrPort()); LogPrint(BCLog::ADDRMAN, "Selected %s from %s\n", info.ToStringAddrPort(), search_tried ? "tried" : "new");
return {info, info.m_last_try}; return {info, info.m_last_try};
} }
// Otherwise start over with a (likely) different bucket, and increased chance factor. // Otherwise start over with a (likely) different bucket, and increased chance factor.
fChanceFactor *= 1.2; 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(bucket_count);
int nUBucketPos = insecure_rand.randrange(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) {
int position = (nUBucketPos + i) % ADDRMAN_BUCKET_SIZE;
int node_id = GetEntry(search_tried, nUBucket, position);
if (node_id != -1) break;
}
// 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 position = (nUBucketPos + i) % ADDRMAN_BUCKET_SIZE;
int nId = GetEntry(search_tried, nUBucket, position);
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.ToStringAddrPort());
return {info, info.m_last_try};
}
// Otherwise start over with a (likely) different bucket, and increased chance factor.
fChanceFactor *= 1.2;
}
}
} }
int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const