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<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, is_dev: bool, ) -> Result<Self>

Initializes a new Router instance.

Source§

impl<N: Network> Router<N>

Source

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

Attempts to connect to the given peer IP.

Returns None if we are already connected to the peer or cannot connect. Otherwise, it returns a handle to the tokio tasks that sets up the connection.

Source

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

Disconnects from the given peer IP, if the peer is connected.

Source

pub fn local_ip(&self) -> SocketAddr

Returns the IP address of this node.

Source

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

Returns true if the given IP is this node.

Source

pub 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

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 is_dev(&self) -> bool

Returns true if the node is in development mode.

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 resolve_to_ambiguous( &self, listener_addr: &SocketAddr, ) -> Option<SocketAddr>

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

Source

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

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

Source

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

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

Source

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

Returns true if the given listener address is trusted.

Source

pub fn max_connected_peers(&self) -> usize

Returns the maximum number of connected peers.

Source

pub fn number_of_connected_peers(&self) -> usize

Returns the number of connected peers.

Source

pub fn number_of_candidate_peers(&self) -> usize

Returns the number of candidate peers.

Source

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

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

Source

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

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

Source

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

Returns all connected peers.

Source

pub 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

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

Returns the list of connected peers.

Source

pub fn candidate_peers(&self) -> HashSet<SocketAddr>

Returns the list of candidate peers.

Source

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

Returns the list of unconnected trusted peers.

Source

pub fn bootstrap_peers(&self) -> Vec<SocketAddr>

Returns the list of bootstrap peers.

Source

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

Returns the list of metrics for the connected peers.

Source

pub fn insert_candidate_peers(&self, peers: &[SocketAddr])

Inserts the given peer IPs to the set of candidate peers.

This method skips adding any given peers if the combined size exceeds the threshold, as the peer providing this list could be subverting the protocol.

Source

pub fn update_last_seen_for_connected_peer(&self, peer_ip: SocketAddr)

Source

pub fn remove_connected_peer(&self, peer_ip: SocketAddr)

Removes the connected peer and adds them to the candidate peers.

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> 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> 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,