Skip to content

Commit

Permalink
chore(sdk): add NetworkPrimitives for NetworkManager (#12530)
Browse files Browse the repository at this point in the history
Co-authored-by: dkathiriya <[email protected]>
  • Loading branch information
lakshya-sky and lakshya-sky authored Nov 16, 2024
1 parent 7745046 commit 2dc9a06
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 107 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/net/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ reth-discv4.workspace = true
reth-discv5.workspace = true
reth-dns-discovery.workspace = true
reth-eth-wire.workspace = true
reth-eth-wire-types.workspace = true
reth-ecies.workspace = true
reth-tasks.workspace = true
reth-transaction-pool.workspace = true
Expand Down Expand Up @@ -111,14 +112,15 @@ serde = [
"reth-dns-discovery/serde",
"reth-eth-wire/serde",
"reth-provider?/serde",
"reth-eth-wire-types/serde",
"alloy-consensus/serde",
"alloy-eips/serde",
"alloy-primitives/serde",
"discv5/serde",
"parking_lot/serde",
"rand/serde",
"smallvec/serde",
"url/serde"
"url/serde",
]
test-utils = [
"dep:reth-provider",
Expand Down
5 changes: 3 additions & 2 deletions crates/net/network/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Builder support for configuring the entire setup.
use reth_eth_wire::{EthNetworkPrimitives, NetworkPrimitives};
use reth_network_api::test_utils::PeersHandleProvider;
use reth_transaction_pool::TransactionPool;
use tokio::sync::mpsc;
Expand All @@ -16,8 +17,8 @@ pub(crate) const ETH_REQUEST_CHANNEL_CAPACITY: usize = 256;

/// A builder that can configure all components of the network.
#[allow(missing_debug_implementations)]
pub struct NetworkBuilder<Tx, Eth> {
pub(crate) network: NetworkManager,
pub struct NetworkBuilder<Tx, Eth, N: NetworkPrimitives = EthNetworkPrimitives> {
pub(crate) network: NetworkManager<N>,
pub(crate) transactions: Tx,
pub(crate) request_handler: Eth,
}
Expand Down
31 changes: 17 additions & 14 deletions crates/net/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use reth_chainspec::{ChainSpecProvider, EthChainSpec, Hardforks};
use reth_discv4::{Discv4Config, Discv4ConfigBuilder, NatResolver, DEFAULT_DISCOVERY_ADDRESS};
use reth_discv5::NetworkStackId;
use reth_dns_discovery::DnsDiscoveryConfig;
use reth_eth_wire::{HelloMessage, HelloMessageWithProtocols, Status};
use reth_eth_wire::{
EthNetworkPrimitives, HelloMessage, HelloMessageWithProtocols, NetworkPrimitives, Status,
};
use reth_network_peers::{mainnet_nodes, pk2id, sepolia_nodes, PeerId, TrustedPeer};
use reth_network_types::{PeersConfig, SessionsConfig};
use reth_primitives::{ForkFilter, Head};
Expand All @@ -32,7 +34,7 @@ pub fn rng_secret_key() -> SecretKey {

/// All network related initialization settings.
#[derive(Debug)]
pub struct NetworkConfig<C> {
pub struct NetworkConfig<C, N: NetworkPrimitives = EthNetworkPrimitives> {
/// The client type that can interact with the chain.
///
/// This type is used to fetch the block number after we established a session and received the
Expand Down Expand Up @@ -66,7 +68,7 @@ pub struct NetworkConfig<C> {
/// first hardfork, `Frontier` for mainnet.
pub fork_filter: ForkFilter,
/// The block importer type.
pub block_import: Box<dyn BlockImport>,
pub block_import: Box<dyn BlockImport<N::Block>>,
/// The default mode of the network.
pub network_mode: NetworkMode,
/// The executor to use for spawning tasks.
Expand All @@ -87,9 +89,9 @@ pub struct NetworkConfig<C> {

// === impl NetworkConfig ===

impl NetworkConfig<()> {
impl<N: NetworkPrimitives> NetworkConfig<(), N> {
/// Convenience method for creating the corresponding builder type
pub fn builder(secret_key: SecretKey) -> NetworkConfigBuilder {
pub fn builder(secret_key: SecretKey) -> NetworkConfigBuilder<N> {
NetworkConfigBuilder::new(secret_key)
}

Expand All @@ -99,7 +101,7 @@ impl NetworkConfig<()> {
}
}

impl<C> NetworkConfig<C> {
impl<C, N: NetworkPrimitives> NetworkConfig<C, N> {
/// Create a new instance with all mandatory fields set, rest is field with defaults.
pub fn new(client: C, secret_key: SecretKey) -> Self
where
Expand Down Expand Up @@ -134,12 +136,13 @@ impl<C> NetworkConfig<C> {
}
}

impl<C> NetworkConfig<C>
impl<C, N> NetworkConfig<C, N>
where
C: BlockNumReader + 'static,
N: NetworkPrimitives,
{
/// Convenience method for calling [`NetworkManager::new`].
pub async fn manager(self) -> Result<NetworkManager, NetworkError> {
pub async fn manager(self) -> Result<NetworkManager<N>, NetworkError> {
NetworkManager::new(self).await
}
}
Expand All @@ -164,7 +167,7 @@ where

/// Builder for [`NetworkConfig`](struct.NetworkConfig.html).
#[derive(Debug)]
pub struct NetworkConfigBuilder {
pub struct NetworkConfigBuilder<N: NetworkPrimitives = EthNetworkPrimitives> {
/// The node's secret key, from which the node's identity is derived.
secret_key: SecretKey,
/// How to configure discovery over DNS.
Expand Down Expand Up @@ -196,7 +199,7 @@ pub struct NetworkConfigBuilder {
/// Whether tx gossip is disabled
tx_gossip_disabled: bool,
/// The block importer type
block_import: Option<Box<dyn BlockImport>>,
block_import: Option<Box<dyn BlockImport<N::Block>>>,
/// How to instantiate transactions manager.
transactions_manager_config: TransactionsManagerConfig,
/// The NAT resolver for external IP
Expand All @@ -206,7 +209,7 @@ pub struct NetworkConfigBuilder {
// === impl NetworkConfigBuilder ===

#[allow(missing_docs)]
impl NetworkConfigBuilder {
impl<N: NetworkPrimitives> NetworkConfigBuilder<N> {
/// Create a new builder instance with a random secret key.
pub fn with_rng_secret_key() -> Self {
Self::new(rng_secret_key())
Expand Down Expand Up @@ -480,7 +483,7 @@ impl NetworkConfigBuilder {
}

/// Sets the block import type.
pub fn block_import(mut self, block_import: Box<dyn BlockImport>) -> Self {
pub fn block_import(mut self, block_import: Box<dyn BlockImport<N::Block>>) -> Self {
self.block_import = Some(block_import);
self
}
Expand All @@ -490,7 +493,7 @@ impl NetworkConfigBuilder {
pub fn build_with_noop_provider<ChainSpec>(
self,
chain_spec: Arc<ChainSpec>,
) -> NetworkConfig<NoopBlockReader<ChainSpec>>
) -> NetworkConfig<NoopBlockReader<ChainSpec>, N>
where
ChainSpec: EthChainSpec + Hardforks + 'static,
{
Expand All @@ -509,7 +512,7 @@ impl NetworkConfigBuilder {
/// The given client is to be used for interacting with the chain, for example fetching the
/// corresponding block for a given block hash we receive from a peer in the status message when
/// establishing a connection.
pub fn build<C>(self, client: C) -> NetworkConfig<C>
pub fn build<C>(self, client: C) -> NetworkConfig<C, N>
where
C: ChainSpecProvider<ChainSpec: Hardforks>,
{
Expand Down
22 changes: 11 additions & 11 deletions crates/net/network/src/eth_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use alloy_eips::BlockHashOrNumber;
use alloy_rlp::Encodable;
use futures::StreamExt;
use reth_eth_wire::{
BlockBodies, BlockHeaders, GetBlockBodies, GetBlockHeaders, GetNodeData, GetReceipts,
HeadersDirection, NodeData, Receipts,
BlockBodies, BlockHeaders, EthNetworkPrimitives, GetBlockBodies, GetBlockHeaders, GetNodeData,
GetReceipts, HeadersDirection, NetworkPrimitives, NodeData, Receipts,
};
use reth_network_api::test_utils::PeersHandle;
use reth_network_p2p::error::RequestResult;
Expand Down Expand Up @@ -54,23 +54,23 @@ const SOFT_RESPONSE_LIMIT: usize = 2 * 1024 * 1024;
/// This can be spawned to another task and is supposed to be run as background service.
#[derive(Debug)]
#[must_use = "Manager does nothing unless polled."]
pub struct EthRequestHandler<C> {
pub struct EthRequestHandler<C, N: NetworkPrimitives = EthNetworkPrimitives> {
/// The client type that can interact with the chain.
client: C,
/// Used for reporting peers.
// TODO use to report spammers
#[allow(dead_code)]
peers: PeersHandle,
/// Incoming request from the [`NetworkManager`](crate::NetworkManager).
incoming_requests: ReceiverStream<IncomingEthRequest>,
incoming_requests: ReceiverStream<IncomingEthRequest<N>>,
/// Metrics for the eth request handler.
metrics: EthRequestHandlerMetrics,
}

// === impl EthRequestHandler ===
impl<C> EthRequestHandler<C> {
impl<C, N: NetworkPrimitives> EthRequestHandler<C, N> {
/// Create a new instance
pub fn new(client: C, peers: PeersHandle, incoming: Receiver<IncomingEthRequest>) -> Self {
pub fn new(client: C, peers: PeersHandle, incoming: Receiver<IncomingEthRequest<N>>) -> Self {
Self {
client,
peers,
Expand Down Expand Up @@ -148,7 +148,7 @@ where
&self,
_peer_id: PeerId,
request: GetBlockHeaders,
response: oneshot::Sender<RequestResult<BlockHeaders>>,
response: oneshot::Sender<RequestResult<BlockHeaders<Header>>>,
) {
self.metrics.eth_headers_requests_received_total.increment(1);
let headers = self.get_headers_response(request);
Expand All @@ -159,7 +159,7 @@ where
&self,
_peer_id: PeerId,
request: GetBlockBodies,
response: oneshot::Sender<RequestResult<BlockBodies>>,
response: oneshot::Sender<RequestResult<BlockBodies<BlockBody>>>,
) {
self.metrics.eth_bodies_requests_received_total.increment(1);
let mut bodies = Vec::new();
Expand Down Expand Up @@ -272,7 +272,7 @@ where

/// All `eth` request related to blocks delegated by the network.
#[derive(Debug)]
pub enum IncomingEthRequest {
pub enum IncomingEthRequest<N: NetworkPrimitives = EthNetworkPrimitives> {
/// Request Block headers from the peer.
///
/// The response should be sent through the channel.
Expand All @@ -282,7 +282,7 @@ pub enum IncomingEthRequest {
/// The specific block headers requested.
request: GetBlockHeaders,
/// The channel sender for the response containing block headers.
response: oneshot::Sender<RequestResult<BlockHeaders>>,
response: oneshot::Sender<RequestResult<BlockHeaders<N::BlockHeader>>>,
},
/// Request Block bodies from the peer.
///
Expand All @@ -293,7 +293,7 @@ pub enum IncomingEthRequest {
/// The specific block bodies requested.
request: GetBlockBodies,
/// The channel sender for the response containing block bodies.
response: oneshot::Sender<RequestResult<BlockBodies>>,
response: oneshot::Sender<RequestResult<BlockBodies<N::BlockBody>>>,
},
/// Request Node Data from the peer.
///
Expand Down
7 changes: 5 additions & 2 deletions crates/net/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
//!
//! ```
//! # async fn launch() {
//! use reth_network::{config::rng_secret_key, NetworkConfig, NetworkManager};
//! use reth_network::{
//! config::rng_secret_key, EthNetworkPrimitives, NetworkConfig, NetworkManager,
//! };
//! use reth_network_peers::mainnet_nodes;
//! use reth_provider::test_utils::NoopProvider;
//!
Expand All @@ -59,7 +61,7 @@
//! let config = NetworkConfig::builder(local_key).boot_nodes(mainnet_nodes()).build(client);
//!
//! // create the network instance
//! let network = NetworkManager::new(config).await.unwrap();
//! let network = NetworkManager::<EthNetworkPrimitives>::new(config).await.unwrap();
//!
//! // keep a handle to the network and spawn it
//! let handle = network.handle().clone();
Expand Down Expand Up @@ -138,6 +140,7 @@ mod state;
mod swarm;

pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols};
pub use reth_eth_wire_types::{EthNetworkPrimitives, NetworkPrimitives};
pub use reth_network_api::{
BlockDownloaderProvider, DiscoveredEvent, DiscoveryEvent, NetworkEvent,
NetworkEventListenerProvider, NetworkInfo, PeerRequest, PeerRequestSender, Peers, PeersInfo,
Expand Down
Loading

0 comments on commit 2dc9a06

Please sign in to comment.