reth_network/
builder.rs

1//! Builder support for configuring the entire setup.
2
3use crate::{
4    eth_requests::EthRequestHandler,
5    transactions::{TransactionPropagationPolicy, TransactionsManager, TransactionsManagerConfig},
6    NetworkHandle, NetworkManager,
7};
8use reth_eth_wire::{EthNetworkPrimitives, NetworkPrimitives};
9use reth_network_api::test_utils::PeersHandleProvider;
10use reth_transaction_pool::TransactionPool;
11use tokio::sync::mpsc;
12
13/// We set the max channel capacity of the `EthRequestHandler` to 256
14/// 256 requests with malicious 10MB body requests is 2.6GB which can be absorbed by the node.
15pub(crate) const ETH_REQUEST_CHANNEL_CAPACITY: usize = 256;
16
17/// A builder that can configure all components of the network.
18#[expect(missing_debug_implementations)]
19pub struct NetworkBuilder<Tx, Eth, N: NetworkPrimitives = EthNetworkPrimitives> {
20    pub(crate) network: NetworkManager<N>,
21    pub(crate) transactions: Tx,
22    pub(crate) request_handler: Eth,
23}
24
25// === impl NetworkBuilder ===
26
27impl<Tx, Eth, N: NetworkPrimitives> NetworkBuilder<Tx, Eth, N> {
28    /// Consumes the type and returns all fields.
29    pub fn split(self) -> (NetworkManager<N>, Tx, Eth) {
30        let Self { network, transactions, request_handler } = self;
31        (network, transactions, request_handler)
32    }
33
34    /// Returns the network manager.
35    pub const fn network(&self) -> &NetworkManager<N> {
36        &self.network
37    }
38
39    /// Returns the mutable network manager.
40    pub const fn network_mut(&mut self) -> &mut NetworkManager<N> {
41        &mut self.network
42    }
43
44    /// Returns the handle to the network.
45    pub fn handle(&self) -> NetworkHandle<N> {
46        self.network.handle().clone()
47    }
48
49    /// Consumes the type and returns all fields and also return a [`NetworkHandle`].
50    pub fn split_with_handle(self) -> (NetworkHandle<N>, NetworkManager<N>, Tx, Eth) {
51        let Self { network, transactions, request_handler } = self;
52        let handle = network.handle().clone();
53        (handle, network, transactions, request_handler)
54    }
55
56    /// Creates a new [`EthRequestHandler`] and wires it to the network.
57    pub fn request_handler<Client>(
58        self,
59        client: Client,
60    ) -> NetworkBuilder<Tx, EthRequestHandler<Client, N>, N> {
61        let Self { mut network, transactions, .. } = self;
62        let (tx, rx) = mpsc::channel(ETH_REQUEST_CHANNEL_CAPACITY);
63        network.set_eth_request_handler(tx);
64        let peers = network.handle().peers_handle().clone();
65        let request_handler = EthRequestHandler::new(client, peers, rx);
66        NetworkBuilder { network, request_handler, transactions }
67    }
68
69    /// Creates a new [`TransactionsManager`] and wires it to the network.
70    pub fn transactions<Pool: TransactionPool>(
71        self,
72        pool: Pool,
73        transactions_manager_config: TransactionsManagerConfig,
74    ) -> NetworkBuilder<TransactionsManager<Pool, N>, Eth, N> {
75        self.transactions_with_policy(pool, transactions_manager_config, Default::default())
76    }
77
78    /// Creates a new [`TransactionsManager`] and wires it to the network.
79    pub fn transactions_with_policy<Pool: TransactionPool, P: TransactionPropagationPolicy>(
80        self,
81        pool: Pool,
82        transactions_manager_config: TransactionsManagerConfig,
83        propagation_policy: P,
84    ) -> NetworkBuilder<TransactionsManager<Pool, N, P>, Eth, N> {
85        let Self { mut network, request_handler, .. } = self;
86        let (tx, rx) = mpsc::unbounded_channel();
87        network.set_transactions(tx);
88        let handle = network.handle().clone();
89        let transactions = TransactionsManager::with_policy(
90            handle,
91            pool,
92            rx,
93            transactions_manager_config,
94            propagation_policy,
95        );
96        NetworkBuilder { network, request_handler, transactions }
97    }
98}