Skip to content

Commit 8af5b54

Browse files
committed
[addrman] Introduce CAddrMan::Impl to encapsulate addrman implementation.
Introduce the pimpl pattern for CAddrMan to separate the implementation details from the externally used object representation. This reduces compile-time dependencies and conceptually clarifies AddrMan's interface from the implementation specifics. Since the unit & fuzz tests currently rely on accessing CAddrMan internals, this commit introduces addrman_impl.h, which is exclusively imported by addrman.cpp and test files. Review hint: git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent f2e5f38 commit 8af5b54

File tree

6 files changed

+371
-245
lines changed

6 files changed

+371
-245
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ endif
117117
BITCOIN_CORE_H = \
118118
addrdb.h \
119119
addrman.h \
120+
addrman_impl.h \
120121
attributes.h \
121122
banman.h \
122123
base58.h \

src/addrman.cpp

Lines changed: 114 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <addrman.h>
7+
#include <addrman_impl.h>
78

89
#include <clientversion.h>
910
#include <hash.h>
@@ -101,7 +102,7 @@ double CAddrInfo::GetChance(int64_t nNow) const
101102
return fChance;
102103
}
103104

104-
CAddrMan::CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio)
105+
AddrManImpl::AddrManImpl(std::vector<bool>&& asmap, bool deterministic, int32_t consistency_check_ratio)
105106
: insecure_rand{deterministic}
106107
, nKey{deterministic ? uint256{1} : insecure_rand.rand256()}
107108
, m_consistency_check_ratio{consistency_check_ratio}
@@ -119,13 +120,13 @@ CAddrMan::CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consiste
119120
}
120121
}
121122

122-
CAddrMan::~CAddrMan()
123+
AddrManImpl::~AddrManImpl()
123124
{
124125
nKey.SetNull();
125126
}
126127

127128
template <typename Stream>
128-
void CAddrMan::Serialize(Stream& s_) const
129+
void AddrManImpl::Serialize(Stream& s_) const
129130
{
130131
LOCK(cs);
131132

@@ -228,7 +229,7 @@ void CAddrMan::Serialize(Stream& s_) const
228229
}
229230

230231
template <typename Stream>
231-
void CAddrMan::Unserialize(Stream& s_)
232+
void AddrManImpl::Unserialize(Stream& s_)
232233
{
233234
LOCK(cs);
234235

@@ -399,16 +400,7 @@ void CAddrMan::Unserialize(Stream& s_)
399400
}
400401
}
401402

402-
// explicit instantiation
403-
template void CAddrMan::Serialize(CHashWriter& s) const;
404-
template void CAddrMan::Serialize(CAutoFile& s) const;
405-
template void CAddrMan::Serialize(CDataStream& s) const;
406-
template void CAddrMan::Unserialize(CAutoFile& s);
407-
template void CAddrMan::Unserialize(CHashVerifier<CAutoFile>& s);
408-
template void CAddrMan::Unserialize(CDataStream& s);
409-
template void CAddrMan::Unserialize(CHashVerifier<CDataStream>& s);
410-
411-
CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
403+
CAddrInfo* AddrManImpl::Find(const CNetAddr& addr, int* pnId)
412404
{
413405
AssertLockHeld(cs);
414406

@@ -423,7 +415,7 @@ CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
423415
return nullptr;
424416
}
425417

426-
CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
418+
CAddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
427419
{
428420
AssertLockHeld(cs);
429421

@@ -437,7 +429,7 @@ CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, in
437429
return &mapInfo[nId];
438430
}
439431

440-
void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
432+
void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
441433
{
442434
AssertLockHeld(cs);
443435

@@ -461,7 +453,7 @@ void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
461453
vRandom[nRndPos2] = nId1;
462454
}
463455

464-
void CAddrMan::Delete(int nId)
456+
void AddrManImpl::Delete(int nId)
465457
{
466458
AssertLockHeld(cs);
467459

@@ -477,7 +469,7 @@ void CAddrMan::Delete(int nId)
477469
nNew--;
478470
}
479471

480-
void CAddrMan::ClearNew(int nUBucket, int nUBucketPos)
472+
void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
481473
{
482474
AssertLockHeld(cs);
483475

@@ -494,7 +486,7 @@ void CAddrMan::ClearNew(int nUBucket, int nUBucketPos)
494486
}
495487
}
496488

497-
void CAddrMan::MakeTried(CAddrInfo& info, int nId)
489+
void AddrManImpl::MakeTried(CAddrInfo& info, int nId)
498490
{
499491
AssertLockHeld(cs);
500492

@@ -547,7 +539,7 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
547539
info.fInTried = true;
548540
}
549541

