Bitcoin Core  24.99.0
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 (const NetGroupManager &netgroupman, bool deterministic, 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)
 
bool Good (const CService &addr, 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)
 
std::optional< AddressPositionFindAddressEntry (const CAddress &addr) 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

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, 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). Initially set to 1 so that "never" is strictly worse. 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)
 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 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 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...
 
bool 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)
 
std::optional< AddressPositionFindAddressEntry_ (const CAddress &addr) 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...
 
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 NetGroupManagerm_netgroupman
 Reference to the netgroup manager. 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 AddrManDeterministic
 

Detailed Description

Definition at line 102 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 158 of file addrman_impl.h.

Constructor & Destructor Documentation

◆ AddrManImpl()

AddrManImpl::AddrManImpl ( const NetGroupManager netgroupman,
bool  deterministic,
int32_t  consistency_check_ratio 
)

Definition at line 107 of file addrman.cpp.

◆ ~AddrManImpl()

AddrManImpl::~AddrManImpl ( )

Definition at line 125 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 1077 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 677 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 546 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 1095 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 689 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 965 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 980 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 473 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 1138 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 816 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

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

Definition at line 419 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 457 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 404 of file addrman.cpp.

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

◆ FindAddressEntry()

std::optional< AddressPosition > AddrManImpl::FindAddressEntry ( const CAddress addr)

Definition at line 1154 of file addrman.cpp.

Here is the call graph for this function:

◆ FindAddressEntry_()

std::optional< AddressPosition > AddrManImpl::FindAddressEntry_ ( const CAddress addr)
private

Definition at line 942 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 1129 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 778 of file addrman.cpp.

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

◆ Good()

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

Definition at line 1086 of file addrman.cpp.

Here is the call graph for this function:

◆ Good_()

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

Definition at line 623 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 AddrManImpl::GUARDED_BY ( cs  )
mutableprivate

Source of random numbers for randomization in inner loops.

◆ GUARDED_BY() [2/10]

int nIdCount AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

last used nId

Definition at line 181 of file addrman_impl.h.

◆ GUARDED_BY() [3/10]

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

table with information about all nIds

◆ GUARDED_BY() [4/10]

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

find an nId based on its network address and port.

◆ GUARDED_BY() [5/10]

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() [6/10]

int nTried AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

Definition at line 195 of file addrman_impl.h.

◆ GUARDED_BY() [7/10]

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

list of "tried" buckets

◆ GUARDED_BY() [8/10]

int nNew AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

number of (unique) "new" entries

Definition at line 201 of file addrman_impl.h.

◆ GUARDED_BY() [9/10]

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

list of "new" buckets

◆ GUARDED_BY() [10/10]

NodeSeconds m_last_good AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

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

Definition at line 207 of file addrman_impl.h.

◆ MakeTried()

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

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

Definition at line 491 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 1103 of file addrman.cpp.

Here is the call graph for this function:

◆ ResolveCollisions_()

void AddrManImpl::ResolveCollisions_ ( )
private

Definition at line 851 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 1120 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 709 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 1111 of file addrman.cpp.

Here is the call graph for this function:

◆ SelectTriedCollision_()

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

Definition at line 914 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 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 131 of file addrman.cpp.

Here is the call graph for this function:

◆ SetServices()

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

Definition at line 1146 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 835 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 1071 of file addrman.cpp.

◆ SwapRandom()

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

Swap two elements in vRandom.

Definition at line 433 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 230 of file addrman.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ AddrManDeterministic

friend class AddrManDeterministic
friend

Definition at line 145 of file addrman_impl.h.

Member Data Documentation

◆ cs

Mutex AddrManImpl::cs
mutableprivate

A mutex to protect the inner data structures.

Definition at line 149 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 171 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 178 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 213 of file addrman_impl.h.

◆ m_netgroupman

const NetGroupManager& AddrManImpl::m_netgroupman
private

Reference to the netgroup manager.

netgroupman must be constructed before addrman and destructed after.

Definition at line 216 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 210 of file addrman_impl.h.

◆ nKey

uint256 AddrManImpl::nKey
private

secret key to randomize bucket select with

Definition at line 155 of file addrman_impl.h.


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