Bitcoin ABC  0.29.2
P2P Digital Currency
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
AddrManImpl Class Reference

#include <addrman_impl.h>

Collaboration diagram for AddrManImpl:
[legend]

Public Member Functions

 AddrManImpl (std::vector< bool > &&asmap, int32_t consistency_check_ratio)
 
 ~AddrManImpl ()
 
template<typename Stream >
void Serialize (Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
template<typename Stream >
void Unserialize (Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
size_t size () const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
bool Add (const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void Good (const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void Attempt (const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void ResolveCollisions () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::pair< CAddress, NodeSecondsSelectTriedCollision () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::pair< CAddress, NodeSecondsSelect (bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::vector< CAddressGetAddr (size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void Connected (const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void SetServices (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
const std::vector< bool > & GetAsmap () const
 
void Clear () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void MakeDeterministic () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 

Private Types

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

Private Member Functions

int nIdCount GUARDED_BY (cs)
 last used nId More...
 
std::unordered_map< int, AddrInfo > mapInfo GUARDED_BY (cs)
 table with information about all nIds More...
 
std::unordered_map< CService, int, CServiceHash > mapAddr GUARDED_BY (cs)
 find an nId based on its network address and port. 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...
 
NodeSeconds m_last_good GUARDED_BY (cs)
 last time Good was called (memory only) More...
 
AddrInfoFind (const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Find an entry. More...
 
AddrInfoCreate (const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 find an entry, creating it if necessary. More...
 
void SwapRandom (unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Swap two elements in vRandom. 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. More...
 
void MakeTried (AddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Move an entry from the "new" table(s) to the "tried" table. More...
 
bool AddSingle (const CAddress &addr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Attempt to add a single address to addrman's new table. More...
 
void Good_ (const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
bool Add_ (const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void Attempt_ (const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::pair< CAddress, NodeSecondsSelect_ (bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::vector< CAddressGetAddr_ (size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void Connected_ (const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void SetServices_ (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void ResolveCollisions_ () EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::pair< CAddress, NodeSecondsSelectTriedCollision_ () EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void Check () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Consistency check, taking into account m_consistency_check_ratio. More...
 
int CheckAddrman () const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Perform consistency check, regardless of m_consistency_check_ratio. More...
 

Private Attributes

Mutex cs
 A mutex to protect the inner data structures. More...
 
FastRandomContext insecure_rand
 Source of random numbers for randomization in inner loops. 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. 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
 
bool deterministic = false
 Use deterministic bucket selection and inner loops randomization. More...
 

Static Private Attributes

static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT
 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 AddrManTest
 
class AddrManCorrupted
 

Detailed Description

Definition at line 109 of file addrman_impl.h.

Member Enumeration Documentation

◆ Format

enum AddrManImpl::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

V4_MULTIPORT 

adds support for multiple ports per IP

Definition at line 170 of file addrman_impl.h.

Constructor & Destructor Documentation

◆ AddrManImpl()

AddrManImpl::AddrManImpl ( std::vector< bool > &&  asmap,
int32_t  consistency_check_ratio 
)

Definition at line 134 of file addrman.cpp.

◆ ~AddrManImpl()

AddrManImpl::~AddrManImpl ( )

Definition at line 139 of file addrman.cpp.

Here is the call graph for this function:

Member Function Documentation

◆ Add()

bool AddrManImpl::Add ( const std::vector< CAddress > &  vAddr,
const CNetAddr source,
std::chrono::seconds  time_penalty 
)

Definition at line 1180 of file addrman.cpp.

Here is the call graph for this function:

◆ Add_()

bool AddrManImpl::Add_ ( const std::vector< CAddress > &  vAddr,
const CNetAddr source,
std::chrono::seconds  time_penalty 
)
private

Definition at line 729 of file addrman.cpp.

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

◆ AddSingle()

bool AddrManImpl::AddSingle ( const CAddress addr,
const CNetAddr source,
std::chrono::seconds  time_penalty 
)
private

Attempt to add a single address to addrman's new table.

See also
AddrMan::Add() for parameters.

Definition at line 583 of file addrman.cpp.

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

◆ Attempt()

void AddrManImpl::Attempt ( const CService addr,
bool  fCountFailure,
NodeSeconds  time 
)

Definition at line 1198 of file addrman.cpp.

Here is the call graph for this function:

◆ Attempt_()

void AddrManImpl::Attempt_ ( const CService addr,
bool  fCountFailure,
NodeSeconds  time 
)
private

Definition at line 745 of file addrman.cpp.

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

◆ Check()

void AddrManImpl::Check ( ) const
private

Consistency check, taking into account m_consistency_check_ratio.

Will std::abort if an inconsistency is detected.

Definition at line 1048 of file addrman.cpp.

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

◆ CheckAddrman()

int AddrManImpl::CheckAddrman ( ) const
private

Perform consistency check, regardless of m_consistency_check_ratio.

Returns
an error code or zero.

Definition at line 1066 of file addrman.cpp.

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

◆ Clear()

void AddrManImpl::Clear ( )

Definition at line 1257 of file addrman.cpp.

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

◆ ClearNew()

void AddrManImpl::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 506 of file addrman.cpp.

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

◆ Connected()

void AddrManImpl::Connected ( const CService addr,
NodeSeconds  time 
)

Definition at line 1239 of file addrman.cpp.

Here is the call graph for this function:

◆ Connected_()

void AddrManImpl::Connected_ ( const CService addr,
NodeSeconds  time 
)
private

Definition at line 902 of file addrman.cpp.

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

◆ Create()

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

find an entry, creating it if necessary.

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

Definition at line 451 of file addrman.cpp.

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

◆ Delete()

void AddrManImpl::Delete ( int  nId)
private

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

Definition at line 491 of file addrman.cpp.

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

◆ Find()

AddrInfo * AddrManImpl::Find ( const CService addr,
int *  pnId = nullptr 
)
private

Find an entry.

Definition at line 434 of file addrman.cpp.

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

◆ GetAddr()

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

Definition at line 1230 of file addrman.cpp.

Here is the call graph for this function:

◆ GetAddr_()

std::vector< CAddress > AddrManImpl::GetAddr_ ( size_t  max_addresses,
size_t  max_pct,
std::optional< Network network 
) const
private

Definition at line 858 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 > & AddrManImpl::GetAsmap ( ) const

Definition at line 1253 of file addrman.cpp.

◆ Good()

void AddrManImpl::Good ( const CService addr,
bool  test_before_evict,
NodeSeconds  time 
)

Definition at line 1190 of file addrman.cpp.

Here is the call graph for this function:

◆ Good_()

void AddrManImpl::Good_ ( const CService addr,
bool  test_before_evict,
NodeSeconds  time 
)
private

Definition at line 667 of file addrman.cpp.

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

◆ GUARDED_BY() [1/9]

int nIdCount AddrManImpl::GUARDED_BY ( cs  )
private

last used nId

◆ GUARDED_BY() [2/9]

std::unordered_map<int, AddrInfo> mapInfo AddrManImpl::GUARDED_BY ( cs  )
private

table with information about all nIds

◆ GUARDED_BY() [3/9]

std::unordered_map<CService, int, CServiceHash> mapAddr AddrManImpl::GUARDED_BY ( cs  )
private

find an nId based on its network address and port.

◆ GUARDED_BY() [4/9]

std::vector<int> vRandom AddrManImpl::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() [5/9]

int nTried AddrManImpl::GUARDED_BY ( cs  )
private

◆ GUARDED_BY() [6/9]

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

list of "tried" buckets

◆ GUARDED_BY() [7/9]

int nNew AddrManImpl::GUARDED_BY ( cs  )
private

number of (unique) "new" entries

◆ GUARDED_BY() [8/9]

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

list of "new" buckets

◆ GUARDED_BY() [9/9]

NodeSeconds m_last_good AddrManImpl::GUARDED_BY ( cs  )
private

last time Good was called (memory only)

◆ MakeDeterministic()

void AddrManImpl::MakeDeterministic ( )

Definition at line 1287 of file addrman.cpp.

Here is the call graph for this function:

◆ MakeTried()

void AddrManImpl::MakeTried ( AddrInfo info,
int  nId 
)
private

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

Definition at line 524 of file addrman.cpp.

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

◆ ResolveCollisions()

void AddrManImpl::ResolveCollisions ( )

Definition at line 1206 of file addrman.cpp.

Here is the call graph for this function:

◆ ResolveCollisions_()

void AddrManImpl::ResolveCollisions_ ( )
private

Definition at line 937 of file addrman.cpp.

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

◆ Select()

std::pair< CAddress, NodeSeconds > AddrManImpl::Select ( bool  newOnly) const

Definition at line 1221 of file addrman.cpp.

Here is the call graph for this function:

◆ Select_()

std::pair< CAddress, NodeSeconds > AddrManImpl::Select_ ( bool  newOnly) const
private

Definition at line 766 of file addrman.cpp.

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

◆ SelectTriedCollision()

std::pair< CAddress, NodeSeconds > AddrManImpl::SelectTriedCollision ( )

Definition at line 1213 of file addrman.cpp.

Here is the call graph for this function:

◆ SelectTriedCollision_()

std::pair< CAddress, NodeSeconds > AddrManImpl::SelectTriedCollision_ ( )
private

Definition at line 1018 of file addrman.cpp.

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

◆ Serialize()

template<typename Stream >
void AddrManImpl::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 ABC 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 ABC 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 143 of file addrman.cpp.

Here is the call graph for this function:

◆ SetServices()

void AddrManImpl::SetServices ( const CService addr,
ServiceFlags  nServices 
)

Definition at line 1246 of file addrman.cpp.

Here is the call graph for this function:

◆ SetServices_()

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

Definition at line 921 of file addrman.cpp.

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

◆ size()

size_t AddrManImpl::size ( ) const

Definition at line 1174 of file addrman.cpp.

◆ SwapRandom()

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

Swap two elements in vRandom.

Definition at line 466 of file addrman.cpp.

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

◆ Unserialize()

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

Definition at line 249 of file addrman.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ AddrManCorrupted

friend class AddrManCorrupted
friend

Definition at line 157 of file addrman_impl.h.

◆ AddrManTest

friend class AddrManTest
friend

Definition at line 156 of file addrman_impl.h.

Member Data Documentation

◆ cs

Mutex AddrManImpl::cs
mutableprivate

A mutex to protect the inner data structures.

Definition at line 161 of file addrman_impl.h.

◆ deterministic

bool AddrManImpl::deterministic = false
private

Use deterministic bucket selection and inner loops randomization.

For testing purpose only.

Definition at line 255 of file addrman_impl.h.

◆ FILE_FORMAT

constexpr Format AddrManImpl::FILE_FORMAT = Format::V4_MULTIPORT
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 188 of file addrman_impl.h.

◆ INCOMPATIBILITY_BASE

constexpr uint8_t AddrManImpl::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 196 of file addrman_impl.h.

◆ insecure_rand

FastRandomContext AddrManImpl::insecure_rand
mutableprivate

Source of random numbers for randomization in inner loops.

Definition at line 164 of file addrman_impl.h.

◆ m_asmap

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

Definition at line 251 of file addrman_impl.h.

◆ m_consistency_check_ratio

const int32_t AddrManImpl::m_consistency_check_ratio
private

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

Definition at line 235 of file addrman_impl.h.

◆ m_tried_collisions

std::set<int> AddrManImpl::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 229 of file addrman_impl.h.

◆ nKey

uint256 AddrManImpl::nKey
private

secret key to randomize bucket select with

Definition at line 167 of file addrman_impl.h.


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