Skip to content

Commit

Permalink
[addrman] Fix new table bucketing during unserialization
Browse files Browse the repository at this point in the history
An addrman entry can appear in up to 8 new table buckets. We store this
entry->bucket indexing during shutdown so that on restart we can restore
the entries to their correct buckets.

Commit ec45646 broke the
deserialization code so that each entry could only be put in up to one
new bucket. Fix that.
  • Loading branch information
jnewbery committed Jan 18, 2021
1 parent 7acda55 commit b4c5fda
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,9 @@ friend class CAddrManTest;
nTried -= nLost;

// Store positions in the new table buckets to apply later (if possible).
std::map<int, int> entryToBucket; // Represents which entry belonged to which bucket when serializing
// An entry may appear in up to ADDRMAN_NEW_BUCKETS_PER_ADDRESS buckets,
// so we store all bucket-entry_index pairs to iterate through later.
std::vector<std::pair<int, int>> bucket_entries;

for (int bucket = 0; bucket < nUBuckets; bucket++) {
int nSize = 0;
Expand All @@ -509,7 +511,7 @@ friend class CAddrManTest;
int nIndex = 0;
s >> nIndex;
if (nIndex >= 0 && nIndex < nNew) {
entryToBucket[nIndex] = bucket;
bucket_entries.emplace_back(bucket, nIndex);
}
}
}
Expand All @@ -523,9 +525,10 @@ friend class CAddrManTest;
s >> serialized_asmap_version;
}

for (int n = 0; n < nNew; n++) {
CAddrInfo &info = mapInfo[n];
int bucket = entryToBucket[n];
for (auto bucket_entry : bucket_entries) {
int bucket{bucket_entry.first};
const int n{bucket_entry.second};
CAddrInfo& info = mapInfo[n];
int nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
if (format >= Format::V2_ASMAP && nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && vvNew[bucket][nUBucketPos] == -1 &&
info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS && serialized_asmap_version == supplied_asmap_version) {
Expand Down

0 comments on commit b4c5fda

Please sign in to comment.