Crate reth_network
source ·Expand description
reth P2P networking.
Ethereum’s networking protocol is specified in devp2p.
In order for a node to join the ethereum p2p network it needs to know what nodes are already part of that network. This includes public identities (public key) and addresses (where to reach them).
Bird’s Eye View
See also diagram in NetworkManager
The Network
is made up of several, separate tasks:
-
Transactions Task
: is a spawnedTransactionsManager
future that:- Responds to incoming transaction related requests
- Requests missing transactions from the
Network
- Broadcasts new transactions received from the
TransactionPool
over theNetwork
-
ETH request Task
: is a spawnedEthRequestHandler
future that:- Responds to incoming ETH related requests:
Headers
,Bodies
- Responds to incoming ETH related requests:
-
Discovery Task
: is a spawnedDiscv4
future that handles peer discovery and emits new peers to theNetwork
-
NetworkManager
task advances the state of theNetwork
, which includes:- Initiating new outgoing connections to discovered peers
- Handling incoming TCP connections from peers
- Peer management
- Route requests:
- from remote peers to corresponding tasks
- from local to remote peers
Usage
Configure and launch a standalone network
The NetworkConfig
is used to configure the network.
It requires an instance of BlockReader
.
use reth_network::{config::rng_secret_key, NetworkConfig, NetworkManager};
use reth_primitives::mainnet_nodes;
use reth_provider::test_utils::NoopProvider;
// This block provider implementation is used for testing purposes.
let client = NoopProvider::default();
// The key that's used for encrypting sessions and to identify our node.
let local_key = rng_secret_key();
let config = NetworkConfig::builder(local_key).boot_nodes(mainnet_nodes()).build(client);
// create the network instance
let network = NetworkManager::new(config).await.unwrap();
// keep a handle to the network and spawn it
let handle = network.handle().clone();
tokio::task::spawn(network);
Configure all components of the Network with the NetworkBuilder
use reth_network::{config::rng_secret_key, NetworkConfig, NetworkManager};
use reth_primitives::mainnet_nodes;
use reth_provider::test_utils::NoopProvider;
use reth_transaction_pool::TransactionPool;
async fn launch<Pool: TransactionPool>(pool: Pool) {
// This block provider implementation is used for testing purposes.
let client = NoopProvider::default();
// The key that's used for encrypting sessions and to identify our node.
let local_key = rng_secret_key();
let config =
NetworkConfig::builder(local_key).boot_nodes(mainnet_nodes()).build(client.clone());
// create the network instance
let (handle, network, transactions, request_handler) = NetworkManager::builder(config)
.await
.unwrap()
.transactions(pool)
.request_handler(client)
.split_with_handle();
}
Feature Flags
serde
(default): Enable serde support for configuration types.test-utils
: Various utilities helpful for writing testsgeth-tests
: Runs tests that require Geth to be installed locally.
Re-exports
pub use config::NetworkConfig;
pub use config::NetworkConfigBuilder;
pub use peers::PeersConfig;
Modules
- Network config support
- Possible errors when interacting with the network.
- Blocks/Headers management for the p2p network.
- Peer related implementations
- Support for handling additional RLPx-based application-level protocols.
- test_utils
test-utils
Common helpers for network testing. Common helpers for network testing. - Transactions management for the p2p network.
Structs
- An established session with a remote peer.
- An abstraction over the configured discovery protocol.
- Front-end API for fetching data from the network.
- This is a superset of HelloMessage that provides additional protocol Protocol information about the number of messages used by each capability in order to do proper message ID multiplexing.
- A builder that can configure all components of the network.
- A shareable network frontend. Used to interact with the network.
- Manages the entire state of the network.
- Info about an active peer session.
- A handler attached to a peer session that’s not authenticated yet, pending Handshake and hello message which exchanges the
capabilities
of the peer. - Internal identifier for active sessions.
- Limits for sessions.
- Manages a set of sessions.
- Configuration options when creating a SessionManager.
Enums
- Message variants an active session can produce and send back to the
SessionManager
- The direction of the connection.
- RLPx disconnect reason.
- Events produced by the
Discovery
manager. - (Non-exhaustive) Events emitted by the network that are of interest for subscribers.
- Protocol related request messages that expect a response
- Events a pending session can produce.
- Errors that can occur during handshaking/authenticating the underlying streams.
- Commands that can be sent to the spawned session.
- Events produced by the
SessionManager
Traits
- Provides event subscription for the network.
- Provides access to modify the network’s additional protocol handlers.