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
AddrManImpl Class Reference

#include <addrman_impl.h>

Collaboration diagram for AddrManImpl:
[legend]

Public Member Functions

 AddrManImpl (std::vector< bool > &&asmap, 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, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void Good (const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void Attempt (const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
void ResolveCollisions () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::pair< CAddress, int64_t > SelectTriedCollision () EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::pair< CAddress, int64_t > Select (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, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
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, AddrInfo > 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...
 
AddrInfoFind (const CNetAddr &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...
 
void Good_ (const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
bool Add_ (const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void Attempt_ (const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::pair< CAddress, int64_t > Select_ (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, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void SetServices_ (const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
 
void ResolveCollisions_ () EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::pair< CAddress, int64_t > SelectTriedCollision_ () EXCLUSIVE_LOCKS_REQUIRED(cs)
 
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...
 

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 AddrManTest
 
class AddrManDeterministic
 

Detailed Description

Definition at line 99 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

Definition at line 155 of file addrman_impl.h.

Constructor & Destructor Documentation

◆ AddrManImpl()

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

Definition at line 106 of file addrman.cpp.

◆ ~AddrManImpl()

AddrManImpl::~AddrManImpl ( )

Definition at line 124 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,
int64_t  nTimePenalty 
)

Definition at line 1027 of file addrman.cpp.

Here is the call graph for this function:

◆ Add_()

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

Definition at line 599 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,
int64_t  nTime 
)

Definition at line 1049 of file addrman.cpp.

Here is the call graph for this function:

◆ Attempt_()

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

Definition at line 674 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 918 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,
int64_t  nTime 
)

Definition at line 1092 of file addrman.cpp.

Here is the call graph for this function:

◆ Connected_()

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

Definition at line 787 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 CNetAddr 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:

◆ ForceCheckAddrman()

int AddrManImpl::ForceCheckAddrman ( ) const
private

Perform consistency check, regardless of m_consistency_check_ratio.

Returns
an error code or zero.

Definition at line 933 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 1083 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 749 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 1108 of file addrman.cpp.

◆ Good()

void AddrManImpl::Good ( const CService addr,
int64_t  nTime 
)

Definition at line 1041 of file addrman.cpp.

Here is the call graph for this function:

◆ Good_()

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

Definition at line 543 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 177 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<CNetAddr, int, CNetAddrHash> mapAddr AddrManImpl::GUARDED_BY ( cs  )
private

find an nId based on its network address

◆ 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 191 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 197 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]

int64_t nLastGood 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 203 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 490 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 1057 of file addrman.cpp.

Here is the call graph for this function:

◆ ResolveCollisions_()

void AddrManImpl::ResolveCollisions_ ( )
private

Definition at line 829 of file addrman.cpp.

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

◆ Select()

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

Definition at line 1074 of file addrman.cpp.

Here is the call graph for this function:

◆ Select_()

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

Definition at line 698 of file addrman.cpp.

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

◆ SelectTriedCollision()

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

Definition at line 1065 of file addrman.cpp.

Here is the call graph for this function:

◆ SelectTriedCollision_()

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

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

Here is the call graph for this function:

◆ SetServices()

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

Definition at line 1100 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 809 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 1021 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 233 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 142 of file addrman_impl.h.

◆ AddrManTest

friend class AddrManTest
friend

Definition at line 141 of file addrman_impl.h.

Member Data Documentation

◆ cs

Mutex AddrManImpl::cs
mutableprivate

A mutex to protect the inner data structures.

Definition at line 146 of file addrman_impl.h.

◆ FILE_FORMAT

constexpr Format AddrManImpl::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 167 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 174 of file addrman_impl.h.

◆ m_asmap

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

Definition at line 225 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 209 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 203 of file addrman_impl.h.

◆ nKey

uint256 AddrManImpl::nKey
private

secret key to randomize bucket select with

Definition at line 152 of file addrman_impl.h.


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