Struct AddressMan

Source
pub struct AddressMan {
    addresses: HashMap<usize, LocalAddress>,
    good_addresses: Vec<usize>,
    good_peers_by_service: HashMap<ServiceFlags, Vec<usize>>,
    peers_by_service: HashMap<ServiceFlags, Vec<usize>>,
    max_size: usize,
    reachable_networks: HashSet<ReachableNetworks>,
}
Expand description

A module that keeps track of known addresses and chooses addresses that our node can connect

Fields§

§addresses: HashMap<usize, LocalAddress>

A map of all peers we know, mapping the address id to the actual address.

§good_addresses: Vec<usize>

All indexes of “good” addresses

Good peers are those which we think are live, and haven’t banned yet. If we try to connect with one peer, and the connection doesn’t succeed, this peer is assumed to be down and removed from good addresses for some time.

§good_peers_by_service: HashMap<ServiceFlags, Vec<usize>>

A map of a set of good peers indexes by their [ServiceFlags]

We use this to make peer selection, if we are looking for a specific kind of peer (like utreexo or CBF peers)

§peers_by_service: HashMap<ServiceFlags, Vec<usize>>

A map of a set of peers indexes by their [ServiceFlags]

This works similarly to good_peers_by_service. However, we keep all peers here, not only good peers

§max_size: usize

The maximum number of entries this address manager can hold

§reachable_networks: HashSet<ReachableNetworks>

The networks we can reach

Implementations§

Source§

impl AddressMan

Source

pub fn new( max_size: Option<usize>, reachable_networks: &[ReachableNetworks], ) -> Self

Creates a new address manager

max_size is the maximum number of addresses to keep in memory. If None is provided, a default of 50,000 addresses is used.

Source

fn time_since_unix() -> u64

Returns the current timestamp since the epoch

Source

pub fn push_addresses(&mut self, addresses: &[LocalAddress])

Add a new address to our list of known address

Source

fn is_net_reachable(&self, address: &LocalAddress) -> bool

Check if we can reach this address based on our reachable networks

Source

fn prune_addresses(&mut self)

Remove addresses that we last heard of, until we are under the limit of addresses to keep.

Source

fn get_addresses_by_service(&self, service: ServiceFlags) -> Vec<LocalAddress>

Return addresses from the AddressMan filtered by their [ServiceFlags].

Source

pub fn enough_addresses(&self) -> bool

Check if we have enough addresses on the address manager.

Source

fn push_if_has_service(&mut self, address: &LocalAddress, service: ServiceFlags)

Source

pub fn get_addresses_to_send(&self) -> Vec<(AddrV2, u64, ServiceFlags, u16)>

Source

fn do_lookup( host: &str, default_port: u16, socks5: Option<SocketAddr>, ) -> Vec<LocalAddress>

Source

pub fn get_seeds_from_dns( seed: &DnsSeed, default_port: u16, socks5: Option<SocketAddr>, ) -> Result<Vec<LocalAddress>, Error>

Source

pub fn get_address_to_connect( &mut self, required_service: ServiceFlags, feeler: bool, ) -> Option<(usize, LocalAddress)>

Returns a new random address to open a new connection, we try to get addresses with a set of features supported for our peers

If no peers are known with the required service bit, we may return a random peer. Service bits are learned from DNS seeds or peer gossip and may be outdated or inaccurate, so we sometimes try random peers expecting they might implement the service.

Source

pub fn dump_peers(&self, datadir: &str) -> Result<()>

Source

pub fn dump_utreexo_peers( &self, datadir: &str, peers_id: &[usize], ) -> Result<()>

Dumps the connected utreexo peers to a file on dir datadir/anchors.json in json format ` inputs are the directory to save the file and the list of ids of the connected utreexo peers

Source

fn get_address_by_service( &self, service: ServiceFlags, ) -> Option<(usize, LocalAddress)>

Source

pub fn start_addr_man(&mut self, datadir: String) -> Vec<LocalAddress>

Source

pub fn rearrange_buckets(&mut self)

This function moves addresses between buckets, like if the ban time of a peer expired, or if we tried to connect to a peer and it failed in the past, but now it might be online again.

Source

fn try_with_service( &self, service: ServiceFlags, ) -> Option<(usize, LocalAddress)>

Attempt to find one random peer that advertises the required service

If we cannot find a peer that advertises the required service, we return any peer that we have in our list of known peers. Luckily, either we’ll connect to a peer that has this but we didn’t know, or one of those peers will give us useful addresses.

Source

fn get_random_address( &self, service: ServiceFlags, ) -> Option<(usize, LocalAddress)>

Source

pub fn update_set_state(&mut self, idx: usize, state: AddressState) -> &mut Self

Updates the state of an address

Source

fn add_peer_to_service(&mut self, idx: usize, service: ServiceFlags)

Adds a peer to the list of peers known to have some service

Source

fn remove_peer_from_service(&mut self, idx: usize, service: ServiceFlags)

Removes a peer from the list of peers known to have some service

Source

fn update_peer_for_service(&mut self, id: usize, service: ServiceFlags)

Updates the list of peers that have a service

If a peer used to advertise a service, but now it doesn’t, we remove it from the list of peers that have that service. If a peer didn’t advertise a service, but now it does, we add it to the list of peers that have that service.

Source

fn update_peer_services_buckets(&mut self, idx: usize)

Updates peers_by_service buckets with the latest service flags info about a peer

This function is called when we receive a version message from a peer, telling which services it advertises.

We only index for Compact Filters and Utreexo. For NODE_NETWORK and NODE_WITNESS we already filter them out when we add them to the address manager, therefore, all peers in this list is already known for having those. And we don’t care about the rest of the services, like NODE_BLOOM.

Source

pub fn update_set_service_flag( &mut self, idx: usize, flags: ServiceFlags, ) -> &mut Self

Updates the service flags after we receive a version message

Source

const fn get_net_seeds(network: Network) -> &'static str

Returns the file path to the seeds file for the given network

Source

pub(crate) fn add_fixed_addresses(&mut self, network: Network)

Reads the hard-coded addresses from the seeds file and adds them to the address manager

This is a last-resort method to try to connect to a peer, if we don’t have any other addresses to connect to.

Trait Implementations§

Source§

impl Clone for AddressMan

Source§

fn clone(&self) -> AddressMan

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more