Bitcoin Core  27.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, std::optional< Network > network) 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...
 
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...
 
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, 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 new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
 
int 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< 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 101 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 160 of file addrman_impl.h.

Constructor & Destructor Documentation

◆ AddrManImpl()

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

Definition at line 111 of file addrman.cpp.

◆ ~AddrManImpl()

AddrManImpl::~AddrManImpl ( )

Definition at line 129 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 1170 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 683 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 552 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 1188 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 695 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 1041 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 1056 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 475 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 1240 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 870 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 418 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 458 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 403 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 1256 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 996 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 1222 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 808 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 1231 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 846 of file addrman.cpp.

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

◆ GetEntry()

int 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
int The nid of the entry. If the addrman position is empty or not found, returns -1.

Definition at line 791 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 1179 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 628 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]

int nIdCount AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

last used nId

Definition at line 183 of file addrman_impl.h.

◆ GUARDED_BY() [3/11]

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

table with information about all nIds

◆ GUARDED_BY() [4/11]

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/11]

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/11]

int nTried AddrManImpl::GUARDED_BY ( cs  )
inlineprivate

Definition at line 197 of file addrman_impl.h.

◆ GUARDED_BY() [7/11]

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

◆ GUARDED_BY() [9/11]

int 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 209 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,
int  nId 
)
private

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

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

Here is the call graph for this function:

◆ ResolveCollisions_()

void AddrManImpl::ResolveCollisions_ ( )
private

Definition at line 905 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,
std::optional< Network network 
) const

Definition at line 1213 of file addrman.cpp.

Here is the call graph for this function:

◆ Select_()

std::pair< CAddress, NodeSeconds > AddrManImpl::Select_ ( bool  new_only,
std::optional< Network network 
) const
private

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

Here is the call graph for this function:

◆ SelectTriedCollision_()

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

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

Here is the call graph for this function:

◆ SetServices()

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

Definition at line 1248 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 889 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 1161 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 1019 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 434 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 147 of file addrman_impl.h.

Member Data Documentation

◆ cs

Mutex AddrManImpl::cs
mutableprivate

A mutex to protect the inner data structures.

Definition at line 151 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 173 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 180 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 215 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 218 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 212 of file addrman_impl.h.

◆ nKey

uint256 AddrManImpl::nKey
private

secret key to randomize bucket select with

Definition at line 157 of file addrman_impl.h.


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