Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
AddrMan Class Reference

Stochastic address manager. More...

#include <addrman.h>

Inheritance diagram for AddrMan:
[legend]

Public Member Functions

 AddrMan (const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
 
 ~AddrMan ()
 
template<typename Stream >
void Serialize (Stream &s_) const
 
template<typename Stream >
void Unserialize (Stream &s_)
 
size_t Size (std::optional< Network > net=std::nullopt, std::optional< bool > in_new=std::nullopt) const
 Return size information about addrman.
 
bool Add (const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty=0s)
 Attempt to add one or more addresses to addrman's new table.
 
bool Good (const CService &addr, NodeSeconds time=Now< NodeSeconds >())
 Mark an address record as accessible and attempt to move it to addrman's tried table.
 
void Attempt (const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
 Mark an entry as connection attempted to.
 
void ResolveCollisions ()
 See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
 
std::pair< CAddress, NodeSecondsSelectTriedCollision ()
 Randomly select an address in the tried table that another address is attempting to evict.
 
std::pair< CAddress, NodeSecondsSelect (bool new_only=false, std::optional< Network > network=std::nullopt) const
 Choose an address to connect to.
 
std::vector< CAddressGetAddr (size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const
 Return all or many randomly selected addresses, optionally by network.
 
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries (bool from_tried) const
 Returns an information-location pair for all addresses in the selected addrman table.
 
void Connected (const CService &addr, NodeSeconds time=Now< NodeSeconds >())
 We have successfully connected to this peer.
 
void SetServices (const CService &addr, ServiceFlags nServices)
 Update an entry's service bits.
 
std::optional< AddressPositionFindAddressEntry (const CAddress &addr)
 Test-only function Find the address record in AddrMan and return information about its position.
 

Protected Attributes

const std::unique_ptr< AddrManImplm_impl
 

Detailed Description

Stochastic address manager.

Design goals:

  • Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
  • Make sure no (localized) attacker can fill the entire table with his nodes/addresses.

To that end:

  • Addresses are organized into buckets that can each store up to 64 entries.
    • Addresses to which our node has not successfully connected go into 1024 "new" buckets.
      • Based on the address range (/16 for IPv4) of the source of information, or if an asmap is provided, the AS it belongs to (for IPv4/IPv6), 64 buckets are selected at random.
      • The actual bucket is chosen from one of these, based on the range in which the address itself is located.
      • The position in the bucket is chosen based on the full address.
      • One single address can occur in up to 8 different buckets to increase selection chances for addresses that are seen frequently. The chance for increasing this multiplicity decreases exponentially.
      • When adding a new address to an occupied position of a bucket, it will not replace the existing entry unless that address is also stored in another bucket or it doesn't meet one of several quality criteria (see IsTerrible for exact criteria).
    • Addresses of nodes that are known to be accessible go into 256 "tried" buckets.
      • Each address range selects at random 8 of these buckets.
      • The actual bucket is chosen from one of these, based on the full address.
      • When adding a new good address to an occupied position of a bucket, a FEELER connection to the old address is attempted. The old entry is only replaced and moved back to the "new" buckets if this attempt was unsuccessful.
    • Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not be observable by adversaries.
    • Several indexes are kept for high performance. Setting m_consistency_check_ratio with the -checkaddrman configuration option will introduce (expensive) consistency checks for the entire data structure.

Definition at line 87 of file addrman.h.

Constructor & Destructor Documentation

◆ AddrMan()

AddrMan::AddrMan ( const NetGroupManager & netgroupman,
bool deterministic,
int32_t consistency_check_ratio )
explicit

Definition at line 1263 of file addrman.cpp.

◆ ~AddrMan()

AddrMan::~AddrMan ( )
default

Member Function Documentation

◆ Add()

bool AddrMan::Add ( const std::vector< CAddress > & vAddr,
const CNetAddr & source,
std::chrono::seconds time_penalty = 0s )

Attempt to add one or more addresses to addrman's new table.

If an address already exists in addrman, the existing entry may be updated (e.g. adding additional service flags). If the existing entry is in the new table, it may be added to more buckets, improving the probability of selection.

Parameters
[in]vAddrAddress records to attempt to add.
[in]sourceThe address of the node that sent us these addr records.
[in]time_penaltyA "time penalty" to apply to the address record's nTime. If a peer sends us an address record with nTime=n, then we'll add it to our addrman with nTime=(n - time_penalty).
Returns
true if at least one address is successfully added, or added to an additional bucket. Unaffected by updates.

Definition at line 1293 of file addrman.cpp.

Here is the caller graph for this function:

◆ Attempt()

void AddrMan::Attempt ( const CService & addr,
bool fCountFailure,
NodeSeconds time = Now<NodeSeconds>() )

Mark an entry as connection attempted to.

Definition at line 1303 of file addrman.cpp.

Here is the caller graph for this function:

◆ Connected()

void AddrMan::Connected ( const CService & addr,
NodeSeconds time = Now<NodeSeconds>() )

We have successfully connected to this peer.

Calling this function updates the CAddress's nTime, which is used in our IsTerrible() decisions and gossiped to peers. Callers should be careful that updating this information doesn't leak topology information to network spies.

net_processing calls this function when it disconnects from a peer to not leak information about currently connected peers.

Parameters
[in]addrThe address of the peer we were connected to
[in]timeThe time that we were last connected to this peer

Definition at line 1333 of file addrman.cpp.

Here is the caller graph for this function:

◆ FindAddressEntry()

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

Test-only function Find the address record in AddrMan and return information about its position.

Parameters
[in]addrThe address record to look up.
Returns
Information about the address record in AddrMan or nullopt if address is not found.

Definition at line 1343 of file addrman.cpp.

◆ GetAddr()

std::vector< CAddress > AddrMan::GetAddr ( size_t max_addresses,
size_t max_pct,
std::optional< Network > network,
const bool filtered = true ) const

Return all or many randomly selected addresses, optionally by network.

Parameters
[in]max_addressesMaximum number of addresses to return (0 = all).
[in]max_pctMaximum percentage of addresses to return (0 = all).
[in]networkSelect only addresses of this network (nullopt = all).
[in]filteredSelect only addresses that are considered good quality (false = all).
Returns
A vector of randomly selected addresses from vRandom.

Definition at line 1323 of file addrman.cpp.

Here is the caller graph for this function:

◆ GetEntries()

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

Returns an information-location pair for all addresses in the selected addrman table.

If an address appears multiple times in the new table, an information-location pair is returned for each occurrence. Addresses only ever appear once in the tried table.

Parameters
[in]from_triedSelects which table to return entries from.
Returns
A vector consisting of pairs of AddrInfo and AddressPosition.

Definition at line 1328 of file addrman.cpp.

Here is the caller graph for this function:

◆ Good()

bool AddrMan::Good ( const CService & addr,
NodeSeconds time = Now<NodeSeconds>() )

Mark an address record as accessible and attempt to move it to addrman's tried table.

Parameters
[in]addrAddress record to attempt to move to tried table.
[in]timeThe time that we were last connected to this peer.
Returns
true if the address is successfully moved from the new table to the tried table.

Definition at line 1298 of file addrman.cpp.

Here is the caller graph for this function:

◆ ResolveCollisions()

void AddrMan::ResolveCollisions ( )

See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.

Definition at line 1308 of file addrman.cpp.

Here is the caller graph for this function:

◆ Select()

std::pair< CAddress, NodeSeconds > AddrMan::Select ( bool new_only = false,
std::optional< Network > network = std::nullopt ) const

Choose an address to connect to.

Parameters
[in]new_onlyWhether to only select addresses from the new table. Passing true returns an address from the new table or an empty pair. Passing false will return an empty pair or an address from either the new or tried table (it does not guarantee a tried entry).
[in]networkSelect only addresses of this network (nullopt = all). Passing a network may slow down the search.
Returns
CAddress The record for the selected peer. seconds The last time we attempted to connect to that peer.

Definition at line 1318 of file addrman.cpp.

Here is the caller graph for this function:

◆ SelectTriedCollision()

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

Randomly select an address in the tried table that another address is attempting to evict.

Returns
CAddress The record for the selected tried peer. seconds The last time we attempted to connect to that peer.

Definition at line 1313 of file addrman.cpp.

Here is the caller graph for this function:

◆ Serialize()

template<typename Stream >
template void AddrMan::Serialize ( Stream & s_) const

Definition at line 1269 of file addrman.cpp.

◆ SetServices()

void AddrMan::SetServices ( const CService & addr,
ServiceFlags nServices )

Update an entry's service bits.

Definition at line 1338 of file addrman.cpp.

Here is the caller graph for this function:

◆ Size()

size_t AddrMan::Size ( std::optional< Network > net = std::nullopt,
std::optional< bool > in_new = std::nullopt ) const

Return size information about addrman.

Parameters
[in]netSelect addresses only from specified network (nullopt = all)
[in]in_newSelect addresses only from one table (true = new, false = tried, nullopt = both)
Returns
Number of unique addresses that match specified options.

Definition at line 1288 of file addrman.cpp.

Here is the caller graph for this function:

◆ Unserialize()

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

Definition at line 1275 of file addrman.cpp.

Member Data Documentation

◆ m_impl

const std::unique_ptr<AddrManImpl> AddrMan::m_impl
protected

Definition at line 90 of file addrman.h.


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