pub struct NetworkNode {
pub gossipsub: Arc<GossipSubManager>,
pub inference_waiters: InferenceWaiters,
pub active_relay_reservations: Arc<RwLock<HashMap<PeerId, Instant>>>,
pub relay_config: RelayConfig,
/* private fields */
}Expand description
IPFRS network node
Fields§
§gossipsub: Arc<GossipSubManager>In-process GossipSub manager for topic-based pub/sub messaging.
Shared with callers so that external code (e.g. distributed_infer)
can publish messages directly without going through the swarm command
channel. The manager is Arc-wrapped so it can be cloned cheaply.
inference_waiters: InferenceWaitersWaiters for inference responses, keyed by request/session ID.
active_relay_reservations: Arc<RwLock<HashMap<PeerId, Instant>>>Active Circuit Relay v2 reservations, keyed by relay peer ID.
Each entry records the std::time::Instant at which the reservation
was obtained so that expired reservations can be detected and renewed.
relay_config: RelayConfigCircuit Relay v2 configuration.
Implementations§
Source§impl NetworkNode
impl NetworkNode
Sourcepub fn new(config: NetworkConfig) -> Result<Self>
pub fn new(config: NetworkConfig) -> Result<Self>
Create a new network node
Sourcepub fn connected_peers(&self) -> Vec<PeerId>
pub fn connected_peers(&self) -> Vec<PeerId>
Get connected peers
Sourcepub async fn disconnect(&mut self, peer_id: PeerId) -> Result<()>
pub async fn disconnect(&mut self, peer_id: PeerId) -> Result<()>
Disconnect from a peer
Sourcepub async fn find_providers(&mut self, cid: &Cid) -> Result<()>
pub async fn find_providers(&mut self, cid: &Cid) -> Result<()>
Find providers for content in DHT (fire and forget)
Sourcepub async fn find_providers_await(
&mut self,
cid: &Cid,
timeout: Duration,
) -> Result<Vec<PeerId>>
pub async fn find_providers_await( &mut self, cid: &Cid, timeout: Duration, ) -> Result<Vec<PeerId>>
Find providers for content in DHT and wait for results
Queries the Kademlia DHT for providers of the given CID and waits up to
timeout for the first set of results. Returns the provider peer IDs.
Sourcepub async fn fetch_block_from_peer(
&mut self,
peer: &PeerId,
cid: &Cid,
) -> Result<Block>
pub async fn fetch_block_from_peer( &mut self, peer: &PeerId, cid: &Cid, ) -> Result<Block>
Fetch a block from a specific peer via Bitswap
This is a best-effort implementation. If the peer is connected and has the block, it will be returned. Otherwise an error is returned and the caller should try the next provider.
Sourcepub async fn find_node(&mut self, peer_id: PeerId) -> Result<()>
pub async fn find_node(&mut self, peer_id: PeerId) -> Result<()>
Find node (closest peers to a given peer ID) using Kademlia
Sourcepub async fn get_closest_local_peers(&mut self) -> Result<Vec<PeerId>>
pub async fn get_closest_local_peers(&mut self) -> Result<Vec<PeerId>>
Get the k-closest peers to our local peer ID
Sourcepub async fn bootstrap_dht(&mut self) -> Result<()>
pub async fn bootstrap_dht(&mut self) -> Result<()>
Bootstrap the DHT (search for our own peer ID to populate routing table)
Sourcepub fn add_peer_address(
&mut self,
peer_id: PeerId,
addr: Multiaddr,
) -> Result<()>
pub fn add_peer_address( &mut self, peer_id: PeerId, addr: Multiaddr, ) -> Result<()>
Add an address for a peer to the routing table
Sourcepub fn get_routing_table_info(&mut self) -> Result<RoutingTableInfo>
pub fn get_routing_table_info(&mut self) -> Result<RoutingTableInfo>
Get routing table information
Sourcepub fn stats(&self) -> NetworkStats
pub fn stats(&self) -> NetworkStats
Get network statistics
Sourcepub fn take_event_receiver(&mut self) -> Option<Receiver<NetworkEvent>>
pub fn take_event_receiver(&mut self) -> Option<Receiver<NetworkEvent>>
Take the event receiver
Sourcepub fn get_external_addresses(&self) -> Vec<Multiaddr>
pub fn get_external_addresses(&self) -> Vec<Multiaddr>
Get confirmed external addresses
Sourcepub fn is_publicly_reachable(&self) -> bool
pub fn is_publicly_reachable(&self) -> bool
Check if node has public reachability
Sourcepub fn is_connected_to(&self, peer_id: &PeerId) -> bool
pub fn is_connected_to(&self, peer_id: &PeerId) -> bool
Check if connected to a specific peer
Sourcepub fn get_peer_count(&self) -> usize
pub fn get_peer_count(&self) -> usize
Get number of connected peers
Sourcepub async fn connect_to_peers(
&mut self,
addrs: Vec<Multiaddr>,
) -> Vec<Result<()>> ⓘ
pub async fn connect_to_peers( &mut self, addrs: Vec<Multiaddr>, ) -> Vec<Result<()>> ⓘ
Connect to multiple peers in batch
Sourcepub async fn disconnect_all(&mut self) -> Result<()>
pub async fn disconnect_all(&mut self) -> Result<()>
Disconnect from all connected peers
Sourcepub fn update_bandwidth(&self, bytes_sent: u64, bytes_received: u64)
pub fn update_bandwidth(&self, bytes_sent: u64, bytes_received: u64)
Update bandwidth statistics manually (for custom tracking)
Sourcepub fn get_bytes_sent(&self) -> u64
pub fn get_bytes_sent(&self) -> u64
Get total bandwidth sent
Sourcepub fn get_bytes_received(&self) -> u64
pub fn get_bytes_received(&self) -> u64
Get total bandwidth received
Sourcepub fn reset_bandwidth_stats(&self)
pub fn reset_bandwidth_stats(&self)
Reset bandwidth statistics
Sourcepub fn get_network_health(&self) -> NetworkHealthSummary
pub fn get_network_health(&self) -> NetworkHealthSummary
Get network health summary
Sourcepub fn is_healthy(&self) -> bool
pub fn is_healthy(&self) -> bool
Check if node is healthy
Sourcepub fn nat_traversal_metrics(&self) -> NatTraversalMetrics
pub fn nat_traversal_metrics(&self) -> NatTraversalMetrics
Get a snapshot of NAT traversal (hole-punch) metrics.
Sourcepub fn publish_inference_request(
&self,
request: &InferenceRequest,
) -> Result<()>
pub fn publish_inference_request( &self, request: &InferenceRequest, ) -> Result<()>
Publish an InferenceRequest to the GossipSub INFERENCE_REQUEST
topic.
Serialises request as JSON and hands it to the local
GossipSubManager. The manager fan-out to all subscribed peers is
simulated in-process; wire integration is provided by the event loop
once a real GossipSub swarm behaviour is wired in.
§Errors
Returns an error when JSON serialisation fails.
Sourcepub async fn register_inference_waiter(
&self,
request_id: String,
) -> Receiver<InferenceResponse>
pub async fn register_inference_waiter( &self, request_id: String, ) -> Receiver<InferenceResponse>
Register a one-shot waiter that will be resolved when an
InferenceResponse with the given request_id is delivered to this
node via deliver_inference_response.
Returns the receiving half of the oneshot channel. The caller should
wrap the await with tokio::time::timeout to bound the wait.
Sourcepub async fn deliver_inference_response(&self, response: InferenceResponse)
pub async fn deliver_inference_response(&self, response: InferenceResponse)
Deliver an InferenceResponse to any registered waiters for
response.request_id.
This is the counterpart of register_inference_waiter. Typically
called from the event loop when a GossipSub message arrives on the
INFERENCE_RESULT topic.
Sourcepub fn publish_inference_response(
&self,
response: &InferenceResponse,
) -> Result<()>
pub fn publish_inference_response( &self, response: &InferenceResponse, ) -> Result<()>
Publish a local InferenceResponse to the GossipSub
INFERENCE_RESULT topic so remote requesters can collect it.
§Errors
Returns an error when JSON serialisation fails.
Source§impl NetworkNode
impl NetworkNode
Sourcepub async fn reserve_relay(&mut self, relay_peer: PeerId) -> Result<()>
pub async fn reserve_relay(&mut self, relay_peer: PeerId) -> Result<()>
Attempt to obtain a Circuit Relay v2 reservation from relay_peer.
The method dials the relay peer (if not already connected) and records
the reservation in active_relay_reservations. In a full
implementation the swarm’s relay::client::Behaviour would send the
actual reservation request; here we perform the dial and record the
reservation optimistically, returning an error if relay v2 is disabled
in the node’s configuration.
§Errors
Returns an error if:
- Relay v2 is disabled in the node or relay config.
- The maximum number of simultaneous reservations is already reached.
- The swarm command channel is not available (node not started).
Sourcepub fn relay_reservations(&self) -> HashMap<PeerId, Instant>
pub fn relay_reservations(&self) -> HashMap<PeerId, Instant>
Return a snapshot of the currently active relay reservations.
Each entry maps a relay PeerId to the std::time::Instant at
which the reservation was obtained.
Sourcepub fn remove_relay_reservation(&mut self, relay_peer: &PeerId)
pub fn remove_relay_reservation(&mut self, relay_peer: &PeerId)
Remove a relay reservation (e.g., when the relay peer disconnects).
Sourcepub fn prune_expired_relay_reservations(&mut self, max_age: Duration)
pub fn prune_expired_relay_reservations(&mut self, max_age: Duration)
Remove reservations older than max_age.
Auto Trait Implementations§
impl !Freeze for NetworkNode
impl !RefUnwindSafe for NetworkNode
impl !Sync for NetworkNode
impl !UnwindSafe for NetworkNode
impl Send for NetworkNode
impl Unpin for NetworkNode
impl UnsafeUnpin for NetworkNode
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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