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//!
55//! // This block provider implementation is used for testing purposes.
56//! let client = NoopProvider::default();
57//!
58//! // The key that's used for encrypting sessions and to identify our node.
59//! let local_key = rng_secret_key();
60//!
61//! let config = NetworkConfig::<_, EthNetworkPrimitives>::builder(local_key)
62//!     .boot_nodes(mainnet_nodes())
63//!     .build(client);
64//!
65//! // create the network instance
66//! let network = NetworkManager::new(config).await.unwrap();
67//!
68//! // keep a handle to the network and spawn it
69//! let handle = network.handle().clone();
70//! tokio::task::spawn(network);
71//!
72//! # }
73//! ```
74//!
75//! ### Configure all components of the Network with the [`NetworkBuilder`]
76//!
77//! ```
78//! use reth_network::{
79//!     config::rng_secret_key, EthNetworkPrimitives, NetworkConfig, NetworkManager,
80//! };
81//! use reth_network_peers::mainnet_nodes;
82//! use reth_storage_api::noop::NoopProvider;
83//! use reth_transaction_pool::TransactionPool;
84//! async fn launch<Pool: TransactionPool>(pool: Pool) {
85//!     // This block provider implementation is used for testing purposes.
86//!     let client = NoopProvider::default();
87//!
88//!     // The key that's used for encrypting sessions and to identify our node.
89//!     let local_key = rng_secret_key();
90//!
91//!     let config = NetworkConfig::<_, EthNetworkPrimitives>::builder(local_key)
92//!         .boot_nodes(mainnet_nodes())
93//!         .build(client.clone());
94//!     let transactions_manager_config = config.transactions_manager_config.clone();
95//!
96//!     // create the network instance
97//!     let (handle, network, transactions, request_handler) = NetworkManager::builder(config)
98//!         .await
99//!         .unwrap()
100//!         .transactions(pool, transactions_manager_config)
101//!         .request_handler(client)
102//!         .split_with_handle();
103//! }
104//! ```
105//!
106//! # Feature Flags
107//!
108//! - `serde` (default): Enable serde support for configuration types.
109//! - `test-utils`: Various utilities helpful for writing tests
110//! - `geth-tests`: Runs tests that require Geth to be installed locally.
111
112#![doc(
113    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
114    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
115    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
116)]
117#![allow(unreachable_pub)]
118#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
119
120#[cfg(any(test, feature = "test-utils"))]
121/// Common helpers for network testing.
122pub mod test_utils;
123
124pub mod cache;
125pub mod config;
126pub mod error;
127pub mod eth_requests;
128pub mod import;
129pub mod message;
130pub mod peers;
131pub mod protocol;
132pub mod transactions;
133
134mod budget;
135mod builder;
136mod discovery;
137mod fetch;
138mod flattened_response;
139mod listener;
140mod manager;
141mod metrics;
142mod network;
143mod session;
144mod state;
145mod swarm;
146
147pub use reth_eth_wire::{DisconnectReason, HelloMessageWithProtocols};
148pub use reth_eth_wire_types::{EthNetworkPrimitives, NetworkPrimitives};
149pub use reth_network_api::{
150    BlockDownloaderProvider, DiscoveredEvent, DiscoveryEvent, NetworkEvent,
151    NetworkEventListenerProvider, NetworkInfo, PeerRequest, PeerRequestSender, Peers, PeersInfo,
152};
153pub use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState};
154pub use reth_network_types::{PeersConfig, SessionsConfig};
155pub use session::{
156    ActiveSessionHandle, ActiveSessionMessage, Direction, EthRlpxConnection, PeerInfo,
157    PendingSessionEvent, PendingSessionHandle, PendingSessionHandshakeError, SessionCommand,
158    SessionEvent, SessionId, SessionManager,
159};
160
161pub use builder::NetworkBuilder;
162pub use config::{NetworkConfig, NetworkConfigBuilder};
163pub use discovery::Discovery;
164pub use fetch::FetchClient;
165pub use flattened_response::FlattenedResponse;
166pub use manager::NetworkManager;
167pub use metrics::TxTypesCounter;
168pub use network::{NetworkHandle, NetworkProtocols};
169pub use swarm::NetworkConnectionState;
170pub use transactions::{FilterAnnouncement, MessageFilter, ValidateTx68};
171
172/// re-export p2p interfaces
173pub use reth_network_p2p as p2p;
174
175/// re-export types crate
176pub use reth_eth_wire_types as types;