Bitcoin Core  0.15.99
P2P Digital Currency
Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes | List of all members
CAddrMan Class Reference

Stochastical (IP) address manager. More...

#include <addrman.h>

Collaboration diagram for CAddrMan:
[legend]

Public Member Functions

template<typename Stream >
void Serialize (Stream &s) const
 serialized format: More...
 
template<typename Stream >
void Unserialize (Stream &s)
 
void Clear ()
 
 CAddrMan ()
 
 ~CAddrMan ()
 
size_t size () const
 Return the number of (unique) addresses in all tables. More...
 
void Check ()
 Consistency check. More...
 
bool Add (const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
 Add a single address. More...
 
bool Add (const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
 Add multiple addresses. More...
 
void Good (const CService &addr, bool test_before_evict=true, int64_t nTime=GetAdjustedTime())
 Mark an entry as accessible. More...
 
void Attempt (const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
 Mark an entry as connection attempted to. More...
 
void ResolveCollisions ()
 See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. More...
 
CAddrInfo SelectTriedCollision ()
 Randomly select an address in tried that another address is attempting to evict. More...
 
CAddrInfo Select (bool newOnly=false)
 Choose an address to connect to. More...
 
std::vector< CAddressGetAddr ()
 Return a bunch of addresses, selected at random. More...
 
void Connected (const CService &addr, int64_t nTime=GetAdjustedTime())
 Mark an entry as currently-connected-to. More...
 
void SetServices (const CService &addr, ServiceFlags nServices)
 

Protected Member Functions

CAddrInfoFind (const CNetAddr &addr, int *pnId=nullptr)
 Find an entry. More...
 
CAddrInfoCreate (const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr)
 find an entry, creating it if necessary. More...
 
void SwapRandom (unsigned int nRandomPos1, unsigned int nRandomPos2)
 Swap two elements in vRandom. More...
 
void MakeTried (CAddrInfo &info, int nId)
 Move an entry from the "new" table(s) to the "tried" table. More...
 
void Delete (int nId)
 Delete an entry. It must not be in tried, and have refcount 0. More...
 
void ClearNew (int nUBucket, int nUBucketPos)
 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)
 Mark an entry "good", possibly moving it from "new" to "tried". More...
 
bool Add_ (const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty)
 Add an entry to the "new" table. More...
 
void Attempt_ (const CService &addr, bool fCountFailure, int64_t nTime)
 Mark an entry as attempted to connect. More...
 
CAddrInfo Select_ (bool newOnly)
 Select an address to connect to, if newOnly is set to true, only the new table is selected from. More...
 
void ResolveCollisions_ ()
 See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. More...
 
CAddrInfo SelectTriedCollision_ ()
 Return a random to-be-evicted tried table address. More...
 
virtual int RandomInt (int nMax)
 Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic. More...
 
void GetAddr_ (std::vector< CAddress > &vAddr)
 Select several addresses at once. More...
 
void Connected_ (const CService &addr, int64_t nTime)
 Mark an entry as currently-connected-to. More...
 
void SetServices_ (const CService &addr, ServiceFlags nServices)
 Update an entry's service bits. More...
 

Protected Attributes

uint256 nKey
 secret key to randomize bucket select with More...
 
FastRandomContext insecure_rand
 Source of random numbers for randomization in inner loops. More...
 

Private Attributes

CCriticalSection cs
 critical section to protect the inner data structures More...
 
int nIdCount
 last used nId More...
 
std::map< int, CAddrInfomapInfo
 table with information about all nIds More...
 
std::map< CNetAddr, int > mapAddr
 find an nId based on its network address More...
 
std::vector< int > vRandom
 randomly-ordered vector of all nIds More...
 
int nTried
 
int vvTried [ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]
 list of "tried" buckets More...
 
int nNew
 number of (unique) "new" entries More...
 
int vvNew [ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]
 list of "new" buckets More...
 
int64_t nLastGood
 last time Good was called (memory only) 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...
 

Detailed Description

Stochastical (IP) address manager.

Definition at line 188 of file addrman.h.

Constructor & Destructor Documentation

CAddrMan::CAddrMan ( )
inline

Definition at line 495 of file addrman.h.

CAddrMan::~CAddrMan ( )
inline

Definition at line 500 of file addrman.h.

Here is the call graph for this function:

Member Function Documentation

bool CAddrMan::Add ( const CAddress addr,
const CNetAddr source,
int64_t  nTimePenalty = 0 
)
inline

Add a single address.

Definition at line 526 of file addrman.h.

Here is the call graph for this function:

Here is the caller graph for this function:

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

Add multiple addresses.

Definition at line 540 of file addrman.h.

Here is the call graph for this function:

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

Add an entry to the "new" table.

Definition at line 254 of file addrman.cpp.

Here is the call graph for this function:

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

Mark an entry as connection attempted to.

Definition at line 564 of file addrman.h.

Here is the caller graph for this function:

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

Mark an entry as attempted to connect.

Definition at line 327 of file addrman.cpp.

void CAddrMan::Check ( )
inline

Consistency check.

Definition at line 513 of file addrman.h.

void CAddrMan::Clear ( )
inline

Definition at line 471 of file addrman.h.

Here is the call graph for this function:

Here is the caller graph for this function:

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

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

Definition at line 127 of file addrman.cpp.

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

Mark an entry as currently-connected-to.

Definition at line 623 of file addrman.h.

Here is the caller graph for this function:

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

Mark an entry as currently-connected-to.

Definition at line 495 of file addrman.cpp.

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

find an entry, creating it if necessary.

nTime and nServices of the found node are updated, if necessary.

Definition at line 81 of file addrman.cpp.

Here is the call graph for this function:

void CAddrMan::Delete ( int  nId)
protected

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

Definition at line 113 of file addrman.cpp.

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

Find an entry.

Definition at line 68 of file addrman.cpp.

std::vector<CAddress> CAddrMan::GetAddr ( )
inline

Return a bunch of addresses, selected at random.

Definition at line 610 of file addrman.h.

Here is the caller graph for this function:

void CAddrMan::GetAddr_ ( std::vector< CAddress > &  vAddr)
protected

Select several addresses at once.

Definition at line 474 of file addrman.cpp.

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

Mark an entry as accessible.

Definition at line 555 of file addrman.h.

Here is the caller graph for this function:

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

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

Definition at line 190 of file addrman.cpp.

Here is the call graph for this function:

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

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

Definition at line 142 of file addrman.cpp.

Here is the call graph for this function:

int CAddrMan::RandomInt ( int  nMax)
protectedvirtual

Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.

Definition at line 533 of file addrman.cpp.

Here is the call graph for this function:

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 573 of file addrman.h.

Here is the caller graph for this function:

void CAddrMan::ResolveCollisions_ ( )
protected

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

Definition at line 537 of file addrman.cpp.

Here is the call graph for this function:

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

Choose an address to connect to.

Definition at line 597 of file addrman.h.

Here is the caller graph for this function:

CAddrInfo CAddrMan::Select_ ( bool  newOnly)
protected

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

Definition at line 349 of file addrman.cpp.

Here is the call graph for this function:

CAddrInfo CAddrMan::SelectTriedCollision ( )
inline

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

Definition at line 582 of file addrman.h.

Here is the caller graph for this function:

CAddrInfo CAddrMan::SelectTriedCollision_ ( )
protected

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

Definition at line 589 of file addrman.cpp.

Here is the call graph for this function:

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

serialized format:

  • version byte (currently 1)
  • 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
  • nNew
  • nTried
  • number of "new" buckets XOR 2**30
  • all nNew addrinfos in vvNew
  • all nTried addrinfos in vvTried
  • for each bucket:
    • number of elements
    • for each element: index

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.

Notice that vvTried, mapAddr and vVector are never encoded explicitly; they are instead reconstructed from the other information.

vvNew is serialized, but only used if ADDRMAN_UNKNOWN_BUCKET_COUNT didn't change, otherwise it is reconstructed as well.

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 ADD_SERIALIZE_METHODS since the serialization and deserialization code has very little in common.

Definition at line 316 of file addrman.h.

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

Definition at line 631 of file addrman.h.

Here is the caller graph for this function:

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

Update an entry's service bits.

Definition at line 515 of file addrman.cpp.

size_t CAddrMan::size ( ) const
inline

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

Definition at line 506 of file addrman.h.

Here is the caller graph for this function:

void CAddrMan::SwapRandom ( unsigned int  nRandomPos1,
unsigned int  nRandomPos2 
)
protected

Swap two elements in vRandom.

Definition at line 93 of file addrman.cpp.

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

Definition at line 366 of file addrman.h.

Here is the call graph for this function:

Member Data Documentation

CCriticalSection CAddrMan::cs
mutableprivate

critical section to protect the inner data structures

Definition at line 192 of file addrman.h.

FastRandomContext CAddrMan::insecure_rand
protected

Source of random numbers for randomization in inner loops.

Definition at line 229 of file addrman.h.

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 222 of file addrman.h.

std::map<CNetAddr, int> CAddrMan::mapAddr
private

find an nId based on its network address

Definition at line 201 of file addrman.h.

std::map<int, CAddrInfo> CAddrMan::mapInfo
private

table with information about all nIds

Definition at line 198 of file addrman.h.

int CAddrMan::nIdCount
private

last used nId

Definition at line 195 of file addrman.h.

uint256 CAddrMan::nKey
protected

secret key to randomize bucket select with

Definition at line 226 of file addrman.h.

int64_t CAddrMan::nLastGood
private

last time Good was called (memory only)

Definition at line 219 of file addrman.h.

int CAddrMan::nNew
private

number of (unique) "new" entries

Definition at line 213 of file addrman.h.

int CAddrMan::nTried
private

Definition at line 207 of file addrman.h.

std::vector<int> CAddrMan::vRandom
private

randomly-ordered vector of all nIds

Definition at line 204 of file addrman.h.

int CAddrMan::vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]
private

list of "new" buckets

Definition at line 216 of file addrman.h.

int CAddrMan::vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]
private

list of "tried" buckets

Definition at line 210 of file addrman.h.


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