reth_network/
builder.rs

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