Skip to content

Commit a5c9b04

Browse files
committed
[addrman] Don't rebucket new table entries unnecessarily
Only rebucket if the asmap checksum has changed, not if the file format has changed but no asmap is provided. Also, don't try to add an entry to another bucket if it already appears in ADDRMAN_NEW_BUCKETS_PER_ADDRESS buckets.
1 parent 8062d92 commit a5c9b04

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/addrman.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ friend class CAddrManTest;
517517
}
518518
}
519519

520+
// Attempt to restore the entry's new buckets if the bucket count and asmap
521+
// checksum haven't changed
520522
uint256 supplied_asmap_checksum;
521523
if (m_asmap.size() != 0) {
522524
supplied_asmap_checksum = SerializeHash(m_asmap);
@@ -525,19 +527,26 @@ friend class CAddrManTest;
525527
if (format >= Format::V2_ASMAP) {
526528
s >> serialized_asmap_checksum;
527529
}
530+
const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
531+
serialized_asmap_checksum == supplied_asmap_checksum};
528532

529533
for (auto bucket_entry : bucket_entries) {
530534
int bucket{bucket_entry.first};
531535
const int entry_index{bucket_entry.second};
532536
CAddrInfo& info = mapInfo[entry_index];
537+
538+
// The entry shouldn't appear in more than
539+
// ADDRMAN_NEW_BUCKETS_PER_ADDRESS. If it has already, just skip
540+
// this bucket_entry.
541+
if (info.nRefCount >= ADDRMAN_NEW_BUCKETS_PER_ADDRESS) continue;
542+
533543
int nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
534-
if (format >= Format::V2_ASMAP && nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && vvNew[bucket][nUBucketPos] == -1 &&
535-
info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS && serialized_asmap_checksum == supplied_asmap_checksum) {
544+
if (restore_bucketing && vvNew[bucket][nUBucketPos] == -1) {
536545
// Bucketing has not changed, using existing bucket positions for the new table
537546
vvNew[bucket][nUBucketPos] = entry_index;
538547
info.nRefCount++;
539548
} else {
540-
// In case the new table data cannot be used (format unknown, bucket count wrong or new asmap),
549+
// In case the new table data cannot be used (bucket count wrong or new asmap),
541550
// try to give them a reference based on their primary source address.
542551
LogPrint(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");
543552
bucket = info.GetNewBucket(nKey, m_asmap);

0 commit comments

Comments
 (0)