Skip to main content

NetworkNode

Struct NetworkNode 

Source
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: InferenceWaiters

Waiters 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: RelayConfig

Circuit Relay v2 configuration.

Implementations§

Source§

impl NetworkNode

Source

pub fn new(config: NetworkConfig) -> Result<Self>

Create a new network node

Source

pub async fn start(&mut self) -> Result<()>

Start the network node

Source

pub async fn stop(&mut self) -> Result<()>

Stop the network node

Source

pub fn peer_id(&self) -> PeerId

Get local peer ID

Source

pub fn listeners(&self) -> Vec<String>

Get listening addresses

Source

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

Get connected peers

Source

pub async fn connect(&mut self, addr: Multiaddr) -> Result<()>

Connect to a peer

Source

pub async fn disconnect(&mut self, peer_id: PeerId) -> Result<()>

Disconnect from a peer

Source

pub async fn provide(&mut self, cid: &Cid) -> Result<()>

Announce content to DHT (provide)

Source

pub async fn find_providers(&mut self, cid: &Cid) -> Result<()>

Find providers for content in DHT (fire and forget)

Source

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.

Source

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.

Source

pub async fn find_node(&mut self, peer_id: PeerId) -> Result<()>

Find node (closest peers to a given peer ID) using Kademlia

Source

pub async fn get_closest_local_peers(&mut self) -> Result<Vec<PeerId>>

Get the k-closest peers to our local peer ID

Source

pub async fn bootstrap_dht(&mut self) -> Result<()>

Bootstrap the DHT (search for our own peer ID to populate routing table)

Source

pub fn add_peer_address( &mut self, peer_id: PeerId, addr: Multiaddr, ) -> Result<()>

Add an address for a peer to the routing table

Source

pub fn get_routing_table_info(&mut self) -> Result<RoutingTableInfo>

Get routing table information

Source

pub fn stats(&self) -> NetworkStats

Get network statistics

Source

pub fn take_event_receiver(&mut self) -> Option<Receiver<NetworkEvent>>

Take the event receiver

Source

pub fn get_external_addresses(&self) -> Vec<Multiaddr>

Get confirmed external addresses

Source

pub fn is_publicly_reachable(&self) -> bool

Check if node has public reachability

Source

pub fn is_connected_to(&self, peer_id: &PeerId) -> bool

Check if connected to a specific peer

Source

pub fn get_peer_count(&self) -> usize

Get number of connected peers

Source

pub async fn connect_to_peers( &mut self, addrs: Vec<Multiaddr>, ) -> Vec<Result<()>>

Connect to multiple peers in batch

Source

pub async fn disconnect_all(&mut self) -> Result<()>

Disconnect from all connected peers

Source

pub fn update_bandwidth(&self, bytes_sent: u64, bytes_received: u64)

Update bandwidth statistics manually (for custom tracking)

Source

pub fn get_bytes_sent(&self) -> u64

Get total bandwidth sent

Source

pub fn get_bytes_received(&self) -> u64

Get total bandwidth received

Source

pub fn reset_bandwidth_stats(&self)

Reset bandwidth statistics

Source

pub fn get_network_health(&self) -> NetworkHealthSummary

Get network health summary

Source

pub fn is_healthy(&self) -> bool

Check if node is healthy

Source

pub fn nat_traversal_metrics(&self) -> NatTraversalMetrics

Get a snapshot of NAT traversal (hole-punch) metrics.

Source

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.

Source

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.

Source

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.

Source

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

Source

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).
Source

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.

Source

pub fn remove_relay_reservation(&mut self, relay_peer: &PeerId)

Remove a relay reservation (e.g., when the relay peer disconnects).

Source

pub fn prune_expired_relay_reservations(&mut self, max_age: Duration)

Remove reservations older than max_age.

Auto Trait Implementations§

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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