4
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
6
6
#include < addrman.h>
7
+ #include < addrman_impl.h>
7
8
8
9
#include < clientversion.h>
9
10
#include < hash.h>
@@ -101,7 +102,7 @@ double CAddrInfo::GetChance(int64_t nNow) const
101
102
return fChance ;
102
103
}
103
104
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)
105
106
: insecure_rand{deterministic}
106
107
, nKey{deterministic ? uint256{1 } : insecure_rand.rand256 ()}
107
108
, m_consistency_check_ratio{consistency_check_ratio}
@@ -119,13 +120,13 @@ CAddrMan::CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consiste
119
120
}
120
121
}
121
122
122
- CAddrMan ::~CAddrMan ()
123
+ AddrManImpl ::~AddrManImpl ()
123
124
{
124
125
nKey.SetNull ();
125
126
}
126
127
127
128
template <typename Stream>
128
- void CAddrMan ::Serialize (Stream& s_) const
129
+ void AddrManImpl ::Serialize (Stream& s_) const
129
130
{
130
131
LOCK (cs);
131
132
@@ -228,7 +229,7 @@ void CAddrMan::Serialize(Stream& s_) const
228
229
}
229
230
230
231
template <typename Stream>
231
- void CAddrMan ::Unserialize (Stream& s_)
232
+ void AddrManImpl ::Unserialize (Stream& s_)
232
233
{
233
234
LOCK (cs);
234
235
@@ -399,16 +400,7 @@ void CAddrMan::Unserialize(Stream& s_)
399
400
}
400
401
}
401
402
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)
412
404
{
413
405
AssertLockHeld (cs);
414
406
@@ -423,7 +415,7 @@ CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
423
415
return nullptr ;
424
416
}
425
417
426
- CAddrInfo* CAddrMan ::Create (const CAddress& addr, const CNetAddr& addrSource, int * pnId)
418
+ CAddrInfo* AddrManImpl ::Create (const CAddress& addr, const CNetAddr& addrSource, int * pnId)
427
419
{
428
420
AssertLockHeld (cs);
429
421
@@ -437,7 +429,7 @@ CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, in
437
429
return &mapInfo[nId];
438
430
}
439
431
440
- void CAddrMan ::SwapRandom (unsigned int nRndPos1, unsigned int nRndPos2) const
432
+ void AddrManImpl ::SwapRandom (unsigned int nRndPos1, unsigned int nRndPos2) const
441
433
{
442
434
AssertLockHeld (cs);
443
435
@@ -461,7 +453,7 @@ void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
461
453
vRandom[nRndPos2] = nId1;
462
454
}
463
455
464
- void CAddrMan ::Delete (int nId)
456
+ void AddrManImpl ::Delete (int nId)
465
457
{
466
458
AssertLockHeld (cs);
467
459
@@ -477,7 +469,7 @@ void CAddrMan::Delete(int nId)
477
469
nNew--;
478
470
}
479
471
480
- void CAddrMan ::ClearNew (int nUBucket, int nUBucketPos)
472
+ void AddrManImpl ::ClearNew (int nUBucket, int nUBucketPos)
481
473
{
482
474
AssertLockHeld (cs);
483
475
@@ -494,7 +486,7 @@ void CAddrMan::ClearNew(int nUBucket, int nUBucketPos)
494
486
}
495
487
}
496
488
497
- void CAddrMan ::MakeTried (CAddrInfo& info, int nId)
489
+ void AddrManImpl ::MakeTried (CAddrInfo& info, int nId)
498
490
{
499
491
AssertLockHeld (cs);
500
492
@@ -547,7 +539,7 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
547
539
info.fInTried = true ;
548
540
}
549
541
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)
551
543
{
552
544
AssertLockHeld (cs);
553
545
@@ -603,7 +595,7 @@ void CAddrMan::Good_(const CService& addr, bool test_before_evict, int64_t nTime
603
595
}
604
596
}
605
597
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)
607
599
{
608
600
AssertLockHeld (cs);
609
601
@@ -678,7 +670,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
678
670
return fNew ;
679
671
}
680
672
681
- void CAddrMan ::Attempt_ (const CService& addr, bool fCountFailure , int64_t nTime)
673
+ void AddrManImpl ::Attempt_ (const CService& addr, bool fCountFailure , int64_t nTime)
682
674
{
683
675
AssertLockHeld (cs);
684
676
@@ -702,7 +694,7 @@ void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
702
694
}
703
695
}
704
696
705
- CAddrInfo CAddrMan ::Select_ (bool newOnly) const
697
+ CAddrInfo AddrManImpl ::Select_ (bool newOnly) const
706
698
{
707
699
AssertLockHeld (cs);
708
700
@@ -753,8 +745,7 @@ CAddrInfo CAddrMan::Select_(bool newOnly) const
753
745
}
754
746
}
755
747
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
758
749
{
759
750
AssertLockHeld (cs);
760
751
@@ -789,7 +780,7 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size
789
780
}
790
781
}
791
782
792
- void CAddrMan ::Connected_ (const CService& addr, int64_t nTime)
783
+ void AddrManImpl ::Connected_ (const CService& addr, int64_t nTime)
793
784
{
794
785
AssertLockHeld (cs);
795
786
@@ -811,7 +802,7 @@ void CAddrMan::Connected_(const CService& addr, int64_t nTime)
811
802
info.nTime = nTime;
812
803
}
813
804
814
- void CAddrMan ::SetServices_ (const CService& addr, ServiceFlags nServices)
805
+ void AddrManImpl ::SetServices_ (const CService& addr, ServiceFlags nServices)
815
806
{
816
807
AssertLockHeld (cs);
817
808
@@ -831,7 +822,7 @@ void CAddrMan::SetServices_(const CService& addr, ServiceFlags nServices)
831
822
info.nServices = nServices;
832
823
}
833
824
834
- void CAddrMan ::ResolveCollisions_ ()
825
+ void AddrManImpl ::ResolveCollisions_ ()
835
826
{
836
827
AssertLockHeld (cs);
837
828
@@ -892,7 +883,7 @@ void CAddrMan::ResolveCollisions_()
892
883
}
893
884
}
894
885
895
- CAddrInfo CAddrMan ::SelectTriedCollision_ ()
886
+ CAddrInfo AddrManImpl ::SelectTriedCollision_ ()
896
887
{
897
888
AssertLockHeld (cs);
898
889
@@ -921,7 +912,7 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
921
912
return mapInfo[id_old];
922
913
}
923
914
924
- void CAddrMan ::Check () const
915
+ void AddrManImpl ::Check () const
925
916
{
926
917
AssertLockHeld (cs);
927
918
@@ -936,7 +927,7 @@ void CAddrMan::Check() const
936
927
}
937
928
}
938
929
939
- int CAddrMan ::ForceCheckAddrman () const
930
+ int AddrManImpl ::ForceCheckAddrman () const
940
931
{
941
932
AssertLockHeld (cs);
942
933
@@ -1024,13 +1015,13 @@ int CAddrMan::ForceCheckAddrman() const
1024
1015
return 0 ;
1025
1016
}
1026
1017
1027
- size_t CAddrMan ::size () const
1018
+ size_t AddrManImpl ::size () const
1028
1019
{
1029
1020
LOCK (cs); // TODO: Cache this in an atomic to avoid this overhead
1030
1021
return vRandom.size ();
1031
1022
}
1032
1023
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)
1034
1025
{
1035
1026
LOCK (cs);
1036
1027
int nAdd = 0 ;
@@ -1044,32 +1035,31 @@ bool CAddrMan::Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, i
1044
1035
return nAdd > 0 ;
1045
1036
}
1046
1037
1047
- void CAddrMan ::Good (const CService &addr, int64_t nTime)
1038
+ void AddrManImpl ::Good (const CService &addr, int64_t nTime)
1048
1039
{
1049
1040
LOCK (cs);
1050
1041
Check ();
1051
1042
Good_ (addr, /* test_before_evict */ true , nTime);
1052
1043
Check ();
1053
1044
}
1054
1045
1055
- void CAddrMan ::Attempt (const CService &addr, bool fCountFailure , int64_t nTime)
1046
+ void AddrManImpl ::Attempt (const CService &addr, bool fCountFailure , int64_t nTime)
1056
1047
{
1057
1048
LOCK (cs);
1058
1049
Check ();
1059
1050
Attempt_ (addr, fCountFailure , nTime);
1060
1051
Check ();
1061
1052
}
1062
1053
1063
-
1064
- void CAddrMan::ResolveCollisions ()
1054
+ void AddrManImpl::ResolveCollisions ()
1065
1055
{
1066
1056
LOCK (cs);
1067
1057
Check ();
1068
1058
ResolveCollisions_ ();
1069
1059
Check ();
1070
1060
}
1071
1061
1072
- CAddrInfo CAddrMan ::SelectTriedCollision ()
1062
+ CAddrInfo AddrManImpl ::SelectTriedCollision ()
1073
1063
{
1074
1064
LOCK (cs);
1075
1065
Check ();
@@ -1078,7 +1068,7 @@ CAddrInfo CAddrMan::SelectTriedCollision()
1078
1068
return ret;
1079
1069
}
1080
1070
1081
- CAddrInfo CAddrMan ::Select (bool newOnly) const
1071
+ CAddrInfo AddrManImpl ::Select (bool newOnly) const
1082
1072
{
1083
1073
LOCK (cs);
1084
1074
Check ();
@@ -1087,7 +1077,7 @@ CAddrInfo CAddrMan::Select(bool newOnly) const
1087
1077
return addrRet;
1088
1078
}
1089
1079
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
1091
1081
{
1092
1082
LOCK (cs);
1093
1083
Check ();
@@ -1097,23 +1087,104 @@ std::vector<CAddress> CAddrMan::GetAddr(size_t max_addresses, size_t max_pct, st
1097
1087
return vAddr;
1098
1088
}
1099
1089
1100
- void CAddrMan ::Connected (const CService &addr, int64_t nTime)
1090
+ void AddrManImpl ::Connected (const CService &addr, int64_t nTime)
1101
1091
{
1102
1092
LOCK (cs);
1103
1093
Check ();
1104
1094
Connected_ (addr, nTime);
1105
1095
Check ();
1106
1096
}
1107
1097
1108
- void CAddrMan ::SetServices (const CService &addr, ServiceFlags nServices)
1098
+ void AddrManImpl ::SetServices (const CService &addr, ServiceFlags nServices)
1109
1099
{
1110
1100
LOCK (cs);
1111
1101
Check ();
1112
1102
SetServices_ (addr, nServices);
1113
1103
Check ();
1114
1104
}
1115
1105
1116
- const std::vector<bool >& CAddrMan ::GetAsmap () const
1106
+ const std::vector<bool >& AddrManImpl ::GetAsmap () const
1117
1107
{
1118
1108
return m_asmap;
1119
1109
}
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