Skip to main content

init

Function init 

Source
pub async fn init<S, C>(
    config: Config,
    inbound_service: S,
    latest_chain_tip: C,
    user_agent: String,
) -> (NetworkService, Arc<Mutex<AddressBook>>, Sender<MisbehaviorReport>, BlockAdvertiser<C>)
where S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + Sync + 'static, S::Future: Send + 'static, C: ChainTip + Clone + Send + Sync + 'static,
Expand description

Initialize a peer set, using a network config, inbound_service, and latest_chain_tip.

The peer set abstracts away peer management to provide a tower::Service representing “the network” that load-balances requests over available peers. The peer set automatically crawls the network to find more peer addresses and opportunistically connects to new peers.

Each peer connection’s message handling is isolated from other connections, unlike in zcashd. The peer connection first attempts to interpret inbound messages as part of a response to a previously-issued request. Otherwise, inbound messages are interpreted as requests and sent to the supplied inbound_service.

Wrapping the inbound_service in tower::load_shed middleware will cause the peer set to shrink when the inbound service is unable to keep up with the volume of inbound requests.

Releasing the last NetworkService handle and unfinished response stops the network’s background tasks and releases its listener and peer connections. Internal polling and crawling do not keep the network alive. Services retained by inbound_service must use NetworkService::background_service rather than retain an owning network handle.

Use NoChainTip to explicitly provide no chain tip receiver.

In addition to returning a service for outbound requests, this method returns a shared AddressBook updated with last-seen timestamps for connected peers. The shared address book should be accessed using a blocking thread, to avoid async task deadlocks.

The fourth result is an independently bounded crate::BlockAdvertiser. Outbound block adverts must use it, not the ordinary buffered service. Its success acknowledges completed local transport writes to selected peers; admission alone and zero-peer fanout are not success. Readiness waits for an eligible connected peer, independently of ordinary request readiness.

§Panics

If config.config.peerset_initial_target_size is zero. (zebra-network expects to be able to connect to at least one peer.)