Skip to content

Commit ca2a9c5

Browse files
refactor: generalize select logic
in preparation for consolidating the logic for searching the new and tried tables, generalize the call paths for both Co-authored-by: Martin Zumsande <[email protected]>
1 parent 052fbcd commit ca2a9c5

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/addrman.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,33 +723,39 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
723723

724724
// Decide if we are going to search the new or tried table
725725
bool search_tried;
726+
int bucket_count;
726727

727728
// Use a 50% chance for choosing between tried and new table entries.
728729
if (!newOnly &&
729730
(nTried > 0 &&
730731
(nNew == 0 || insecure_rand.randbool() == 0))) {
731732
search_tried = true;
733+
bucket_count = ADDRMAN_TRIED_BUCKET_COUNT;
732734
} else {
733735
search_tried = false;
736+
bucket_count = ADDRMAN_NEW_BUCKET_COUNT;
734737
}
735738

736739
if (search_tried) {
737740
// use a tried node
738741
double fChanceFactor = 1.0;
739742
while (1) {
740743
// Pick a tried bucket, and an initial position in that bucket.
741-
int nKBucket = insecure_rand.randrange(ADDRMAN_TRIED_BUCKET_COUNT);
744+
int nKBucket = insecure_rand.randrange(bucket_count);
742745
int nKBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
743746
// Iterate over the positions of that bucket, starting at the initial one,
744747
// and looping around.
745748
int i;
746749
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
747-
if (vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break;
750+
int position = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE;
751+
int node_id = GetEntry(search_tried, nKBucket, position);
752+
if (node_id != -1) break;
748753
}
749754
// If the bucket is entirely empty, start over with a (likely) different one.
750755
if (i == ADDRMAN_BUCKET_SIZE) continue;
751756
// Find the entry to return.
752-
int nId = vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE];
757+
int position = (nKBucketPos + i) % ADDRMAN_BUCKET_SIZE;
758+
int nId = GetEntry(search_tried, nKBucket, position);
753759
const auto it_found{mapInfo.find(nId)};
754760
assert(it_found != mapInfo.end());
755761
const AddrInfo& info{it_found->second};
@@ -766,18 +772,21 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
766772
double fChanceFactor = 1.0;
767773
while (1) {
768774
// Pick a new bucket, and an initial position in that bucket.
769-
int nUBucket = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
775+
int nUBucket = insecure_rand.randrange(bucket_count);
770776
int nUBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
771777
// Iterate over the positions of that bucket, starting at the initial one,
772778
// and looping around.
773779
int i;
774780
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
775-
if (vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break;
781+
int position = (nUBucketPos + i) % ADDRMAN_BUCKET_SIZE;
782+
int node_id = GetEntry(search_tried, nUBucket, position);
783+
if (node_id != -1) break;
776784
}
777785
// If the bucket is entirely empty, start over with a (likely) different one.
778786
if (i == ADDRMAN_BUCKET_SIZE) continue;
779787
// Find the entry to return.
780-
int nId = vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE];
788+
int position = (nUBucketPos + i) % ADDRMAN_BUCKET_SIZE;
789+
int nId = GetEntry(search_tried, nUBucket, position);
781790
const auto it_found{mapInfo.find(nId)};
782791
assert(it_found != mapInfo.end());
783792
const AddrInfo& info{it_found->second};

0 commit comments

Comments
 (0)