Replace #[async_trait] with ->imp for traits in eth_wire (#6756)

This commit is contained in:
Abner Zheng
2024-02-24 00:45:25 +08:00
committed by GitHub
parent 761c707e41
commit 34402caeee
10 changed files with 28 additions and 33 deletions

1
Cargo.lock generated
View File

@ -6066,7 +6066,6 @@ dependencies = [
"alloy-rlp",
"arbitrary",
"async-stream",
"async-trait",
"bytes",
"derive_more",
"ethers-core",

View File

@ -35,7 +35,6 @@ tokio-stream.workspace = true
pin-project.workspace = true
tracing.workspace = true
snap = "1.0.5"
async-trait.workspace = true
# arbitrary utils
arbitrary = { workspace = true, features = ["derive"], optional = true }

View File

@ -5,7 +5,7 @@ use futures::{Sink, SinkExt};
use reth_codecs::derive_arbitrary;
use reth_ecies::stream::ECIESStream;
use reth_primitives::bytes::{Buf, BufMut};
use std::fmt::Display;
use std::{fmt::Display, future::Future};
use thiserror::Error;
use tokio::io::AsyncWrite;
use tokio_util::codec::{Encoder, Framed};
@ -149,19 +149,17 @@ impl Decodable for DisconnectReason {
/// This trait is meant to allow higher level protocols like `eth` to disconnect from a peer, using
/// lower-level disconnect functions (such as those that exist in the `p2p` protocol) if the
/// underlying stream supports it.
#[async_trait::async_trait]
pub trait CanDisconnect<T>: Sink<T> + Unpin {
/// Disconnects from the underlying stream, using a [`DisconnectReason`] as disconnect
/// information if the stream implements a protocol that can carry the additional disconnect
/// metadata.
async fn disconnect(
fn disconnect(
&mut self,
reason: DisconnectReason,
) -> Result<(), <Self as Sink<T>>::Error>;
) -> impl Future<Output = Result<(), <Self as Sink<T>>::Error>> + Send;
}
// basic impls for things like Framed<TcpStream, etc>
#[async_trait::async_trait]
impl<T, I, U> CanDisconnect<I> for Framed<T, U>
where
T: AsyncWrite + Unpin + Send,
@ -175,7 +173,6 @@ where
}
}
#[async_trait::async_trait]
impl<S> CanDisconnect<bytes::Bytes> for ECIESStream<S>
where
S: AsyncWrite + Unpin + Send,

View File

@ -315,7 +315,6 @@ where
}
}
#[async_trait::async_trait]
impl<S> CanDisconnect<EthMessage> for EthStream<S>
where
S: CanDisconnect<Bytes> + Send,

View File

@ -354,7 +354,6 @@ impl Sink<Bytes> for ProtocolProxy {
}
}
#[async_trait::async_trait]
impl CanDisconnect<Bytes> for ProtocolProxy {
async fn disconnect(
&mut self,

View File

@ -280,7 +280,6 @@ where
}
}
#[async_trait::async_trait]
impl<S, E> CanDisconnect<Bytes> for MuxDemuxStream<S>
where
S: Sink<Bytes, Error = E> + CanDisconnect<Bytes> + Unpin + Send + Sync,
@ -344,7 +343,6 @@ impl Sink<Bytes> for StreamClone {
}
}
#[async_trait::async_trait]
impl CanDisconnect<Bytes> for StreamClone {
async fn disconnect(&mut self, _reason: DisconnectReason) -> Result<(), MuxDemuxError> {
Err(CannotDisconnectP2PStream)

View File

@ -195,7 +195,6 @@ where
}
}
#[async_trait::async_trait]
impl<S> CanDisconnect<Bytes> for P2PStream<S>
where
S: Sink<Bytes, Error = io::Error> + Unpin + Send + Sync,

View File

