@@ -753,108 +753,6 @@ CAddrInfo CAddrMan::Select_(bool newOnly) const
753753 }
754754}
755755
756- void CAddrMan::Check () const
757- {
758- AssertLockHeld (cs);
759-
760- // Run consistency checks 1 in m_consistency_check_ratio times if enabled
761- if (m_consistency_check_ratio == 0 ) return ;
762- if (insecure_rand.randrange (m_consistency_check_ratio) >= 1 ) return ;
763-
764- const int err{ForceCheckAddrman ()};
765- if (err) {
766- LogPrintf (" ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n " , err);
767- assert (false );
768- }
769- }
770-
771- int CAddrMan::ForceCheckAddrman () const
772- {
773- AssertLockHeld (cs);
774-
775- LogPrint (BCLog::ADDRMAN, " Addrman checks started: new %i, tried %i, total %u\n " , nNew, nTried, vRandom.size ());
776-
777- std::unordered_set<int > setTried;
778- std::unordered_map<int , int > mapNew;
779-
780- if (vRandom.size () != (size_t )(nTried + nNew))
781- return -7 ;
782-
783- for (const auto & entry : mapInfo) {
784- int n = entry.first ;
785- const CAddrInfo& info = entry.second ;
786- if (info.fInTried ) {
787- if (!info.nLastSuccess )
788- return -1 ;
789- if (info.nRefCount )
790- return -2 ;
791- setTried.insert (n);
792- } else {
793- if (info.nRefCount < 0 || info.nRefCount > ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
794- return -3 ;
795- if (!info.nRefCount )
796- return -4 ;
797- mapNew[n] = info.nRefCount ;
798- }
799- const auto it{mapAddr.find (info)};
800- if (it == mapAddr.end () || it->second != n) {
801- return -5 ;
802- }
803- if (info.nRandomPos < 0 || (size_t )info.nRandomPos >= vRandom.size () || vRandom[info.nRandomPos ] != n)
804- return -14 ;
805- if (info.nLastTry < 0 )
806- return -6 ;
807- if (info.nLastSuccess < 0 )
808- return -8 ;
809- }
810-
811- if (setTried.size () != (size_t )nTried)
812- return -9 ;
813- if (mapNew.size () != (size_t )nNew)
814- return -10 ;
815-
816- for (int n = 0 ; n < ADDRMAN_TRIED_BUCKET_COUNT; n++) {
817- for (int i = 0 ; i < ADDRMAN_BUCKET_SIZE; i++) {
818- if (vvTried[n][i] != -1 ) {
819- if (!setTried.count (vvTried[n][i]))
820- return -11 ;
821- const auto it{mapInfo.find (vvTried[n][i])};
822- if (it == mapInfo.end () || it->second .GetTriedBucket (nKey, m_asmap) != n) {
823- return -17 ;
824- }
825- if (it->second .GetBucketPosition (nKey, false , n) != i) {
826- return -18 ;
827- }
828- setTried.erase (vvTried[n][i]);
829- }
830- }
831- }
832-
833- for (int n = 0 ; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
834- for (int i = 0 ; i < ADDRMAN_BUCKET_SIZE; i++) {
835- if (vvNew[n][i] != -1 ) {
836- if (!mapNew.count (vvNew[n][i]))
837- return -12 ;
838- const auto it{mapInfo.find (vvNew[n][i])};
839- if (it == mapInfo.end () || it->second .GetBucketPosition (nKey, true , n) != i) {
840- return -19 ;
841- }
842- if (--mapNew[vvNew[n][i]] == 0 )
843- mapNew.erase (vvNew[n][i]);
844- }
845- }
846- }
847-
848- if (setTried.size ())
849- return -13 ;
850- if (mapNew.size ())
851- return -15 ;
852- if (nKey.IsNull ())
853- return -16 ;
854-
855- LogPrint (BCLog::ADDRMAN, " Addrman checks completed successfully\n " );
856- return 0 ;
857- }
858756
859757void CAddrMan::GetAddr_ (std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) const
860758{
@@ -1023,6 +921,109 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
1023921 return mapInfo[id_old];
1024922}
1025923
924+ void CAddrMan::Check () const
925+ {
926+ AssertLockHeld (cs);
927+
928+ // Run consistency checks 1 in m_consistency_check_ratio times if enabled
929+ if (m_consistency_check_ratio == 0 ) return ;
930+ if (insecure_rand.randrange (m_consistency_check_ratio) >= 1 ) return ;
931+
932+ const int err{ForceCheckAddrman ()};
933+ if (err) {
934+ LogPrintf (" ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n " , err);
935+ assert (false );
936+ }
937+ }
938+
939+ int CAddrMan::ForceCheckAddrman () const
940+ {
941+ AssertLockHeld (cs);
942+
943+ LogPrint (BCLog::ADDRMAN, " Addrman checks started: new %i, tried %i, total %u\n " , nNew, nTried, vRandom.size ());
944+
945+ std::unordered_set<int > setTried;
946+ std::unordered_map<int , int > mapNew;
947+
948+ if (vRandom.size () != (size_t )(nTried + nNew))
949+ return -7 ;
950+
951+ for (const auto & entry : mapInfo) {
952+ int n = entry.first ;
953+ const CAddrInfo& info = entry.second ;
954+ if (info.fInTried ) {
955+ if (!info.nLastSuccess )
956+ return -1 ;
957+ if (info.nRefCount )
958+ return -2 ;
959+ setTried.insert (n);
960+ } else {
961+ if (info.nRefCount < 0 || info.nRefCount > ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
962+ return -3 ;
963+ if (!info.nRefCount )
964+ return -4 ;
965+ mapNew[n] = info.nRefCount ;
966+ }
967+ const auto it{mapAddr.find (info)};
968+ if (it == mapAddr.end () || it->second != n) {
969+ return -5 ;
970+ }
971+ if (info.nRandomPos < 0 || (size_t )info.nRandomPos >= vRandom.size () || vRandom[info.nRandomPos ] != n)
972+ return -14 ;
973+ if (info.nLastTry < 0 )
974+ return -6 ;
975+ if (info.nLastSuccess < 0 )
976+ return -8 ;
977+ }
978+
979+ if (setTried.size () != (size_t )nTried)
980+ return -9 ;
981+ if (mapNew.size () != (size_t )nNew)
982+ return -10 ;
983+
984+ for (int n = 0 ; n < ADDRMAN_TRIED_BUCKET_COUNT; n++) {
985+ for (int i = 0 ; i < ADDRMAN_BUCKET_SIZE; i++) {
986+ if (vvTried[n][i] != -1 ) {
987+ if (!setTried.count (vvTried[n][i]))
988+ return -11 ;
989+ const auto it{mapInfo.find (vvTried[n][i])};
990+ if (it == mapInfo.end () || it->second .GetTriedBucket (nKey, m_asmap) != n) {
991+ return -17 ;
992+ }
993+ if (it->second .GetBucketPosition (nKey, false , n) != i) {
994+ return -18 ;
995+ }
996+ setTried.erase (vvTried[n][i]);
997+ }
998+ }
999+ }
1000+
1001+ for (int n = 0 ; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
1002+ for (int i = 0 ; i < ADDRMAN_BUCKET_SIZE; i++) {
1003+ if (vvNew[n][i] != -1 ) {
1004+ if (!mapNew.count (vvNew[n][i]))
1005+ return -12 ;
1006+ const auto it{mapInfo.find (vvNew[n][i])};
1007+ if (it == mapInfo.end () || it->second .GetBucketPosition (nKey, true , n) != i) {
1008+ return -19 ;
1009+ }
1010+ if (--mapNew[vvNew[n][i]] == 0 )
1011+ mapNew.erase (vvNew[n][i]);
1012+ }
1013+ }
1014+ }
1015+
1016+ if (setTried.size ())
1017+ return -13 ;
1018+ if (mapNew.size ())
1019+ return -15 ;
1020+ if (nKey.IsNull ())
1021+ return -16 ;
1022+
1023+ LogPrint (BCLog::ADDRMAN, " Addrman checks completed successfully\n " );
1024+ return 0 ;
1025+ }
1026+
10261027size_t CAddrMan::size () const
10271028{
10281029 LOCK (cs); // TODO: Cache this in an atomic to avoid this overhead
0 commit comments