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_network_peers::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_network_peers::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());
let transactions_manager_config = config.transactions_manager_config.clone();
// create the network instance
let (handle, network, transactions, request_handler) = NetworkManager::builder(config)
.await
.unwrap()
.transactions(pool, transactions_manager_config)
.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 transactions::FilterAnnouncement;
pub use transactions::MessageFilter;
pub use transactions::ValidateTx68;
Modules§
- Network cache support
- Network config support
- Possible errors when interacting with the network.
- Blocks/Headers management for the p2p network.
- This module provides an abstraction over block import in the form of the
BlockImport
trait. - Capability messaging
- 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.
Macros§
- Measures the duration of executing the given code block. The duration is added to the given accumulator value passed as a mutable reference.
- Metered poll of the given stream. Breaks with
true
if there maybe is more work. - Polls the given stream. Breaks with
true
if there maybe is more work.
Structs§
- An established session with a remote peer.
- An abstraction over the configured discovery protocol.
- Front-end API for fetching data from the network.
- Flatten a [Receiver] message in order to get rid of the [RecvError] result
- 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 Cloneable connection for sending requests directly to the session of a peer.
- Config type for initiating a
PeersManager
instance. - 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.
- Manages a set of sessions.
- Configuration options for peer session management.
- Counts the number of transactions by their type in a block or collection.
Enums§
- Message variants an active session can produce and send back to the
SessionManager
- The direction of the connection.
- RLPx disconnect reason.
- Represents events related to peer discovery in the network.
- Events produced by the
Discovery
manager. - Connection types that support the ETH protocol.
- Represents the state of the connection of the node. If shutting down, new connections won’t be established. When in hibernation mode, the node will not initiate new outbound connections. This is beneficial for sync stages that do not require a network connection.
- (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
- The state the network is currently in when it comes to synchronization.
Traits§
- Provides client for downloading blocks.
- Provides event subscription for the network.
- Provides general purpose information about the network.
- Provides access to modify the network’s additional protocol handlers.
- An updater for updating the SyncState and status of the network.
- Provides an API for managing the peers of the network.
- Provides general purpose information about Peers in the network.