Router

Struct Router 

Source
pub struct Router<N: Network>(/* private fields */);
Expand description

The router keeps track of connected and connecting peers. The actual network communication happens in Inbound/Outbound, which is implemented by Validator, Prover, and Client.

Implementations§

Source§

impl<N: Network> Router<N>

Source

pub async fn handshake<'a>( &'a self, peer_addr: SocketAddr, stream: &'a mut TcpStream, peer_side: ConnectionSide, genesis_header: Header<N>, restrictions_id: Field<N>, ) -> Result<Option<ChallengeRequest<N>>>

Executes the handshake protocol.

Source§

impl<N: Network> Router<N>

Source

pub fn send_ping( &self, peer_ip: SocketAddr, block_locators: Option<BlockLocators<N>>, ) -> bool

Sends a “Ping” message to the given peer.

Returns false if the peer does not exist or disconnected.

Source

pub fn send( &self, peer_ip: SocketAddr, message: Message<N>, ) -> Option<Receiver<Result<()>>>

Sends the given message to specified peer.

This function returns as soon as the message is queued to be sent, without waiting for the actual delivery; instead, the caller is provided with a oneshot::Receiver which can be used to determine when and whether the message has been delivered.

This returns None, if the peer does not exist or disconnected.

Source§

impl<N: Network> Router<N>

Source

pub async fn new( node_ip: SocketAddr, node_type: NodeType, account: Account<N>, ledger: Arc<dyn LedgerService<N>>, trusted_peers: &[SocketAddr], max_peers: u16, rotate_external_peers: bool, allow_external_peers: bool, storage_mode: StorageMode, is_dev: bool, ) -> Result<Self>

Initializes a new Router instance.

Source§

impl<N: Network> Router<N>

Source

pub fn is_valid_message_version(&self, message_version: u32) -> bool

Returns true if the message version is valid.

Source

pub fn node_type(&self) -> NodeType

Returns the node type.

Source

pub fn private_key(&self) -> &PrivateKey<N>

Returns the account private key of the node.

Source

pub fn view_key(&self) -> &ViewKey<N>

Returns the account view key of the node.

Source

pub fn address(&self) -> Address<N>

Returns the account address of the node.

Source

pub fn cache(&self) -> &Cache<N>

Returns a reference to the cache.

Source

pub fn rotate_external_peers(&self) -> bool

Returns true if the node is periodically evicting more external peers.

Source

pub fn allow_external_peers(&self) -> bool

Returns true if the node is engaging in P2P gossip to request more peers.

Source

pub fn resolve_to_listener( &self, connected_addr: SocketAddr, ) -> Option<SocketAddr>

Returns the listener IP address from the (ambiguous) peer address.

Source

pub fn connected_metrics(&self) -> Vec<(SocketAddr, NodeType)>

Returns the list of metrics for the connected peers.

Source

pub fn update_last_seen_for_connected_peer(&self, peer_ip: SocketAddr)

Source

pub fn spawn<T: Future<Output = ()> + Send + 'static>(&self, future: T)

Spawns a task with the given future; it should only be used for long-running tasks.

Source

pub async fn shut_down(&self)

Shuts down the router.

Trait Implementations§

Source§

impl<N: Clone + Network> Clone for Router<N>

Source§

fn clone(&self) -> Router<N>

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

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

Performs copy-assignment from source. Read more
Source§

impl<N: Network> CommunicationService for Router<N>

Source§

type Message = Message<N>

The message type.

Source§

fn prepare_block_request(start_height: u32, end_height: u32) -> Self::Message

Prepares a block request to be sent.

Source§

