@@ -43,17 +43,17 @@ static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
43
43
/* * The maximum time we'll spend trying to resolve a tried table collision, in seconds */
44
44
static constexpr int64_t ADDRMAN_TEST_WINDOW{40 *60 }; // 40 minutes
45
45
46
- int AddrInfo::GetTriedBucket (const uint256& nKey, const std::vector< bool >& asmap ) const
46
+ int AddrInfo::GetTriedBucket (const uint256& nKey, const NetGroupManager& netgroupman ) const
47
47
{
48
48
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 ();
50
50
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
51
51
}
52
52
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
54
54
{
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 ();
57
57
uint64_t hash2 = (CHashWriter (SER_GETHASH, 0 ) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash ();
58
58
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
59
59
}
@@ -99,11 +99,11 @@ double AddrInfo::GetChance(int64_t nNow) const
99
99
return fChance ;
100
100
}
101
101
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)
103
103
: insecure_rand{deterministic}
104
104
, nKey{deterministic ? uint256{1 } : insecure_rand.rand256 ()}
105
105
, m_consistency_check_ratio{consistency_check_ratio}
106
- , m_asmap{ std::move (asmap) }
106
+ , m_netgroupman{netgroupman }
107
107
{
108
108
for (auto & bucket : vvNew) {
109
109
for (auto & entry : bucket) {
@@ -218,11 +218,7 @@ void AddrManImpl::Serialize(Stream& s_) const
218
218
}
219
219
// Store asmap checksum after bucket entries so that it
220
220
// 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 ();
226
222
}
227
223
228
224
template <typename Stream>
@@ -298,7 +294,7 @@ void AddrManImpl::Unserialize(Stream& s_)
298
294
for (int n = 0 ; n < nTried; n++) {
299
295
AddrInfo info;
300
296
s >> info;
301
- int nKBucket = info.GetTriedBucket (nKey, m_asmap );
297
+ int nKBucket = info.GetTriedBucket (nKey, m_netgroupman );
302
298
int nKBucketPos = info.GetBucketPosition (nKey, false , nKBucket);
303
299
if (info.IsValid ()
304
300
&& vvTried[nKBucket][nKBucketPos] == -1 ) {
@@ -335,10 +331,7 @@ void AddrManImpl::Unserialize(Stream& s_)
335
331
// If the bucket count and asmap checksum haven't changed, then attempt
336
332
// to restore the entries to the buckets/positions they were in before
337
333
// 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 ()};
342
335
uint256 serialized_asmap_checksum;
343
336
if (format >= Format::V2_ASMAP) {
344
337
s >> serialized_asmap_checksum;
@@ -371,7 +364,7 @@ void AddrManImpl::Unserialize(Stream& s_)
371
364
} else {
372
365
// In case the new table data cannot be used (bucket count wrong or new asmap),
373
366
// 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 );
375
368
bucket_position = info.GetBucketPosition (nKey, true , bucket);
376
369
if (vvNew[bucket][bucket_position] == -1 ) {
377
370
vvNew[bucket][bucket_position] = entry_index;
@@ -495,7 +488,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
495
488
AssertLockHeld (cs);
496
489
497
490
// 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 )};
499
492
for (int n = 0 ; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
500
493
const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
501
494
const int pos{info.GetBucketPosition (nKey, true , bucket)};
@@ -510,7 +503,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
510
503
assert (info.nRefCount == 0 );
511
504
512
505
// which tried bucket to move the entry to
513
- int nKBucket = info.GetTriedBucket (nKey, m_asmap );
506
+ int nKBucket = info.GetTriedBucket (nKey, m_netgroupman );
514
507
int nKBucketPos = info.GetBucketPosition (nKey, false , nKBucket);
515
508
516
509
// 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)
526
519
nTried--;
527
520
528
521
// find which new bucket it belongs to
529
- int nUBucket = infoOld.GetNewBucket (nKey, m_asmap );
522
+ int nUBucket = infoOld.GetNewBucket (nKey, m_netgroupman );
530
523
int nUBucketPos = infoOld.GetBucketPosition (nKey, true , nUBucket);
531
524
ClearNew (nUBucket, nUBucketPos);
532
525
assert (vvNew[nUBucket][nUBucketPos] == -1 );
@@ -594,7 +587,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
594
587
nNew++;
595
588
}
596
589
597
- int nUBucket = pinfo->GetNewBucket (nKey, source, m_asmap );
590
+ int nUBucket = pinfo->GetNewBucket (nKey, source, m_netgroupman );
598
591
int nUBucketPos = pinfo->GetBucketPosition (nKey, true , nUBucket);
599
592
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1 ;
600
593
if (vvNew[nUBucket][nUBucketPos] != nId) {
@@ -610,7 +603,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
610
603
pinfo->nRefCount ++;
611
604
vvNew[nUBucket][nUBucketPos] = nId;
612
605
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);
614
607
} else {
615
608
if (pinfo->nRefCount == 0 ) {
616
609
Delete (nId);
@@ -650,7 +643,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
650
643
651
644
652
645
// 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 );
654
647
int tried_bucket_pos = info.GetBucketPosition (nKey, false , tried_bucket);
655
648
656
649
// 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
669
662
// move nId to the tried tables
670
663
MakeTried (info, nId);
671
664
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);
673
666
return true ;
674
667
}
675
668
}
@@ -863,7 +856,7 @@ void AddrManImpl::ResolveCollisions_()
863
856
AddrInfo& info_new = mapInfo[id_new];
864
857
865
858
// 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 );
867
860
int tried_bucket_pos = info_new.GetBucketPosition (nKey, false , tried_bucket);
868
861
if (!info_new.IsValid ()) { // id_new may no longer map to a valid address
869
862
erase_collision = true ;
@@ -929,7 +922,7 @@ std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision_()
929
922
const AddrInfo& newInfo = mapInfo[id_new];
930
923
931
924
// 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 );
933
926
int tried_bucket_pos = newInfo.GetBucketPosition (nKey, false , tried_bucket);
934
927
935
928
const AddrInfo& info_old = mapInfo[vvTried[tried_bucket][tried_bucket_pos]];
@@ -945,13 +938,13 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry_(const CAddress& ad
945
938
if (!addr_info) return std::nullopt;
946
939
947
940
if (addr_info->fInTried ) {
948
- int bucket{addr_info->GetTriedBucket (nKey, m_asmap )};
941
+ int bucket{addr_info->GetTriedBucket (nKey, m_netgroupman )};
949
942
return AddressPosition (/* tried_in=*/ true ,
950
943
/* multiplicity_in=*/ 1 ,
951
944
/* bucket_in=*/ bucket,
952
945
/* position_in=*/ addr_info->GetBucketPosition (nKey, false , bucket));
953
946
} else {
954
- int bucket{addr_info->GetNewBucket (nKey, m_asmap )};
947
+ int bucket{addr_info->GetNewBucket (nKey, m_netgroupman )};
955
948
return AddressPosition (/* tried_in=*/ false ,
956
949
/* multiplicity_in=*/ addr_info->nRefCount ,
957
950
/* bucket_in=*/ bucket,
@@ -1026,7 +1019,7 @@ int AddrManImpl::CheckAddrman() const
1026
1019
if (!setTried.count (vvTried[n][i]))
1027
1020
return -11 ;
1028
1021
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) {
1030
1023
return -17 ;
1031
1024
}
1032
1025
if (it->second .GetBucketPosition (nKey, false , n) != i) {
@@ -1154,13 +1147,8 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& add
1154
1147
return entry;
1155
1148
}
1156
1149
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)) {}
1164
1152
1165
1153
AddrMan::~AddrMan () = default ;
1166
1154
@@ -1235,11 +1223,6 @@ void AddrMan::SetServices(const CService& addr, ServiceFlags nServices)
1235
1223
m_impl->SetServices (addr, nServices);
1236
1224
}
1237
1225
1238
- const std::vector<bool >& AddrMan::GetAsmap () const
1239
- {
1240
- return m_impl->GetAsmap ();
1241
- }
1242
-
1243
1226
std::optional<AddressPosition> AddrMan::FindAddressEntry (const CAddress& addr)
1244
1227
{
1245
1228
return m_impl->FindAddressEntry (addr);
0 commit comments