diff --git a/Cargo.lock b/Cargo.lock index 15070dd47..245c1d06d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7560,6 +7560,7 @@ dependencies = [ "alloy-primitives", "alloy-rpc-types-admin", "auto_impl", + "derive_more", "enr", "futures", "reth-eth-wire-types", @@ -7609,7 +7610,6 @@ dependencies = [ name = "reth-network-types" version = "1.0.3" dependencies = [ - "derive_more", "humantime-serde", "reth-ethereum-forks", "reth-net-banlist", @@ -7617,7 +7617,6 @@ dependencies = [ "reth-network-peers", "serde", "serde_json", - "tokio", "tracing", ] diff --git a/bin/reth/src/lib.rs b/bin/reth/src/lib.rs index d207e2ed5..801ce2666 100644 --- a/bin/reth/src/lib.rs +++ b/bin/reth/src/lib.rs @@ -127,7 +127,9 @@ pub mod tasks { /// Re-exported from `reth_network`. pub mod network { pub use reth_network::*; - pub use reth_network_api::{noop, NetworkInfo, Peers, PeersHandleProvider, PeersInfo}; + pub use reth_network_api::{ + noop, test_utils::PeersHandleProvider, NetworkInfo, Peers, PeersInfo, + }; } /// Re-exported from `reth_transaction_pool`. diff --git a/crates/net/network-api/Cargo.toml b/crates/net/network-api/Cargo.toml index cc8318b35..b516d467a 100644 --- a/crates/net/network-api/Cargo.toml +++ b/crates/net/network-api/Cargo.toml @@ -33,6 +33,7 @@ thiserror.workspace = true serde = { workspace = true, features = ["derive"], optional = true } tokio = { workspace = true, features = ["sync"] } auto_impl.workspace = true +derive_more.workspace = true [features] default = ["serde"] diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs index b3fd06afd..7c7c8084b 100644 --- a/crates/net/network-api/src/lib.rs +++ b/crates/net/network-api/src/lib.rs @@ -18,10 +18,11 @@ pub mod downloaders; pub mod error; /// Implementation of network traits for that does nothing. pub mod noop; +pub mod test_utils; pub use alloy_rpc_types_admin::EthProtocolInfo; pub use reth_network_p2p::BlockClient; -pub use reth_network_types::{PeerKind, PeersHandle, Reputation, ReputationChangeKind}; +pub use reth_network_types::{PeerKind, Reputation, ReputationChangeKind}; pub use downloaders::BlockDownloaderProvider; pub use error::NetworkError; @@ -162,15 +163,6 @@ pub trait Peers: PeersInfo { ) -> impl Future, NetworkError>> + Send; } -/// Provides an API for managing the peers of the network. -#[auto_impl::auto_impl(&, Arc)] -pub trait PeersHandleProvider { - /// Returns the [`PeersHandle`] that can be cloned and shared. - /// - /// The [`PeersHandle`] can be used to interact with the network's peer set. - fn peers_handle(&self) -> &PeersHandle; -} - /// Info about an active peer session. #[derive(Debug, Clone)] pub struct PeerInfo { diff --git a/crates/net/network-api/src/test_utils/mod.rs b/crates/net/network-api/src/test_utils/mod.rs new file mode 100644 index 000000000..a3108a476 --- /dev/null +++ b/crates/net/network-api/src/test_utils/mod.rs @@ -0,0 +1,5 @@ +//! API for integration testing network components. + +pub mod peers_manager; + +pub use peers_manager::{PeerCommand, PeersHandle, PeersHandleProvider}; diff --git a/crates/net/network-types/src/peers/handle.rs b/crates/net/network-api/src/test_utils/peers_manager.rs similarity index 59% rename from crates/net/network-types/src/peers/handle.rs rename to crates/net/network-api/src/test_utils/peers_manager.rs index 1503fd1bd..44d9ba024 100644 --- a/crates/net/network-types/src/peers/handle.rs +++ b/crates/net/network-api/src/test_utils/peers_manager.rs @@ -1,12 +1,21 @@ -//! Async peer handle. +//! Interaction with `reth_network::PeersManager`, for integration testing. Otherwise +//! `reth_network::NetworkManager` manages `reth_network::PeersManager`. use std::net::SocketAddr; use derive_more::Constructor; use reth_network_peers::{NodeRecord, PeerId}; +use reth_network_types::{Peer, ReputationChangeKind}; use tokio::sync::{mpsc, oneshot}; -use crate::{Peer, PeerCommand, ReputationChangeKind}; +/// Provides an API for managing the peers of the network. +#[auto_impl::auto_impl(&, Arc)] +pub trait PeersHandleProvider { + /// Returns the [`PeersHandle`] that can be cloned and shared. + /// + /// The [`PeersHandle`] can be used to interact with the network's peer set. + fn peers_handle(&self) -> &PeersHandle; +} /// A communication channel to the `PeersManager` to apply manual changes to the peer set. #[derive(Clone, Debug, Constructor)] @@ -53,3 +62,20 @@ impl PeersHandle { rx.await.unwrap_or_default() } } + +/// Commands the `PeersManager` listens for. +#[derive(Debug)] +pub enum PeerCommand { + /// Command for manually add + Add(PeerId, SocketAddr), + /// Remove a peer from the set + /// + /// If currently connected this will disconnect the session + Remove(PeerId), + /// Apply a reputation change to the given peer. + ReputationChange(PeerId, ReputationChangeKind), + /// Get information about a peer + GetPeer(PeerId, oneshot::Sender>), + /// Get node information on all peers + GetPeers(oneshot::Sender>), +} diff --git a/crates/net/network-types/Cargo.toml b/crates/net/network-types/Cargo.toml index 6db629207..23d169101 100644 --- a/crates/net/network-types/Cargo.toml +++ b/crates/net/network-types/Cargo.toml @@ -16,9 +16,6 @@ workspace = true reth-network-peers.workspace = true reth-net-banlist.workspace = true reth-ethereum-forks.workspace = true - -# async -tokio = { workspace = true, features = ["sync"] } reth-network-p2p.workspace = true # io @@ -28,7 +25,6 @@ serde_json = { workspace = true } # misc tracing.workspace = true -derive_more.workspace = true [features] serde = ["dep:serde", "dep:humantime-serde", "reth-network-p2p/serde"] diff --git a/crates/net/network-types/src/lib.rs b/crates/net/network-types/src/lib.rs index f45f63d1e..f49b90fb7 100644 --- a/crates/net/network-types/src/lib.rs +++ b/crates/net/network-types/src/lib.rs @@ -24,10 +24,9 @@ pub use reth_network_p2p::reputation::{Reputation, ReputationChangeKind, Reputat pub use backoff::BackoffKind; pub use peers::{ addr::PeerAddr, - handle::PeersHandle, kind::PeerKind, reputation::{is_banned_reputation, ReputationChangeOutcome, DEFAULT_REPUTATION}, state::PeerConnectionState, - ConnectionsConfig, Peer, PeerCommand, PeersConfig, + ConnectionsConfig, Peer, PeersConfig, }; pub use session::{SessionLimits, SessionsConfig}; diff --git a/crates/net/network-types/src/peers/cmd.rs b/crates/net/network-types/src/peers/cmd.rs deleted file mode 100644 index 253b5a103..000000000 --- a/crates/net/network-types/src/peers/cmd.rs +++ /dev/null @@ -1,25 +0,0 @@ -//! Commands sent to peer manager. - -use std::net::SocketAddr; - -use reth_network_peers::{NodeRecord, PeerId}; -use tokio::sync::oneshot; - -use crate::{Peer, ReputationChangeKind}; - -/// Commands the `PeersManager` listens for. -#[derive(Debug)] -pub enum PeerCommand { - /// Command for manually add - Add(PeerId, SocketAddr), - /// Remove a peer from the set - /// - /// If currently connected this will disconnect the session - Remove(PeerId), - /// Apply a reputation change to the given peer. - ReputationChange(PeerId, ReputationChangeKind), - /// Get information about a peer - GetPeer(PeerId, oneshot::Sender>), - /// Get node information on all peers - GetPeers(oneshot::Sender>), -} diff --git a/crates/net/network-types/src/peers/mod.rs b/crates/net/network-types/src/peers/mod.rs index c80db89f7..15f080cc5 100644 --- a/crates/net/network-types/src/peers/mod.rs +++ b/crates/net/network-types/src/peers/mod.rs @@ -1,13 +1,10 @@ pub mod addr; -pub mod cmd; pub mod config; -pub mod handle; pub mod kind; pub mod state; pub use reth_network_p2p::reputation; -pub use cmd::PeerCommand; pub use config::{ConnectionsConfig, PeersConfig}; pub use reputation::ReputationChangeWeights; diff --git a/crates/net/network/src/builder.rs b/crates/net/network/src/builder.rs index 8a9c01fab..e6a5d9566 100644 --- a/crates/net/network/src/builder.rs +++ b/crates/net/network/src/builder.rs @@ -1,6 +1,6 @@ //! Builder support for configuring the entire setup. -use reth_network_api::PeersHandleProvider; +use reth_network_api::test_utils::PeersHandleProvider; use reth_transaction_pool::TransactionPool; use tokio::sync::mpsc; diff --git a/crates/net/network/src/eth_requests.rs b/crates/net/network/src/eth_requests.rs index a18f0656b..9eebbe086 100644 --- a/crates/net/network/src/eth_requests.rs +++ b/crates/net/network/src/eth_requests.rs @@ -13,9 +13,9 @@ use reth_eth_wire::{ BlockBodies, BlockHeaders, GetBlockBodies, GetBlockHeaders, GetNodeData, GetReceipts, HeadersDirection, NodeData, Receipts, }; +use reth_network_api::test_utils::PeersHandle; use reth_network_p2p::error::RequestResult; use reth_network_peers::PeerId; -use reth_network_types::PeersHandle; use reth_primitives::{BlockBody, BlockHashOrNumber, Header}; use reth_storage_api::{BlockReader, HeaderProvider, ReceiptProvider}; use tokio::sync::{mpsc::Receiver, oneshot}; diff --git a/crates/net/network/src/fetch/client.rs b/crates/net/network/src/fetch/client.rs index 4644dc77c..28023c0a2 100644 --- a/crates/net/network/src/fetch/client.rs +++ b/crates/net/network/src/fetch/client.rs @@ -6,6 +6,7 @@ use std::sync::{ }; use futures::{future, future::Either}; +use reth_network_api::test_utils::PeersHandle; use reth_network_p2p::{ bodies::client::{BodiesClient, BodiesFut}, download::DownloadClient, @@ -14,7 +15,7 @@ use reth_network_p2p::{ priority::Priority, }; use reth_network_peers::PeerId; -use reth_network_types::{PeersHandle, ReputationChangeKind}; +use reth_network_types::ReputationChangeKind; use reth_primitives::{Header, B256}; use tokio::sync::{mpsc::UnboundedSender, oneshot}; diff --git a/crates/net/network/src/fetch/mod.rs b/crates/net/network/src/fetch/mod.rs index 7de7e27a1..169e2a343 100644 --- a/crates/net/network/src/fetch/mod.rs +++ b/crates/net/network/src/fetch/mod.rs @@ -15,13 +15,14 @@ use std::{ use futures::StreamExt; use reth_eth_wire::{GetBlockBodies, GetBlockHeaders}; +use reth_network_api::test_utils::PeersHandle; use reth_network_p2p::{ error::{EthResponseValidator, PeerRequestResult, RequestError, RequestResult}, headers::client::HeadersRequest, priority::Priority, }; use reth_network_peers::PeerId; -use reth_network_types::{PeersHandle, ReputationChangeKind}; +use reth_network_types::ReputationChangeKind; use reth_primitives::{BlockBody, Header, B256}; use tokio::sync::{mpsc, mpsc::UnboundedSender, oneshot}; use tokio_stream::wrappers::UnboundedReceiverStream; diff --git a/crates/net/network/src/lib.rs b/crates/net/network/src/lib.rs index 6abe33c0a..786356329 100644 --- a/crates/net/network/src/lib.rs +++ b/crates/net/network/src/lib.rs @@ -138,9 +138,7 @@ mod state; mod swarm; pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols}; -pub use reth_network_api::{ - BlockDownloaderProvider, NetworkInfo, Peers, PeersHandleProvider, PeersInfo, -}; +pub use reth_network_api::{BlockDownloaderProvider, NetworkInfo, Peers, PeersInfo}; pub use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState}; pub use reth_network_types::{PeersConfig, SessionsConfig}; pub use session::{ diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 2dd534d22..04d8efc18 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -34,9 +34,9 @@ use reth_eth_wire::{ }; use reth_fs_util::{self as fs, FsPathError}; use reth_metrics::common::mpsc::UnboundedMeteredSender; -use reth_network_api::{EthProtocolInfo, NetworkStatus, PeerInfo}; +use reth_network_api::{test_utils::PeersHandle, EthProtocolInfo, NetworkStatus, PeerInfo}; use reth_network_peers::{NodeRecord, PeerId}; -use reth_network_types::{PeerAddr, PeersHandle, ReputationChangeKind}; +use reth_network_types::{PeerAddr, ReputationChangeKind}; use reth_primitives::ForkId; use reth_storage_api::BlockNumReader; use reth_tasks::shutdown::GracefulShutdown; diff --git a/crates/net/network/src/network.rs b/crates/net/network/src/network.rs index ed7beea9d..cb54b7b7b 100644 --- a/crates/net/network/src/network.rs +++ b/crates/net/network/src/network.rs @@ -11,15 +11,15 @@ use parking_lot::Mutex; use reth_discv4::Discv4; use reth_eth_wire::{DisconnectReason, NewBlock, NewPooledTransactionHashes, SharedTransactions}; use reth_network_api::{ - BlockDownloaderProvider, NetworkError, NetworkInfo, NetworkStatus, PeerInfo, Peers, - PeersHandleProvider, PeersInfo, + test_utils::{PeersHandle, PeersHandleProvider}, + BlockDownloaderProvider, NetworkError, NetworkInfo, NetworkStatus, PeerInfo, Peers, PeersInfo, }; use reth_network_p2p::{ sync::{NetworkSyncUpdater, SyncState, SyncStateProvider}, BlockClient, }; use reth_network_peers::{NodeRecord, PeerId}; -use reth_network_types::{PeerAddr, PeerKind, PeersHandle, Reputation, ReputationChangeKind}; +use reth_network_types::{PeerAddr, PeerKind, Reputation, ReputationChangeKind}; use reth_primitives::{Head, TransactionSigned, B256}; use reth_tokio_util::{EventSender, EventStream}; use secp256k1::SecretKey; diff --git a/crates/net/network/src/peers.rs b/crates/net/network/src/peers.rs index dc05b96fc..7dccf3545 100644 --- a/crates/net/network/src/peers.rs +++ b/crates/net/network/src/peers.rs @@ -12,14 +12,15 @@ use std::{ use futures::StreamExt; use reth_eth_wire::{errors::EthStreamError, DisconnectReason}; use reth_net_banlist::BanList; +use reth_network_api::test_utils::{PeerCommand, PeersHandle}; use reth_network_peers::{NodeRecord, PeerId}; use reth_network_types::{ peers::{ config::PeerBackoffDurations, reputation::{DEFAULT_REPUTATION, MAX_TRUSTED_PEER_REPUTATION_CHANGE}, }, - ConnectionsConfig, Peer, PeerAddr, PeerCommand, PeerConnectionState, PeerKind, PeersConfig, - PeersHandle, ReputationChangeKind, ReputationChangeOutcome, ReputationChangeWeights, + ConnectionsConfig, Peer, PeerAddr, PeerConnectionState, PeerKind, PeersConfig, + ReputationChangeKind, ReputationChangeOutcome, ReputationChangeWeights, }; use reth_primitives::ForkId; use thiserror::Error; diff --git a/crates/net/network/src/test_utils/testnet.rs b/crates/net/network/src/test_utils/testnet.rs index 2357ff324..aac4b67fc 100644 --- a/crates/net/network/src/test_utils/testnet.rs +++ b/crates/net/network/src/test_utils/testnet.rs @@ -12,9 +12,11 @@ use futures::{FutureExt, StreamExt}; use pin_project::pin_project; use reth_chainspec::MAINNET; use reth_eth_wire::{protocol::Protocol, DisconnectReason, HelloMessageWithProtocols}; -use reth_network_api::{NetworkInfo, Peers, PeersHandleProvider}; +use reth_network_api::{ + test_utils::{PeersHandle, PeersHandleProvider}, + NetworkInfo, Peers, +}; use reth_network_peers::PeerId; -use reth_network_types::PeersHandle; use reth_provider::test_utils::NoopProvider; use reth_storage_api::{BlockReader, BlockReaderIdExt, HeaderProvider, StateProviderFactory}; use reth_tasks::TokioTaskExecutor; diff --git a/examples/custom-rlpx-subprotocol/src/main.rs b/examples/custom-rlpx-subprotocol/src/main.rs index 18745d84d..8dc95641e 100644 --- a/examples/custom-rlpx-subprotocol/src/main.rs +++ b/examples/custom-rlpx-subprotocol/src/main.rs @@ -15,9 +15,9 @@ use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4}; use reth::builder::NodeHandle; use reth_network::{ config::SecretKey, protocol::IntoRlpxSubProtocol, NetworkConfig, NetworkManager, - NetworkProtocols, PeersHandleProvider, + NetworkProtocols, }; -use reth_network_api::NetworkInfo; +use reth_network_api::{test_utils::PeersHandleProvider, NetworkInfo}; use reth_node_ethereum::EthereumNode; use reth_provider::test_utils::NoopProvider; use subprotocol::{