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, EthNetworkPrimitives, NetworkConfig, NetworkManager,
};
use reth_network_peers::mainnet_nodes;
use reth_storage_api::noop::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::<_, EthNetworkPrimitives>::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, EthNetworkPrimitives, NetworkConfig, NetworkManager,
};
use reth_network_peers::mainnet_nodes;
use reth_storage_api::noop::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::<_, EthNetworkPrimitives>::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;
pub use reth_network_p2p as p2p;
pub use reth_eth_wire_types as types;
Modules§
- cache
- Network cache support
- config
- Network config support
- error
- Possible errors when interacting with the network.
- eth_
requests - Blocks/Headers management for the p2p network.
- import
- This module provides an abstraction over block import in the form of the
BlockImport
trait. - message
- Capability messaging
- peers
- Peer related implementations
- protocol
- Support for handling additional RLPx-based application-level protocols.
- test_
utils test-utils
- Common helpers for network testing. Common helpers for network testing.
- transactions
- Transactions management for the p2p network.
Macros§
- duration_
metered_ exec - 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_ nested_ stream_ with_ budget - Metered poll of the given stream. Breaks with
true
if there maybe is more work. - poll_
nested_ stream_ with_ budget - Polls the given stream. Breaks with
true
if there maybe is more work.
Structs§
- Active
Session Handle - An established session with a remote peer.
- Discovery
- An abstraction over the configured discovery protocol.
- EthNetwork
Primitives - Network primitive types used by Ethereum networks.
- Fetch
Client - Front-end API for fetching data from the network.
- Flattened
Response - Flatten a [Receiver] message in order to get rid of the [RecvError] result
- Hello
Message With Protocols - 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. - Network
Builder - A builder that can configure all components of the network.
- Network
Handle - A shareable network frontend. Used to interact with the network.
- Network
Manager - Manages the entire state of the network.
- Peer
Info - Info about an active peer session.
- Peer
Request Sender - A Cloneable connection for sending requests directly to the session of a peer.
- Peers
Config - Config type for initiating a
PeersManager
instance. - Pending
Session Handle - A handler attached to a peer session that’s not authenticated yet, pending Handshake and hello
message which exchanges the
capabilities
of the peer. - Session
Id - Internal identifier for active sessions.
- Session
Manager - Manages a set of sessions.
- Sessions
Config - Configuration options for peer session management.
- TxTypes
Counter - Counts the number of transactions by their type in a block or collection.
Enums§
- Active
Session Message - Message variants an active session can produce and send back to the
SessionManager
- Direction
- The direction of the connection.
- Disconnect
Reason - RLPx disconnect reason.
- Discovered
Event - Represents events related to peer discovery in the network.
- Discovery
Event - Events produced by the
Discovery
manager. - EthRlpx
Connection - Connection types that support the ETH protocol.
- Network
Connection State - 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.
- Network
Event - (Non-exhaustive) Network events representing peer lifecycle events and session requests.
- Peer
Request - Protocol related request messages that expect a response
- Pending
Session Event - Events a pending session can produce.
- Pending
Session Handshake Error - Errors that can occur during handshaking/authenticating the underlying streams.
- Session
Command - Commands that can be sent to the spawned session.
- Session
Event - Events produced by the
SessionManager
- Sync
State - The state the network is currently in when it comes to synchronization.
Traits§
- Block
Downloader Provider - Provides client for downloading blocks.
- Network
Event Listener Provider - Provides event subscription for the network.
- Network
Info - Provides general purpose information about the network.
- Network
Primitives - Abstraction over primitive types which might appear in network messages. See
[
crate::EthMessage
] for more context. - Network
Protocols - Provides access to modify the network’s additional protocol handlers.
- Network
Sync Updater - An updater for updating the SyncState and status of the network.
- Peers
- Provides an API for managing the peers of the network.
- Peers
Info - Provides general purpose information about Peers in the network.