Bitcoin Core  22.99.0
P2P Digital Currency
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
CAddrMan Class Reference

Stochastical (IP) address manager. More...

#include <addrman.h>

Inheritance diagram for CAddrMan:
[legend]
Collaboration diagram for CAddrMan:
[legend]

Public Member Functions

template<typename Stream >
void Serialize (Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
template<typename Stream >
void Unserialize (Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
 CAddrMan (std::vector< bool > asmap, bool deterministic, int32_t consistency_check_ratio)
 
 ~CAddrMan ()
 
size_t size () const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Return the number of (unique) addresses in all tables. More...
 
bool Add (const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Add addresses to addrman's new table. More...
 
void Good (const CService &addr, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Mark an entry as accessible. More...
 
void Attempt (const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Mark an entry as connection attempted to. More...
 
void ResolveCollisions () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. More...
 
CAddrInfo SelectTriedCollision () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Randomly select an address in tried that another address is attempting to evict. More...
 
CAddrInfo Select (bool newOnly=false) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Choose an address to connect to. More...
 
std::vector< CAddressGetAddr (size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Return all or many randomly selected addresses, optionally by network. More...
 
void Connected (const CService &addr, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 Outer function for Connected_() More...
 
void SetServices (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
const std::vector< bool > & GetAsmap () const
 

Private Types

enum  Format : uint8_t { V0_HISTORICAL = 0, V1_DETERMINISTIC = 1, V2_ASMAP = 2, V3_BIP155 = 3 }
 Serialization versions. More...
 

Private Member Functions

FastRandomContext insecure_rand GUARDED_BY (cs)
 Source of random numbers for randomization in inner loops. More...
 
int nIdCount GUARDED_BY (cs)
 last used nId More...
 
std::unordered_map< int, CAddrInfo > mapInfo GUARDED_BY (cs)
 table with information about all nIds More...
 
std::unordered_map< CNetAddr, int, CNetAddrHash > mapAddr GUARDED_BY (cs)
 find an nId based on its network address More...
 
std::vector< int > vRandom GUARDED_BY (cs)
 randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class, so any changes to it (even in const methods) are also unobservable. More...
 
int nTried GUARDED_BY (cs)
 
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZEGUARDED_BY (cs)
 list of "tried" buckets More...
 
int nNew GUARDED_BY (cs)
 number of (unique) "new" entries More...
 
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZEGUARDED_BY (cs)
 list of "new" buckets More...
 
int64_t nLastGood GUARDED_BY (cs)
 last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse. More...
 
CAddrInfoFind (const CNetAddr &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Find an entry. More...
 
CAddrInfoCreate (const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Create a new entry and add it to the internal data structures mapInfo, mapAddr and vRandom. More...
 
void SwapRandom (unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Swap two elements in vRandom. More...
 
void MakeTried (CAddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Move an entry from the "new" table(s) to the "tried" table. More...
 
void Delete (int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Delete an entry. It must not be in tried, and have refcount 0. More...
 
void ClearNew (int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Clear a position in a "new" table. This is the only place where entries are actually deleted. More...
 
void Good_ (const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Mark an entry "good", possibly moving it from "new" to "tried". More...
 
bool Add_ (const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Add an entry to the "new" table. More...
 
void Attempt_ (const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Mark an entry as attempted to connect. More...
 
CAddrInfo Select_ (bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Select an address to connect to, if newOnly is set to true, only the new table is selected from. More...
 
void ResolveCollisions_ () EXCLUSIVE_LOCKS_REQUIRED(cs)
 See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. More...
 
CAddrInfo SelectTriedCollision_ () EXCLUSIVE_LOCKS_REQUIRED(cs)
 Return a random to-be-evicted tried table address. More...
 
void Check () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Consistency check, taking into account m_consistency_check_ratio. Will std::abort if an inconsistency is detected. More...
 
int ForceCheckAddrman () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Perform consistency check, regardless of m_consistency_check_ratio. More...
 
void GetAddr_ (std::vector< CAddress > &vAddr, size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Return all or many randomly selected addresses, optionally by network. More...
 
void Connected_ (const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
 We have successfully connected to this peer. More...
 
void SetServices_ (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Update an entry's service bits. More...
 

Private Attributes

Mutex cs
 A mutex to protect the inner data structures. More...
 
uint256 nKey
 secret key to randomize bucket select with More...
 
std::set< int > m_tried_collisions
 Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions. More...
 
const int32_t m_consistency_check_ratio
 Perform consistency checks every m_consistency_check_ratio operations (if non-zero). More...
 
const std::vector< bool > m_asmap
 

Static Private Attributes

static constexpr Format FILE_FORMAT = Format::V3_BIP155
 The maximum format this software knows it can unserialize. More...
 
static constexpr uint8_t INCOMPATIBILITY_BASE = 32
 The initial value of a field that is incremented every time an incompatible format change is made (such that old software versions would not be able to parse and understand the new file format). More...
 

Friends

class CAddrManTest
 
class CAddrManDeterministic
 

Detailed Description

Stochastical (IP) address manager.

Definition at line 142 of file addrman.h.

Member Enumeration Documentation

◆ Format

enum CAddrMan::Format : uint8_t
private

Serialization versions.

Enumerator
V0_HISTORICAL 

historic format, before commit e6b343d88

V1_DETERMINISTIC 

for pre-asmap files

V2_ASMAP 

for files including asmap version

V3_BIP155 

same as V2_ASMAP plus addresses are in BIP155 format

Definition at line 286 of file addrman.h.

Constructor & Destructor Documentation

◆ CAddrMan()

CAddrMan::CAddrMan ( std::vector< bool >  asmap,
bool  deterministic,
int32_t  consistency_check_ratio 
)
explicit

Definition at line 104 of file addrman.cpp.

◆ ~CAddrMan()

CAddrMan::~CAddrMan ( )
inline

Definition at line 153 of file addrman.h.

Here is the call graph for this function:

Member Function Documentation

◆ Add()

bool CAddrMan::Add ( const std::vector< CAddress > &  vAddr,
const CNetAddr source,
int64_t  nTimePenalty = 0 
)
inline

Add addresses to addrman's new table.

Definition at line 167 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Add_()

bool CAddrMan::Add_ ( const CAddress addr,
const CNetAddr source,
int64_t  nTimePenalty 
)
private

Add an entry to the "new" table.

Definition at line 601 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Attempt()

void CAddrMan::Attempt ( const CService addr,
bool  fCountFailure,
int64_t  nTime = GetAdjustedTime() 
)
inline

Mark an entry as connection attempted to.

Definition at line 193 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Attempt_()

void CAddrMan::Attempt_ ( const CService addr,
bool  fCountFailure,
int64_t  nTime 
)
private

Mark an entry as attempted to connect.

Definition at line 676 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Check()

void CAddrMan::Check ( ) const
private

Consistency check, taking into account m_consistency_check_ratio. Will std::abort if an inconsistency is detected.

Definition at line 751 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearNew()

void CAddrMan::ClearNew ( int  nUBucket,
int  nUBucketPos 
)
private

Clear a position in a "new" table. This is the only place where entries are actually deleted.

Definition at line 475 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Connected()

void CAddrMan::Connected ( const CService addr,
int64_t  nTime = GetAdjustedTime() 
)
inline

Outer function for Connected_()

Definition at line 255 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Connected_()

void CAddrMan::Connected_ ( const CService addr,
int64_t  nTime 
)
private

We have successfully connected to this peer.

Calling this function updates the CAddress's nTime, which is used in our IsTerrible() decisions and gossiped to peers. Callers should be careful that updating this information doesn't leak topology information to network spies.

net_processing calls this function when it disconnects from a peer to not leak information about currently connected peers.

Parameters
[in]addrThe address of the peer we were connected to
[in]nTimeThe time that we were last connected to this peer

Definition at line 889 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Create()

CAddrInfo * CAddrMan::Create ( const CAddress addr,
const CNetAddr addrSource,
int *  pnId = nullptr 
)
private

Create a new entry and add it to the internal data structures mapInfo, mapAddr and vRandom.

Definition at line 421 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Delete()

void CAddrMan::Delete ( int  nId)
private

Delete an entry. It must not be in tried, and have refcount 0.

Definition at line 459 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Find()

CAddrInfo * CAddrMan::Find ( const CNetAddr addr,
int *  pnId = nullptr 
)
private

Find an entry.

Definition at line 406 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ForceCheckAddrman()

int CAddrMan::ForceCheckAddrman ( ) const
private

Perform consistency check, regardless of m_consistency_check_ratio.

Returns
an error code or zero.

Definition at line 766 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAddr()

std::vector<CAddress> CAddrMan::GetAddr ( size_t  max_addresses,
size_t  max_pct,
std::optional< Network network 
) const
inline

Return all or many randomly selected addresses, optionally by network.

Parameters
[in]max_addressesMaximum number of addresses to return (0 = all).
[in]max_pctMaximum percentage of addresses to return (0 = all).
[in]networkSelect only addresses of this network (nullopt = all).

Definition at line 243 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAddr_()

void CAddrMan::GetAddr_ ( std::vector< CAddress > &  vAddr,
size_t  max_addresses,
size_t  max_pct,
std::optional< Network network 
) const
private

Return all or many randomly selected addresses, optionally by network.

Parameters
[out]vAddrVector of randomly selected addresses from vRandom.
[in]max_addressesMaximum number of addresses to return (0 = all).
[in]max_pctMaximum percentage of addresses to return (0 = all).
[in]networkSelect only addresses of this network (nullopt = all).

Definition at line 854 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetAsmap()

const std::vector<bool>& CAddrMan::GetAsmap ( ) const
inline

Definition at line 273 of file addrman.h.

Here is the caller graph for this function:

◆ Good()

void CAddrMan::Good ( const CService addr,
int64_t  nTime = GetAdjustedTime() 
)
inline

Mark an entry as accessible.

Definition at line 183 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Good_()

void CAddrMan::Good_ ( const CService addr,
bool  test_before_evict,
int64_t  time 
)
private

Mark an entry "good", possibly moving it from "new" to "tried".

Definition at line 545 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GUARDED_BY() [1/10]

FastRandomContext insecure_rand CAddrMan::GUARDED_BY ( cs  )
mutableprivate

Source of random numbers for randomization in inner loops.

◆ GUARDED_BY() [2/10]

int nIdCount CAddrMan::GUARDED_BY ( cs  )
inlineprivate

last used nId

Definition at line 308 of file addrman.h.

◆ GUARDED_BY() [3/10]

std::unordered_map<int, CAddrInfo> mapInfo CAddrMan::GUARDED_BY ( cs  )
private

table with information about all nIds

◆ GUARDED_BY() [4/10]

std::unordered_map<CNetAddr, int, CNetAddrHash> mapAddr CAddrMan::GUARDED_BY ( cs  )
private

find an nId based on its network address

◆ GUARDED_BY() [5/10]

std::vector<int> vRandom CAddrMan::GUARDED_BY ( cs  )
mutableprivate

randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class, so any changes to it (even in const methods) are also unobservable.

◆ GUARDED_BY() [6/10]

int nTried CAddrMan::GUARDED_BY ( cs  )
inlineprivate

Definition at line 322 of file addrman.h.

◆ GUARDED_BY() [7/10]

int vvTried [ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] CAddrMan::GUARDED_BY ( cs  )
private

list of "tried" buckets

◆ GUARDED_BY() [8/10]

int nNew CAddrMan::GUARDED_BY ( cs  )
inlineprivate

number of (unique) "new" entries

Definition at line 328 of file addrman.h.

◆ GUARDED_BY() [9/10]

int vvNew [ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] CAddrMan::GUARDED_BY ( cs  )
private

list of "new" buckets

◆ GUARDED_BY() [10/10]

int64_t nLastGood CAddrMan::GUARDED_BY ( cs  )
inlineprivate

last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.

Definition at line 334 of file addrman.h.

◆ MakeTried()

void CAddrMan::MakeTried ( CAddrInfo info,
int  nId 
)
private

Move an entry from the "new" table(s) to the "tried" table.

Definition at line 492 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ResolveCollisions()

void CAddrMan::ResolveCollisions ( )
inline

See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.

Definition at line 203 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ResolveCollisions_()

void CAddrMan::ResolveCollisions_ ( )
private

See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.

Definition at line 931 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Select()

CAddrInfo CAddrMan::Select ( bool  newOnly = false) const
inline

Choose an address to connect to.

Definition at line 226 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Select_()

CAddrInfo CAddrMan::Select_ ( bool  newOnly) const
private

Select an address to connect to, if newOnly is set to true, only the new table is selected from.

Definition at line 700 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SelectTriedCollision()

CAddrInfo CAddrMan::SelectTriedCollision ( )
inline

Randomly select an address in tried that another address is attempting to evict.

Definition at line 213 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SelectTriedCollision_()

CAddrInfo CAddrMan::SelectTriedCollision_ ( )
private

Return a random to-be-evicted tried table address.

Definition at line 992 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Serialize()

template<typename Stream >
template void CAddrMan::Serialize ( Stream &  s_) const

Serialized format.

  • format version byte (
    See also
    Format)
  • lowest compatible format version byte. This is used to help old software decide whether to parse the file. For example:
    • Bitcoin Core version N knows how to parse up to format=3. If a new format=4 is introduced in version N+1 that is compatible with format=3 and it is known that version N will be able to parse it, then version N+1 will write (format=4, lowest_compatible=3) in the first two bytes of the file, and so version N will still try to parse it.
    • Bitcoin Core version N+2 introduces a new incompatible format=5. It will write (format=5, lowest_compatible=5) and so any versions that do not know how to parse format=5 will not try to read the file.
  • nKey
  • nNew
  • nTried
  • number of "new" buckets XOR 2**30
  • all new addresses (total count: nNew)
  • all tried addresses (total count: nTried)
  • for each new bucket:
    • number of elements
    • for each element: index in the serialized "all new addresses"
  • asmap checksum

2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it as incompatible. This is necessary because it did not check the version number on deserialization.

vvNew, vvTried, mapInfo, mapAddr and vRandom are never encoded explicitly; they are instead reconstructed from the other information.

This format is more complex, but significantly smaller (at most 1.5 MiB), and supports changes to the ADDRMAN_ parameters without breaking the on-disk structure.

We don't use SERIALIZE_METHODS since the serialization and deserialization code has very little in common.

Definition at line 123 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetServices()

void CAddrMan::SetServices ( const CService addr,
ServiceFlags  nServices 
)
inline

Definition at line 264 of file addrman.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetServices_()

void CAddrMan::SetServices_ ( const CService addr,
ServiceFlags  nServices 
)
private

Update an entry's service bits.

Definition at line 911 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

size_t CAddrMan::size ( ) const
inline

Return the number of (unique) addresses in all tables.

Definition at line 159 of file addrman.h.

Here is the caller graph for this function:

◆ SwapRandom()

void CAddrMan::SwapRandom ( unsigned int  nRandomPos1,
unsigned int  nRandomPos2 
) const
private

Swap two elements in vRandom.

Definition at line 435 of file addrman.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Unserialize()

template<typename Stream >
template void CAddrMan::Unserialize ( Stream &  s_)

Definition at line 226 of file addrman.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ CAddrManDeterministic

friend class CAddrManDeterministic
friend

Definition at line 428 of file addrman.h.

◆ CAddrManTest

friend class CAddrManTest
friend

Definition at line 427 of file addrman.h.

Member Data Documentation

◆ cs

Mutex CAddrMan::cs
mutableprivate

A mutex to protect the inner data structures.

Definition at line 277 of file addrman.h.

◆ FILE_FORMAT

constexpr Format CAddrMan::FILE_FORMAT = Format::V3_BIP155
staticconstexprprivate

The maximum format this software knows it can unserialize.

Also, we always serialize in this format. The format (first byte in the serialized stream) can be higher than this and still this software may be able to unserialize the file - if the second byte (see lowest_compatible in Unserialize()) is less or equal to this.

Definition at line 298 of file addrman.h.

◆ INCOMPATIBILITY_BASE

constexpr uint8_t CAddrMan::INCOMPATIBILITY_BASE = 32
staticconstexprprivate

The initial value of a field that is incremented every time an incompatible format change is made (such that old software versions would not be able to parse and understand the new file format).

This is 32 because we overtook the "key size" field which was 32 historically.

Note
Don't increment this. Increment lowest_compatible in Serialize() instead.

Definition at line 305 of file addrman.h.

◆ m_asmap

const std::vector<bool> CAddrMan::m_asmap
private

Definition at line 356 of file addrman.h.

◆ m_consistency_check_ratio

const int32_t CAddrMan::m_consistency_check_ratio
private

Perform consistency checks every m_consistency_check_ratio operations (if non-zero).

Definition at line 340 of file addrman.h.

◆ m_tried_collisions

std::set<int> CAddrMan::m_tried_collisions
private

Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.

Definition at line 334 of file addrman.h.

◆ nKey

uint256 CAddrMan::nKey
private

secret key to randomize bucket select with

Definition at line 283 of file addrman.h.


The documentation for this class was generated from the following files: