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: usizeThe maximum number of entries this address manager can hold
reachable_networks: HashSet<ReachableNetworks>The networks we can reach
Implementations§
Source§impl AddressMan
impl AddressMan
Sourcepub fn new(
max_size: Option<usize>,
reachable_networks: &[ReachableNetworks],
) -> Self
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.
Sourcefn time_since_unix() -> u64
fn time_since_unix() -> u64
Returns the current timestamp since the epoch
Sourcepub fn push_addresses(&mut self, addresses: &[LocalAddress])
pub fn push_addresses(&mut self, addresses: &[LocalAddress])
Add a new address to our list of known address
Sourcefn is_net_reachable(&self, address: &LocalAddress) -> bool
fn is_net_reachable(&self, address: &LocalAddress) -> bool
Check if we can reach this address based on our reachable networks
Sourcefn prune_addresses(&mut self)
fn prune_addresses(&mut self)
Remove addresses that we last heard of, until we are under the limit of addresses to keep.
Sourcefn get_addresses_by_service(&self, service: ServiceFlags) -> Vec<LocalAddress>
fn get_addresses_by_service(&self, service: ServiceFlags) -> Vec<LocalAddress>
Return addresses from the AddressMan filtered by their [ServiceFlags].
Sourcepub fn enough_addresses(&self) -> bool
pub fn enough_addresses(&self) -> bool
Check if we have enough addresses on the address manager.
fn push_if_has_service(&mut self, address: &LocalAddress, service: ServiceFlags)
pub fn get_addresses_to_send(&self) -> Vec<(AddrV2, u64, ServiceFlags, u16)>
fn do_lookup( host: &str, default_port: u16, socks5: Option<SocketAddr>, ) -> Vec<LocalAddress>
pub fn get_seeds_from_dns( seed: &DnsSeed, default_port: u16, socks5: Option<SocketAddr>, ) -> Result<Vec<LocalAddress>, Error>
Sourcepub fn get_address_to_connect(
&mut self,
required_service: ServiceFlags,
feeler: bool,
) -> Option<(usize, LocalAddress)>
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.
pub fn dump_peers(&self, datadir: &str) -> Result<()>
Sourcepub fn dump_utreexo_peers(
&self,
datadir: &str,
peers_id: &[usize],
) -> Result<()>
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
fn get_address_by_service( &self, service: ServiceFlags, ) -> Option<(usize, LocalAddress)>
pub fn start_addr_man(&mut self, datadir: String) -> Vec<LocalAddress>
Sourcepub fn rearrange_buckets(&mut self)
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.
Sourcefn try_with_service(
&self,
service: ServiceFlags,
) -> Option<(usize, LocalAddress)>
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.
fn get_random_address( &self, service: ServiceFlags, ) -> Option<(usize, LocalAddress)>
Sourcepub fn update_set_state(&mut self, idx: usize, state: AddressState) -> &mut Self
pub fn update_set_state(&mut self, idx: usize, state: AddressState) -> &mut Self
Updates the state of an address
Sourcefn add_peer_to_service(&mut self, idx: usize, service: ServiceFlags)
fn add_peer_to_service(&mut self, idx: usize, service: ServiceFlags)
Adds a peer to the list of peers known to have some service
Sourcefn remove_peer_from_service(&mut self, idx: usize, service: ServiceFlags)
fn remove_peer_from_service(&mut self, idx: usize, service: ServiceFlags)
Removes a peer from the list of peers known to have some service
Sourcefn update_peer_for_service(&mut self, id: usize, service: ServiceFlags)
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.
Sourcefn update_peer_services_buckets(&mut self, idx: usize)
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.
Sourcepub fn update_set_service_flag(
&mut self,
idx: usize,
flags: ServiceFlags,
) -> &mut Self
pub fn update_set_service_flag( &mut self, idx: usize, flags: ServiceFlags, ) -> &mut Self
Updates the service flags after we receive a version message
Sourceconst fn get_net_seeds(network: Network) -> &'static str
const fn get_net_seeds(network: Network) -> &'static str
Returns the file path to the seeds file for the given network
Sourcepub(crate) fn add_fixed_addresses(&mut self, network: Network)
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
impl Clone for AddressMan
Source§fn clone(&self) -> AddressMan
fn clone(&self) -> AddressMan
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more