550-
void CAddrMan::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
542+
void AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
551543
{
552544
AssertLockHeld(cs);
553545

@@ -603,7 +595,7 @@ void CAddrMan::Good_(const CService& addr, bool test_before_evict, int64_t nTime
603595
}
604596
}
605597

606-
bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty)
598+
bool AddrManImpl::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty)
607599
{
608600
AssertLockHeld(cs);
609601

@@ -678,7 +670,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
678670
return fNew;
679671
}
680672

681-
void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
673+
void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
682674
{
683675
AssertLockHeld(cs);
684676

@@ -702,7 +694,7 @@ void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
702694
}
703695
}
704696

705-
CAddrInfo CAddrMan::Select_(bool newOnly) const
697+
CAddrInfo AddrManImpl::Select_(bool newOnly) const
706698
{
707699
AssertLockHeld(cs);
708700

@@ -753,8 +745,7 @@ CAddrInfo CAddrMan::Select_(bool newOnly) const
753745
}
754746
}
755747

756-
757-
void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const
748+
void AddrManImpl::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const
758749
{
759750
AssertLockHeld(cs);
760751

@@ -789,7 +780,7 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size
789780
}
790781
}
791782

792-
void CAddrMan::Connected_(const CService& addr, int64_t nTime)
783+
void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
793784
{
794785
AssertLockHeld(cs);
795786

@@ -811,7 +802,7 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
811802
info.nTime = nTime;
812803
}
813804

814-
void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
805+
void AddrManImpl::SetServices_(const CService& addr, ServiceFlags nServices)
815806
{
816807
AssertLockHeld(cs);
817808

@@ -831,7 +822,7 @@ void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
831822
info.nServices = nServices;
832823
}
833824

834-
void CAddrMan::ResolveCollisions_()
825+
void AddrManImpl::ResolveCollisions_()
835826
{
836827
AssertLockHeld(cs);
837828

@@ -892,7 +883,7 @@ void CAddrMan::ResolveCollisions_()
892883
}
893884
}
894885

895-
CAddrInfo CAddrMan::SelectTriedCollision_()
886+
CAddrInfo AddrManImpl::SelectTriedCollision_()
896887
{
897888
AssertLockHeld(cs);
898889

@@ -921,7 +912,7 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
921912
return mapInfo[id_old];
922913
}
923914

924-
void CAddrMan::Check() const
915+
void AddrManImpl::Check() const
925916
{
926917
AssertLockHeld(cs);
927918

@@ -936,7 +927,7 @@ void CAddrMan::Check() const
936927
}
937928
}
938929

939-
int CAddrMan::ForceCheckAddrman() const
930+
int AddrManImpl::ForceCheckAddrman() const
940931
{
941932
AssertLockHeld(cs);
942933

@@ -1024,13 +1015,13 @@ int CAddrMan::ForceCheckAddrman() const
10241015
return 0;
10251016
}
10261017

1027-
size_t CAddrMan::size() const
1018+
size_t AddrManImpl::size() const
10281019
{
10291020
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
10301021
return vRandom.size();
10311022
}
10321023

1033-
bool CAddrMan::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
1024+
bool AddrManImpl::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
10341025
{
10351026
LOCK(cs);
10361027
int nAdd = 0;
@@ -1044,32 +1035,31 @@ bool CAddrMan::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, i
10441035
return nAdd > 0;
10451036
}
10461037

1047-
void CAddrMan::Good(const CService &addr, int64_t nTime)
1038+
void AddrManImpl::Good(const CService &addr, int64_t nTime)
10481039
{
10491040
LOCK(cs);
10501041
Check();
10511042
Good_(addr, /* test_before_evict */ true, nTime);
10521043
Check();
10531044
}
10541045

1055-
void CAddrMan::Attempt(const CService &addr, bool fCountFailure, int64_t nTime)
1046+
void AddrManImpl::Attempt(const CService &addr, bool fCountFailure, int64_t nTime)
10561047
{
10571048
LOCK(cs);
10581049
Check();
10591050
Attempt_(addr, fCountFailure, nTime);
10601051
Check();
10611052
}
10621053

1063-
1064-
void CAddrMan::ResolveCollisions()
1054+
void AddrManImpl::ResolveCollisions()
10651055
{
10661056
LOCK(cs);
10671057
Check();
10681058
ResolveCollisions_();
10691059
Check();
10701060
}
10711061

1072-
CAddrInfo CAddrMan::SelectTriedCollision()
1062+
CAddrInfo AddrManImpl::SelectTriedCollision()
10731063
{
10741064
LOCK(cs);
10751065
Check();
@@ -1078,7 +1068,7 @@ CAddrInfo CAddrMan::SelectTriedCollision()
10781068
return ret;
10791069
}
10801070

