reth_network/lib.rs
1//! reth P2P networking.
2//!
3//! Ethereum's networking protocol is specified in [devp2p](https://github.com/ethereum/devp2p).
4//!
5//! In order for a node to join the ethereum p2p network it needs to know what nodes are already
6//! part of that network. This includes public identities (public key) and addresses (where to reach
7//! them).
8//!
9//! ## Bird's Eye View
10//!
11//! See also diagram in [`NetworkManager`]
12//!
13//! The `Network` is made up of several, separate tasks:
14//!
15//! - `Transactions Task`: is a spawned
16//! [`TransactionsManager`](crate::transactions::TransactionsManager) future that:
17//!
18//! * Responds to incoming transaction related requests
19//! * Requests missing transactions from the `Network`
20//! * Broadcasts new transactions received from the
21//! [`TransactionPool`](reth_transaction_pool::TransactionPool) over the `Network`
22//!
23//! - `ETH request Task`: is a spawned
24//! [`EthRequestHandler`](crate::eth_requests::EthRequestHandler) future that:
25//!
26//! * Responds to incoming ETH related requests: `Headers`, `Bodies`
27//!
28//! - `Discovery Task`: is a spawned [`Discv4`](reth_discv4::Discv4) future that handles peer
29//! discovery and emits new peers to the `Network`
30//!
31//! - [`NetworkManager`] task advances the state of the `Network`, which includes:
32//!
33//! * Initiating new _outgoing_ connections to discovered peers
34//! * Handling _incoming_ TCP connections from peers
35//! * Peer management
36//! * Route requests:
37//! - from remote peers to corresponding tasks
38//! - from local to remote peers
39//!
40//! ## Usage
41//!
42//! ### Configure and launch a standalone network
43//!
44//! The [`NetworkConfig`] is used to configure the network.
45//! It requires an instance of [`BlockReader`](reth_storage_api::BlockReader).
46//!
47//! ```
48//! # async fn launch() {
49//! use reth_network::{
50//! config::rng_secret_key, EthNetworkPrimitives, NetworkConfig, NetworkManager,
51//! };
52//! use reth_network_peers::mainnet_nodes;
53//! use reth_storage_api::noop::NoopProvider;
54//! use reth_tasks::Runtime;
55//!
56//! // This block provider implementation is used for testing purposes.
57//! let client = NoopProvider::default();
58//!
59//! // The key that's used for encrypting sessions and to identify our node.
60//! let local_key = rng_secret_key();
61//!
62//! let config = NetworkConfig::<_, EthNetworkPrimitives>::builder(local_key, Runtime::test())
63//! .boot_nodes(mainnet_nodes())
64//! .build(client);
65//!
66//! // create the network instance
67//! let network = NetworkManager::new(config).await.unwrap();
68//!
69//! // keep a handle to the network and spawn it
70//! let handle = network.handle().clone();
71//! tokio::task::spawn(network);
72//!
73//! # }
74//! ```
75//!
76//! ### Configure all components of the Network with the [`NetworkBuilder`]
77//!
78//! ```
79//! use reth_network::{
80//! config::rng_secret_key, EthNetworkPrimitives, NetworkConfig, NetworkManager,
81//! };
82//! use reth_network_peers::mainnet_nodes;
83//! use reth_storage_api::noop::NoopProvider;
84//! use reth_tasks::Runtime;
85//! use reth_transaction_pool::TransactionPool;
86//! async fn launch<Pool: TransactionPool>(pool: Pool) {
87//! // This block provider implementation is used for testing purposes.
88//! let client = NoopProvider::default();
89//!
90//! // The key that's used for encrypting sessions and to identify our node.
91//! let local_key = rng_secret_key();
92//!
93//! let config = NetworkConfig::<_, EthNetworkPrimitives>::builder(local_key, Runtime::test())
94//! .boot_nodes(mainnet_nodes())
95//! .build(client.clone());
96//! let transactions_manager_config = config.transactions_manager_config.clone();
97//!
98//! // create the network instance
99//! let (handle, network, transactions, request_handler) = NetworkManager::builder(config)
100//! .await
101//! .unwrap()
102//! .transactions(pool, transactions_manager_config)
103//! .request_handler(client)
104//! .split_with_handle();
105//! }
106//! ```
107//!
108//! # Feature Flags
109//!
110//! - `serde` (default): Enable serde support for configuration types.
111//! - `test-utils`: Various utilities helpful for writing tests
112
113#![doc(
114 html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
115 html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
116 issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
117)]
118#![allow(unreachable_pub)]
119#![cfg_attr(not(test), warn(unused_crate_dependencies))]
120#![cfg_attr(docsrs, feature(doc_cfg))]
121
122#[cfg(any(test, feature = "test-utils"))]
123/// Common helpers for network testing.
124pub mod test_utils;
125
126pub mod cache;
127pub mod config;
128pub mod error;
129pub mod eth_requests;
130pub mod import;
131pub mod message;
132pub mod peers;
133pub mod protocol;
134pub mod transactions;
135
136mod budget;
137mod builder;
138mod discovery;
139mod fetch;
140mod flattened_response;
141mod listener;
142mod manager;
143mod metrics;
144mod network;
145mod required_block_filter;
146mod session;
147mod state;
148mod swarm;
149mod trusted_peers_resolver;
150
151pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols};
152pub use reth_eth_wire_types::{primitives, EthNetworkPrimitives, NetworkPrimitives};
153pub use reth_network_api::{
154 events, BlockDownloaderProvider, DiscoveredEvent, DiscoveryEvent, NetworkEvent,
155 NetworkEventListenerProvider, NetworkInfo, PeerRequest, PeerRequestSender, Peers, PeersInfo,
156};
157pub use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState};
158pub use reth_network_types::{PeersConfig, SessionsConfig};
159pub use session::{
160 ActiveSessionHandle, ActiveSessionMessage, Direction, EthRlpxConnection, PeerInfo,
161 PendingSessionEvent, PendingSessionHandle, PendingSessionHandshakeError, SessionCommand,
162 SessionEvent, SessionId, SessionManager,
163};
164
165pub use builder::NetworkBuilder;
166pub use config::{NetworkConfig, NetworkConfigBuilder};
167pub use discovery::Discovery;
168pub use fetch::FetchClient;
169pub use flattened_response::FlattenedResponse;
170pub use manager::NetworkManager;
171pub use metrics::TxTypesCounter;
172pub use network::{NetworkHandle, NetworkProtocols};
173pub use swarm::NetworkConnectionState;
174
175/// re-export p2p interfaces
176pub use reth_network_p2p as p2p;
177
178/// re-export types crates
179pub mod types {
180 pub use reth_discv4::NatResolver;
181 pub use reth_eth_wire_types::*;
182 pub use reth_network_types::*;
183}
184
185use aquamarine as _;
186
187use smallvec as _;