Bitcoin
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
CAddrMan Class Reference

#include <addrman.h>

Public Member Functions

template<typename Stream >
void Serialize (Stream &s) const
 
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)
 
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) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Find an entry. More...
 
CAddrInfoCreate (const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void SwapRandom (unsigned int nRandomPos1, unsigned int nRandomPos2) 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) 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 GetAddr_ (std::vector< CAddress > &vAddr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Select several addresses at once. More...
 
void Connected_ (const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Mark an entry as currently-connected-to. More...
 
void SetServices_ (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Update an entry's service bits. More...
 

Protected Attributes

CCriticalSection cs
 critical section to protect the inner data structures More...
 
uint256 nKey
 secret key to randomize bucket select with More...
 
FastRandomContext insecure_rand
 Source of random numbers for randomization in inner loops. More...
 

Private Member Functions

int nIdCount GUARDED_BY (cs)
 last used nId More...
 
std::map< int, CAddrInfo > mapInfo GUARDED_BY (cs)
 table with information about all nIds More...
 
std::map< CNetAddr, int > 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 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) More...
 

Private Attributes

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

Constructor & Destructor Documentation

◆ CAddrMan()

CAddrMan::CAddrMan ( )
inline

◆ ~CAddrMan()

CAddrMan::~CAddrMan ( )
inline

Member Function Documentation

◆ Add() [1/2]

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

Add a single address.

◆ Add() [2/2]

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

Add multiple addresses.

◆ Add_()

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

Add an entry to the "new" table.

◆ Attempt()

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

Mark an entry as connection attempted to.

◆ Attempt_()

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

Mark an entry as attempted to connect.

◆ Check()

void CAddrMan::Check ( )
inline

Consistency check.

◆ Clear()

void CAddrMan::Clear ( )
inline

◆ ClearNew()

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.

◆ Connected()

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

Mark an entry as currently-connected-to.

◆ Connected_()

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

Mark an entry as currently-connected-to.

◆ Create()

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.

◆ Delete()

void CAddrMan::Delete ( int  nId)
protected

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

◆ Find()

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

Find an entry.

◆ GetAddr()

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

Return a bunch of addresses, selected at random.

◆ GetAddr_()

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

Select several addresses at once.

◆ Good()

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

Mark an entry as accessible.

◆ Good_()

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".

◆ GUARDED_BY() [1/9]

int nIdCount CAddrMan::GUARDED_BY ( cs  )
private

last used nId

◆ GUARDED_BY() [2/9]

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

table with information about all nIds

◆ GUARDED_BY() [3/9]

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

find an nId based on its network address

◆ GUARDED_BY() [4/9]

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

randomly-ordered vector of all nIds

◆ GUARDED_BY() [5/9]

int nTried CAddrMan::GUARDED_BY ( cs  )
private

◆ GUARDED_BY() [6/9]

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

list of "tried" buckets

◆ GUARDED_BY() [7/9]

int nNew CAddrMan::GUARDED_BY ( cs  )
private

number of (unique) "new" entries

◆ GUARDED_BY() [8/9]

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

list of "new" buckets

◆ GUARDED_BY() [9/9]

int64_t nLastGood CAddrMan::GUARDED_BY ( cs  )
private

last time Good was called (memory only)

◆ MakeTried()

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

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

◆ ResolveCollisions()

void CAddrMan::ResolveCollisions ( )
inline

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

◆ ResolveCollisions_()

void CAddrMan::ResolveCollisions_ ( )
protected

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

◆ Select()

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

Choose an address to connect to.

◆ Select_()

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.

◆ SelectTriedCollision()

CAddrInfo CAddrMan::SelectTriedCollision ( )
inline

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

◆ SelectTriedCollision_()

CAddrInfo CAddrMan::SelectTriedCollision_ ( )
protected

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

◆ Serialize()

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.

◆ SetServices()

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

◆ SetServices_()

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

Update an entry's service bits.

◆ size()

size_t CAddrMan::size ( ) const
inline

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

◆ SwapRandom()

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

Swap two elements in vRandom.

◆ Unserialize()

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

Member Data Documentation

◆ cs

CCriticalSection CAddrMan::cs
mutableprotected

critical section to protect the inner data structures

◆ insecure_rand

FastRandomContext CAddrMan::insecure_rand
protected

Source of random numbers for randomization in inner loops.

◆ 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.

◆ nKey

uint256 CAddrMan::nKey
protected

secret key to randomize bucket select with


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