@ -13,11 +13,10 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use async_trait::async_trait;
use reth_eth_wire::{DisconnectReason, EthVersion, Status};
use reth_primitives::{NodeRecord, PeerId};
use reth_rpc_types::NetworkStatus;
use std::{net::SocketAddr, sync::Arc, time::Instant};
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
pub use error::NetworkError;
pub use reputation::{Reputation, ReputationChangeKind};
@ -32,13 +31,12 @@ pub mod reputation;
pub mod noop;
/// Provides general purpose information about the network.
#[async_trait]
pub trait NetworkInfo: Send + Sync {
/// Returns the [`SocketAddr`] that listens for incoming connections.
fn local_addr(&self) -> SocketAddr;
/// Returns the current status of the network being ran by the local node.
async fn network_status(&self) -> Result<NetworkStatus, NetworkError>;
fn network_status(&self) -> impl Future<Output = Result<NetworkStatus, NetworkError>> + Send;
/// Returns the chain id
fn chain_id(&self) -> u64;
@ -66,7 +64,6 @@ pub trait PeersInfo: Send + Sync {
}
/// Provides an API for managing the peers of the network.
#[async_trait]
pub trait Peers: PeersInfo {
/// Adds a peer to the peer set.
fn add_peer(&self, peer: PeerId, addr: SocketAddr) {
@ -82,31 +79,42 @@ pub trait Peers: PeersInfo {
fn add_peer_kind(&self, peer: PeerId, kind: PeerKind, addr: SocketAddr);
/// Returns the rpc [PeerInfo] for all connected [PeerKind::Trusted] peers.
async fn get_trusted_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
self.get_peers_by_kind(PeerKind::Trusted).await
fn get_trusted_peers(
&self,
) -> impl Future<Output = Result<Vec<PeerInfo>, NetworkError>> + Send {
self.get_peers_by_kind(PeerKind::Trusted)
}
/// Returns the rpc [PeerInfo] for all connected [PeerKind::Basic] peers.
async fn get_basic_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
self.get_peers_by_kind(PeerKind::Basic).await
fn get_basic_peers(&self) -> impl Future<Output = Result<Vec<PeerInfo>, NetworkError>> + Send {
self.get_peers_by_kind(PeerKind::Basic)
}
/// Returns the rpc [PeerInfo] for all connected peers with the given kind.
async fn get_peers_by_kind(&self, kind: PeerKind) -> Result<Vec<PeerInfo>, NetworkError>;
fn get_peers_by_kind(
&self,
kind: PeerKind,
) -> impl Future<Output = Result<Vec<PeerInfo>, NetworkError>> + Send;
/// Returns the rpc [PeerInfo] for all connected peers.
async fn get_all_peers(&self) -> Result<Vec<PeerInfo>, NetworkError>;
fn get_all_peers(&self) -> impl Future<Output = Result<Vec<PeerInfo>, NetworkError>> + Send;
/// Returns the rpc [PeerInfo] for the given peer id.
///
/// Returns `None` if the peer is not connected.
async fn get_peer_by_id(&self, peer_id: PeerId) -> Result<Option<PeerInfo>, NetworkError>;
fn get_peer_by_id(
&self,
peer_id: PeerId,
) -> impl Future<Output = Result<Option<PeerInfo>, NetworkError>> + Send;
/// Returns the rpc [PeerInfo] for the given peers if they are connected.
///
/// Note: This only returns peers that are connected, unconnected peers are ignored but keeping
/// the order in which they were requested.
async fn get_peers_by_id(&self, peer_ids: Vec<PeerId>) -> Result<Vec<PeerInfo>, NetworkError>;
fn get_peers_by_id(
&self,
peer_ids: Vec<PeerId>,
) -> impl Future<Output = Result<Vec<PeerInfo>, NetworkError>> + Send;
/// Removes a peer from the peer set that corresponds to given kind.
fn remove_peer(&self, peer: PeerId, kind: PeerKind);
@ -121,7 +129,10 @@ pub trait Peers: PeersInfo {
fn reputation_change(&self, peer_id: PeerId, kind: ReputationChangeKind);
/// Get the reputation of a peer.
async fn reputation_by_id(&self, peer_id: PeerId) -> Result<Option<Reputation>, NetworkError>;
fn reputation_by_id(
&self,
peer_id: PeerId,
) -> impl Future<Output = Result<Option<Reputation>, NetworkError>> + Send;
}
/// Represents the kind of peer

View File

@ -8,7 +8,6 @@ use crate::{
ReputationChangeKind,
};
use alloy_chains::Chain;
use async_trait::async_trait;
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
use reth_primitives::{NodeRecord, PeerId};
@ -22,7 +21,6 @@ use std::net::{IpAddr, SocketAddr};
#[non_exhaustive]
pub struct NoopNetwork;
#[async_trait]
impl NetworkInfo for NoopNetwork {
fn local_addr(&self) -> SocketAddr {
(IpAddr::from(std::net::Ipv4Addr::UNSPECIFIED), DEFAULT_DISCOVERY_PORT).into()
@ -69,7 +67,6 @@ impl PeersInfo for NoopNetwork {
}
}
#[async_trait]
impl Peers for NoopNetwork {
fn add_peer_kind(&self, _peer: PeerId, _kind: PeerKind, _addr: SocketAddr) {}

View File

@ -2,7 +2,6 @@ use crate::{
config::NetworkMode, discovery::DiscoveryEvent, manager::NetworkEvent, message::PeerRequest,
peers::PeersHandle, protocol::RlpxSubProtocol, swarm::NetworkConnectionState, FetchClient,
};
use async_trait::async_trait;
use parking_lot::Mutex;
use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions};
use reth_interfaces::sync::{NetworkSyncUpdater, SyncState, SyncStateProvider};
@ -226,7 +225,6 @@ impl PeersInfo for NetworkHandle {
}
}
#[async_trait]
impl Peers for NetworkHandle {
/// Sends a message to the [`NetworkManager`](crate::NetworkManager) to add a peer to the known
/// set, with the given kind.
@ -288,7 +286,6 @@ impl Peers for NetworkHandle {
}
}
#[async_trait]
impl NetworkInfo for NetworkHandle {
fn local_addr(&self) -> SocketAddr {
*self.inner.listener_address.lock()