Bitcoin Core 28.99.0
P2P Digital Currency
Classes | 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]

Classes

struct  NewTriedCount
 

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 (std::optional< Network > net, std::optional< bool > in_new) 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 new_only, const std::unordered_set< Network > &networks) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::vector< CAddressGetAddr (size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
 
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries (bool from_tried) 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...
 
nid_type nIdCount GUARDED_BY (cs)
 last used nId More...
 
std::unordered_map< nid_type, AddrInfo > mapInfo GUARDED_BY (cs)
 table with information about all nIds More...
 
std::unordered_map< CService, nid_type, CServiceHash > mapAddr GUARDED_BY (cs)
 find an nId based on its network address and port. More...
 
std::vector< nid_type > 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)
 
nid_type 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...
 
nid_type 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...
 
std::unordered_map< Network, NewTriedCount > m_network_counts GUARDED_BY (cs)
 Number of entries in addrman per network and new/tried table. More...
 
AddrInfoFind (const CService &addr, nid_type *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
 Find an entry. More...
 
AddrInfoCreate (const CAddress &addr, const CNetAddr &addrSource, nid_type *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 (nid_type 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, nid_type 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 new_only, const std::unordered_set< Network > &networks) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
nid_type GetEntry (bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 Helper to generalize looking up an addrman entry from either table. More...
 
std::vector< CAddressGetAddr_ (size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries_ (bool from_tried) 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)
 
size_t Size_ (std::optional< Network > net, std::optional< bool > in_new) const 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< nid_typem_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 107 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 166 of file addrman_impl.h.

Constructor & Destructor Documentation

◆ AddrManImpl()

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

Definition at line 109 of file addrman.cpp.

◆ ~AddrManImpl()

AddrManImpl::~AddrManImpl ( )

Definition at line 127 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 1177 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 681 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 550 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 1195 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 693 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 1063 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 1247 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 877 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,
nid_type pnId = nullptr 
)
private

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

Definition at line 416 of file addrman.cpp.

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

◆ Delete()

void AddrManImpl::Delete ( nid_type  nId)
private

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

Definition at line 456 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,
nid_type pnId = nullptr 
)
private

Find an entry.

Definition at line 401 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 1263 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 1003 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 bool  filtered = true 
) const

Definition at line 1229 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 bool  filtered = true 
) const
private

Definition at line 812 of file addrman.cpp.

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

◆ GetEntries()

std::vector< std::pair< AddrInfo, AddressPosition > > AddrManImpl::GetEntries ( bool  from_tried) const

Definition at line 1238 of file addrman.cpp.

Here is the call graph for this function:

◆ GetEntries_()

std::vector< std::pair< AddrInfo, AddressPosition > > AddrManImpl::GetEntries_ ( bool  from_tried) const
private

Definition at line 853 of file addrman.cpp.

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

◆ GetEntry()

nid_type AddrManImpl::GetEntry ( bool  use_tried,
size_t  bucket,
size_t  position 
) const
private

Helper to generalize looking up an addrman entry from either table.

Returns
nid_type The nid of the entry. If the addrman position is empty or not found, returns -1.

Definition at line 795 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 1186 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 626 of file addrman.cpp.

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

◆ GUARDED_BY() [1/11]

FastRandomContext insecure_rand AddrManImpl::GUARDED_BY ( cs  )
mutableprivate

Source of random numbers for randomization in inner loops.

◆ GUARDED_BY() [2/11]

nid_type nIdCount AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

last used nId

Definition at line 189 of file addrman_impl.h.

◆ GUARDED_BY() [3/11]

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

table with information about all nIds

◆ GUARDED_BY() [4/11]

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

find an nId based on its network address and port.

◆ GUARDED_BY() [5/11]

std::vector< nid_type > 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/11]

int nTried AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

Definition at line 203 of file addrman_impl.h.

◆ GUARDED_BY() [7/11]

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

list of "tried" buckets

◆ GUARDED_BY() [8/11]

int nNew AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

number of (unique) "new" entries

Definition at line 209 of file addrman_impl.h.

◆ GUARDED_BY() [9/11]

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

list of "new" buckets

◆ GUARDED_BY() [10/11]

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 215 of file addrman_impl.h.

◆ GUARDED_BY() [11/11]

std::unordered_map< Network, NewTriedCount > m_network_counts AddrManImpl::GUARDED_BY ( cs  )
private

Number of entries in addrman per network and new/tried table.

◆ MakeTried()

void AddrManImpl::MakeTried ( AddrInfo info,
nid_type  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 1203 of file addrman.cpp.

Here is the call graph for this function:

◆ ResolveCollisions_()

void AddrManImpl::ResolveCollisions_ ( )
private

Definition at line 912 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  new_only,
const std::unordered_set< Network > &  networks 
) const

Definition at line 1220 of file addrman.cpp.

Here is the call graph for this function:

◆ Select_()

std::pair< CAddress, NodeSeconds > AddrManImpl::Select_ ( bool  new_only,
const std::unordered_set< Network > &  networks 
) const
private

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

Here is the call graph for this function:

◆ SelectTriedCollision_()

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

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

Here is the call graph for this function:

◆ SetServices()

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

Definition at line 1255 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 896 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 ( std::optional< Network net,
std::optional< bool >  in_new 
) const

Definition at line 1168 of file addrman.cpp.

Here is the call graph for this function:

◆ Size_()

size_t AddrManImpl::Size_ ( std::optional< Network net,
std::optional< bool >  in_new 
) const
private

Definition at line 1026 of file addrman.cpp.

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

◆ SwapRandom()

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

Swap two elements in vRandom.

Definition at line 432 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 231 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 153 of file addrman_impl.h.

Member Data Documentation

◆ cs

Mutex AddrManImpl::cs
mutableprivate

A mutex to protect the inner data structures.

Definition at line 157 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 179 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 186 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 221 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 224 of file addrman_impl.h.

◆ m_tried_collisions

std::set<nid_type> 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 218 of file addrman_impl.h.

◆ nKey

uint256 AddrManImpl::nKey
private

secret key to randomize bucket select with

Definition at line 163 of file addrman_impl.h.


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