Skip to content

Commit

Permalink
Remove unnecessary PeerTransport generics
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jan 24, 2022
1 parent aa88bd3 commit 1fe25a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
9 changes: 4 additions & 5 deletions zebra-network/src/peer/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
S::Future: Send,
C: ChainTip + Clone + Send + 'static,
{
handshaker: Handshake<S, TcpStream, C>,
handshaker: Handshake<S, C>,
}

impl<S, C> Clone for Connector<S, C>
Expand All @@ -49,7 +49,7 @@ where
S::Future: Send,
C: ChainTip + Clone + Send + 'static,
{
pub fn new(handshaker: Handshake<S, TcpStream, C>) -> Self {
pub fn new(handshaker: Handshake<S, C>) -> Self {
Connector { handshaker }
}
}
Expand Down Expand Up @@ -87,15 +87,14 @@ where
connection_tracker,
}: OutboundConnectorRequest = req;

let mut hs = self.handshaker.clone();
let hs = self.handshaker.clone();
let connected_addr = ConnectedAddr::new_outbound_direct(addr);
let connector_span = info_span!("connector", peer = ?connected_addr);

async move {
let tcp_stream = TcpStream::connect(addr).await?;
hs.ready().await?;
let client = hs
.call(HandshakeRequest::<TcpStream> {
.oneshot(HandshakeRequest::<TcpStream> {
data_stream: tcp_stream,
connected_addr,
connection_tracker,
Expand Down
38 changes: 11 additions & 27 deletions zebra-network/src/peer/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Initial [`Handshake`s] with Zebra peers over a `PeerTransport`.
use std::{
cmp::min,
collections::HashSet,
fmt,
future::Future,
marker::PhantomData,
net::{IpAddr, Ipv4Addr, SocketAddr},
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -54,12 +55,11 @@ use crate::{
/// To avoid hangs, each handshake (or its connector) should be:
/// - launched in a separate task, and
/// - wrapped in a timeout.
pub struct Handshake<S, PeerTransport, C = NoChainTip>
pub struct Handshake<S, C = NoChainTip>
where
S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
S::Future: Send,
C: ChainTip + Clone + Send + 'static,
PeerTransport: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
config: Config,
user_agent: String,
Expand All @@ -73,16 +73,13 @@ where
nonces: Arc<futures::lock::Mutex<HashSet<Nonce>>>,

parent_span: Span,

_phantom_data: PhantomData<PeerTransport>,
}

impl<S, PeerTransport, C> Clone for Handshake<S, PeerTransport, C>
impl<S, C> Clone for Handshake<S, C>
where
S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
S::Future: Send,
C: ChainTip + Clone + Send + 'static,
PeerTransport: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
fn clone(&self) -> Self {
Self {
Expand All @@ -96,7 +93,6 @@ where
minimum_peer_version: self.minimum_peer_version.clone(),
nonces: self.nonces.clone(),
parent_span: self.parent_span.clone(),
_phantom_data: self._phantom_data,
}
}
}
Expand Down Expand Up @@ -340,12 +336,11 @@ impl fmt::Debug for ConnectedAddr {
}

/// A builder for `Handshake`.
pub struct Builder<S, PeerTransport, C = NoChainTip>
pub struct Builder<S, C = NoChainTip>
where
S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
S::Future: Send,
C: ChainTip + Clone + Send + 'static,
PeerTransport: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
config: Option<Config>,
our_services: Option<PeerServices>,
Expand All @@ -356,16 +351,13 @@ where
address_book_updater: Option<tokio::sync::mpsc::Sender<MetaAddrChange>>,
inv_collector: Option<broadcast::Sender<(InventoryHash, SocketAddr)>>,
latest_chain_tip: C,

_phantom_data: PhantomData<PeerTransport>,
}

impl<S, PeerTransport, C> Builder<S, PeerTransport, C>
impl<S, C> Builder<S, C>
where
S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
S::Future: Send,
C: ChainTip + Clone + Send + 'static,
PeerTransport: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
/// Provide a config. Mandatory.
pub fn with_config(mut self, config: Config) -> Self {
Expand Down Expand Up @@ -425,10 +417,7 @@ where
/// constant over network upgrade activations.
///
/// Use [`NoChainTip`] to explicitly provide no chain tip.
pub fn with_latest_chain_tip<NewC>(
self,
latest_chain_tip: NewC,
) -> Builder<S, PeerTransport, NewC>
pub fn with_latest_chain_tip<NewC>(self, latest_chain_tip: NewC) -> Builder<S, NewC>
where
NewC: ChainTip + Clone + Send + 'static,
{
Expand All @@ -443,7 +432,6 @@ where
user_agent: self.user_agent,
relay: self.relay,
inv_collector: self.inv_collector,
_phantom_data: self._phantom_data,
}
}

Expand All @@ -458,7 +446,7 @@ where
/// Consume this builder and produce a [`Handshake`].
///
/// Returns an error only if any mandatory field was unset.
pub fn finish(self) -> Result<Handshake<S, PeerTransport, C>, &'static str> {
pub fn finish(self) -> Result<Handshake<S, C>, &'static str> {
let config = self.config.ok_or("did not specify config")?;
let inbound_service = self
.inbound_service
Expand Down Expand Up @@ -491,19 +479,17 @@ where
minimum_peer_version,
nonces,
parent_span: Span::current(),
_phantom_data: self._phantom_data,
})
}
}

impl<S, PeerTransport> Handshake<S, PeerTransport, NoChainTip>
impl<S> Handshake<S, NoChainTip>
where
S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
S::Future: Send,
PeerTransport: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{
/// Create a builder that configures a [`Handshake`] service.
pub fn builder() -> Builder<S, PeerTransport, NoChainTip> {
pub fn builder() -> Builder<S, NoChainTip> {
// We don't derive `Default` because the derive inserts a `where S:
// Default` bound even though `Option<S>` implements `Default` even if
// `S` does not.
Expand All @@ -516,7 +502,6 @@ where
address_book_updater: None,
inv_collector: None,
latest_chain_tip: NoChainTip,
_phantom_data: PhantomData::default(),
}
}
}
Expand Down Expand Up @@ -745,8 +730,7 @@ where
pub connection_tracker: ConnectionTracker,
}

impl<S, PeerTransport, C> Service<HandshakeRequest<PeerTransport>>
for Handshake<S, PeerTransport, C>
impl<S, PeerTransport, C> Service<HandshakeRequest<PeerTransport>> for Handshake<S, C>
where
S: Service<Request, Response = Response, Error = BoxError> + Clone + Send + 'static,
S::Future: Send,
Expand Down

0 comments on commit 1fe25a9

Please sign in to comment.