diff --git a/Cargo.lock b/Cargo.lock index e70b1f16c..49c5a4306 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3607,6 +3607,7 @@ dependencies = [ "reth-executor", "reth-interfaces", "reth-network", + "reth-network-api", "reth-primitives", "reth-provider", "reth-rlp", @@ -3993,6 +3994,7 @@ dependencies = [ "reth-interfaces", "reth-metrics-derive", "reth-net-common", + "reth-network-api", "reth-primitives", "reth-provider", "reth-rlp", @@ -4010,6 +4012,10 @@ dependencies = [ "tracing", ] +[[package]] +name = "reth-network-api" +version = "0.1.0" + [[package]] name = "reth-primitives" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 766fe3b9f..ec9a87eca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "crates/net/discv4", "crates/net/dns", "crates/net/nat", + "crates/net/network-api", "crates/net/network", "crates/net/ipc", "crates/net/rpc", diff --git a/bin/reth/Cargo.toml b/bin/reth/Cargo.toml index d99ff60b5..faf7a2ed5 100644 --- a/bin/reth/Cargo.toml +++ b/bin/reth/Cargo.toml @@ -20,6 +20,7 @@ reth-executor = { path = "../../crates/executor" } # reth-rpc = {path = "../../crates/net/rpc"} reth-rlp = { path = "../../crates/common/rlp" } reth-network = {path = "../../crates/net/network", features = ["serde"] } +reth-network-api = {path = "../../crates/net/network-api" } reth-downloaders = {path = "../../crates/net/downloaders" } reth-cli-utils = { path = "../../crates/cli/utils" } reth-tracing = { path = "../../crates/tracing" } diff --git a/bin/reth/src/node/mod.rs b/bin/reth/src/node/mod.rs index ba55a39bf..26604cbcc 100644 --- a/bin/reth/src/node/mod.rs +++ b/bin/reth/src/node/mod.rs @@ -21,6 +21,7 @@ use reth_downloaders::{bodies, headers}; use reth_executor::Config as ExecutorConfig; use reth_interfaces::consensus::ForkchoiceState; use reth_network::NetworkEvent; +use reth_network_api::NetworkInfo; use reth_primitives::{BlockNumber, H256}; use reth_stages::{ metrics::HeaderMetrics, diff --git a/crates/net/network-api/Cargo.toml b/crates/net/network-api/Cargo.toml new file mode 100644 index 000000000..0ebd92dc7 --- /dev/null +++ b/crates/net/network-api/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "reth-network-api" +version = "0.1.0" +edition = "2021" +license = "MIT OR Apache-2.0" +repository = "https://github.com/paradigmxyz/reth" +readme = "README.md" +description = "Network interfaces" + +[dependencies] \ No newline at end of file diff --git a/crates/net/network-api/src/lib.rs b/crates/net/network-api/src/lib.rs new file mode 100644 index 000000000..374f994ea --- /dev/null +++ b/crates/net/network-api/src/lib.rs @@ -0,0 +1,18 @@ +#![warn(missing_docs, unreachable_pub)] +#![deny(unused_must_use, rust_2018_idioms)] +#![doc(test( + no_crate_inject, + attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables)) +))] + +//! Reth network interface definitions. +//! +//! Provides abstractions for the reth-network crate. + +use std::net::SocketAddr; + +/// Provides general purpose information about the network +pub trait NetworkInfo: Send + Sync { + /// Returns the [`SocketAddr`] that listens for incoming connections. + fn local_addr(&self) -> SocketAddr; +} diff --git a/crates/net/network/Cargo.toml b/crates/net/network/Cargo.toml index 4d0605e3f..2612776fb 100644 --- a/crates/net/network/Cargo.toml +++ b/crates/net/network/Cargo.toml @@ -20,6 +20,7 @@ normal = [ reth-interfaces = { path = "../../interfaces" } reth-primitives = { path = "../../primitives" } reth-net-common = { path = "../common" } +reth-network-api = { path = "../network-api" } reth-discv4 = { path = "../discv4" } reth-eth-wire = { path = "../eth-wire" } reth-ecies = { path = "../ecies" } diff --git a/crates/net/network/src/eth_requests.rs b/crates/net/network/src/eth_requests.rs index 7fc739abe..3bbc62882 100644 --- a/crates/net/network/src/eth_requests.rs +++ b/crates/net/network/src/eth_requests.rs @@ -17,6 +17,7 @@ use std::{ sync::Arc, task::{Context, Poll}, }; + use tokio::sync::{mpsc::UnboundedReceiver, oneshot}; use tokio_stream::wrappers::UnboundedReceiverStream; diff --git a/crates/net/network/src/network.rs b/crates/net/network/src/network.rs index 50cb9aad6..5333a169d 100644 --- a/crates/net/network/src/network.rs +++ b/crates/net/network/src/network.rs @@ -13,6 +13,7 @@ use reth_interfaces::{ sync::{SyncState, SyncStateProvider, SyncStateUpdater}, }; use reth_net_common::bandwidth_meter::BandwidthMeter; +use reth_network_api::NetworkInfo; use reth_primitives::{PeerId, TransactionSigned, TxHash, H256, U256}; use std::{ net::SocketAddr, @@ -64,11 +65,6 @@ impl NetworkHandle { self.inner.num_active_peers.load(Ordering::Relaxed) } - /// Returns the [`SocketAddr`] that listens for incoming connections. - pub fn local_addr(&self) -> SocketAddr { - *self.inner.listener_address.lock() - } - /// Returns the [`PeerId`] used in the network. pub fn peer_id(&self) -> &PeerId { &self.inner.local_peer_id @@ -210,6 +206,14 @@ impl NetworkHandle { } } +// === API Implementations === + +impl NetworkInfo for NetworkHandle { + fn local_addr(&self) -> SocketAddr { + *self.inner.listener_address.lock() + } +} + impl StatusUpdater for NetworkHandle { /// Update the status of the node. fn update_status(&self, height: u64, hash: H256, total_difficulty: U256) { diff --git a/crates/net/network/tests/it/connect.rs b/crates/net/network/tests/it/connect.rs index 1b6a4f352..08fe64a95 100644 --- a/crates/net/network/tests/it/connect.rs +++ b/crates/net/network/tests/it/connect.rs @@ -14,6 +14,7 @@ use reth_interfaces::{ }; use reth_net_common::ban_list::BanList; use reth_network::{NetworkConfig, NetworkEvent, NetworkManager, PeersConfig}; +use reth_network_api::NetworkInfo; use reth_primitives::{HeadersDirection, NodeRecord, PeerId}; use reth_provider::test_utils::NoopProvider; use reth_transaction_pool::test_utils::testing_pool; diff --git a/crates/net/network/tests/it/requests.rs b/crates/net/network/tests/it/requests.rs index 949a7ff91..6a8762161 100644 --- a/crates/net/network/tests/it/requests.rs +++ b/crates/net/network/tests/it/requests.rs @@ -8,6 +8,7 @@ use reth_interfaces::p2p::{ bodies::client::BodiesClient, headers::client::{HeadersClient, HeadersRequest}, }; +use reth_network_api::NetworkInfo; use reth_primitives::{ Block, Bytes, Header, HeadersDirection, Signature, Transaction, TransactionKind, TransactionSigned, TxEip2930, H256, U256,