Skip to content

Commit 4880641

Browse files
refactor: consolidate select logic for new and tried tables
Co-authored-by: Martin Zumsande <[email protected]>
1 parent ca2a9c5 commit 4880641

File tree

1 file changed

+31
-60
lines changed

1 file changed

+31
-60
lines changed

src/addrman.cpp

Lines changed: 31 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -736,68 +736,39 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
736736
bucket_count = ADDRMAN_NEW_BUCKET_COUNT;
737737
}
738738

739-
if (search_tried) {
740-
// use a tried node
741-
double fChanceFactor = 1.0;
742-
while (1) {
743-
// Pick a tried bucket, and an initial position in that bucket.
744-
int nKBucket = insecure_rand.randrange(bucket_count);
745-
int nKBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
746-
// Iterate over the positions of that bucket, starting at the initial one,
747-
// and looping around.
748-
int i;
749-
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
750-
int position = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE;
751-
int node_id = GetEntry(search_tried, nKBucket, position);
752-
if (node_id != -1) break;
753-
}
754-
// If the bucket is entirely empty, start over with a (likely) different one.
755-
if (i == ADDRMAN_BUCKET_SIZE) continue;
756-
// Find the entry to return.
757-
int position = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE;
758-
int nId = GetEntry(search_tried, nKBucket, position);
759-
const auto it_found{mapInfo.find(nId)};
760-
assert(it_found != mapInfo.end());
761-
const AddrInfo& info{it_found->second};
762-
// With probability GetChance() * fChanceFactor, return the entry.
763-
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
764-
LogPrint(BCLog::ADDRMAN, "Selected %s from tried\n", info.ToStringAddrPort());
765-
return {info, info.m_last_try};
766-
}
767-
// Otherwise start over with a (likely) different bucket, and increased chance factor.
768-
fChanceFactor *= 1.2;
739+
double fChanceFactor = 1.0;
740+
while (1) {
741+
// Pick a bucket, and an initial position in that bucket.
742+
int nBucket = insecure_rand.randrange(bucket_count);
743+
int nBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
744+
745+
// Iterate over the positions of that bucket, starting at the initial one,
746+
// and looping around.
747+
int i;
748+
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
749+
int position = (nBucketPos + i) % ADDRMAN_BUCKET_SIZE;
750+
int node_id = GetEntry(search_tried, nBucket, position);
751+
if (node_id != -1) break;
769752
}
770-
} else {
771-
// use a new node
772-
double fChanceFactor = 1.0;
773-
while (1) {
774-
// Pick a new bucket, and an initial position in that bucket.
775-
int nUBucket = insecure_rand.randrange(bucket_count);
776-
int nUBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
777-
// Iterate over the positions of that bucket, starting at the initial one,
778-
// and looping around.
779-
int i;
780-
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
781-
int position = (nUBucketPos + i) % ADDRMAN_BUCKET_SIZE;
782-
int node_id = GetEntry(search_tried, nUBucket, position);
783-
if (node_id != -1) break;
784-
}
785-
// If the bucket is entirely empty, start over with a (likely) different one.
786-
if (i == ADDRMAN_BUCKET_SIZE) continue;
787-
// Find the entry to return.
788-
int position = (nUBucketPos + i) % ADDRMAN_BUCKET_SIZE;
789-
int nId = GetEntry(search_tried, nUBucket, position);
790-
const auto it_found{mapInfo.find(nId)};
791-
assert(it_found != mapInfo.end());
792-
const AddrInfo& info{it_found->second};
793-
// With probability GetChance() * fChanceFactor, return the entry.
794-
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
795-
LogPrint(BCLog::ADDRMAN, "Selected %s from new\n", info.ToStringAddrPort());
796-
return {info, info.m_last_try};
797-
}
798-
// Otherwise start over with a (likely) different bucket, and increased chance factor.
799-
fChanceFactor *= 1.2;
753+
754+
// If the bucket is entirely empty, start over with a (likely) different one.
755+
if (i == ADDRMAN_BUCKET_SIZE) continue;
756+
757+
// Find the entry to return.
758+
int position = (nBucketPos + i) % ADDRMAN_BUCKET_SIZE;
759+
int nId = GetEntry(search_tried, nBucket, position);
760+
const auto it_found{mapInfo.find(nId)};
761+
assert(it_found != mapInfo.end());
762+
const AddrInfo& info{it_found->second};
763+
764+
// With probability GetChance() * fChanceFactor, return the entry.
765+
if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
766+
LogPrint(BCLog::ADDRMAN, "Selected %s from %s\n", info.ToStringAddrPort(), search_tried ? "tried" : "new");
767+
return {info, info.m_last_try};
800768
}
769+
770+
// Otherwise start over with a (likely) different bucket, and increased chance factor.
771+
fChanceFactor *= 1.2;
801772
}
802773
}
803774

0 commit comments

Comments
 (0)