@ -138,7 +138,7 @@ CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, in
return & mapInfo [ nId ] ;
}
void CAddrMan : : SwapRandom ( unsigned int nRndPos1 , unsigned int nRndPos2 )
void CAddrMan : : SwapRandom ( unsigned int nRndPos1 , unsigned int nRndPos2 ) const
{
AssertLockHeld ( cs ) ;
@ -150,11 +150,13 @@ void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2)
int nId1 = vRandom [ nRndPos1 ] ;
int nId2 = vRandom [ nRndPos2 ] ;
assert ( mapInfo . count ( nId1 ) = = 1 ) ;
assert ( mapInfo . count ( nId2 ) = = 1 ) ;
const auto it_1 { mapInfo . find ( nId1 ) } ;
const auto it_2 { mapInfo . find ( nId2 ) } ;
assert ( it_1 ! = mapInfo . end ( ) ) ;
assert ( it_2 ! = mapInfo . end ( ) ) ;
mapInfo [ nId1 ] . nRandomPos = nRndPos2 ;
mapInfo[ nId2 ] . nRandomPos = nRndPos1 ;
it_1- > second . nRandomPos = nRndPos2 ;
it_2- > second . nRandomPos = nRndPos1 ;
vRandom [ nRndPos1 ] = nId2 ;
vRandom [ nRndPos2 ] = nId1 ;
@ -410,7 +412,7 @@ void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
}
}
CAddrInfo CAddrMan : : Select_ ( bool newOnly )
CAddrInfo CAddrMan : : Select_ ( bool newOnly ) const
{
AssertLockHeld ( cs ) ;
@ -433,8 +435,9 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
nKBucketPos = ( nKBucketPos + insecure_rand . randbits ( ADDRMAN_BUCKET_SIZE_LOG2 ) ) % ADDRMAN_BUCKET_SIZE ;
}
int nId = vvTried [ nKBucket ] [ nKBucketPos ] ;
assert ( mapInfo . count ( nId ) = = 1 ) ;
CAddrInfo & info = mapInfo [ nId ] ;
const auto it_found { mapInfo . find ( nId ) } ;
assert ( it_found ! = mapInfo . end ( ) ) ;
const CAddrInfo & info { it_found - > second } ;
if ( insecure_rand . randbits ( 30 ) < fChanceFactor * info . GetChance ( ) * ( 1 < < 30 ) )
return info ;
fChanceFactor * = 1.2 ;
@ -450,8 +453,9 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
nUBucketPos = ( nUBucketPos + insecure_rand . randbits ( ADDRMAN_BUCKET_SIZE_LOG2 ) ) % ADDRMAN_BUCKET_SIZE ;
}
int nId = vvNew [ nUBucket ] [ nUBucketPos ] ;
assert ( mapInfo . count ( nId ) = = 1 ) ;
CAddrInfo & info = mapInfo [ nId ] ;
const auto it_found { mapInfo . find ( nId ) } ;
assert ( it_found ! = mapInfo . end ( ) ) ;
const CAddrInfo & info { it_found - > second } ;
if ( insecure_rand . randbits ( 30 ) < fChanceFactor * info . GetChance ( ) * ( 1 < < 30 ) )
return info ;
fChanceFactor * = 1.2 ;
@ -503,15 +507,15 @@ int CAddrMan::Check_()
for ( int n = 0 ; n < ADDRMAN_TRIED_BUCKET_COUNT ; n + + ) {
for ( int i = 0 ; i < ADDRMAN_BUCKET_SIZE ; i + + ) {
if ( vvTried [ n ] [ i ] ! = - 1 ) {
if ( ! setTried . count ( vvTried [ n ] [ i ] ) )
return - 11 ;
if ( mapInfo [ vvTried [ n ] [ i ] ] . GetTriedBucket ( nKey , m_asmap ) ! = n )
return - 17 ;
if ( mapInfo [ vvTried [ n ] [ i ] ] . GetBucketPosition ( nKey , false , n ) ! = i )
return - 18 ;
setTried . erase ( vvTried [ n ] [ i ] ) ;
}
if ( vvTried [ n ] [ i ] ! = - 1 ) {
if ( ! setTried . count ( vvTried [ n ] [ i ] ) )
return - 11 ;
if ( mapInfo [ vvTried [ n ] [ i ] ] . GetTriedBucket ( nKey , m_asmap ) ! = n )
return - 17 ;
if ( mapInfo [ vvTried [ n ] [ i ] ] . GetBucketPosition ( nKey , false , n ) ! = i )
return - 18 ;
setTried . erase ( vvTried [ n ] [ i ] ) ;
}
}
}
@ -539,7 +543,7 @@ int CAddrMan::Check_()
}
# endif
void CAddrMan : : GetAddr_ ( std : : vector < CAddress > & vAddr , size_t max_addresses , size_t max_pct , std : : optional < Network > network )
void CAddrMan : : GetAddr_ ( std : : vector < CAddress > & vAddr , size_t max_addresses , size_t max_pct , std : : optional < Network > network ) const
{
AssertLockHeld ( cs ) ;
@ -559,9 +563,10 @@ void CAddrMan::GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size
int nRndPos = insecure_rand . randrange ( vRandom . size ( ) - n ) + n ;
SwapRandom ( n , nRndPos ) ;
assert ( mapInfo . count ( vRandom [ n ] ) = = 1 ) ;
const auto it { mapInfo . find ( vRandom [ n ] ) } ;
assert ( it ! = mapInfo . end ( ) ) ;
const CAddrInfo & ai = mapInfo [ vRandom [ n ] ] ;
const CAddrInfo & ai { it - > second } ;
// Filter by network (optional)
if ( network ! = std : : nullopt & & ai . GetNetClass ( ) ! = network ) continue ;