mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
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>
This commit is contained in:
parent
052fbcd5a7
commit
ca2a9c5f8f
1 changed files with 15 additions and 6 deletions
|
@ -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
|
// Decide if we are going to search the new or tried table
|
||||||
bool search_tried;
|
bool search_tried;
|
||||||
|
int bucket_count;
|
||||||
|
|
||||||
// Use a 50% chance for choosing between tried and new table entries.
|
// Use a 50% chance for choosing between tried and new table entries.
|
||||||
if (!newOnly &&
|
if (!newOnly &&
|
||||||
(nTried > 0 &&
|
(nTried > 0 &&
|
||||||
(nNew == 0 || insecure_rand.randbool() == 0))) {
|
(nNew == 0 || insecure_rand.randbool() == 0))) {
|
||||||
search_tried = true;
|
search_tried = true;
|
||||||
|
bucket_count = ADDRMAN_TRIED_BUCKET_COUNT;
|
||||||
} else {
|
} else {
|
||||||
search_tried = false;
|
search_tried = false;
|
||||||
|
bucket_count = ADDRMAN_NEW_BUCKET_COUNT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (search_tried) {
|
if (search_tried) {
|
||||||
|
@ -738,18 +741,21 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
|
||||||
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 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);
|
int nKBucketPos = 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) {
|
||||||
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 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 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)};
|
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};
|
||||||
|
@ -766,18 +772,21 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool newOnly) const
|
||||||
double fChanceFactor = 1.0;
|
double fChanceFactor = 1.0;
|
||||||
while (1) {
|
while (1) {
|
||||||
// Pick a new bucket, and an initial position in that bucket.
|
// 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);
|
int nUBucketPos = 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) {
|
||||||
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 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 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)};
|
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};
|
||||||
|
|
Loading…
Add table
Reference in a new issue