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 spawned TransactionsManager future that:

    • Responds to incoming transaction related requests
    • Requests missing transactions from the Network
    • Broadcasts new transactions received from the TransactionPool over the Network
  • ETH request Task: is a spawned EthRequestHandler future that:

    • Responds to incoming ETH related requests: Headers, Bodies
  • Discovery Task: is a spawned Discv4 future that handles peer discovery and emits new peers to the Network

  • NetworkManager task advances the state of the Network, 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 tests
  • geth-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_utilstest-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§

ActiveSessionHandle
An established session with a remote peer.
Discovery
An abstraction over the configured discovery protocol.
EthNetworkPrimitives
Network primitive types used by Ethereum networks.
FetchClient
Front-end API for fetching data from the network.
FlattenedResponse
Flatten a [Receiver] message in order to get rid of the [RecvError] result
HelloMessageWithProtocols
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.
NetworkBuilder
A builder that can configure all components of the network.
NetworkHandle
A shareable network frontend. Used to interact with the network.
NetworkManager
Manages the entire state of the network.
PeerInfo
Info about an active peer session.
PeerRequestSender
A Cloneable connection for sending requests directly to the session of a peer.
PeersConfig
Config type for initiating a PeersManager instance.
PendingSessionHandle
A handler attached to a peer session that’s not authenticated yet, pending Handshake and hello message which exchanges the capabilities of the peer.
SessionId
Internal identifier for active sessions.
SessionManager
Manages a set of sessions.
SessionsConfig
Configuration options for peer session management.
TxTypesCounter
Counts the number of transactions by their type in a block or collection.

Enums§

ActiveSessionMessage
Message variants an active session can produce and send back to the SessionManager
Direction
The direction of the connection.
DisconnectReason
RLPx disconnect reason.
DiscoveredEvent
Represents events related to peer discovery in the network.
DiscoveryEvent
Events produced by the Discovery manager.
EthRlpxConnection
Connection types that support the ETH protocol.
NetworkConnectionState
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.
NetworkEvent
(Non-exhaustive) Network events representing peer lifecycle events and session requests.
PeerRequest
Protocol related request messages that expect a response
PendingSessionEvent
Events a pending session can produce.
PendingSessionHandshakeError
Errors that can occur during handshaking/authenticating the underlying streams.
SessionCommand
Commands that can be sent to the spawned session.
SessionEvent
Events produced by the SessionManager
SyncState
The state the network is currently in when it comes to synchronization.

Traits§

BlockDownloaderProvider
Provides client for downloading blocks.
NetworkEventListenerProvider
Provides event subscription for the network.
NetworkInfo
Provides general purpose information about the network.
NetworkPrimitives
Abstraction over primitive types which might appear in network messages. See [crate::EthMessage] for more context.
NetworkProtocols
Provides access to modify the network’s additional protocol handlers.
NetworkSyncUpdater
An updater for updating the SyncState and status of the network.
Peers
Provides an API for managing the peers of the network.
PeersInfo
Provides general purpose information about Peers in the network.