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>
impl<N: Network> Router<N>
Sourcepub 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>>
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>
impl<N: Network> Router<N>
Sourcepub fn send_ping(
&self,
peer_ip: SocketAddr,
block_locators: Option<BlockLocators<N>>,
) -> bool
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.
Sourcepub fn send(
&self,
peer_ip: SocketAddr,
message: Message<N>,
) -> Option<Receiver<Result<()>>>
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>
impl<N: Network> Router<N>
Sourcepub 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>
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>
impl<N: Network> Router<N>
Sourcepub fn connect(&self, peer_ip: SocketAddr) -> Option<JoinHandle<bool>>
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.
Sourcepub fn disconnect(&self, peer_ip: SocketAddr) -> JoinHandle<bool>
pub fn disconnect(&self, peer_ip: SocketAddr) -> JoinHandle<bool>
Disconnects from the given peer IP, if the peer is connected.
Sourcepub fn local_ip(&self) -> SocketAddr
pub fn local_ip(&self) -> SocketAddr
Returns the IP address of this node.
Sourcepub fn is_local_ip(&self, ip: &SocketAddr) -> bool
pub fn is_local_ip(&self, ip: &SocketAddr) -> bool
Returns true
if the given IP is this node.
Sourcepub fn is_valid_peer_ip(&self, ip: &SocketAddr) -> bool
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.
Sourcepub fn is_valid_message_version(&self, message_version: u32) -> bool
pub fn is_valid_message_version(&self, message_version: u32) -> bool
Returns true
if the message version is valid.
Sourcepub fn private_key(&self) -> &PrivateKey<N>
pub fn private_key(&self) -> &PrivateKey<N>
Returns the account private key of the node.
Sourcepub fn rotate_external_peers(&self) -> bool
pub fn rotate_external_peers(&self) -> bool
Returns true
if the node is periodically evicting more external peers.
Sourcepub fn allow_external_peers(&self) -> bool
pub fn allow_external_peers(&self) -> bool
Returns true
if the node is engaging in P2P gossip to request more peers.
Sourcepub fn resolve_to_listener(
&self,
connected_addr: &SocketAddr,
) -> Option<SocketAddr>
pub fn resolve_to_listener( &self, connected_addr: &SocketAddr, ) -> Option<SocketAddr>
Returns the listener IP address from the (ambiguous) peer address.
Sourcepub fn resolve_to_ambiguous(
&self,
listener_addr: &SocketAddr,
) -> Option<SocketAddr>
pub fn resolve_to_ambiguous( &self, listener_addr: &SocketAddr, ) -> Option<SocketAddr>
Returns the (ambiguous) peer address from the listener IP address.
Sourcepub fn is_connecting(&self, listener_addr: &SocketAddr) -> bool
pub fn is_connecting(&self, listener_addr: &SocketAddr) -> bool
Returns true
if the node is connecting to the given peer’s listener address.
Sourcepub fn is_connected(&self, listener_addr: &SocketAddr) -> bool
pub fn is_connected(&self, listener_addr: &SocketAddr) -> bool
Returns true
if the node is connected to the given peer listener address.
Sourcepub fn is_trusted(&self, listener_addr: &SocketAddr) -> bool
pub fn is_trusted(&self, listener_addr: &SocketAddr) -> bool
Returns true
if the given listener address is trusted.
Sourcepub fn max_connected_peers(&self) -> usize
pub fn max_connected_peers(&self) -> usize
Returns the maximum number of connected peers.
Sourcepub fn number_of_connected_peers(&self) -> usize
pub fn number_of_connected_peers(&self) -> usize
Returns the number of connected peers.
Sourcepub fn number_of_candidate_peers(&self) -> usize
pub fn number_of_candidate_peers(&self) -> usize
Returns the number of candidate peers.
Sourcepub fn get_connected_peer(
&self,
listener_addr: &SocketAddr,
) -> Option<ConnectedPeer<N>>
pub fn get_connected_peer( &self, listener_addr: &SocketAddr, ) -> Option<ConnectedPeer<N>>
Returns the connected peer given the peer IP, if it exists.
Sourcepub fn get_peers(&self) -> Vec<Peer<N>>
pub fn get_peers(&self) -> Vec<Peer<N>>
Returns the list of all peers (connected, connecting, and candidate).
Sourcepub fn get_connected_peers(&self) -> Vec<ConnectedPeer<N>>
pub fn get_connected_peers(&self) -> Vec<ConnectedPeer<N>>
Returns all connected peers.
Sourcepub fn filter_connected_peers<P: FnMut(&ConnectedPeer<N>) -> bool>(
&self,
predicate: P,
) -> Vec<ConnectedPeer<N>>
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.
Sourcepub fn connected_peers(&self) -> Vec<SocketAddr>
pub fn connected_peers(&self) -> Vec<SocketAddr>
Returns the list of connected peers.
Sourcepub fn candidate_peers(&self) -> HashSet<SocketAddr>
pub fn candidate_peers(&self) -> HashSet<SocketAddr>
Returns the list of candidate peers.
Sourcepub fn unconnected_trusted_peers(&self) -> HashSet<SocketAddr>
pub fn unconnected_trusted_peers(&self) -> HashSet<SocketAddr>
Returns the list of unconnected trusted peers.
Sourcepub fn bootstrap_peers(&self) -> Vec<SocketAddr>
pub fn bootstrap_peers(&self) -> Vec<SocketAddr>
Returns the list of bootstrap peers.
Sourcepub fn connected_metrics(&self) -> Vec<(SocketAddr, NodeType)>
pub fn connected_metrics(&self) -> Vec<(SocketAddr, NodeType)>
Returns the list of metrics for the connected peers.
Sourcepub fn insert_candidate_peers(&self, peers: &[SocketAddr])
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.
pub fn update_last_seen_for_connected_peer(&self, peer_ip: SocketAddr)
Sourcepub fn remove_connected_peer(&self, peer_ip: SocketAddr)
pub fn remove_connected_peer(&self, peer_ip: SocketAddr)
Removes the connected peer and adds them to the candidate peers.
Trait Implementations§
Source§impl<N: Network> Writing for Router<N>
impl<N: Network> Writing for Router<N>
Source§fn codec(&self, _addr: SocketAddr, _side: ConnectionSide) -> Self::Codec
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>
type Codec = MessageCodec<N>
Encoder
used to write outbound messages to the target stream.Source§type Message = Message<N>
type Message = Message<N>
Self::Codec
.Source§fn message_queue_depth(&self) -> usize
fn message_queue_depth(&self) -> usize
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,
fn enable_writing<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: Sync + 'async_trait,
Source§fn unicast(
&self,
addr: SocketAddr,
message: Self::Message,
) -> Result<Receiver<Result<(), Error>>, Error>
fn unicast( &self, addr: SocketAddr, message: Self::Message, ) -> Result<Receiver<Result<(), Error>>, Error>
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 moreSource§fn broadcast(&self, message: Self::Message) -> Result<(), Error>
fn broadcast(&self, message: Self::Message) -> Result<(), Error>
Writing::unicast
for each address returned by [Tcp::connected_addrs
]. Read moreAuto 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> 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> 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 more