use std::future::Future;
use tokio::io::{AsyncRead, AsyncWrite};
use tower::{util::Oneshot, Service};
use zakura_chain::{chain_tip::NoChainTip, parameters::Network};
use crate::{
peer::{self, Client, ConnectedAddr, HandshakeRequest},
peer_set::ActiveConnectionCounter,
BoxError, Config, P2pStack, Request, Response,
};
#[cfg(test)]
mod tests;
pub fn connect_isolated_with_inbound<PeerTransport, InboundService>(
network: &Network,
data_stream: PeerTransport,
user_agent: String,
inbound_service: InboundService,
) -> impl Future<Output = Result<Client, BoxError>>
where
PeerTransport: AsyncRead + AsyncWrite + Unpin + Send + 'static,
InboundService:
Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
InboundService::Future: Send,
{
let config = Config {
network: network.clone(),
p2p_stack: P2pStack::Legacy,
..Config::default()
};
let handshake = peer::Handshake::builder()
.with_config(config)
.with_inbound_service(inbound_service)
.with_user_agent(user_agent)
.with_latest_chain_tip(NoChainTip)
.finish()
.expect("provided mandatory builder parameters");
let connected_addr = ConnectedAddr::new_isolated();
let connection_tracker = ActiveConnectionCounter::new_counter().track_connection();
Oneshot::new(
handshake,
HandshakeRequest {
data_stream,
connected_addr,
connection_tracker,
},
)
}