pub async fn init<S, C>(
config: Config,
inbound_service: S,
latest_chain_tip: C,
user_agent: String,
) -> (Buffer<BoxService<Request, Response, BoxError>, Request>, Arc<Mutex<AddressBook>>)
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.
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.
§Panics
If config.config.peerset_initial_target_size
is zero.
(zebra-network expects to be able to connect to at least one peer.)