fn send<'life0, 'async_trait>( &'life0 self, peer_ip: SocketAddr, message: Self::Message, ) -> Pin<Box<dyn Future<Output = Option<Receiver<Result<()>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Sends the given message to specified peer.

This function returns as soon as the message is queued to be sent, without waiting for the actual delivery; instead, the caller is provided with a [oneshot::Receiver] which can be used to determine when and whether the message has been delivered.

Source§

impl<N: Network> Deref for Router<N>

Source§

type Target = Arc<InnerRouter<N>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<N: Network> P2P for Router<N>

Source§

fn tcp(&self) -> &Tcp

Returns a reference to the TCP instance.

Source§

impl<N: Network> PeerPoolHandling<N> for Router<N>

Source§

const MAXIMUM_POOL_SIZE: usize = 10_000usize

The maximum number of peers permitted to be stored in the peer pool.
Source§

const OWNER: &str = "[Router]"

Source§

const PEER_SLASHING_COUNT: usize = 200usize

The number of candidate peers to be removed from the pool once MAXIMUM_POOL_SIZE is reached. It must be lower than MAXIMUM_POOL_SIZE.
Source§

fn peer_pool(&self) -> &RwLock<HashMap<SocketAddr, Peer<N>>>

Source§

fn resolver(&self) -> &RwLock<Resolver<N>>

Source§

fn is_dev(&self) -> bool

Returns true if the owning node is in development mode.
Source§

fn local_ip(&self) -> SocketAddr

Returns the listener address of this node.
Source§

fn is_local_ip(&self, addr: SocketAddr) -> bool

Returns true if the given IP is this node.
Source§

fn is_valid_peer_ip(&self, ip: SocketAddr) -> bool

Returns true if the given IP is not this node, is not a bogon address, and is not unspecified.
Source§

fn max_connected_peers(&self) -> usize

Returns the maximum number of connected peers.
Source§

fn check_connection_attempt(&self, listener_addr: SocketAddr) -> Result<bool>

Ensure we are allowed to connect to the given listener address of a peer. Read more
Source§

fn connect(&self, listener_addr: SocketAddr) -> Option<JoinHandle<bool>>

Attempts to connect to the given peer’s listener address. Read more
Source§

fn disconnect(&self, listener_addr: SocketAddr) -> JoinHandle<bool>

Disconnects from the given peer IP, if the peer is connected. The returned boolean indicates whether the peer was actually disconnected from, or if this was a noop.
Source§

fn downgrade_peer_to_candidate(&self, listener_addr: SocketAddr)

Downgrades a connected peer to candidate status.
Source§

fn insert_candidate_peers(&self, listener_addrs: Vec<(SocketAddr, Option<u32>)>)

Adds new candidate peers to the peer pool, ensuring their validity and following the limit on the number of peers in the pool. The listener addresses may be paired with the last known block height of the associated peer.
Source§

fn remove_peer(&self, listener_addr: SocketAddr)

Completely removes an entry from the peer pool.
Source§

fn resolve_to_ambiguous(&self, listener_addr: SocketAddr) -> Option<SocketAddr>

Returns the connected peer address from the listener IP address.
Source§

fn resolve_to_aleo_addr(&self, listener_addr: SocketAddr) -> Option<Address<N>>

Returns the connected peer aleo address from the listener IP address.
Source§

fn is_connecting(&self, listener_addr: SocketAddr) -> bool

Returns true if the node is connecting to the given peer’s listener address.
Source§

fn is_connected(&self, listener_addr: SocketAddr) -> bool

Returns true if the node is connected to the given peer listener address.
Source§

fn is_trusted(&self, listener_addr: SocketAddr) -> bool

Returns true if the given listener address is trusted.
Source§

fn number_of_peers(&self) -> usize

Returns the number of all peers.
Source§

fn number_of_connected_peers(&self) -> usize

Returns the number of connected peers.
Source§

fn number_of_connecting_peers(&self) -> usize

Returns the number of connecting peers.
Source§

fn number_of_candidate_peers(&self) -> usize

Returns the number of candidate peers.
Source§

fn get_connected_peer( &self, listener_addr: SocketAddr, ) -> Option<ConnectedPeer<N>>

Returns the connected peer given the peer IP, if it exists.
Source§

fn update_connected_peer<F: FnMut(&mut ConnectedPeer<N>)>( &self, listener_addr: &SocketAddr, update_fn: F, ) -> bool

Updates the connected peer - if it exists - given the peer IP and a closure. The returned status indicates whether the update was successful, i.e. the peer had existed.
Source§

fn get_peers(&self) -> Vec<Peer<N>>

Returns the list of all peers (connected, connecting, and candidate).
Source§

fn get_connected_peers(&self) -> Vec<ConnectedPeer<N>>

Returns all connected peers.
Source§

fn get_best_connected_peers( &self, max_entries: Option<usize>, ) -> Vec<ConnectedPeer<N>>

Returns an optionally bounded list of all connected peers sorted by their block height (highest first) and failure count (lowest first).
Source§

fn filter_connected_peers<P: FnMut(&ConnectedPeer<N>) -> bool>( &self, predicate: P, ) -> Vec<ConnectedPeer<N>>

Returns all connected peers that satisify the given predicate.
Source§

fn connected_peers(&self) -> Vec<SocketAddr>

Returns the list of connected peers.
Source§

fn trusted_peers(&self) -> Vec<SocketAddr>

Returns the list of trusted peers.
Source§

fn get_candidate_peers(&self) -> Vec<CandidatePeer>

Returns the list of candidate peers.
Source§

fn unconnected_trusted_peers(&self) -> HashSet<SocketAddr>

Returns the list of unconnected trusted peers.
Source§

fn load_cached_peers( storage_mode: &StorageMode, filename: &str, ) -> Result<Vec<SocketAddr>>

Loads any previously cached peer addresses so they can be introduced as initial candidate peers to connect to.
Source§

fn save_best_peers( &self, storage_mode: &StorageMode, filename: &str, max_entries: Option<usize>, ) -> Result<()>

Preserve the peers who have the greatest known block heights, and the lowest number of registered network failures.
Source§

fn add_connecting_peer(&self, listener_addr: SocketAddr) -> bool

Source§

fn ip_ban_peer(&self, listener_addr: SocketAddr, reason: Option<&str>)

Temporarily IP-ban and disconnect from the peer with the given listener address and an optional reason for the ban. This also removes the peer from the candidate pool.
Source§

fn is_ip_banned(&self, ip: IpAddr) -> bool

Check whether the given IP address is currently banned.
Source§

fn update_ip_ban(&self, ip: IpAddr)

Insert or update a banned IP.
Source§

impl<N: Network> Writing for Router<N>

Source§

fn codec(&self, _addr: SocketAddr, _side: ConnectionSide) -> Self::Codec

Creates an [Encoder] used to write the outbound messages to the target stream. The side parameter indicates the connection side from the node’s perspective.

Source§

type Codec = MessageCodec<N>

The user-supplied Encoder used to write outbound messages to the target stream.
Source§

type Message = Message<N>

The type of the outbound messages; unless their serialization is expensive and the message is broadcasted (in which case it would get serialized multiple times), serialization should be done in the implementation of Self::Codec.
Source§

fn message_queue_depth(&self) -> usize

The depth of per-connection queues used to send outbound messages; the greater it is, the more outbound messages the node can enqueue. Setting it to a large value is not recommended, as doing it might obscure potential issues with your implementation (like slow serialization) or network. Read more
Source§

fn enable_writing<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: Sync + 'async_trait,

Prepares the node to send messages.
Source§

fn unicast( &self, addr: SocketAddr, message: Self::Message, ) -> Result<Receiver<Result<(), Error>>, Error>

Sends the provided message to the specified SocketAddr. Returns as soon as the message is queued to be sent, without waiting for the actual delivery; instead, the caller is provided with a oneshot::Receiver which can be used to determine when and whether the message has been delivered. Read more
Source§

fn broadcast(&self, message: Self::Message) -> Result<(), Error>
where Self::Message: Clone,

Broadcasts the provided message to all connected peers. Returns as soon as the message is queued to be sent to all the peers, without waiting for the actual delivery. This method doesn’t provide the means to check when and if the messages actually get delivered; you can achieve that by calling Writing::unicast for each address returned by [Tcp::connected_addrs]. Read more

Auto Trait Implementations§

§

impl<N> Freeze for Router<N>

§

impl<N> !RefUnwindSafe for Router<N>

§

impl<N> Send for Router<N>

§

impl<N> Sync for Router<N>

§

impl<N> Unpin for Router<N>

§

impl<N> !UnwindSafe for Router<N>

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, dest: *mut u8)

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> ErasedDestructor for T
where T: 'static,