aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmiti Uttarwar <amiti@uttarwar.org>2023-02-18 18:13:00 -0700
committerAmiti Uttarwar <amiti@uttarwar.org>2023-03-17 17:59:02 -0700
commit48806412e2bcd023b78fc05f6c9ce092360d1db1 (patch)
treee77067a57e9e19bac1f11f00d6e5a457cffb38b4
parentca2a9c5f8f14b792a14e81f73b1910a4c8799b93 (diff)
refactor: consolidate select logic for new and tried tables
Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
-rw-r--r--src/addrman.cpp91
1 files changed, 31 insertions, 60 deletions
diff --git a/src/addrman.cpp b/src/addrman.cpp
index afaa040b0f..69ad13a912 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -736,68 +736,39 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
bucket_count = ADDRMAN_NEW_BUCKET_COUNT;
}
- if (search_tried) {
- // 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(bucket_count);
- int nKBucketPos = 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 = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE;
- int node_id = GetEntry(search_tried, nKBucket, 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 = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE;
- int nId = GetEntry(search_tried, nKBucket, 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 tried\n", info.ToStringAddrPort());
- return {info, info.m_last_try};
- }
- // Otherwise start over with a (likely) different bucket, and increased chance factor.
- fChanceFactor *= 1.2;
+ double fChanceFactor = 1.0;
+ while (1) {
+ // Pick a bucket, and an initial position in that bucket.
+ int nBucket = insecure_rand.randrange(bucket_count);
+ int nBucketPos = 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 = (nBucketPos + i) % ADDRMAN_BUCKET_SIZE;
+ int node_id = GetEntry(search_tried, nBucket, position);
+ if (node_id != -1) break;
}
- } 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;
+
+ // 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 = (nBucketPos + i) % ADDRMAN_BUCKET_SIZE;
+ int nId = GetEntry(search_tried, nBucket, 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 %s\n", info.ToStringAddrPort(), search_tried ? "tried" : "new");
+ return {info, info.m_last_try};
}
+
+ // Otherwise start over with a (likely) different bucket, and increased chance factor.
+ fChanceFactor *= 1.2;
}
}