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, 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 tests
  • geth-tests: Runs tests that require Geth to be installed locally.

Re-exports§

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_utilstest-utils
    Common helpers for network testing. Common helpers for network testing.
  • Transactions management for the p2p network.

Macros§

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.
  • 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§

Traits§