pub struct AddressBook { /* private fields */ }
Expand description
A database of peer listener addresses, their advertised services, and information on when they were last seen.
§Security
Address book state must be based on outbound connections to peers.
If the address book is updated incorrectly:
- malicious peers can interfere with other peers’
AddressBook
state, or - Zebra can advertise unreachable addresses to its own peers.
§Adding Addresses
The address book should only contain Zcash listener port addresses from peers on the configured network. These addresses can come from:
- DNS seeders
- addresses gossiped by other peers
- the canonical address (
Version.address_from
) provided by each peer, particularly peers on inbound connections.
The remote addresses of inbound connections must not be added to the address book, because they contain ephemeral outbound ports, not listener ports.
Isolated connections must not add addresses or update the address book.
§Updating Address State
Updates to address state must be based on outbound connections to peers.
Updates must not be based on:
- the remote addresses of inbound connections, or
- the canonical address of any connection.
Implementations§
Source§impl AddressBook
impl AddressBook
Sourcepub fn new(
local_listener: SocketAddr,
network: &Network,
max_connections_per_ip: usize,
span: Span,
) -> AddressBook
pub fn new( local_listener: SocketAddr, network: &Network, max_connections_per_ip: usize, span: Span, ) -> AddressBook
Construct an AddressBook
with the given local_listener
on network
.
Uses the supplied tracing::Span
for address book operations.
Sourcepub fn address_metrics_watcher(&self) -> Receiver<AddressMetrics>
pub fn address_metrics_watcher(&self) -> Receiver<AddressMetrics>
Return a watch channel for the address book metrics.
The metrics in the watch channel are only updated when the address book updates, so they can be significantly outdated if Zebra is disconnected or hung.
The current metrics value is marked as seen.
So Receiver::changed
will only return after the next address book update.
Sourcepub fn local_listener_meta_addr(&self, now: DateTime<Utc>) -> MetaAddr
pub fn local_listener_meta_addr(&self, now: DateTime<Utc>) -> MetaAddr
Get the local listener address.
This address contains minimal state, but it is not sanitized.
Sourcepub fn local_listener_socket_addr(&self) -> SocketAddr
pub fn local_listener_socket_addr(&self) -> SocketAddr
Get the local listener SocketAddr
.
Sourcepub fn fresh_get_addr_response(&self) -> Vec<MetaAddr>
pub fn fresh_get_addr_response(&self) -> Vec<MetaAddr>
Get the active addresses in self
in random order with sanitized timestamps,
including our local listener address.
Limited to the number of peer addresses Zebra should give out per GetAddr
request.
Sourcepub fn cacheable(&self, now: DateTime<Utc>) -> Vec<MetaAddr>
pub fn cacheable(&self, now: DateTime<Utc>) -> Vec<MetaAddr>
Get the active addresses in self
, in preferred caching order,
excluding our local listener address.
Sourcepub fn get(&mut self, addr: PeerSocketAddr) -> Option<MetaAddr>
pub fn get(&mut self, addr: PeerSocketAddr) -> Option<MetaAddr>
Look up addr
in the address book, and return its MetaAddr
.
Converts addr
to a canonical address before looking it up.
Sourcepub fn update(&mut self, change: MetaAddrChange) -> Option<MetaAddr>
pub fn update(&mut self, change: MetaAddrChange) -> Option<MetaAddr>
Apply change
to the address book, returning the updated MetaAddr
,
if the change was valid.
§Correctness
All changes should go through update
, so that the address book
only contains valid outbound addresses.
Change addresses must be canonical PeerSocketAddr
s. This makes sure that
each address book entry has a unique IP address.
§Security
This function must apply every attempted, responded, and failed change to the address book. This prevents rapid reconnections to the same peer.
As an exception, this function can ignore all changes for specific
PeerSocketAddr
s. Ignored addresses will never be used to connect to
peers.
Sourcepub fn pending_reconnection_addr(&mut self, addr: PeerSocketAddr) -> bool
pub fn pending_reconnection_addr(&mut self, addr: PeerSocketAddr) -> bool
Returns true if the given PeerSocketAddr
is pending a reconnection
attempt.
Sourcepub fn peers(&self) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
pub fn peers(&self) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
Return an iterator over all peers.
Returns peers in reconnection attempt order, including recently connected peers.
Sourcepub fn reconnection_peers(
&self,
instant_now: Instant,
chrono_now: DateTime<Utc>,
) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
pub fn reconnection_peers( &self, instant_now: Instant, chrono_now: DateTime<Utc>, ) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
Return an iterator over peers that are due for a reconnection attempt, in reconnection attempt order.
Sourcepub fn state_peers(
&self,
state: PeerAddrState,
) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
pub fn state_peers( &self, state: PeerAddrState, ) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
Return an iterator over all the peers in state
,
in reconnection attempt order, including recently connected peers.
Sourcepub fn maybe_connected_peers(
&self,
instant_now: Instant,
chrono_now: DateTime<Utc>,
) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
pub fn maybe_connected_peers( &self, instant_now: Instant, chrono_now: DateTime<Utc>, ) -> impl DoubleEndedIterator<Item = MetaAddr> + '_
Return an iterator over peers that might be connected, in reconnection attempt order.
Trait Implementations§
Source§impl AddressBookPeers for AddressBook
impl AddressBookPeers for AddressBook
Source§impl Clone for AddressBook
impl Clone for AddressBook
Source§fn clone(&self) -> AddressBook
fn clone(&self) -> AddressBook
Clone the addresses, address limit, local listener address, and span.
Cloned address books have a separate metrics struct watch channel, and an empty last address log.
All address books update the same prometheus metrics.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for AddressBook
impl !RefUnwindSafe for AddressBook
impl Send for AddressBook
impl Sync for AddressBook
impl Unpin for AddressBook
impl !UnwindSafe for AddressBook
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.