@@ -43,17 +43,17 @@ static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
4343/* * The maximum time we'll spend trying to resolve a tried table collision, in seconds */
4444static constexpr int64_t ADDRMAN_TEST_WINDOW{40 *60 }; // 40 minutes
4545
46- int AddrInfo::GetTriedBucket (const uint256& nKey, const std::vector< bool >& asmap ) const
46+ int AddrInfo::GetTriedBucket (const uint256& nKey, const NetGroupManager& netgroupman ) const
4747{
4848 uint64_t hash1 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetKey ()).GetCheapHash ();
49- uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetGroup (asmap ) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash ();
49+ uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << netgroupman. GetGroup (* this ) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash ();
5050 return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
5151}
5252
53- int AddrInfo::GetNewBucket (const uint256& nKey, const CNetAddr& src, const std::vector< bool >& asmap ) const
53+ int AddrInfo::GetNewBucket (const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman ) const
5454{
55- std::vector<unsigned char > vchSourceGroupKey = src .GetGroup (asmap );
56- uint64_t hash1 = (CHashWriter (SER_GETHASH, 0 ) << nKey << GetGroup (asmap ) << vchSourceGroupKey).GetCheapHash ();
55+ std::vector<unsigned char > vchSourceGroupKey = netgroupman .GetGroup (src );
56+ uint64_t hash1 = (CHashWriter (SER_GETHASH, 0 ) << nKey << netgroupman. GetGroup (* this ) << vchSourceGroupKey).GetCheapHash ();
5757 uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash ();
5858 return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
5959}
@@ -99,11 +99,11 @@ double AddrInfo::GetChance(int64_t nNow) const
9999 return fChance ;
100100}
101101
102- AddrManImpl::AddrManImpl (std::vector< bool >&& asmap , bool deterministic, int32_t consistency_check_ratio)
102+ AddrManImpl::AddrManImpl (const NetGroupManager& netgroupman , bool deterministic, int32_t consistency_check_ratio)
103103 : insecure_rand{deterministic}
104104 , nKey{deterministic ? uint256{1 } : insecure_rand.rand256 ()}
105105 , m_consistency_check_ratio{consistency_check_ratio}
106- , m_asmap{ std::move (asmap) }
106+ , m_netgroupman{netgroupman }
107107{
108108 for (auto & bucket : vvNew) {
109109 for (auto & entry : bucket) {
@@ -218,11 +218,7 @@ void AddrManImpl::Serialize(Stream& s_) const
218218 }
219219 // Store asmap checksum after bucket entries so that it
220220 // can be ignored by older clients for backward compatibility.
221- uint256 asmap_checksum;
222- if (m_asmap.size () != 0 ) {
223- asmap_checksum = SerializeHash (m_asmap);
224- }
225- s << asmap_checksum;
221+ s << m_netgroupman.GetAsmapChecksum ();
226222}
227223
228224template <typename Stream>
@@ -298,7 +294,7 @@ void AddrManImpl::Unserialize(Stream& s_)
298294 for (int n = 0 ; n < nTried; n++) {
299295 AddrInfo info;
300296 s >> info;
301- int nKBucket = info.GetTriedBucket (nKey, m_asmap );
297+ int nKBucket = info.GetTriedBucket (nKey, m_netgroupman );
302298 int nKBucketPos = info.GetBucketPosition (nKey, false , nKBucket);
303299 if (info.IsValid ()
304300 && vvTried[nKBucket][nKBucketPos] == -1 ) {
@@ -335,10 +331,7 @@ void AddrManImpl::Unserialize(Stream& s_)
335331 // If the bucket count and asmap checksum haven't changed, then attempt
336332 // to restore the entries to the buckets/positions they were in before
337333 // serialization.
338- uint256 supplied_asmap_checksum;
339- if (m_asmap.size () != 0 ) {
340- supplied_asmap_checksum = SerializeHash (m_asmap);
341- }
334+ uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum ()};
342335 uint256 serialized_asmap_checksum;
343336 if (format >= Format::V2_ASMAP) {
344337 s >> serialized_asmap_checksum;
@@ -371,7 +364,7 @@ void AddrManImpl::Unserialize(Stream& s_)
371364 } else {
372365 // In case the new table data cannot be used (bucket count wrong or new asmap),
373366 // try to give them a reference based on their primary source address.
374- bucket = info.GetNewBucket (nKey, m_asmap );
367+ bucket = info.GetNewBucket (nKey, m_netgroupman );
375368 bucket_position = info.GetBucketPosition (nKey, true , bucket);
376369 if (vvNew[bucket][bucket_position] == -1 ) {
377370 vvNew[bucket][bucket_position] = entry_index;
@@ -495,7 +488,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
495488 AssertLockHeld (cs);
496489
497490 // remove the entry from all new buckets
498- const int start_bucket{info.GetNewBucket (nKey, m_asmap )};
491+ const int start_bucket{info.GetNewBucket (nKey, m_netgroupman )};
499492 for (int n = 0 ; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
500493 const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
501494 const int pos{info.GetBucketPosition (nKey, true , bucket)};
@@ -510,7 +503,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
510503 assert (info.nRefCount == 0 );
511504
512505 // which tried bucket to move the entry to
513- int nKBucket = info.GetTriedBucket (nKey, m_asmap );
506+ int nKBucket = info.GetTriedBucket (nKey, m_netgroupman );
514507 int nKBucketPos = info.GetBucketPosition (nKey, false , nKBucket);
515508
516509 // first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
@@ -526,7 +519,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
526519 nTried--;
527520
528521 // find which new bucket it belongs to
529- int nUBucket = infoOld.GetNewBucket (nKey, m_asmap );
522+ int nUBucket = infoOld.GetNewBucket (nKey, m_netgroupman );
530523 int nUBucketPos = infoOld.GetBucketPosition (nKey, true , nUBucket);
531524 ClearNew (nUBucket, nUBucketPos);
532525 assert (vvNew[nUBucket][nUBucketPos] == -1 );
@@ -594,7 +587,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
594587 nNew++;
595588 }
596589
597- int nUBucket = pinfo->GetNewBucket (nKey, source, m_asmap );
590+ int nUBucket = pinfo->GetNewBucket (nKey, source, m_netgroupman );
598591 int nUBucketPos = pinfo->GetBucketPosition (nKey, true , nUBucket);
599592 bool fInsert = vvNew[nUBucket][nUBucketPos] == -1 ;
600593 if (vvNew[nUBucket][nUBucketPos] != nId) {
@@ -610,7 +603,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
610603 pinfo->nRefCount ++;
611604 vvNew[nUBucket][nUBucketPos] = nId;
612605 LogPrint (BCLog::ADDRMAN, " Added %s mapped to AS%i to new[%i][%i]\n " ,
613- addr.ToString (), addr .GetMappedAS (m_asmap ), nUBucket, nUBucketPos);
606+ addr.ToString (), m_netgroupman .GetMappedAS (addr ), nUBucket, nUBucketPos);
614607 } else {
615608 if (pinfo->nRefCount == 0 ) {
616609 Delete (nId);
@@ -650,7 +643,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
650643
651644
652645 // which tried bucket to move the entry to
653- int tried_bucket = info.GetTriedBucket (nKey, m_asmap );
646+ int tried_bucket = info.GetTriedBucket (nKey, m_netgroupman );
654647 int tried_bucket_pos = info.GetBucketPosition (nKey, false , tried_bucket);
655648
656649 // Will moving this address into tried evict another entry?
@@ -669,7 +662,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
669662 // move nId to the tried tables
670663 MakeTried (info, nId);
671664 LogPrint (BCLog::ADDRMAN, " Moved %s mapped to AS%i to tried[%i][%i]\n " ,
672- addr.ToString (), addr .GetMappedAS (m_asmap ), tried_bucket, tried_bucket_pos);
665+ addr.ToString (), m_netgroupman .GetMappedAS (addr ), tried_bucket, tried_bucket_pos);
673666 return true ;
674667 }
675668}
@@ -863,7 +856,7 @@ void AddrManImpl::ResolveCollisions_()
863856 AddrInfo& info_new = mapInfo[id_new];
864857
865858 // Which tried bucket to move the entry to.
866- int tried_bucket = info_new.GetTriedBucket (nKey, m_asmap );
859+ int tried_bucket = info_new.GetTriedBucket (nKey, m_netgroupman );
867860 int tried_bucket_pos = info_new.GetBucketPosition (nKey, false , tried_bucket);
868861 if (!info_new.IsValid ()) { // id_new may no longer map to a valid address
869862 erase_collision = true ;
@@ -929,7 +922,7 @@ std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision_()
929922 const AddrInfo& newInfo = mapInfo[id_new];
930923
931924 // which tried bucket to move the entry to
932- int tried_bucket = newInfo.GetTriedBucket (nKey, m_asmap );
925+ int tried_bucket = newInfo.GetTriedBucket (nKey, m_netgroupman );
933926 int tried_bucket_pos = newInfo.GetBucketPosition (nKey, false , tried_bucket);
934927
935928 const AddrInfo& info_old = mapInfo[vvTried[tried_bucket][tried_bucket_pos]];
@@ -945,13 +938,13 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry_(const CAddress& ad
945938 if (!addr_info) return std::nullopt ;
946939
947940 if (addr_info->fInTried ) {
948- int bucket{addr_info->GetTriedBucket (nKey, m_asmap )};
941+ int bucket{addr_info->GetTriedBucket (nKey, m_netgroupman )};
949942 return AddressPosition (/* tried_in=*/ true ,
950943 /* multiplicity_in=*/ 1 ,
951944 /* bucket_in=*/ bucket,
952945 /* position_in=*/ addr_info->GetBucketPosition (nKey, false , bucket));
953946 } else {
954- int bucket{addr_info->GetNewBucket (nKey, m_asmap )};
947+ int bucket{addr_info->GetNewBucket (nKey, m_netgroupman )};
955948 return AddressPosition (/* tried_in=*/ false ,
956949 /* multiplicity_in=*/ addr_info->nRefCount ,
957950 /* bucket_in=*/ bucket,
@@ -1026,7 +1019,7 @@ int AddrManImpl::CheckAddrman() const
10261019 if (!setTried.count (vvTried[n][i]))
10271020 return -11 ;
10281021 const auto it{mapInfo.find (vvTried[n][i])};
1029- if (it == mapInfo.end () || it->second .GetTriedBucket (nKey, m_asmap ) != n) {
1022+ if (it == mapInfo.end () || it->second .GetTriedBucket (nKey, m_netgroupman ) != n) {
10301023 return -17 ;
10311024 }
10321025 if (it->second .GetBucketPosition (nKey, false , n) != i) {
@@ -1154,13 +1147,8 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& add
11541147 return entry;
11551148}
11561149
1157- const std::vector<bool >& AddrManImpl::GetAsmap () const
1158- {
1159- return m_asmap;
1160- }
1161-
1162- AddrMan::AddrMan (std::vector<bool > asmap, bool deterministic, int32_t consistency_check_ratio)
1163- : m_impl(std::make_unique<AddrManImpl>(std::move(asmap), deterministic, consistency_check_ratio)) {}
1150+ AddrMan::AddrMan (const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
1151+ : m_impl(std::make_unique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}
11641152
11651153AddrMan::~AddrMan () = default ;
11661154
@@ -1235,11 +1223,6 @@ void AddrMan::SetServices(const CService& addr, ServiceFlags nServices)
12351223 m_impl->SetServices (addr, nServices);
12361224}
12371225
1238- const std::vector<bool >& AddrMan::GetAsmap () const
1239- {
1240- return m_impl->GetAsmap ();
1241- }
1242-
12431226std::optional<AddressPosition> AddrMan::FindAddressEntry (const CAddress& addr)
12441227{
12451228 return m_impl->FindAddressEntry (addr);
0 commit comments