Skip to content

Commit 051ba32

Browse files
mzumsandesipa
andcommitted
addrman, refactor: introduce user-defined type for internal nId
This makes it easier to track which spots refer to an nId (as opposed to, for example, bucket index etc. which also use int) Co-authored-by: Pieter Wuille <pieter@wuille.net>
1 parent c91cabb commit 051ba32

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

src/addrman.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void AddrManImpl::Serialize(Stream& s_) const
188188

189189
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
190190
s << nUBuckets;
191-
std::unordered_map<int, int> mapUnkIds;
191+
std::unordered_map<nid_type, int> mapUnkIds;
192192
int nIds = 0;
193193
for (const auto& entry : mapInfo) {
194194
mapUnkIds[entry.first] = nIds;
@@ -398,7 +398,7 @@ void AddrManImpl::Unserialize(Stream& s_)
398398
}
399399
}
400400

401-
AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
401+
AddrInfo* AddrManImpl::Find(const CService& addr, nid_type* pnId)
402402
{
403403
AssertLockHeld(cs);
404404

@@ -413,11 +413,11 @@ AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
413413
return nullptr;
414414
}
415415

416-
AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
416+
AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId)
417417
{
418418
AssertLockHeld(cs);
419419

420-
int nId = nIdCount++;
420+
nid_type nId = nIdCount++;
421421
mapInfo[nId] = AddrInfo(addr, addrSource);
422422
mapAddr[addr] = nId;
423423
mapInfo[nId].nRandomPos = vRandom.size();
@@ -438,8 +438,8 @@ void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
438438

439439
assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
440440

441-
int nId1 = vRandom[nRndPos1];
442-
int nId2 = vRandom[nRndPos2];
441+
nid_type nId1 = vRandom[nRndPos1];
442+
nid_type nId2 = vRandom[nRndPos2];
443443

444444
const auto it_1{mapInfo.find(nId1)};
445445
const auto it_2{mapInfo.find(nId2)};
@@ -453,7 +453,7 @@ void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
453453
vRandom[nRndPos2] = nId1;
454454
}
455455

456-
void AddrManImpl::Delete(int nId)
456+
void AddrManImpl::Delete(nid_type nId)
457457
{
458458
AssertLockHeld(cs);
459459

@@ -476,7 +476,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
476476

477477
// if there is an entry in the specified bucket, delete it.
478478
if (vvNew[nUBucket][nUBucketPos] != -1) {
479-
int nIdDelete = vvNew[nUBucket][nUBucketPos];
479+
nid_type nIdDelete = vvNew[nUBucket][nUBucketPos];
480480
AddrInfo& infoDelete = mapInfo[nIdDelete];
481481
assert(infoDelete.nRefCount > 0);
482482
infoDelete.nRefCount--;
@@ -488,7 +488,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
488488
}
489489
}
490490

