Skip to content

Commit 6b22681

Browse files
committed
[netgroupman] Add GetMappedAS() and GetGroup()
These currently call through to the CNetAddr methods. The logic will be moved in a future commit.
1 parent 1943156 commit 6b22681

File tree

8 files changed

+91
-74
lines changed

8 files changed

+91
-74
lines changed

src/addrman.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */
4444
static 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
}
@@ -298,7 +298,7 @@ void AddrManImpl::Unserialize(Stream& s_)
298298
for (int n = 0; n < nTried; n++) {
299299
AddrInfo info;
300300
s >> info;
301-
int nKBucket = info.GetTriedBucket(nKey, m_netgroupman.GetAsmap());
301+
int nKBucket = info.GetTriedBucket(nKey, m_netgroupman);
302302
int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
303303
if (info.IsValid()
304304
&& vvTried[nKBucket][nKBucketPos] == -1) {
@@ -371,7 +371,7 @@ void AddrManImpl::Unserialize(Stream& s_)
371371
} else {
372372
// In case the new table data cannot be used (bucket count wrong or new asmap),
373373
// try to give them a reference based on their primary source address.
374-
bucket = info.GetNewBucket(nKey, m_netgroupman.GetAsmap());
374+
bucket = info.GetNewBucket(nKey, m_netgroupman);
375375
bucket_position = info.GetBucketPosition(nKey, true, bucket);
376376
if (vvNew[bucket][bucket_position] == -1) {
377377
vvNew[bucket][bucket_position] = entry_index;
@@ -495,7 +495,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
495495
AssertLockHeld(cs);
496496

497497
// remove the entry from all new buckets
498-
const int start_bucket{info.GetNewBucket(nKey, m_netgroupman.GetAsmap())};
498+
const int start_bucket{info.GetNewBucket(nKey, m_netgroupman)};
499499
for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
500500
const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
501501
const int pos{info.GetBucketPosition(nKey, true, bucket)};
@@ -510,7 +510,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
510510
assert(info.nRefCount == 0);
511511

512512
// which tried bucket to move the entry to
513-
int nKBucket = info.GetTriedBucket(nKey, m_netgroupman.GetAsmap());
513+
int nKBucket = info.GetTriedBucket(nKey, m_netgroupman);
514514
int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
515515

516516
// first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
@@ -526,7 +526,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
526526
nTried--;
527527

528528
// find which new bucket it belongs to
529-
int nUBucket = infoOld.GetNewBucket(nKey, m_netgroupman.GetAsmap());
529+
int nUBucket = infoOld.GetNewBucket(nKey, m_netgroupman);
530530
int nUBucketPos = infoOld.GetBucketPosition(nKey, true, nUBucket);
531531
ClearNew(nUBucket, nUBucketPos);
532532
assert(vvNew[nUBucket][nUBucketPos] == -1);
@@ -594,7 +594,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_
594594
nNew++;
595595
}
596596

597-
int nUBucket = pinfo->GetNewBucket(nKey, source, m_netgroupman.GetAsmap());
597+
int nUBucket = pinfo->GetNewBucket(nKey, source, m_netgroupman);
598598
int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
599599
bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
600600
if (vvNew[nUBucket][nUBucketPos] != nId) {
@@ -650,7 +650,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nT
650650

651651

652652
// which tried bucket to move the entry to
653-
int tried_bucket = info.GetTriedBucket(nKey, m_netgroupman.GetAsmap());
653+
int tried_bucket = info.GetTriedBucket(nKey, m_netgroupman);
654654
int tried_bucket_pos = info.GetBucketPosition(nKey, false, tried_bucket);
655655

656656
// Will moving this address into tried evict another entry?
@@ -863,7 +863,7 @@ void AddrManImpl::ResolveCollisions_()
863863
AddrInfo& info_new = mapInfo[id_new];
864864

865865
// Which tried bucket to move the entry to.
866-
int tried_bucket = info_new.GetTriedBucket(nKey, m_netgroupman.GetAsmap());
866+
int tried_bucket = info_new.GetTriedBucket(nKey, m_netgroupman);
867867
int tried_bucket_pos = info_new.GetBucketPosition(nKey, false, tried_bucket);
868868
if (!info_new.IsValid()) { // id_new may no longer map to a valid address
869869
erase_collision = true;
@@ -929,7 +929,7 @@ std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision_()
929929
const AddrInfo& newInfo = mapInfo[id_new];
930930

931931
// which tried bucket to move the entry to
932-
int tried_bucket = newInfo.GetTriedBucket(nKey, m_netgroupman.GetAsmap());
932+
int tried_bucket = newInfo.GetTriedBucket(nKey, m_netgroupman);
933933
int tried_bucket_pos = newInfo.GetBucketPosition(nKey, false, tried_bucket);
934934

935935
const AddrInfo& info_old = mapInfo[vvTried[tried_bucket][tried_bucket_pos]];
@@ -945,13 +945,13 @@ std::optional<AddressPosition> AddrManImpl::FindAddressEntry_(const CAddress& ad
945945
if (!addr_info) return std::nullopt;
946946

947947
if(addr_info->fInTried) {
948-
int bucket{addr_info->GetTriedBucket(nKey, m_netgroupman.GetAsmap())};
948+
int bucket{addr_info->GetTriedBucket(nKey, m_netgroupman)};
949949
return AddressPosition(/*tried_in=*/true,
950950
/*multiplicity_in=*/1,
951951
/*bucket_in=*/bucket,
952952
/*position_in=*/addr_info->GetBucketPosition(nKey, false, bucket));
953953
} else {
954-
int bucket{addr_info->GetNewBucket(nKey, m_netgroupman.GetAsmap())};
954+
int bucket{addr_info->GetNewBucket(nKey, m_netgroupman)};
955955
return AddressPosition(/*tried_in=*/false,
956956
/*multiplicity_in=*/addr_info->nRefCount,
957957
/*bucket_in=*/bucket,
@@ -1026,7 +1026,7 @@ int AddrManImpl::CheckAddrman() const
10261026
if (!setTried.count(vvTried[n][i]))
10271027
return -11;
10281028
const auto it{mapInfo.find(vvTried[n][i])};
1029-
if (it == mapInfo.end() || it->second.GetTriedBucket(nKey, m_netgroupman.GetAsmap()) != n) {
1029+
if (it == mapInfo.end() || it->second.GetTriedBucket(nKey, m_netgroupman) != n) {
10301030
return -17;
10311031
}
10321032
if (it->second.GetBucketPosition(nKey, false, n) != i) {

src/addrman_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ class AddrInfo : public CAddress
7676
}
7777

7878
//! Calculate in which "tried" bucket this entry belongs
79-
int GetTriedBucket(const uint256 &nKey, const std::vector<bool> &asmap) const;
79+
int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
8080

8181
//! Calculate in which "new" bucket this entry belongs, given a certain source
82-
int GetNewBucket(const uint256 &nKey, const CNetAddr& src, const std::vector<bool> &asmap) const;
82+
int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
8383

8484
//! Calculate in which "new" bucket this entry belongs, using its default source
85-
int GetNewBucket(const uint256 &nKey, const std::vector<bool> &asmap) const
85+
int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
8686
{
87-
return GetNewBucket(nKey, source, asmap);
87+
return GetNewBucket(nKey, source, netgroupman);
8888
}
8989

9090
//! Calculate in which position of a bucket to store this entry.

src/net.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
19971997
case ConnectionType::BLOCK_RELAY:
19981998
case ConnectionType::ADDR_FETCH:
19991999
case ConnectionType::FEELER:
2000-
setConnected.insert(pnode->addr.GetGroup(m_netgroupman.GetAsmap()));
2000+
setConnected.insert(m_netgroupman.GetGroup(pnode->addr));
20012001
} // no default case, so the compiler can warn about missing cases
20022002
}
20032003
}
@@ -2071,7 +2071,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
20712071
m_anchors.pop_back();
20722072
if (!addr.IsValid() || IsLocal(addr) || !IsReachable(addr) ||
20732073
!HasAllDesirableServiceFlags(addr.nServices) ||
2074-
setConnected.count(addr.GetGroup(m_netgroupman.GetAsmap()))) continue;
2074+
setConnected.count(m_netgroupman.GetGroup(addr))) continue;
20752075
addrConnect = addr;
20762076
LogPrint(BCLog::NET, "Trying to make an anchor connection to %s\n", addrConnect.ToString());
20772077
break;
@@ -2112,7 +2112,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
21122112
}
21132113

21142114
// Require outbound connections, other than feelers, to be to distinct network groups
2115-
if (!fFeeler && setConnected.count(addr.GetGroup(m_netgroupman.GetAsmap()))) {
2115+
if (!fFeeler && setConnected.count(m_netgroupman.GetGroup(addr))) {
21162116
break;
21172117
}
21182118

@@ -2863,7 +2863,7 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats) const
28632863
for (CNode* pnode : m_nodes) {
28642864
vstats.emplace_back();
28652865
pnode->CopyStats(vstats.back());
2866-
vstats.back().m_mapped_as = pnode->addr.GetMappedAS(m_netgroupman.GetAsmap());
2866+
vstats.back().m_mapped_as = m_netgroupman.GetMappedAS(pnode->addr);
28672867
}
28682868
}
28692869

@@ -3096,9 +3096,9 @@ CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
30963096
return CSipHasher(nSeed0, nSeed1).Write(id);
30973097
}
30983098

3099-
uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad) const
3099+
uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& address) const
31003100
{
3101-
std::vector<unsigned char> vchNetGroup(ad.GetGroup(m_netgroupman.GetAsmap()));
3101+
std::vector<unsigned char> vchNetGroup(m_netgroupman.GetGroup(address));
31023102

31033103
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(vchNetGroup.data(), vchNetGroup.size()).Finalize();
31043104
}

src/netgroup.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <netgroup.h>
6+
7+
std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
8+
{
9+
return address.GetGroup(m_asmap);
10+
}
11+
12+
uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
13+
{
14+
return address.GetMappedAS(m_asmap);
15+
}

src/netgroup.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_NETGROUP_H
66
#define BITCOIN_NETGROUP_H
77

8+
#include <netaddress.h>
9+
810
#include <vector>
911

1012
/**
@@ -20,6 +22,10 @@ class NetGroupManager {
2022
* exists, since the data is const. */
2123
const std::vector<bool>& GetAsmap() const { return m_asmap; }
2224

25+
std::vector<unsigned char> GetGroup(const CNetAddr& address) const;
26+
27+
uint32_t GetMappedAS(const CNetAddr& address) const;
28+
2329
private:
2430
/** Compressed IP->ASN mapping, loaded from a file when a node starts.
2531
*

0 commit comments

Comments
 (0)