1081-
CAddrInfo CAddrMan::Select(bool newOnly) const
1071+
CAddrInfo AddrManImpl::Select(bool newOnly) const
10821072
{
10831073
LOCK(cs);
10841074
Check();
@@ -1087,7 +1077,7 @@ CAddrInfo CAddrMan::Select(bool newOnly) const
10871077
return addrRet;
10881078
}
10891079

1090-
std::vector<CAddress> CAddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1080+
std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
10911081
{
10921082
LOCK(cs);
10931083
Check();
@@ -1097,23 +1087,104 @@ std::vector<CAddress> CAddrMan::GetAddr(size_t max_addresses, size_t max_pct, st
10971087
return vAddr;
10981088
}
10991089

1100-
void CAddrMan::Connected(const CService &addr, int64_t nTime)
1090+
void AddrManImpl::Connected(const CService &addr, int64_t nTime)
11011091
{
11021092
LOCK(cs);
11031093
Check();
11041094
Connected_(addr, nTime);
11051095
Check();
11061096
}
11071097

1108-
void CAddrMan::SetServices(const CService &addr, ServiceFlags nServices)
1098+
void AddrManImpl::SetServices(const CService &addr, ServiceFlags nServices)
11091099
{
11101100
LOCK(cs);
11111101
Check();
11121102
SetServices_(addr, nServices);
11131103
Check();
11141104
}
11151105

1116-
const std::vector<bool>& CAddrMan::GetAsmap() const
1106+
const std::vector<bool>& AddrManImpl::GetAsmap() const
11171107
{
11181108
return m_asmap;
11191109
}
1110+
1111+
CAddrMan::CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio)
1112+
: m_impl(std::make_unique<AddrManImpl>(std::move(asmap), deterministic, consistency_check_ratio)) {}
1113+
1114+
CAddrMan::~CAddrMan() = default;
1115+
1116+
template <typename Stream>
1117+
void CAddrMan::Serialize(Stream& s_) const
1118+
{
1119+
m_impl->Serialize<Stream>(s_);
1120+
}
1121+
1122+
template <typename Stream>
1123+
void CAddrMan::Unserialize(Stream& s_)
1124+
{
1125+
m_impl->Unserialize<Stream>(s_);
1126+
}
1127+
1128+
// explicit instantiation
1129+
template void CAddrMan::Serialize(CHashWriter& s) const;
1130+
template void CAddrMan::Serialize(CAutoFile& s) const;
1131+
template void CAddrMan::Serialize(CDataStream& s) const;
1132+
template void CAddrMan::Unserialize(CAutoFile& s);
1133+
template void CAddrMan::Unserialize(CHashVerifier<CAutoFile>& s);
1134+
template void CAddrMan::Unserialize(CDataStream& s);
1135+
template void CAddrMan::Unserialize(CHashVerifier<CDataStream>& s);
1136+
1137+
size_t CAddrMan::size() const
1138+
{
1139+
return m_impl->size();
1140+
}
1141+
1142+
bool CAddrMan::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
1143+
{
1144+
return m_impl->Add(vAddr, source, nTimePenalty);
1145+
}
1146+
1147+
void CAddrMan::Good(const CService &addr, int64_t nTime)
1148+
{
1149+
m_impl->Good(addr, nTime);
1150+
}
1151+
1152+
void CAddrMan::Attempt(const CService &addr, bool fCountFailure, int64_t nTime)
1153+
{
1154+
m_impl->Attempt(addr, fCountFailure, nTime);
1155+
}
1156+
1157+
void CAddrMan::ResolveCollisions()
1158+
{
1159+
m_impl->ResolveCollisions();
1160+
}
1161+
1162+
CAddrInfo CAddrMan::SelectTriedCollision()
1163+
{
1164+
return m_impl->SelectTriedCollision();
1165+
}
1166+
1167+
CAddrInfo CAddrMan::Select(bool newOnly) const
1168+
{
1169+
return m_impl->Select(newOnly);
1170+
}
1171+
1172+
std::vector<CAddress> CAddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1173+
{
1174+
return m_impl->GetAddr(max_addresses, max_pct, network);
1175+
}
1176+
1177+
void CAddrMan::Connected(const CService &addr, int64_t nTime)
1178+
{
1179+
m_impl->Connected(addr, nTime);
1180+
}
1181+
1182+
void CAddrMan::SetServices(const CService &addr, ServiceFlags nServices)
1183+
{
1184+
m_impl->SetServices(addr, nServices);
1185+
}
1186+
1187+
const std::vector<bool>& CAddrMan::GetAsmap() const
1188+
{
1189+
return m_impl->GetAsmap();
1190+
}

0 commit comments

Comments
 (0)