diff options
author | Amiti Uttarwar <amiti@uttarwar.org> | 2023-02-18 18:01:18 -0700 |
---|---|---|
committer | Amiti Uttarwar <amiti@uttarwar.org> | 2023-03-17 17:59:02 -0700 |
commit | ca2a9c5f8f14b792a14e81f73b1910a4c8799b93 (patch) | |
tree | c1d18609ea79b50b82aa7ecad08c185511be34d2 | |
parent | 052fbcd5a791855406141e85d32e42e297220fe9 (diff) |
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 <mzumsande@gmail.com>
-rw-r--r-- | src/addrman.cpp | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/addrman.cpp b/src/addrman.cpp index 966cf6b043..afaa040b0f 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -723,14 +723,17 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const // Decide if we are going to search the new or tried table bool search_tried; + int bucket_count; // Use a 50% chance for choosing between tried and new table entries. if (!newOnly && (nTried > 0 && (nNew == 0 || insecure_rand.randbool() == 0))) { search_tried = true; + bucket_count = ADDRMAN_TRIED_BUCKET_COUNT; } else { search_tried = false; + bucket_count = ADDRMAN_NEW_BUCKET_COUNT; } if (search_tried) { @@ -738,18 +741,21 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const 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 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) { - if (vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break; + 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 nId = vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE]; + 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}; @@ -766,18 +772,21 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const 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 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) { - if (vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break; + 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 nId = vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE]; + 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}; |