491-
void AddrManImpl::MakeTried(AddrInfo& info, int nId)
491+
void AddrManImpl::MakeTried(AddrInfo& info, nid_type nId)
492492
{
493493
AssertLockHeld(cs);
494494

@@ -515,7 +515,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
515515
// first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
516516
if (vvTried[nKBucket][nKBucketPos] != -1) {
517517
// find an item to evict
518-
int nIdEvict = vvTried[nKBucket][nKBucketPos];
518+
nid_type nIdEvict = vvTried[nKBucket][nKBucketPos];
519519
assert(mapInfo.count(nIdEvict) == 1);
520520
AddrInfo& infoOld = mapInfo[nIdEvict];
521521

@@ -554,7 +554,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
554554
if (!addr.IsRoutable())
555555
return false;
556556

557-
int nId;
557+
nid_type nId;
558558
AddrInfo* pinfo = Find(addr, &nId);
559559

560560
// Do not set a penalty for a source's self-announcement
@@ -627,7 +627,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, NodeSecond
627627
{
628628
AssertLockHeld(cs);
629629

630-
int nId;
630+
nid_type nId;
631631

632632
m_last_good = time;
633633

@@ -753,7 +753,8 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, std::option
753753

754754
// Iterate over the positions of that bucket, starting at the initial one,
755755
// and looping around.
756-
int i, position, node_id;
756+
int i, position;
757+
nid_type node_id;
757758
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
758759
position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
759760
node_id = GetEntry(search_tried, bucket, position);
@@ -786,7 +787,7 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, std::option
786787
}
787788
}
788789

789-
int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
790+
nid_type AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
790791
{
791792
AssertLockHeld(cs);
792793

@@ -849,7 +850,7 @@ std::vector<std::pair<AddrInfo, AddressPosition>> AddrManImpl::GetEntries_(bool
849850
std::vector<std::pair<AddrInfo, AddressPosition>> infos;
850851
for (int bucket = 0; bucket < bucket_count; ++bucket) {
851852
for (int position = 0; position < ADDRMAN_BUCKET_SIZE; ++position) {
852-
int id = GetEntry(from_tried, bucket, position);
853+
nid_type id = GetEntry(from_tried, bucket, position);
853854
if (id >= 0) {
854855
AddrInfo info = mapInfo.at(id);
855856
AddressPosition location = AddressPosition(
@@ -904,8 +905,8 @@ void AddrManImpl::ResolveCollisions_()
904905
{
905906
AssertLockHeld(cs);
906907

907-
for (std::set<int>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
908-
int id_new = *it;
908+
for (std::set<nid_type>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
909+
nid_type id_new = *it;
909910

910911
bool erase_collision = false;
911912

@@ -923,7 +924,7 @@ void AddrManImpl::ResolveCollisions_()
923924
} else if (vvTried[tried_bucket][tried_bucket_pos] != -1) { // The position in the tried bucket is not empty
924925

925926
// Get the to-be-evicted address that is being tested
926-
int id_old = vvTried[tried_bucket][tried_bucket_pos];
927+
nid_type id_old = vvTried[tried_bucket][tried_bucket_pos];
927928
AddrInfo& info_old = mapInfo[id_old];
928929

929930
const auto current_time{Now<NodeSeconds>()};
@@ -969,11 +970,11 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision_()
969970

970971
if (m_tried_collisions.size() == 0) return {};
971972

972-
std::set<int>::iterator it = m_tried_collisions.begin();
973+
std::set<nid_type>::iterator it = m_tried_collisions.begin();
973974

974975
// Selects a random element from m_tried_collisions
975976
std::advance(it, insecure_rand.randrange(m_tried_collisions.size()));
976-
int id_new = *it;
977+
nid_type id_new = *it;
977978

978979
// If id_new not found in mapInfo remove it from m_tried_collisions
979980
if (mapInfo.count(id_new) != 1) {
@@ -1058,15 +1059,15 @@ int AddrManImpl::CheckAddrman() const
10581059
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
10591060
strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
10601061

1061-
std::unordered_set<int> setTried;
1062-
std::unordered_map<int, int> mapNew;
1062+
std::unordered_set<nid_type> setTried;
1063+
std::unordered_map<nid_type, int> mapNew;
10631064
std::unordered_map<Network, NewTriedCount> local_counts;
10641065

10651066
if (vRandom.size() != (size_t)(nTried + nNew))
10661067
return -7;
10671068

10681069
for (const auto& entry : mapInfo) {
1069-
int n = entry.first;
1070+
nid_type n = entry.first;
10701071
const AddrInfo& info = entry.second;
10711072
if (info.fInTried) {
10721073
if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) {

src/addrman_impl.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ static constexpr int ADDRMAN_NEW_BUCKET_COUNT{1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2
3232
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
3333
static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
3434

35+
/** User-defined type for the internally used nIds */
36+
using nid_type = int;
37+
3538
/**
3639
* Extended statistics about a CAddress
3740
*/
@@ -179,36 +182,36 @@ class AddrManImpl
179182
static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
180183

181184
//! last used nId
182-
int nIdCount GUARDED_BY(cs){0};
185+
nid_type nIdCount GUARDED_BY(cs){0};
183186

184187
//! table with information about all nIds
185-
std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
188+
std::unordered_map<nid_type, AddrInfo> mapInfo GUARDED_BY(cs);
186189

187190
//! find an nId based on its network address and port.
188-
std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
191+
std::unordered_map<CService, nid_type, CServiceHash> mapAddr GUARDED_BY(cs);
189192

190193
//! randomly-ordered vector of all nIds
191194
//! This is mutable because it is unobservable outside the class, so any
192195
//! changes to it (even in const methods) are also unobservable.
193-
mutable std::vector<int> vRandom GUARDED_BY(cs);
196+
mutable std::vector<nid_type> vRandom GUARDED_BY(cs);
194197

195198
// number of "tried" entries
196199
int nTried GUARDED_BY(cs){0};
197200

198201
//! list of "tried" buckets
199-
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
202+
nid_type vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
200203

201204
//! number of (unique) "new" entries
202205
int nNew GUARDED_BY(cs){0};
203206

204207
//! list of "new" buckets
205-
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
208+
nid_type vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
206209

207210
//! last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
208211
NodeSeconds m_last_good GUARDED_BY(cs){1s};
209212

210213
//! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
211-
std::set<int> m_tried_collisions;
214+
std::set<nid_type> m_tried_collisions;
212215

213216
/** Perform consistency checks every m_consistency_check_ratio operations (if non-zero). */
214217
const int32_t m_consistency_check_ratio;
@@ -225,22 +228,22 @@ class AddrManImpl
225228
std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
226229

227230
//! Find an entry.
228-
AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
231+
AddrInfo* Find(const CService& addr, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
229232

230233
//! Create a new entry and add it to the internal data structures mapInfo, mapAddr and vRandom.
231-
AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
234+
AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
232235

233236
//! Swap two elements in vRandom.
234237
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
235238

236239
//! Delete an entry. It must not be in tried, and have refcount 0.
237-
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
240+
void Delete(nid_type nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
238241

239242
//! Clear a position in a "new" table. This is the only place where entries are actually deleted.
240243
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
241244

242245
//! Move an entry from the "new" table(s) to the "tried" table
243-
void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
246+
void MakeTried(AddrInfo& info, nid_type nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
244247

245248
/** Attempt to add a single address to addrman's new table.
246249
* @see AddrMan::Add() for parameters. */
@@ -256,9 +259,9 @@ class AddrManImpl
256259

257260
/** Helper to generalize looking up an addrman entry from either table.
258261
*
259-
* @return int The nid of the entry. If the addrman position is empty or not found, returns -1.
262+
* @return nid_type The nid of the entry. If the addrman position is empty or not found, returns -1.
260263
* */
261-
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
264+
nid_type GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
262265

263266
std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
264267

src/test/fuzz/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class AddrManDeterministic : public AddrMan
186186
return false;
187187
}
188188

189-
auto IdsReferToSameAddress = [&](int id, int other_id) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs, other.m_impl->cs) {
189+
auto IdsReferToSameAddress = [&](nid_type id, nid_type other_id) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs, other.m_impl->cs) {
190190
if (id == -1 && other_id == -1) {
191191
return true;
192192
}

0 commit comments

Comments
 (0)